// Josiah Boning // // input.c // Code to read in a variety of audio filetypes // Currently only supports .WAV // #include "project.h" #include #include #include #include /** * The high-level read-me-a-file function. * * This basically just passes the file off the correct file-reading routine * based on filetype. */ int input(char filename[], double** arrayptr) { if (strstr(filename, ".wav")-filename == strlen(filename) - strlen(".wav")) { return readWav(filename, arrayptr); } } /** * Read in a WAV file. * * Processess a WAV file into raw waveform data. Currently only supports WAV * files with PCM encoding, one track of audio data (mono), a 44.1 khz sample * rate, and 8-bit unsigned integer sampling. */ int readWav(char filename[], double** arrayptr) { FILE* fin; char string[1024]; int size, samplerate, byterate; short audiofmt, numchannels, blockalign, bitspersample; int i; unsigned char tmp; double* datas; fin=fopen(filename,"r"); fread(string, sizeof(char), 4, fin); // "RIFF" if (strncmp(string, "RIFF", 4)) { printf("Not a valid WAV file!\n"); exit(0); } fread(string, sizeof(char), 4, fin); fread(string, sizeof(char), 4, fin); // "WAVE" if (strncmp(string, "WAVE", 4)) { printf("Not a valid WAV file!\n"); exit(0); } fread(string, sizeof(char), 4, fin); // "fmt " if (strncmp(string, "fmt ", 4)) { printf("Not a valid WAV file!\n"); exit(0); } fread(&size, sizeof(char), 4, fin); fread(&audiofmt, sizeof(char), 2, fin); if (audiofmt != 1) { printf("The WAV file must use PCM encoding.\n"); exit(0); } fread(&numchannels, sizeof(char), 2, fin); if (numchannels != 1) { printf("The WAV file must be mono.\n"); exit(0); } fread(&samplerate, sizeof(char), 4, fin); if (samplerate != 44100) { printf("The WAV file must have 44100 samples/sec.\n"); exit(0); } fread(&byterate, sizeof(char), 4, fin); fread(&blockalign, sizeof(char), 2, fin); fread(&bitspersample, sizeof(char), 2, fin); if (bitspersample != 8) { printf("The WAV file must use 8-bit sampling.\n"); exit(0); } fread(string, sizeof(char), 4, fin); if (strncmp(string, "data", 4)) { printf("Malformed WAV file.\n"); exit(0); } fread(&size, sizeof(char), 4, fin); //printf("%i\n", size); //size = 1 * ITEMS_PER_TRANS; int numitems = size; datas = (double*) fftw_malloc(size * sizeof(double)); for (i = 0; i < numitems; i++) { fread(&tmp, sizeof(char), 1, fin); datas[i] = (double) tmp - 128; } fclose(fin); *arrayptr = datas; return numitems; }