Digital Sound & Music: Concepts, Applications, & Science, Chapter 5, last updated 6/25/2013
#define TYPE char
#define RATE 44100 //sampling rate
#define SIZE sizeof(TYPE) //size of sample, in bytes
#define CHANNELS 1 //number of stereo channels
#define PI 3.14159
#define SAMPLE_MAX (pow(2,SIZE*8 - 1) - 1)
void writeToSoundDevice(TYPE* buf, int deviceID, int buffSize) {
int status;
status = write(deviceID, buf, buffSize);
if (status != buffSize)
perror("Wrote wrong number of bytes\n");
status = ioctl(deviceID, SOUND_PCM_SYNC, 0);
if (status == -1)
perror("SOUND_PCM_SYNC failed\n");
int main(int argc, char* argv[])
int deviceID, arg, status, i, numSamples;
numSamples = atoi(argv[1]);
TYPE* samples = (TYPE *) malloc((size_t) numSamples * sizeof(TYPE)*
FILE *inFile = fopen(argv[2], "rb");
fread(samples, (size_t)sizeof(TYPE), numSamples*CHANNELS, inFile);
deviceID = open("/dev/dsp", O_WRONLY, 0);
if (deviceID 0)
perror("Opening /dev/dsp failed\n");
arg = SIZE * 8;
status = ioctl(deviceID, SOUND_PCM_WRITE_BITS, &arg);
if (status == -1)
perror("Unable to set sample size\n");
status = ioctl(deviceID, SOUND_PCM_WRITE_CHANNELS, &arg);
if (status == -1)
perror("Unable to set number of channels\n");
arg = RATE;
status = ioctl(deviceID, SOUND_PCM_WRITE_RATE, &arg);
if (status == -1)
perror("Unable to set number of bits\n");
writeToSoundDevice(samples, deviceID, numSamples * CHANNELS);
FILE *outFile = fopen(argv[3], "wb");
fwrite(samples, 1, numSamples*CHANNELS, outFile);
Program 5.1 Reading raw data in a C++ program, OSS library
When you write a low-level sound program such as this, you need to use a sound library
such as OSS or ALSA. There's a lot of documentation online that will guide you through
Previous Page Next Page