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 scipy.io.wavfile as wf

p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paFloat32,
                channels=2,
                rate=44100,
                input=True,
                input_device_index=0,
                frames_per_buffer=1024)
frames = [[],[]]  # Initialize array to store frames

print("recording...")

for i in range(0, 256):
    data = stream.read(1024)
    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))


stream.close()
p.terminate()

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

Comments

You need to log in to post comments