#include #include #include "waveio.h" #define SAMPLE_RATE 44100 //#define HEX int main(int argc, char **argv) { FILE *srcfile, *wavfile; int sample1, sample2, scan; int len = 0; char buf[1024]; if(argc < 3) { printf("Usage: %s srcfile wavfile\n", *argv); printf(" Convert srcfile to a WAV file. srcfile must be a text file\n"); printf(" with one (for mono) or two (for stereo) 16-bit decimal samples per line.\n"); printf("Example: %s mysounds.raw mysounds.wav\n", *argv); exit(0); } if(NULL == (srcfile = fopen(argv[1], "r"))) { printf("Error opening source file\n"); exit(1); } if(NULL == (wavfile = open_wav(argv[2],SAMPLE_RATE))) { printf("Error opening destination file\n"); exit(1); } while(fgets(buf, 1024, srcfile)) { #ifdef HEX if(sscanf(buf, "%x, %x", &sample1, &sample2) < 2) if((scan = sscanf(buf, "%x %x", &sample1, &sample2)) < 2) { #else if(fscanf(srcfile, "%d, %d", &sample1, &sample2) < 2) if((scan = fscanf(srcfile, "%d %d", &sample1, &sample2)) < 2) { #endif if(scan == 1) sample2 = sample1; // mono sound else sample1 = sample2 = 0; } fwrite(&sample1, 2, 1, wavfile); // left channel fwrite(&sample2, 2, 1, wavfile); // right channel len+=2; } close_wav(wavfile,len); fclose(srcfile); }