不能用 mingus 演奏音符

Can't play a note with mingus

我开始使用 mingus 来尝试重现 python 中的一些笔记。基于 answered here,我试过:

from mingus.midi import fluidsynth

fluidsynth.init('/home/btc/Escritorio/SinestesiaRCB/gfx/ViolinsLong.sf2',"alsa")

fluidsynth.play_Note(64,0,100)
#Also tried with Note("C-5") and so forth

在其他中使用 one of this sf2 files。但后来我得到了错误:

fluidsynth: warning: Failed to set thread to high priority

fluidsynth: warning: No preset found on channel 9 [bank=128 prog=0]

研究了一下,this answer said:

For General MIDI compatibility, the default sound font instrument assignments are bank 0, program 0 ("Acoustic Grand Piano") for channels 0–8 and 10–15, and bank 128, program 0 ("Default Drum Set") for channel 9. Apparently, your sound font does not have the latter. This does not matter if your MIDI file does not assume General MIDI compatible instruments and does not try to play drum sounds on channel 9.

但除此之外,这可能会使事情变得更清楚一些,我仍然不知道如何解决它。

另外,如果我使用 same file 作为第一个 link 的答案,那么我得到的错误就是这个错误(也不知道如何解决):

fluidsynth: warning: Failed to set thread to high priority

fluidsynth: warning: Failed to pin the sample data to RAM; swapping is possible.

更新

运行 具有 sudo 权限的程序 消除了这个错误,但它 不正常 。这样,我得到的错误是:

QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-root'

我认为这可能是 fluidsynth 驱动程序中所选驱动程序模式的问题,但我已经尝试了所有(alsa、oss 等),结果相同。为了完整起见,我 运行 将它放在虚拟机中,并且它内部的其他声音在我的主机扬声器中正确再现。

但是根据this answer,我想应该通过传递给它正确的声音环境变量来解决。我不知道。我试过:

pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY python3 /home/btc/Escritorio/SinestesiaRCB/SinestesiaRCB.py

需要文件的完整路径,因为这需要 sudo 权限并更改其工作目录,最后出现相同的错误:XDG_RUNTIME_DIR.

更新 2

如果我 运行 它带有 sudo -E 选项,错误将被一个新的替换:

QStandardPaths: wrong ownership on runtime directory /run/user/1000, 1000 instead of 0

我正在阅读 in some webs1000 用户应该是默认用户而不是 root。

因此,我做了一个:

sudo chown root:root /run/user/1000

刚刚试了一下,然后应用程序 运行s 没有失败,但仍然没有声音

更新 3

基于this example and this Q&A,我试过多次使用 time sleep ,并在 play_note 之后(也在之前)使用 raw_input,但它还是没声音。

我会回答我自己的问题,但是 debug/solving 过程的很大一部分是在问题本身中完成更新。

最后一部分是让它发出声音,甚至没有等待 sleep 就让它工作了。甚至在 play_note 函数之前和之后都没有这样做。我可以说这个函数总是返回True,所以这个音符从一开始就被期望发声。问题是,SF2 文件(将近 150MB)已成功加载,或者至少看起来是这样,因为它也返回 True 并且速度肯定非常快。

解决方法

让我们在执行我的脚本时(就在问题中的更新 3 之前)达到没有打印错误的地步后继续。

我想检查我的脚本有多少 CPU 的使用率,所以我在我的 Linux 终端上使用 top 并找到 pulseaudio 运行 来自几天前:

杀死这个进程让它终于响起。但是,我必须说,在play_Note()函数之后加了一个time.sleep(),大约0.25秒,目的是让它完整地播放音符。