Sox Can Hear ME Talk!

Sox can now hear me talk! That was a pain to set up. Getting the microphone to work was the easy part. The I2S microphone installation went well following the adafruit installation instructions, as mentioned in the post Sox can Hear, Sox can Talk, but getting python to get any audio out of it was painful.


For this reason I am sharing the code below


import pyaudio
import numpy as np
import as wf

p = pyaudio.PyAudio()
stream =,
frames = [[],[]]  # Initialize array to store frames


for i in range(0, 256):
    data =
    decoded = np.frombuffer(data, np.float32)
    decodedSplit = np.stack((decoded[::2], decoded[1::2]), axis=0)  # channels on separate axes
    frames = np.append(frames, decodedSplit, axis=1)
    #data = np.fromstring(data, np.float32).astype(np.float64)
    #data = np.max(np.abs(data))
    #print('{:.4f}'.format(data), '*' * int(data * 20))


wf.write('test.wav', 44100, frames.T)

It looks quite simple now, but the main issue is that the audio recording would not work on int16, resulting in static, it has to be float32 inside for the format. As it is float32, the scipi write wave format has to be used instead, and of course they are the transpose of each other. Hopefully this helps someone


You need to log in to post comments