用Python从MIDI到实际音频输出(声音合成)的最简单方法是什么?

3

我正在开发一个需要从大量预加载的“.mid”文件中创建声音的应用程序。

我使用Python和Kivy创建应用程序,因为我已经使用这些工具创建了一个应用程序,并且它们是我所知道的唯一代码。 我制作的另一个应用程序根本不使用任何声音。

当然,我希望确保我编写的代码可以跨平台使用。

现在,我只是想证明我可以从midi音符创建任何真实的声音。

我采用了另一个回答建议的使用FluidSynth和Mingus的代码:

from mingus.midi import fluidsynth

fluidsynth.init('/usr/share/sounds/sf2/FluidR3_GM.sf2',"alsa")
fluidsynth.play_Note(64,0,100)

但我什么都听不到,而且出现了这个错误:
fluidsynth: warning: Failed to pin the sample data to RAM; swapping is possible.

我为什么会收到这个错误提示?怎么修复它?这是最简单的方法吗,还是正确的方法?


你是在Linux上运行吗?确保你的用户在“audio”组中。另外,那只是一个警告,所以它不应该阻止脚本运行,因此它可能实际上与没有声音的问题无关。您还需要验证是否已安装并正确工作alsa声音驱动程序(否则只需从init()调用的第二个参数中删除“alsa”,这样操作系统将为您选择默认驱动程序)。 - Max Worg
1个回答

2

我可能错了,但是我认为你传递给.play_Note()的第二个参数是“0”通道,这是不正确的。请尝试以下代码:

fluidsynth.play_Note(64,1,100)

或者(来自一些文档

from mingus.containers.note import Note
n = Note("C", 4)
n.channel = 1
n.velocity = 50
fluidSynth.play_Note(n)

更新:

在该方法的源代码中,只引用了1-16个通道,并将默认通道设置为1:

def play_Note(self, note, channel = 1, velocity = 100):
        """Plays a Note object on a channel[1-16] with a \
velocity[0-127]. You can either specify the velocity and channel \
here as arguments or you can set the Note.velocity and Note.channel \
attributes, which will take presedence over the function arguments."""
        if hasattr(note, 'velocity'):
            velocity = note.velocity
        if hasattr(note, 'channel'):
            channel = note.channel
        self.fs.noteon(int(channel), int(note) + 12, int(velocity))
        return True

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接