pyaudio 以非阻塞方式保存多个 .WAV 文件
pyaudio save multiple .WAV file with nonblocking
更新:
现在我发现我们可以在回调函数中粘贴一些代码并结束了更多问题:
什么时候调用和停止回调函数?我们什么时候打开和关闭流?
回调函数可以return流数据(audio_data来自代码)。由于我们没有调用该函数,我相信 pyaudio 会在内部调用它。如何从回调中获取 return 流数据?
import pyaudio
import wave
import numpy as np
import npstreams
import time
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
audio = pyaudio.PyAudio()
channel_1_frames = []
channel_2_frames = []
fulldata = np.array([])
def callback(in_data, frame_count, time_info, flag):
global b,a,fulldata #global variables for filter coefficients and array
audio_data = np.fromstring(in_data, dtype=np.int16)
channel_1 = audio_data[0::CHANNELS]
channel_2 = audio_data[1::CHANNELS]
data1 = channel_1.tostring()
data2 = channel_2.tostring()
channel_1_frames.append(data1)
channel_2_frames.append(data2)
wf1 = wave.open('Channel_1.wav', 'wb')
wf2 = wave.open('Channel_2.wav', 'wb')
wf1.setnchannels(1)
wf2.setnchannels(1)
wf1.setsampwidth(audio.get_sample_size(FORMAT))
wf2.setsampwidth(audio.get_sample_size(FORMAT))
wf1.setframerate(RATE)
wf2.setframerate(RATE)
wf1.writeframes(b''.join(channel_1_frames))
wf2.writeframes(b''.join(channel_2_frames))
wf1.close()
wf2.close()
return (audio_data, pyaudio.paContinue)
stream = audio.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
#frames_per_buffer=CHUNK,
stream_callback=callback)
stream.start_stream()
while stream.is_active():
time.sleep(10)
stream.stop_stream()
stream.close()
audio.terminate()
=============================================
我正在尝试将多个频道录制到多个 .WAV 文件中。
我可以用 stream.read() 和 numpy 数组分离成不同的数组,并保存到 .WAV 文件
stream = audio.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
print("* recording")
channel_1_frames = []
channel_2_frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
# convert string to numpy array
data_array = np.frombuffer(data, dtype='int16')
# select channel
channel_1 = data_array[0::CHANNELS]
channel_2 = data_array[1::CHANNELS]
# convert numpy array to string
data1 = channel_1.tostring()
data2 = channel_2.tostring()
channel_1_frames.append(data1)
channel_2_frames.append(data2)
stream.stop_stream()
stream.close()
audio.terminate()
但是,根据模块文档,https://people.csail.mit.edu/hubert/pyaudio/docs/#class-stream,它说 stream.read() 和 stream.write() 不应该用于非阻塞。
我从 Github 中找到了一个很好的非阻塞 pyaudio 示例:https://gist.github.com/sloria/5693955
哪个没有使用 stream.read().
我不确定我是否可以在没有 stream.read() 的情况下读取和转换 steam numpy 数组
那么是否仍然可以将流导出到不同的 .WAV 中?并使其非阻塞?
谢谢
随着我在编码方面学到更多,我找到了答案。
A1:回调函数运行随流停止
######open stream with out starting automatically
audio = pyaudio.PyAudio()
stream = audio.open(format=format,
channels=2,
rate=44100,
input=True,
frames_per_buffer=44100,
stream_callback=self.get_callback(),
start=False)
######start,stop stream
stream.start_stream()
stream.close()
audio.terminate()
A2:实时抓取数据,我们可以使用queue
self.recorded_frames = queue.Queue()
def get_callback(self):
def callback(in_data, frame_count, time_info, status):
self.recorded_frames.put(np.frombuffer(in_data, dtype=np.int16))
return in_data, pyaudio.paContinue
return callback
更新: 现在我发现我们可以在回调函数中粘贴一些代码并结束了更多问题:
什么时候调用和停止回调函数?我们什么时候打开和关闭流?
回调函数可以return流数据(audio_data来自代码)。由于我们没有调用该函数,我相信 pyaudio 会在内部调用它。如何从回调中获取 return 流数据?
import pyaudio
import wave
import numpy as np
import npstreams
import time
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
audio = pyaudio.PyAudio()
channel_1_frames = []
channel_2_frames = []
fulldata = np.array([])
def callback(in_data, frame_count, time_info, flag):
global b,a,fulldata #global variables for filter coefficients and array
audio_data = np.fromstring(in_data, dtype=np.int16)
channel_1 = audio_data[0::CHANNELS]
channel_2 = audio_data[1::CHANNELS]
data1 = channel_1.tostring()
data2 = channel_2.tostring()
channel_1_frames.append(data1)
channel_2_frames.append(data2)
wf1 = wave.open('Channel_1.wav', 'wb')
wf2 = wave.open('Channel_2.wav', 'wb')
wf1.setnchannels(1)
wf2.setnchannels(1)
wf1.setsampwidth(audio.get_sample_size(FORMAT))
wf2.setsampwidth(audio.get_sample_size(FORMAT))
wf1.setframerate(RATE)
wf2.setframerate(RATE)
wf1.writeframes(b''.join(channel_1_frames))
wf2.writeframes(b''.join(channel_2_frames))
wf1.close()
wf2.close()
return (audio_data, pyaudio.paContinue)
stream = audio.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
#frames_per_buffer=CHUNK,
stream_callback=callback)
stream.start_stream()
while stream.is_active():
time.sleep(10)
stream.stop_stream()
stream.close()
audio.terminate()
=============================================
我正在尝试将多个频道录制到多个 .WAV 文件中。 我可以用 stream.read() 和 numpy 数组分离成不同的数组,并保存到 .WAV 文件
stream = audio.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
print("* recording")
channel_1_frames = []
channel_2_frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
# convert string to numpy array
data_array = np.frombuffer(data, dtype='int16')
# select channel
channel_1 = data_array[0::CHANNELS]
channel_2 = data_array[1::CHANNELS]
# convert numpy array to string
data1 = channel_1.tostring()
data2 = channel_2.tostring()
channel_1_frames.append(data1)
channel_2_frames.append(data2)
stream.stop_stream()
stream.close()
audio.terminate()
但是,根据模块文档,https://people.csail.mit.edu/hubert/pyaudio/docs/#class-stream,它说 stream.read() 和 stream.write() 不应该用于非阻塞。
我从 Github 中找到了一个很好的非阻塞 pyaudio 示例:https://gist.github.com/sloria/5693955 哪个没有使用 stream.read().
我不确定我是否可以在没有 stream.read() 的情况下读取和转换 steam numpy 数组 那么是否仍然可以将流导出到不同的 .WAV 中?并使其非阻塞?
谢谢
随着我在编码方面学到更多,我找到了答案。
A1:回调函数运行随流停止
######open stream with out starting automatically
audio = pyaudio.PyAudio()
stream = audio.open(format=format,
channels=2,
rate=44100,
input=True,
frames_per_buffer=44100,
stream_callback=self.get_callback(),
start=False)
######start,stop stream
stream.start_stream()
stream.close()
audio.terminate()
A2:实时抓取数据,我们可以使用queue
self.recorded_frames = queue.Queue()
def get_callback(self):
def callback(in_data, frame_count, time_info, status):
self.recorded_frames.put(np.frombuffer(in_data, dtype=np.int16))
return in_data, pyaudio.paContinue
return callback