python-soundfile icon indicating copy to clipboard operation
python-soundfile copied to clipboard

Can this package take a list of ADC values and convert it to an audio file?

Open gabemorris12 opened this issue 2 years ago • 3 comments
trafficstars

I have a simple question and would like to come to an understanding to see if my plan will work. I am looking to use a Raspberry Pi Pico to get ADC data from this electret microphone device. I plan to capture this ADC data with a known sampling rate and send it to a computer, so that I can use this package to convert the data into an audio file.

So the computer script would look something like this:

import soundfile as sf

# Use some communication method with the Pico to get a list of ADC values
data = []  # list of ADC values from 0 to 65535

sf.write("audio.wav", data, samplerate=44100)

I am wondering if this will work or if there is something more. I read in the docs that a one dimensional data list can create a mono file, but I am not sure what that means. Could someone provide some feedback on this topic? Will it work as is?

gabemorris12 avatar Jul 14 '23 02:07 gabemorris12

It will work, but you will have to set the data format manually, probably format='RAW' and subtype='PCM_16', and you need to supply the data as an int16 numpy array, and shift the values to go from -32768...32767 instead of 0...65536 (numpy.asarray(numpy.array(data) - 2**16), dtype='int16') should do the trick).

bastibe avatar Jul 15 '23 09:07 bastibe

@bastibe Are you sure? The RP Pico is a microcontroller which runs micropython.

On the ARM cortex RPi's I can confirm that you can save sound using soundfile.

RonaldAJ avatar Jul 15 '23 09:07 RonaldAJ

@bastibe I appreciate the response, and I'll be sure to give this a try in the coming weeks!

@RonaldAJ I'm using the Raspberry Pi Pico just as a means of getting data. Soundfile will be used on a windows 10 computer or a Linux based Rapberry Pi.

gabemorris12 avatar Jul 15 '23 15:07 gabemorris12