#include #include using namespace std; /* * Program to demonstrate audiofile library for reading and writing * audio files. Compile with "g++ -laudiofile -lm main.cc" * * Author: Anselm Blumer, ablumer@cs.tufts.edu, September 2009 */ int main (int argc, char * const argv[]) { AFfilehandle infile; AFfilehandle outfile; AFfilesetup infilesetup; AFfilesetup outfilesetup; int bytesperframe; int numframes; int framesread; int chans; // number of channels (2 for stereo) int sampfmt; // format code int sampwidth; // bits per sample (per channel) int writeval; // number of frames actually written int samplebytes; // total number of sample bytes in file int arraysize; // total number of samples in file double samplerate; // number of samples per second // Get a FileSetup data structure and open the input file infilesetup = afNewFileSetup(); infile = afOpenFile( "tmp.aif", "r", infilesetup ); if (!infile) { cout << "Unable to open input file" << endl; return 1; } // Get and print file characteristics bytesperframe = (int) afGetFrameSize( infile, AF_DEFAULT_TRACK, 0 ); numframes = (int) afGetFrameCount ( infile, AF_DEFAULT_TRACK); cout << "File contains " << numframes << " frames with " << bytesperframe << " bytes each" << endl; samplebytes = (bytesperframe * numframes); arraysize = samplebytes >> 1; char *samples = new char[ samplebytes ]; short *samplearray = (short *) samples; chans = afGetChannels (infile, AF_DEFAULT_TRACK); cout << chans << " channels" << endl; afGetSampleFormat( infile, AF_DEFAULT_TRACK, &sampfmt, &sampwidth ); cout << "Sample format: " << sampfmt << " width: " << sampwidth << endl; samplerate = afGetRate( infile, AF_DEFAULT_TRACK ); cout << "Sample rate: " << samplerate << endl; // Read the samples from the file and print the first six framesread = afReadFrames( infile, AF_DEFAULT_TRACK, samples, numframes ); cout << framesread << " frames were read" << endl; cout << "First six samples:" << endl; for (int i=0; i<6; i++) { cout << samplearray[i] << endl; } // Get a FileSetup data structure and open the output file outfilesetup = afNewFileSetup(); afInitFileFormat (outfilesetup, AF_FILE_AIFF); outfile = afOpenFile( "tmp2.aif", "w", outfilesetup ); // Write the samples to the output file and print confirmation writeval = afWriteFrames( outfile, AF_DEFAULT_TRACK, samples, numframes ); cout << "afWriteFrames returned " << writeval << endl; // Close both files afCloseFile( infile ); afCloseFile( outfile ); return 0; }