5.3.1 Reading and Writing Audio Files in MATLAB

5.3.1 Reading and Writing Audio Files in MATLAB

A number of exercises in this book ask that you experiment with audio processing by reading an audio file into MATLAB or a C++ program, manipulate it in some way, and either listen directly to the result or write the result back to a file. In Chapter 2 we introduced how to read in WAV files in MATLAB. Let’s look at this in more detail now.

In some cases, you may want to work with raw audio data coming from an external source. One way to generate raw audio data is to take a clip of music or sound and, in Audacity or some other higher level tool, save or export the clip as an uncompressed or raw file. Audacity allows you to save in uncompressed WAV or AIFF formats (Figure 5.31).

Figure 5.31 Exporting an uncompressed audio file in Audacity
Figure 5.31 Exporting an uncompressed audio file in Audacity

From the next input box that pops open, you can see that although this file is being saved as uncompressed PCM, it still has a WAV header on it containing metadata.

Figure 5.32 Prompt for metadata to be stored in header of uncompressed WAV file
Figure 5.32 Prompt for metadata to be stored in header of uncompressed WAV file

The WAV file can be read an array in MATLAB with the following:

xWav = audioread('HornsE04Mono.wav');

The audioread function strips the header off and places the raw audio values into the array x. These values have a maximum range from -1 to 1. If you want to know the sampling rate sr and bit depth b, you can use this:

[xWav, sr, b] = audioread('HornsE04Mono.wav');

If the file is stereo, you’ll get a two-dimensional array with the same number of samples in each channel.

Adobe Audition allows you to save an audio file as raw data with no header by deselecting the “Save extra non-audio information” box (Figure 5.33).

Figure 5.33 Saving a RAW audio file in Adobe Audition
Figure 5.33 Saving a RAW audio file in Adobe Audition

When you save a file in this manner, you need to remember the sampling rate and bit depth. A raw audio file is read in MATLAB as follows:

fid = fopen('HornsE04Mono.raw', 'r');
xRaw16 = fread(fid, 'int16');

fid is the file handle, and the r in single quotes means that the file is being opened to be read. The type specifier int16 is used in fread so that MATLAB knows to interpret the input as 16-bit signed integers. MATLAB’s workspace window should show you that the values are in the maximum range of -32768 to 32767 ($$-2^{b-1}\: to\: 2^{b-1}-1$$) (Figure 5.34). The values don’t span the maximum range possible because they were at low amplitude to begin with.

For HornsE04Mono_8bits.raw, an 8-bit file, you can use

fid2 = fopen('HornsE04Mono_8bits.raw', 'r');
xRaw8 = fread(fid2, 'int8');

The values of xRaw8 range from -128 to 127, as shown in Figure 5.34.

Figure 5.34 Values of variables after reading in audio files in MATLAB
Figure 5.34 Values of variables after reading in audio files in MATLAB

A stereo RAW file has twice as many samples as the corresponding mono file, with the samples for the two stereo channels interleaved.

You can use the fwrite function in MATLAB to write the file back out to permanent storage after you’ve manipulated the values.