这段代码的目的是使用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.发布于 2021-03-02 00:36:18
看起来你需要安装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://stackoverflow.com/questions/66420621
复制相似问题