一、硬件和声卡主板
确认声卡设备
sudo aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: rockchipdp0 [rockchip,dp0], device 0: rockchip,dp0 spdif-hifi-0 [rockchip,dp0 spdif-hifi-0]
Subdevices: 1/1
Subdevice #0: subdevice #0
card 1: rockchipdp1 [rockchip,dp1], device 0: rockchip,dp1 spdif-hifi-0 [rockchip,dp1 spdif-hifi-0]
Subdevices: 1/1
Subdevice #0: subdevice #0
card 3: rockchipes8316c [rockchip,es8316-codec], device 0: fe470000.i2s-ES8316 HiFi es8316.7-0010-0 [fe470000.i2s-ES8316 HiFi es8316.7-0010-0]
Subdevices: 0/1
Subdevice #0: subdevice #0
card 4: rockchiphdmi0 [rockchip-hdmi0], device 0: rockchip-hdmi0 i2s-hifi-0 [rockchip-hdmi0 i2s-hifi-0]
Subdevices: 1/1
Subdevice #0: subdevice #0
card 5: rockchiphdmi1 [rockchip-hdmi1], device 0: rockchip-hdmi1 i2s-hifi-0 [rockchip-hdmi1 i2s-hifi-0]
Subdevices: 1/1
Subdevice #0: subdevice #0
安装fluidsynth
sudo apt-get install fluidsynth
下载音源文件:
启动fluidsynth
sudo fluidsynth -a alsa -m alsa_seq -l -i ./gnusmas_gm_soundfont_2.01.sf2 &
sudo aconnect -o //确认启动情况
安装对应版本的python库
sudo pip install python-rtmidi==1.4.7 -i https://pypi.tuna.tsinghua.edu.cn/simple/
sudo pip install mido==1.2.10 -i https://pypi.tuna.tsinghua.edu.cn/simple/
sudo pip list|grep mid
mido 1.2.10
python-rtmidi 1.4.7
编写代码播放歌曲:
import mido
# 获取所有可用的MIDI设备
port = mido.get_output_names()
print(port)
midi_device = port[2]
print(f"open device {midi_device}")
output = mido.open_output(port[2], autoreset=True)
# 解析MIDI文件
midi_file = mido.MidiFile(
"midi/delacey-dream-it-possible.mid"
) # 替换为你实际的MIDI文件路径
for msg in midi_file.play():
data_ser = msg.bytes()
# print(msg)
# print(data_ser)
data_ser_hex = bytearray(data_ser)
print(" ".join(f"{byte:02X}" for byte in data_ser_hex))
# if not msg.is_meta:
# if msg.type == "note_on" or msg.type == "note_off":
output.send(msg)