有人用PyAudio成功播放了5.1频道吗?
我写了一个程序,可以简单地从stdin播放PCM(RAW,16bit / 48kHz)。 它应该像这样使用:
cat some_2ch.pcm | python my_player.py 1 2
cat some_6ch.pcm | python my_player.py 1 6
我尝试使用此程序播放2、4、6个频道,但播放6个频道时不起作用。所有频道中都有噪音,它们使我几乎听不到音乐。
由于该问题仅在6声道播放中发生,因此我怀疑我的声卡功能异常,并尝试了其他声卡,但出现了噪音。
我确认我的PCM文件是正常的。 (我将其导入Audacity,并且没有噪音。)
环境:
程序:my_player.py
import sys
import pyaudio
class MyPlayer(object):
def __init__(self, device_id, channels):
self.pcm_bit = pyaudio.paInt16
self.pcm_rate = 48000
self.dev_output = device_id
self.buf_size = 1024 * channels
self.channels = channels
def play(self):
pa = pyaudio.PyAudio()
stream = pa.open(format=self.pcm_bit,
rate=self.pcm_rate,
channels=self.channels,
frames_per_buffer=self.buf_size,
input=False,
output=True,
output_device_index=self.dev_output)
stream.start_stream()
# read data from stdin and play them
while stream.is_active():
buffer = sys.stdin.buffer.read(self.buf_size)
if len(buffer) == 0:
break
stream.write(buffer)
stream.stop_stream()
stream.close()
pa.terminate()
if __name__ == '__main__':
# usage: python my_player.py OUTPUT_DEVICE_ID CHANNELS
# example: cat audio6ch.pcm | python my_player.py 1 6
dev = int(sys.argv[1])
c = int(sys.argv[2])
mp = MyPlayer(dev, c)
mp.play()