Code to read Wav header file producing strange results? C -



Code to read Wav header file producing strange results? C -

i need read header variables wave file , display are. using next code, output has numbers far large. i've searched solutions hours. help much appreciated! thanks. got wave soundfile format https://ccrma.stanford.edu/courses/422/projects/waveformat/

output: wav file header information: filesize 3884 bytes riff header riff wave header wave subchunk1id fmt chunk size (based on bits used) 604962816 subchunk1size 268435456 sampling rate 288030720 bits per sample 2048 audioformat 256 number of channels 2048 byte rate 288030720 subchunk2id subchunk2size 1684108385

here source:

#include <stdio.h> #include <stdlib.h> typedef struct wav_header { char riff[4]; int chunksize; char wave[4]; char fmt[4]; int subchunk1size; short int audioformat; short int numofchan; int samplespersec; int bytespersec; short int blockalign; short int bitspersample; int subchunk2size; char subchunk2id[4]; }wav_hdr; int getfilesize(file *infile); int main(int argc,char *argv[]) { //check startup conditions if(argc >= 2); //we have plenty arguments -- go on else { printf("\nusage: programme requires filename argument -- please seek again\n"); exit(0);} wav_hdr wavheader; file *wavfile; int headersize = sizeof(wav_hdr),filelength = 0; wavfile = fopen(argv[1],"r"); if(wavfile == null) { printf("unable open wave file\n"); exit(exit_failure); } fread(&wavheader,headersize,1,wavfile); filelength = getfilesize(wavfile); fclose(wavfile); printf("\nwav file header information:\n"); printf("filesize\t\t\t%d bytes\n",filelength); printf("riff header\t\t\t%c%c%c%c\n",wavheader.riff[0],wavheader.riff[1],wavheader.riff[2],wavheader.riff[3]); printf("wave header\t\t\t%c%c%c%c\n",wavheader.wave[0],wavheader.wave[1],wavheader.wave[2],wavheader.wave[3]); printf("subchunk1id\t\t\t%c%c%c%c\n",wavheader.fmt[0],wavheader.fmt[1],wavheader.fmt[2],wavheader.fmt[3]); printf("chunk size (based on bits used)\t%d\n",wavheader.chunksize); printf("subchunk1size\t\t\t%d\n",wavheader.subchunk1size); printf("sampling rate\t\t\t%d\n",wavheader.samplespersec); //sampling frequency of wav file printf("bits per sample\t\t\t%d\n",wavheader.bitspersample); //number of bits used per sample printf("audioformat\t\t\t%d\n",wavheader.audioformat); printf("number of channels\t\t%d\n",wavheader.bitspersample); //number of channels (mono=1/sterio=2) printf("byte rate\t\t\t%d\n",wavheader.bytespersec); //number of bytes per sec printf("subchunk2id\t\t\t%c%c%c%c\n",wavheader.subchunk2id[0],wavheader.subchunk2id[1],wavheader.subchunk2id[2],wavheader.subchunk2id[3]); printf("subchunk2size\t\t\t%d\n",wavheader.subchunk2size); printf("\n"); homecoming 0; } int getfilesize(file *infile) { int filesize = 0; fseek(infile,0,seek_end); filesize=ftell(infile); fseek(infile,0,seek_set); homecoming filesize; }`

so, code works -- if compile same compiler , o/s author of file format spec using (32-bit windows). you're hoping compiler has laid out struct need match file bytes. example, can compile , run on win32 , read wav file -- right variable part of header variability failed code for.

having written great deal of code manipulate variety of file formats, advise give on trying read structs , instead create few simple utility functions things "read next 4 bytes , turn them int".

notice things "extra format bytes". parts of file format depend on values in previous parts of file format. that's why need think of dynamic reading process rather 1 big read grab headers. it's not hard maintain result highly portable c work between operating systems without relying on o/s specific things stat() or adding library dependencies things htonl() -- should portability (even portability different compiler or different compiler options on same o/s) desirable.

c wav

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -