python播放音乐流_Python:使用PyAudio(或其他工具)进行实时音频流传输?

Currently I'm using NumPy to generate the WAV file from a NumPy array. I wonder if it's possible to play the NumPy array in realtime before it's actually written to the hard drive. All examples I found using PyAudio rely on writing the NumPy array to a WAV file first, but I'd like to have a preview function that just spits out the NumPy array to the audio output.

Should be cross-platform, too. I'm using Python 3 (Anaconda distribution).

解决方案

This has worked! Thanks for help!

def generate_sample(self, ob, preview):

print("* Generating sample...")

tone_out = array(ob, dtype=int16)

if preview:

print("* Previewing audio file...")

bytestream = tone_out.tobytes()

pya = pyaudio.PyAudio()

stream = pya.open(format=pya.get_format_from_width(width=2), channels=1, rate=OUTPUT_SAMPLE_RATE, output=True)

stream.write(bytestream)

stream.stop_stream()

stream.close()

pya.terminate()

print("* Preview completed!")

else:

write('sound.wav', SAMPLE_RATE, tone_out)

print("* Wrote audio file!")

Seems so simple now, but when you don't know Python very well, it seems like hell.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值