1
我正在嘗試做一個實時播放器/記錄器循環器。我剛剛開始並贏得PyAudio。如何將NumPy數組流入流中?
目前,我能夠創建一個流並將其存儲到一個numpy數組中,但我無法在需要時將它發送回pyaudio流。
我想使用數組,因爲我寧願將所有內容都存儲在內存中,直到我想將其保存爲波形文件。
請注意,我嘗試使用Matth的python-sounddevice播放它,但我也無法播放它。我無法找到實現這一目標的方法。
Here's我當前的代碼:
import sys
import pyaudio
import numpy as np
import sounddevice as sd
p = pyaudio.PyAudio()
datos_numpy=np.array([])
CHUNK=64
CHANNELS=2
FORMAT=pyaudio.paFloat32
RATE=44100
is_recording=False
is_playing=False
def callback(in_data, frame_count, time_info, status):
global datos_numpy, is_playing, is_recording
if is_recording==True:
if in_data:
print 'recording'
temp_data=np.fromstring(in_data, dtype=np.float32)
datos_numpy = np.append(datos_numpy,temp_data)
else:
print('no input')
return in_data, pyaudio.paContinue
elif is_playing==True:
print 'playing'
#out_data= ???? HERE IS MY PROBLEM
#return out_data, pyaudio.paContinue
else:
return in_data, pyaudio.paContinue
stream = p.open(format=pyaudio.paFloat32,
channels=CHANNELS,
rate=RATE,
output=True,
input=True,
input_device_index=6,
output_device_index=14,
frames_per_buffer=CHUNK,
stream_callback=callback)
def record():
global is_playing, is_recording
is_recording=True
is_playing=False
def play():
global is_playing, is_recording, datos_numpy
is_recording=False
is_playing=True
sd.play(datos_numpy, 44100)