Python 3.x RaspBerry Pi 4+;上带有PyGame的音频cdrom没有声音;,使用ALSA

Python 3.x RaspBerry Pi 4+;上带有PyGame的音频cdrom没有声音;,使用ALSA,python-3.x,audio,raspberry-pi,pygame,alsa,Python 3.x,Audio,Raspberry Pi,Pygame,Alsa,我尝试使用PyGame在带有python3的覆盆子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) [M

我尝试使用PyGame在带有python3的覆盆子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环境中没有问题。 覆盆子使它更复杂一点

我的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音频频道没有任何问题,但我的raspi不喜欢“挂载”音频cd中的音频曲目。 我还对安装在我的raspi上的VLC播放器进行了测试。 打开介质(cdrom)并打开parallel
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   >                                  │
└──────────────────────────────────────────────────────────────────────────────
┌────────────────────────────── AlsaMixer v1.1.8──────────────────────────────┐
│   Gerät:bcm2835 ALSA F1:Hilfe│
│    芯片:Broadcom混音器F2:系统信息│
│ Ansicht:F3:[Wiedergabe]F4:Aufnahme F5:Alle F6:Soundkarte auswählen│
│ 元件:PCM[dB-Änderung:-9,58]Esc:Beenden│
│                                     ┌──┐                                     │
│                                     │  │                                     │
│                                     │▒▒│                                     │
│                                     │▒▒│                                     │
│                                     ├──┤                                     │
│                                     │面向对象│                                     │
│                                     └──┘                                     │
│                                      59│
│                                  <  PCM>│
└──────────────────────────────────────────────────────────────────────────────
到目前为止,我认为这不是我的raspi声音配置的问题,而是PyGame的问题。似乎PyGame无法与ALSA合作。
我不知道我犯了什么错误。希望这里的其他人能给我一个建议。

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

您是否将CDROM连接到Raspberry PI?是的,它是一个没有音频输出的外部USB CDROM。正如我所描述的,VLC播放音频曲目,并通过Raspberry Pi的模拟输出将声音传送给我的外部Bose SoundTouch 10。在我看来,PyGame向CDROM发送命令(我可以听到CDROM的音轨变化和工作声音),但它不向外部扬声器发送音乐。没有音乐,就没有乐趣!!!我没有连接到RPi的CDROM,因此无法看到此问题。我甚至无法在笔记本上查看它,因为我有几年没有CDROM了。坦白地说,我很惊讶有人还在使用CDROM。我的问题的解决方案非常非常简单!!!