Extract Wav File Data for Code
A python script to process WAV files; removing silence, changing resolution, and outputting specific numbers of samples.
Back in 2018, I developed Seismograf, a Eurorack module that outputs drum samples. I wanted a way of quickly trying out different samples, so wrote a python script that would automate the processing and conversion of WAV files for me, outputting the samples in a format that I could easily paste into code. I didn’t plan on sharing that script, but I recently saw someone trying to do the same thing on Facebook and figured it might be worth sharing my script.
For Seismograf, I needed 10 bit samples that would fit neatly into ‘power of two’ length blocks like 1024 words or 2048 words (where a word was 14 bits long).
I wanted to be able to trial many different samples quickly, so I really didn’t want to have to waste time in a sound processing program manually trimming away silence, or amplifying the sound, or downsampling to 10 bit, or whatever. That would be really time consuming, so I tried to automate those tasks and wrote a python script to do it all for me.
It outputs a txt file containing comma-separated decimal-representations of the wav file in a specified resolution and with a specified number of steps
I saw a synth DIY’er wanting to do similar on Facebook (actually I was tagged in the post), so figured I’d share this script.
Use The Script
Get the script here, or download it with wget:
wget https://gist.githubusercontent.com/Jetroid/e1f8c3234d9831b90fd19d8aadb609a1/raw/626a671e847cbadfa397614014b3dbee68adc432/waveread.py
Then, install python’s wave
library:
pip install wave
Then you can execute the program like this (make sure you’re using python 3):
python waveread.py /path/to/sound.wav r s
where r
represents the resolution in bits, and s
represents the number of samples in bits.
So for example, to process the file bassdrum.wav
, and output 4096 samples (4096 = 2^12) that have 10 bit resolution, run the script like this:
python waveread.py ./bassdrum.wav 10 12
What this script does
You provide it with three parameters: the filename; the resolution in bits; and the number of steps in bits (ie 11 bit steps = 2048 total samples outputted)
The script then
- Opens the wav file you give it.
- Attempts to find the start and end of the sample, then crops away any silence
- Rescales the audio so that the loudest (highest amplitude) part of the sample is equal to the highest/lowest possible value for your given resolution. (ie: All wav files processed will have the same ‘loudness’)
- Determines the ratio
n
between the number of samples needed and the number of samples available. Example: if the cropped wav file has 8192 samples, and we want to output 2048 samples, then the ration = 4.0
- Selects every
n
th sample from the cropped wav. Therefore we get the correct number of samples. - Writes all of the new samples to a
.txt
file with the same name as wav file, in the same directory as the wav file. The data is represented as decimals. Each sample is separated by a comma. For ease of viewing, a line break has been inserted every 16 samples.
What this script does not do
- Losslessly recreate the sample. By definition, some data is lost to A) downsampling to the correct resolution. B) downsampling to the correct number of steps.
- It doesn’t have an option to output every sample in a wav file / output non-power-of-two numbers of samples. Sorry, but I wrote this for my needs and I needed specific numbers of samples. The script will probably be
- Correct the centerline / silence line / zero line. (I’m not sure what to call this). If your sample has ‘zero’ (silence) as a value above or below that, this script will not correct that, and may manipulate the sample incorrectly.
Please credit
If you use this script for anything other than personal use, please credit myself (Jet Holt) and Frequency Central.
Example:
“Thanks to Jet Holt and Frequency Central for waveread.py.”
Include hyperlinks to jetholt.com and frequencycentral.co.uk where possible.
P.S. I'm late to the party, but I recently got a twitter account that you can follow here.