Digital Sound & Music: Concepts, Applications, & Science, Chapter 5, last updated 6/25/2013
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.
5.3.2 Raw Audio Data in C++
In Chapters 2 and 3, we showed you how to generate sinusoidal waveforms as arrays of values
that could be sent directly to the sound device and played. But what if you want to work with
more complicated sounds and music in C++? In this case, you'll need to be able to read audio
files into your programs in order to experiment with audio processing.
In C++, raw audio data in 8-bit or 16-bit format can be read into an array of characters
(char) or short integers (short), respectively, assuming that characters are stored in 8 bits and
short integers are 16 bits on your system. This is demonstrated in Program 5.1. You can use this
in the programming exercise on dithering and mu-law encoding.
//This program runs under OSS
//The program expects an 8-bit raw sound file.
//You can alter it to read a 16-bit file into short ints
#include sys/ioctl.h //for ioctl()
#include math.h //sin(), floor()
#include stdio.h //perror
#include fcntl.h //open
#include linux/soundcard.h //SOUND_PCM*
#include stdlib.h
#include unistd.h
using namespace std;
Previous Page Next Page