python-3.x - 使用 ALSA 在 RaspBerry Pi 4+ 上使用 PyGame 的音频 cdrom 没有声音

标签 python-3.x audio raspberry-pi pygame alsa

我尝试使用 PyGame 在带有 python3 的 Raspberry Pi4 上播放旧的音频 cd 收藏。
据我所知,cd audio 的数据结构与声音或流媒体不同,并且音频 CD 不像数据 CD 那样安装在文件系统中,因此需要特殊处理。
以下非常简单的程序在 Windows 10 下从 Python 的命令行以及 PyCharm IDE 中运行:

PS C:\Users\manfr> python
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 22:45:29) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from pygame import cdrom
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
>>> import pygame
>>> import time
>>> pygame.init()
(6, 0)
>>> cdrom.init()
>>> cdrom.CD(0).init()
>>>
>>> i = 0
>>> while i == 0:
...     cdrom.CD(0).play(5)
...     time.sleep(10)
...     i = 1
...     cdrom.CD(0).stop()
...
>>>

同样在 PyCharm 中没有问题:
from pygame import cdrom
import pygame
import time

pygame.init()
cdrom.init()
cdrom.CD(0).init()

i = 0
while i == 0:
    cdrom.CD(0).play(5)
    time.sleep(10)
    i = 1
    cdrom.CD(0).stop()

PyCharms“运行”窗口中的结果:
D:\Python-Programme\gui-beispiel\venv\Scripts\python.exe "D:/Python-Programme/gui-beispiel/CD-Project/Thread Test 10 PyGame playlist vereinfacht.py"
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html

Process finished with exit code 0```
And now the same code starting in the 'Python Console' of PyCharm.

在 Windows 10 环境下没有问题。
RaspberryPi 使它更复杂一些。

我的 RaspBerry Pi 上的相同程序不返回任何音乐,无论是从命令行(python 3.x)还是在 Thonny Python IDE 下。
从 python 命令行解释器运行:
pi@raspberrypi:~ $ python3
Python 3.7.3 (default, Dec 20 2019, 18:57:59) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> #!/usr/bin/python3
... import pygame
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
>>> from pygame import *
>>> from pygame import cdrom
>>> import time
>>> 
>>> pygame.init()
(6, 0)
>>> cdrom.init()
>>> cdrom.CD(0).init()
>>> 
>>> i = 0
>>> while i == 0:
...     print('cdrom bussy, but I hear no music')
...     cdrom.CD(0).play(5)
...     time.sleep(20)
...     i = 1
...     cdrom.CD(0).stop()
...     print("no idea what's went wrong :-(")
... 
cdrom bussy, but I hear no music
no idea what's went wrong :-(
>>> 

以下命令都运行良好:
pi@raspberrypi:~ $ speaker-test -t wav -c 2
speaker-test 1.1.8
Playback device is default
Stream parameters are 48000 Hz, S16_LE, 2 channels
WAV file (s)
Rate is 48000 Hz (requested: 48000 Hz)
Buffer size from 480 to 32768
Period size from 480 to 32768
Use maximum buffer size 32768
Periods = 4
set: period_size = 8192
set: buffer_size = 32768
  0 - Front left
  1 - Front right

pi@raspberrypi:~ $ omxplayer /home/pi/test.wav
Audio codec pcm_u8 channels 1 samplerate 11025 bitspersample 8
Subtitle count: 0, state: off, index: 1, delay: 0
have a nice day ;)

pi@raspberrypi:~ $ omxplayer /home/pi/song1.mp3
Audio codec mp3float channels 2 samplerate 44100 bitspersample 16
Subtitle count: 0, state: off, index: 1, delay: 0
have a nice day ;)

在我看来,我对 wav/mp3 音频 channel 没有任何问题,但我的 raspi 不喜欢“已安装”音频 cd 中的音轨。
我还对安装在我的 raspi 上的 VLC 播放器进行了测试。
打开媒体 (cdrom) 并打开并行 alsamixer显示,vlc 与 ALSA 成功通信。例如。我可以改变响度。
这是 ALSA 的信息屏幕以获取更多信息。
┌────────────────────────────── AlsaMixer v1.1.8 ──────────────────────────────┐
│   Gerät: bcm2835 ALSA                              F1:  Hilfe                │
│    Chip: Broadcom Mixer                            F2:  System-Informationen │
│ Ansicht: F3:[Wiedergabe] F4: Aufnahme  F5: Alle    F6:  Soundkarte auswählen │
│ Element: PCM [dB-Änderung: -9,58]                  Esc: Beenden              │
│                                     ┌──┐                                     │
│                                     │  │                                     │
│                                     │▒▒│                                     │
│                                     │▒▒│                                     │
│                                     ├──┤                                     │
│                                     │OO│                                     │
│                                     └──┘                                     │
│                                      59                                      │
│                                  <  PCM   >                                  │
└──────────────────────────────────────────────────────────────────────────────

到目前为止我认为不是我的raspi声音配置的问题,而是PyGame的问题。 PyGame 似乎不适用于 ALSA。
我不知道我犯了什么错误。希望这里的其他人可以给我一个建议。

最佳答案

PyGame 仅控制音频 CD(播放、暂停、停止等)。音乐本身必须取自 CDROM 的音频输出!较旧的设备确实有这样的输出,但我从未见过具有这种音频输出的新 USB 驱动器。所以我不能在我的项目中使用 PyGame。希望我的错能帮助其他人不要落入同一个陷阱。

关于python-3.x - 使用 ALSA 在 RaspBerry Pi 4+ 上使用 PyGame 的音频 cdrom 没有声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61759327/

相关文章:

java - 使用java识别mp3末尾的静音

c++ - 如何在 C++ 中使用 ffmpeg 从视频中提取音频?

haskell - 使 Haskell 代码更加地道(440 Hz 音调)

c++ - 意外的重定位类型 0x03

c - 在 Raspberry PI 上用 C 从图像中读取 RGB 数据

python - 使用正则表达式在 python 中的字符串中将 Boolean true 转换为 True

python - 判断两行中第一行是否为标题

python - 使用 python3 为 msvc 构建 boost python - 链接器错误

python-3.x - Python 中带有字符串的 3D 散点图

python - 开机运行的Shell脚本无法运行mysql