Manipulating sound samples

Many of you will use the SD card to input sound samples (and image data) to your E100 program. These will probably be stored in the SDRAM, since the E100 memory is too small to hold bulk data. This web page describes how to create and manipulate files that can be downloaded onto an SD card.

Binary data files

You will probably want to input sound and image data as 16-bit signed numbers (sometimes called binary data). Files that store numbers directly like this are called binary files. You are probably more accustomed to plain-text files, which are filled with ASCII representations of data.

To understand the difference between binary data and ASCII representations of numbers, consider two ways to write the number 12 to a file. You could write 12 to a file as two characters '1' and '2'. Each of these two characters would be written by its ASCII value. '1' is represented in ASCII by the number 49, and '2' is represented in ASCII by the number 50. To read this, your E100 program would have to read two numbers (49 and 50), translate each number from ASCII to a decimal digit, then compute the number that those two characters represented. This is unnecessarily cumbersome.

Instead of writing the number 12 as two characters (each represented by a number), you could instead simply write the number 12 as binary data to a binary file. Then your E100 could read the single number 12 from the SD card directly into a variable.

You will not be able to view binary files in a normal text editor. Text editors are meant to view plain-text files, so they interpret each number as an ASCII representation of a character. For example, if you used an editor to view a binary file that contained the number 12, your editor would try to display the character represented by ASCII value 12 (which is the line feed character and is unprintable). Instead, you will need to manipulate or view the file with a program that is intended to handle binary data, such as hexdump or your own program.

Extracting sound samples from WAV files

You may find it useful to extract sound samples from normal sound files. You can use the sox program on Linux to create a file with sound samples but no WAV format headers. For example, run the following command to extract sound samples from a WAV file file1.wav into a file file2.raw:

sox file1.wav -w -s -c 1 -r 8000 file2.raw
The .raw file will be a binary file that is filled with 16-bit numbers.

You will probably need to manipulate the data given in .raw files, e.g. to combine sound samples or to add header information such as the number of samples. To manipulate binary data in this way, you will probably need to write your own programs, e.g., in C++ or Matlab.

Writing binary data in C++

In C++, you can read and write binary data through the fstream interface's read and write methods. For example, the following program writes the 16-bit number 12 as binary data to a file.

#include <fstream>

using namespace std;

main()
{
    ofstream file;
    short number = 12;	// short is 2 bytes (16 bits)

    file.open("datafile", ios::out | ios::binary);
    file.write( (char *) &number, sizeof(number) );
}

Writing binary data in Matlab

In MATLAB, you can use the fread and fwrite functions to read and write binary data (remember that this is 16-bit signed data, so use the 'int16' precision). For example, the following program writes the 16-bit number 12 as binary data to a file.

number = 12;
fid = fopen('datafile', 'w');
fwrite(fid, number, 'int16');
fclose(fid);

Combining files

The Unix utility cat can concatenate multiple files into a single file. E.g., to concatenate three files file1.raw, file2.raw, and file3.raw into a single, combined file all.raw, run the following command:

cat file1.raw file2.raw file3.raw > all.raw