You are on page 1of 4

How to process audio WAV files in embedded systems http://dev.emcelettronica.

com/print/51974

Your Electronics Open Source


(http://dev.emcelettronica.com)
Home > Blog > brumbarchris's blog > Contenuti

How to process audio WAV files in


embedded systems
By brumbarchris
Created 10/01/2008 - 09:35

Technology Audio DAC embedded system microcontroller mp3 Sound wav wav file

Let's see how we can process audio WAV files in embedded systems.
A little while ago I was asked to make a small circuit based around a microcontroller and a
Digital to Analog Converter that would output various sounds corresponding to different
stimuli. These stimuli would be commands received by the microcontroller via UART, and the
output sounds would simulate a switch, or a closed door, or other ambient sounds from a
working environment. Building the hardware circuit was not that difficult – see Figure 1 for
block diagram (although I had to put in some serious efforts to keep the cost of the BOM as
low as possible) but it was the first time I was faced with outputting sound! So I had to learn.

The first obstacle that I have encountered was to determine how the output sounds will really
sound like. The description “I need a sound that will simulate the pressing of a button” is good
enough, but… how do you really make it? Fortunately, there are many available websites
providing WAV files to be used in PC applications. I came across these accidentally but I was
happy to find there various sounds that fitted the description: Soundjay, Pachd, Soundsnap

Once I found the “digital” sounds that I needed I realized the major problem I was facing was
the lack of memory. All the sounds on these websites are relatively good quality sounds,
meaning they are sampled at 44.1 kHz and each sample is coded on 16 or 32 bits. Of course,
this does not pose any problem for a PC, where you have plenty of memory available, but
unfortunately for my poor “embedded system”, I realized that for a short good quality sound

1 din 4 07.10.2008 22:15


How to process audio WAV files in embedded systems http://dev.emcelettronica.com/print/51974

lasting about 200 ms (44.1 kHz with 16bit/sample) I needed…17640 bytes of ROM on chip.
Due to cost constraints, I was bound not to use any memory chip external to the
microcontroller, but a single sound took more than the entire program memory available
(16kbyte)! Clearly, a trade-off had to be made. What I soon realized was that for such simple
sounds, which are far from music or even speech, there was no need for such good quality.
Or instead of 44.1 kHz sampling frequency I realized I could go lower. And even 16bit/sample
seemed too much, I was sure I could go even lower (8bit/sample was my target).
Performing the calculations again, I was happy to realize that my new conditions allowed for a
200ms sound sampled at 20kHz with 8bit/sample to be stored in a mere 4000 bytes…which
meant my 16kbyte program memory was enough for three sounds and the program itself. But
so far all this was theory. All I had were these computations and some WAV files. The real
challenge (for me at least – not being a software developer, but rather a hardware engineer)
was to turn a WAV file in a C array to be used in the program running on the micro,
something like:

unsigned char sound_samples[4000]={ 55, 66, 81, 83, 76, 66, 63, 62, 66, 62, 62, 82, 91, 85,
77, 70, 67, 68, 75, 80, 77, 79, 76, 74, 70, 63, 67, 70, 67, 65, 73, 71, 71, 72, 70, 61, 64, 59,
54, 62, 71, 73, 67, 67, 76, 81, 81, 70, …55, 43}

I also needed to translate the WAV file from a dual polarity signal centered on 0V into a
positive signal, with the smallest sample value corresponding to 0V. I will describe the
processing that is required to do this, organized in two steps:

Step 1 – processing the WAV file itself

First I had to convert the WAV file from 44.1 kHz to 20 kHz and for this I used one of the tools
available online called Cool Edit. There are several trial versions available out there but the
newer ones do not allow saving the modified file, so I had to turn to an older version of this
tool, called Cool Edit 2000. An open source tool that might allow you to perform the same
processing, in case you need it, is Audacity, available at: Audacity.Sourceforge

I will not detail the usage of these tools, as it is pretty straightforward (you launch the tool,
open the WAV file, cut it to 200ms, convert it from stereo to mono, change the sampling
frequency (from the top menu choose Tracks? Resample and in the text box that opens write
the new sampling frequency that you need – 20 kHz in my case) and then save the new wav
file. If you play the new WAV file on the PC, you will be able to spot a small degeneration of

2 din 4 07.10.2008 22:15


How to process audio WAV files in embedded systems http://dev.emcelettronica.com/print/51974

the quality of the sound. Once this step was completed, I had the WAV file just as I wanted it,
now I had to extract the samples from it!

Step 2 – Extracting the audio samples from the WAV file

This was a real problem, mainly because there is no available tool to do this (not any that I
know of, anyway). So I had to write my own software that would do that and I had to do some
reading about the WAV file format. Fortunately, none of these tasks is rocket science. The
WAV file format is pretty simple:

The first 44 bytes in the WAV file contain a lot of information, but which is not of any use to us
right now. Basically, these first 44 bytes form a header indicating (amongst others): the
sample frequency (but we already know that: 20 kHz), the number of bits per sample (we
know that too: 16bits/sample), the length of the “Data” section – meaning how many samples
are in the file (but we know that too: 4000 samples, because I cut the file to 200ms in the
audio editing tool at step 1). So what I really needed from this file was the data (all 4000
samples), which is to be found from the 44th byte in the file forth! For this, I had to write my
own code using C# programming language, with a free (and open source too!) compiler
known as Sharpdevelop and available under: Codeplex

Even if you are not a professional programmer, C# allows you to easily manipulate text and
binary files. The program I wrote [see the attachment] takes a wav file as an input
generating the C array that I need. It reads each sample from the wav file, adds a fixed
amount to each samples (so that all samples become positive numbers), scales it down from
16 bit to 8 bit (a simple division by 256) and then creates a text file which contains the array in
the standard C form:

unsigned char sound_samples[4000]={ 55, 66, 81, 83, 76, 66, 63, 62, 66, 62, 62, 82, 91, 85,
77, 70, 67, 68, 75, 80, 77, 79, 76, 74, 70, 63, 67, 70, 67, 65, 73, 71, 71, 72, 70, 61, 64, 59,
54, 62, 71, 73, 67, 67, 76, 81, 81, 70, …55, 43}

The effect of this processing can easily be illustrated with a excel graph, showing the sound
signal before processing and the sound signal after processing. The graph shows in blue the
raw samples (centered on 0), and in yellow the samples translated to a positive interval (by
adding a fixed amount).

3 din 4 07.10.2008 22:15


How to process audio WAV files in embedded systems http://dev.emcelettronica.com/print/51974

So, after dividing the translated samples by 256 (to convert them to 8 bit/sample) the C#
program simply generates a text file containing the C array.

Outputting the sound to the speaker

Once this processing was completed, I was ready to embed this array in the C program that I
wrote for the microcontroller. So far I had only theory and computer processing. But I had yet
to hear a sound from the speaker of the embedded system. The microcontroller program was
not so difficult to write, as I was familiar somewhat with this device. 20 kHz sampling
frequency means each sample had to be sent to the DAC every 50us. This amount of time is
more than enough to write one or two bytes via SPI (8 MHz clock frequency) and after a short
effort I was delighted to hear in the speaker the sound that simulates the “pressing of a
button”!

Attachment Size
WAV_Extract.zip 16.09 KB

Trademarks

Source URL: http://dev.emcelettronica.com/how-process-audio-wav-files-embedded-systems

4 din 4 07.10.2008 22:15

You might also like