Audio simulations that will knock your SoX off!Frank Eory
10/21/2010 8:23 PM EDT
If you work in audio hardware design, I'm sure there are times when you've wanted to actually hear the results of a particular signal processing task -- to hear the output of your simulation -- to make sure the algorithm or filter achieved the effect you wanted. Sometimes examining the FFT of the simulation output just doesn't cut it...you really want to HEAR it.
Now you're probably thinking, why not just use the wavread & wavwrite functions in Matlab? After all, if you're working in audio, you probably have access to Matlab.
But even if you do have Matlab, when you're working on a hardware design, at some point you're simulating Verilog or VHDL RTL, or a combination of RTL and SPICE in a mixed-signal simulation. Perhaps your simulation environment has all the analysis capability you need, except for the ability to read & write .wav files -- files you could actually play and hear -- and as much as you love Matlab, it's a pain to go back & forth between tools, maybe even between computers, just to be able to hear the results of your fabulous signal processing.
To the rescue comes Sox, a multi-platform utility that does audio format conversions and a whole bunch of other stuff.
Sox is one of the few programs that can read a plain text file of numeric values and turn it into a .wav file. Likewise, it can read a .wav file and turn it into a text file of numeric values, so you can make more interesting stimuli for your simulations that just the usual sine waves!
Take for example a hypothetical Verilog RTL simulation in which you have captured the audio sample times and audio sample values for an output channel in a text file as floating point numbers, like this:
Just add a one line comment at the top of the file that indicates the sampling rate in Hz, as follows:
; Sample Rate 44100
Make sure your text file name ends in .dat and sox will understand exactly how to convert it in a simple one line shell command:
sox -V outfile.dat outfile.wav
sox can also play the resulting .wav file on most platforms.
And again, for more interesting stimuli to run through those Verilog or mixed-signal simulations, you can run:
sox -V piece_of_music.wav piece_of_music.dat
and you will get a text file of sample times and audio sample values that, with minor massaging, can be applied as stimulus to just about any simulator you might be using.
I recently had a need to do just such a text-to-wav conversion and despite my best search efforts, found it difficult to find a clear example of using sox for this conversion.
So if anyone else has a similar need, I hope you find this helpful.