diff options
Diffstat (limited to 'src/wavfile.c')
-rw-r--r-- | src/wavfile.c | 88 |
1 files changed, 45 insertions, 43 deletions
diff --git a/src/wavfile.c b/src/wavfile.c index 9ea231b..81fa879 100644 --- a/src/wavfile.c +++ b/src/wavfile.c @@ -12,71 +12,73 @@ http://www.nd.edu/~dthain/courses/cse20211/fall2013/wavfile #include <string.h> struct wavfile_header { - char riff_tag[4]; - int riff_length; - char wave_tag[4]; - char fmt_tag[4]; - int fmt_length; - short audio_format; - short num_channels; - int sample_rate; - int byte_rate; - short block_align; - short bits_per_sample; - char data_tag[4]; - int data_length; + char riff_tag[4]; + int riff_length; + char wave_tag[4]; + char fmt_tag[4]; + int fmt_length; + short audio_format; + short num_channels; + int sample_rate; + int byte_rate; + short block_align; + short bits_per_sample; + char data_tag[4]; + int data_length; }; FILE * wavfile_open( const char *filename, int rate ) { - struct wavfile_header header; + struct wavfile_header header; - int samples_per_second = rate; - int bits_per_sample = 16; + int samples_per_second = rate; + int bits_per_sample = 16; - strncpy(header.riff_tag,"RIFF",4); - strncpy(header.wave_tag,"WAVE",4); - strncpy(header.fmt_tag,"fmt ",4); - strncpy(header.data_tag,"data",4); + strncpy(header.riff_tag,"RIFF",4); + strncpy(header.wave_tag,"WAVE",4); + strncpy(header.fmt_tag,"fmt ",4); + strncpy(header.data_tag,"data",4); - header.riff_length = 0; - header.fmt_length = 16; - header.audio_format = 1; - header.num_channels = 2; - header.sample_rate = samples_per_second; - header.byte_rate = samples_per_second*(bits_per_sample/8); - header.block_align = bits_per_sample/8; - header.bits_per_sample = bits_per_sample; - header.data_length = 0; + int channels = 2; - FILE * file = fopen(filename,"w+"); - if(!file) return 0; + header.riff_length = 0; + header.fmt_length = 16; + header.audio_format = 1; + header.num_channels = channels; + header.sample_rate = samples_per_second; + header.byte_rate = samples_per_second*(bits_per_sample/8)*channels; + header.block_align = channels*bits_per_sample/8; + header.bits_per_sample = bits_per_sample; + header.data_length = 0; - fwrite(&header,sizeof(header),1,file); + FILE * file = fopen(filename,"w+"); + if(!file) return 0; - fflush(file); + fwrite(&header,sizeof(header),1,file); - return file; + fflush(file); + + return file; } void wavfile_write( FILE *file, short data[], int length ) { - fwrite(data,sizeof(short),length,file); + fwrite(data,sizeof(short),length,file); } void wavfile_close( FILE *file ) { - int file_length = ftell(file); + int file_length = ftell(file); - int data_length = file_length - sizeof(struct wavfile_header); - fseek(file,sizeof(struct wavfile_header) - sizeof(int),SEEK_SET); - fwrite(&data_length,sizeof(data_length),1,file); + int data_length = file_length - sizeof(struct wavfile_header); + fseek(file,sizeof(struct wavfile_header) - sizeof(int),SEEK_SET); + fwrite(&data_length,sizeof(data_length),1,file); - int riff_length = file_length - 8; - fseek(file,4,SEEK_SET); - fwrite(&riff_length,sizeof(riff_length),1,file); + int riff_length = file_length - 8; + fseek(file,4,SEEK_SET); + fwrite(&riff_length,sizeof(riff_length),1,file); - fclose(file); + fclose(file); } |