用fluidsynth合成MIDI的Python方法

7 投票
3 回答
10268 浏览
提问于 2025-04-16 16:24

我无法导入fluidsynth。[也许有更好的模块?]

我想用python或pygame合成midi音乐。我可以从pygame发送midi事件。

我在使用mingus,感觉pyfluidsynth会是个不错的选择,最简单的。

我觉得这意味着pyfluidsynth已经安装了,但好像没有单独安装fluidsynth。我不确定它是否需要一个'fluidsynth'的安装程序才能正常工作?

test.py:

import fluidsynth
print ":("

错误信息:

Traceback (most recent call last):
  File "test.py", line 1, in <module>
    import fluidsynth
  File "C:\Users\jake\AppData\Roaming\Python\Python27\site-packages\fluidsynth.py", line 34, in <module>
    raise ImportError, "Couldn't find the FluidSynth library."
ImportError: Couldn't find the FluidSynth library.

使用的环境:python 2.7-win32

3 个回答

0

看了看 fluidsynth.py,你可能猜得没错。你应该试着把 fluidsynth.dll 放在你系统的库搜索路径里(最简单的办法可能就是把它放在和你的脚本或者 fluidsynth.py 在同一个文件夹里)。

我觉得这个压缩包(通过谷歌找到的)里包含了必要的 win32 二进制文件:http://svn.drdteam.org/zdoom/fluidsynth.7z

3

是的,你还需要FuildSynth这个库(在Windows上是dll文件)。

要让它正常工作,你需要:

  • fluidsynth 1.1.6
  • python26(32位)
  • pyFluidSynth 1.1.4
  • Windows操作系统

我把所有东西放在了同一个文件夹里(包括fluidsynth的dll文件、PyFluidSynth模块和Python脚本)。

然后在pyFluidSynth模块中修改了以下几行代码(从第30行开始):

# A short circuited or expression to find the FluidSynth library
# (mostly needed for Windows distributions of libfluidsynth supplied with QSynth)
# and Dynamically link the FluidSynth library

lib = find_library('fluidsynth') or find_library('libfluidsynth') or find_library('libfluidsynth-1')
if lib is None:
   _fl = ctypes.cdll.LoadLibrary("./libfluidsynth")
   lib = "ok";
else:
  _fl = CDLL(lib)   

if lib is None:
    raise ImportError, "Couldn't find the FluidSynth library."


# Helper function for declaring function prototypes

这样设置后,一切运行得很好。

3

Python的fluidsynth模块需要找到FluidSynth这个程序的库文件,也就是fluidsynth.dll。

要获取这个文件,你可以去下载、编译并安装这个链接里的内容:http://sourceforge.net/projects/fluidsynth/files/fluidsynth-1.1.3/

或者

你也可以找一些使用fluidsynth的项目,比如QSynth,这些项目里可能已经包含了预先编译好的.dll文件。

撰写回答