/* * seq2wav, part of AutoZen * Steven James * Linux Labs http://www.linuxlabs.com * http://www.linuxlabs.com/software/AutoZen.html * * This is Free software, licensed under the GNU Public License V2. * * Version: 1.3 */ #include #include #include #include #include #include #include #include ////////////////////////////////// // // .wav header // ////////////////////////////////// typedef struct { char riff[4]; // = "RIFF" unsigned int total_length; char id[8]; // = "WAVEfmt_" int fmtlen; // = 0x10 short int format; // = 01 short int channels; unsigned int sample_rate; unsigned int bytes_per_second; short int bytes_per_sample; short int bits_per_sample; char filler[4]; // = "DATA" unsigned int sample_bytes; } __attribute__ ((packed)) wav_header; wav_header wh; ////////////////////////////////// // // Waveform generation // ///////////////////////////////// #define SAMPLE_RATE 44100 #define MAX_HARMONICS 10 #define DEFAULT_HARMONICS 3 #define ZERO_CROSSING 32768 int nHarmonics = DEFAULT_HARMONICS; int *WaveTable; double curval; double harmonic_curtimeL[MAX_HARMONICS]; double harmonic_curtimeR[MAX_HARMONICS]; double curtime; double curtime2; double increment=300; double detune=10.0; double volume = 50.0; /* sequencer variables */ FILE *fSequence=NULL; char szInstruction[1024]; double target; double dBeatIncrement; int count=0; volatile int seconds=0; // functions int InitWaveTable(unsigned int SampleRate) { unsigned int i; double increment = (2*M_PI) / SampleRate; double Current=0; WaveTable = (int *) calloc(SampleRate,sizeof(int)); for(i=0;i 100) volume = 100; else if (volume <0) volume = 0; } if(!strcmp(token,"FADE")) { token = strtok(NULL," ,\n"); target = atof(token); token = strtok(NULL," ,\n"); i = atoi(token); i*=10; dBeatIncrement = target - volume; dBeatIncrement/= (i); for(; i ; i--) { count += GenerateSamples(increment, detune, 1, Out); volume += dBeatIncrement; } } if(!strcmp(token,"BASE")) { /* set the base frequency */ token = strtok(NULL," ,\n"); increment = atof(token); } if(!strcmp(token,"HARMONICS")) { /* set the base frequency */ token = strtok(NULL," ,\n"); i = atoi(token); if(i \n",argv[0]); printf("\t = sequence file to read\n"); printf("\t the .wav file to create\n"); return(-1); } if((fSequence = fopen(argv[1],"r")) == NULL) { fprintf(stderr,"ERROR: cannot open %s for reading\n",argv[1]); return(-2); } if((fOut = fopen(argv[2],"w")) == NULL) { fprintf(stderr,"ERROR: cannot open %s for writing\n",argv[2]); return(-3); } InitWaveTable(SAMPLE_RATE); InitHeaderConstants(); fwrite(&wh,sizeof(wav_header), 1, fOut); RunSequence(fSequence, fOut); fclose(fSequence); fclose(fOut); // now, apply fixup to .wav header stat(argv[2], &statbuf); wh.total_length = statbuf.st_size - 8; wh.sample_bytes = statbuf.st_size - sizeof(wav_header); fOut = fopen(argv[2], "r+"); fwrite(&wh,sizeof(wav_header), 1, fOut); fclose (fOut); return(0); }