howto stream numpy array into pyaudio stream?

喜你入骨 提交于 2021-02-04 16:06:24

问题


I'm writing a code that supposed to give some audio output to the user based on his action, and I want to generate the sound rather than having a fixed number of wav files to play. Now, what I'm doing is to generate the signal in numpy format, store the data in a wav file and then read the same file into pyaudio. I think this is redundant, however, I couldn't find a way to do that. My question is, can I stream a numpy array (or a regular list) directly into my the pyaudio to play?


回答1:


If its just playback and does not need to be synchronised to anything then you can just do the following:

# Open stream with correct settings
stream = self.p.open(format=pyaudio.paFloat32,
                         channels=CHANNELS,
                         rate=48000,
                         output=True,
                         output_device_index=1
                         )
# Assuming you have a numpy array called samples
data = samples.astype(np.float32).tostring()
stream.write(data)

I use this method and it works fine for me. If you need to record at the same time then this won't work.




回答2:


If you are just looking to generate audio tones then below code may be useful,

It does need pyaudio that can be installed as

pip install pyaudio

Sample Code

#Play a fixed frequency sound.
from __future__ import division
import math
import pyaudio

#See http://en.wikipedia.org/wiki/Bit_rate#Audio
BITRATE = 44100 #number of frames per second/frameset.      

#See http://www.phy.mtu.edu/~suits/notefreqs.html
FREQUENCY = 2109.89 #Hz, waves per second, 261.63=C4-note.
LENGTH = 1.2 #seconds to play sound

NUMBEROFFRAMES = int(BITRATE * LENGTH)
RESTFRAMES = NUMBEROFFRAMES % BITRATE
WAVEDATA = ''    

for x in xrange(NUMBEROFFRAMES):
WAVEDATA = WAVEDATA+chr(int(math.sin(x/((BITRATE/FREQUENCY)/math.pi))*127+128))    

#fill remainder of frameset with silence
for x in xrange(RESTFRAMES): 
WAVEDATA = WAVEDATA+chr(128)

p = pyaudio.PyAudio()
stream = p.open(format = p.get_format_from_width(1), 
            channels = 1, 
            rate = BITRATE, 
            output = True)
stream.write(WAVEDATA)
stream.stop_stream()
stream.close()
p.terminate()

Code is slightly modified from this askubuntu site




回答3:


You can directly stream the data through pyaudio, there is no need to write and read a .wav file.

import pyaudio
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paFloat32,
                channels=1,
                rate=44100,
                frames_per_buffer=1024,
                output=True,
                output_device_index=1
                )
samples = np.sin(np.arange(50000)/20)
stream.write(samples.astype(np.float32).tostring())
stream.close()


来源:https://stackoverflow.com/questions/30675731/howto-stream-numpy-array-into-pyaudio-stream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!