Yuanda Zhu / Mbed 2 deprecated KeypadPiano

Dependencies:   4DGL-uLCD-SE SDFileSystem Speaker mbed wave_player

Fork of Beat_Demo_full_full by James Hawkins

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers wavfile.c Source File

wavfile.c

00001 /*
00002 A simple sound library for CSE 20211 by Douglas Thain
00003 For course assignments, you should not change this file.
00004 For complete documentation, see:
00005 http://www.nd.edu/~dthain/courses/cse20211/fall2013/wavfile
00006 */
00007 
00008 #include "wavfile.h"
00009 
00010 #include <stdio.h>
00011 #include <stdlib.h>
00012 #include <string.h>
00013 
00014 struct wavfile_header {
00015     char    riff_tag[4];
00016     int riff_length;
00017     char    wave_tag[4];
00018     char    fmt_tag[4];
00019     int fmt_length;
00020     short   audio_format;
00021     short   num_channels;
00022     int sample_rate;
00023     int byte_rate;
00024     short   block_align;
00025     short   bits_per_sample;
00026     char    data_tag[4];
00027     int data_length;
00028 };
00029 
00030 FILE * wavfile_open( const char *filename )
00031 {
00032     
00033     struct wavfile_header header;
00034 
00035     int samples_per_second = WAVFILE_SAMPLES_PER_SECOND;
00036     int bits_per_sample = 16;
00037 
00038     strncpy(header.riff_tag,"RIFF",4);
00039     strncpy(header.wave_tag,"WAVE",4);
00040     strncpy(header.fmt_tag,"fmt ",4);
00041     strncpy(header.data_tag,"data",4);
00042 
00043     header.riff_length = 0;
00044     header.fmt_length = 16;
00045     header.audio_format = 1;
00046     header.num_channels = 1;
00047     header.sample_rate = samples_per_second;
00048     header.byte_rate = samples_per_second*(bits_per_sample/8);
00049     header.block_align = bits_per_sample/8;
00050     header.bits_per_sample = bits_per_sample;
00051     header.data_length = 0;
00052 
00053 
00054     FILE * file = fopen(filename,"w");
00055     if(!file) return 0;
00056 
00057     fwrite(&header,sizeof(header),1,file);
00058 
00059     fflush(file);
00060 
00061     return file;
00062 
00063 }
00064 
00065 void wavfile_write( FILE *file, short data[], int length )
00066 {
00067     fwrite(data,sizeof(short),length,file);
00068 }
00069 
00070 void wavfile_close( FILE *file )
00071 {
00072     int file_length = ftell(file);
00073 
00074     int data_length = file_length - sizeof(struct wavfile_header);
00075     fseek(file,sizeof(struct wavfile_header) - sizeof(int),SEEK_SET);
00076     fwrite(&data_length,sizeof(data_length),1,file);
00077 
00078     int riff_length = file_length - 8;
00079     fseek(file,4,SEEK_SET);
00080     fwrite(&riff_length,sizeof(riff_length),1,file);
00081 
00082     fclose(file);
00083 }