'pyaudio save multiple .WAV file with nonblocking

Updates: Now I found out that we can paste some code in the callback function and ended up more questions:

  1. When will be call and stop the callback functions? when we open and close the stream?

  2. The callback function can return the stream data(audio_data from the code). As we did not call the function, the pyaudio do it internally I believe. How do I get the return stream data from callback?

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()

=============================================

I am trying to record multiple channels into multiple .WAV file. I can do that with stream.read() and numpy array to separate into different array, and save to .WAV file

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()

However, from the module documentation, https://people.csail.mit.edu/hubert/pyaudio/docs/#class-stream, it said stream.read() and stream.write() should not be used for non-blocking.

And I found a good non-blocking pyaudio example from Github: https://gist.github.com/sloria/5693955 Which did not use stream.read().

I am not sure if I can read and turn steam numpy array without stream.read() So is it still possible to export the stream in to different .WAV? and make it non blocking?

Thanks



Solution 1:[1]

As I learn more in coding, I found the answers.

A1: The callback function run and stop with the stream.

######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: To capture data in real time, we can use 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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Giorgos Xou