//------------------------------------------------------------------- // mknoises.cpp // // This program generates the Pulse Code Modulation data for a // stereophonic demonstration for one second of 'white noise', // (using the 'rand()' library-function to generate pulse-data // first for the left channel and then for the right channel). // You can play the 'noises.wav' file with our 'pcmplay' tool. // // compile using: $ make mknoises // execute using: $ mknoises // // NOTE: This program is a revision of our 'makewave.cpp' demo // which created a Waveform Audio File for a single channel of // audio playback. Here the audio file's data-chunk has twice // the previous size because each 'sample' describes the 8-bit // intensity for a left-hand channel and a right-hand channel. // // programmer: ALLAN CRUSE // written on: 14 SEP 2005 //------------------------------------------------------------------- #include // for printf(), perror() #include // for open() #include // for exit() #include // for write(), close() #define SAMPLE_RATE 8000 #define SAMPLE_SIZE 2 #define NUM_SAMPLES SAMPLE_RATE unsigned char pulsecode[ NUM_SAMPLES * SAMPLE_SIZE ]; char filename[] = "noises.wav"; int main( int argc, char **argv ) { // specify our default parameters for tone generation int sampleRate = SAMPLE_RATE; int numSamples = NUM_SAMPLES; int amplitude = 64; int silence = 128; // generate the pulse-code data for the stereo demonstration int period = NUM_SAMPLES / 2; for (int sample = 0; sample < NUM_SAMPLES; sample++) { int pulse = rand() % amplitude; int signal_left = ( sample < period ) ? 1 : 0; int signal_right = ( sample < period ) ? 0 : 1; int pulse_left = pulse * signal_left; int pulse_right = pulse * signal_right; pulsecode[ 2*sample + 0 ] = silence + pulse_left; pulsecode[ 2*sample + 1 ] = silence + pulse_right; } //---------------------------------------------- // now store the pulse-code data as a .wav file //---------------------------------------------- // setup the .wav file parameters int formatTag = 1; int nChannels = 2; int fmt_ChunkSize = 16; int dataChunkSize = numSamples * nChannels; int riffChunkSize = 4 + (8+fmt_ChunkSize) + (8+dataChunkSize); int bitsPerSample = 8; int samplesPerSec = sampleRate; int avBytesPerSec = samplesPerSec * nChannels; int frameAlignmnt = nChannels * (bitsPerSample/8); // open the .wav file for writing int fd = open( filename, O_WRONLY | O_CREAT | O_TRUNC, 0666 ); if ( fd < 0 ) { perror( filename ); exit(1); } // write the RIFF chunk write( fd, "RIFF", 4 ); write( fd, &riffChunkSize, 4 ); write( fd, "WAVE", 4 ); // write the FORMAT chunk write( fd, "fmt ", 4 ); write( fd, &fmt_ChunkSize, 4 ); write( fd, &formatTag, 2 ); write( fd, &nChannels, 2 ); write( fd, &samplesPerSec, 4 ); write( fd, &avBytesPerSec, 4 ); write( fd, &frameAlignmnt, 2 ); write( fd, &bitsPerSample, 2 ); // write the DATA chunk write( fd, "data", 4 ); write( fd, &dataChunkSize, 4 ); unsigned char *dat = pulsecode; int morebytes = dataChunkSize; while ( morebytes > 0 ) { int nbytes = write( fd, dat, morebytes ); if ( nbytes < 0 ) break; morebytes -= nbytes; dat += nbytes; } // close the .wav file close( fd ); // show the filename and filesize to our user printf( "\npulse-code data saved as \'%s\' ", filename ); printf( "(%d bytes) \n\n", 8+riffChunkSize ); }