Search code examples
arrayspython-3.xaudiomicrophonepyaudio

Combining chunks of pyaudio data into a single longer chunk


I have a pyaudio stream running like so:

self.stream = self.microphone.open(format=pyaudio.paInt16, channels=1, rate=self.SAMPLING_RATE, input=True, output=True, frames_per_buffer=self.SAMPLES_PER_CHUNK)

and I am saving each chunk to an array after decoding it through numpy like so:

data = self.stream.read(self.SAMPLES_PER_CHUNK)
data = np.frombuffer(data, dtype='b')
recorded.append(list(data))

And I would later on like to be able to combine these chunks into a single array and save them to a wav file like this:

from scipy.io.wavfile import write

total = []
for i in recorded[start:stop]:
     total += i # the issue is here

write('output2.wav', 48000, np.array(total).astype('int16'))

But clearly it is not as simple as merging the arrays as the output file is always just a snippet of static. Could someone tell me how I should be doing this?


Solution

  • I actually realized that it was a question of decoding the data which means that if you change this:

    data = np.frombuffer(data, dtype='b')

    To this:

    data = np.frombuffer(data, dtype='int16')

    The rest of the code works just fine