0

这段代码的目的是使用 QPushButton 创建一个按钮,然后使用 Pyfluidsynth 库创建一个声音。我已经导入了 time 和 pyfluidsynth,但我也尝试导入了 fluidsynth(该选项在那里,但我不知道有什么区别,两者都与我安装的一个库一起提供)。这是代码:

import sys
import time
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
import pyfluidsynth

class App(QWidget):

    def __init__(self):
        super().__init__()
        self.title = 'PyQt5 button - pythonspot.com'
        self.left = 10
        self.top = 10
        self.width = 320
        self.height = 200
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        button = QPushButton('PyQt5 button', self)
        button.setToolTip('This is an example button')
        button.move(100, 70)
        button.clicked.connect(self.PlayNote)

        self.show()

    def PlayNote(self):
        fs = pyfluidsynth.Synth()
        fs.start()

        sfid = fs.sfload("acoustic_grand_piano_ydp_20080910.sf2")
        fs.program_select(0, sfid, 0, 0)

        fs.noteon(0, 60, 30)
        time.sleep(1.0)

        fs.delete()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

但是,运行时,结果是 ImportError:

Traceback (most recent call last):
  File "/Volumes/T7/MIDI Project/MIDI calculation.py", line 6, in <module>
    import pyfluidsynth
  File "/Users/ricochetnkweso/Library/Python/3.7/lib/python/site-packages/pyfluidsynth.py", line 43, in <module>
    raise ImportError("Couldn't find the FluidSynth library.")
ImportError: Couldn't find the FluidSynth library.
4

1 回答 1

0

看起来您需要安装 FluidSynth 的共享库。这是因为 Pyfluidsynth 仅绑定到 FluidSynth 的 API。

所以需要在运行 Pyfluidsynth 的机器上安装一个 FluidSynth 版本。共享库是包含其他程序可以使用的编译代码的文件。

您可以使用以下链接获取适用于您机器的 Fluidsynth 发行包。

https://github.com/nwhitehead/pyfluidsynth#requirements

REQUIREMENTS

FluidSynth 2.0.0 (or later version) (earlier versions are not supported. While they probably work, some features will be unavailble) http://www.fluidsynth.org/

    Windows/Android Binaries: https://github.com/FluidSynth/fluidsynth/releases
    MacOS/Linux Distributions: https://github.com/FluidSynth/fluidsynth/wiki/Download#distributions
    Building from Source: https://github.com/FluidSynth/fluidsynth/wiki/BuildingWithCMake

其他安装/编译信息位于:
https ://github.com/FluidSynth/fluidsynth/wiki/Download

于 2021-03-01T16:36:17.653 回答