views:

31

answers:

3

I am trying to read the .wav file and give the raw data as input to the FFT algorithm.I have used the following code to read the .wav file.

char ChunkID[4], Format[4], Subchunk1ID[4],Subchunk2ID[4];
int ChunkSize,Subchunk1Size, SampleRate, ByteRate,Subchunk2Size;
short AudioFormat, NumChannels, BlockAlign, BitsPerSample;
short *Data;

// Read the wave file
FILE *fhandle=fopen([var UTF8String],"rb");
fread(ChunkID,1,4,fhandle);
fread(&ChunkSize,4,1,fhandle);
fread(Format,1,4,fhandle);
fread(Subchunk1ID,1,4,fhandle);
fread(&Subchunk1Size,4,1,fhandle);
fread(&AudioFormat,2,1,fhandle);
fread(&NumChannels,2,1,fhandle);
fread(&SampleRate,4,1,fhandle);
fread(&ByteRate,4,1,fhandle);
fread(&BlockAlign,2,1,fhandle);
fread(&BitsPerSample,2,1,fhandle);
fread(&Subchunk2ID,1,4,fhandle);
fread(&Subchunk2Size,4,1,fhandle);
Data=(short*) malloc (sizeof(short)*Subchunk2Size/(BitsPerSample/8));  // Create an element for every sample
fread(Data,BitsPerSample/8,Subchunk2Size/(BitsPerSample/8),fhandle); // Reading raw audio data
fclose(fhandle);

The ChunkID gives the value 'caff' and the format gives 'desc' i cant see any value in data.Do i miss anything.i want to give the raw sound data as input to FFt.Please help me out.Give me right approach for this.

+1  A: 

i cant see any value in data.

What does this mean, specifically? Are you saying Data has zero length?

Data=(short*) malloc (sizeof(short)*Subchunk2Size/(BitsPerSample/8));  // Create an element for every sample

What is the actual value of sizeof(short)*Subchunk2Size/(BitsPerSample/8)?

FILE *fhandle=fopen([var UTF8String],"rb");

Are you sure var has a correct value? I assume it's an NSString variable that represents a path to a .wav sound file. Does that file exist? Does it have an appropriate length?

char ChunkID[4], Format[4], Subchunk1ID[4],Subchunk2ID[4];
...
fread(ChunkID,1,4,fhandle);
fread(Format,1,4,fhandle);
fread(Subchunk1ID,1,4,fhandle);
fread(&Subchunk2ID,1,4,fhandle);

Is there a reason why you're reading in Subchunk2ID differently than the other three variables you've declared (and then set) in the same manner? This looks like a typo to me.

Finally, don't name your variables in Objective-C with starting capital letters. You should only do that for class names. That is a style consideration/convention you should follow when programming in Cocoa.

Shaggy Frog
In the debugger if i check the value assigned to the 'data',its giving 0
Warrior
var has the correct path to the audio file.
Warrior
Quick iPhone tip that may not be related: use [[NSBundle mainBundle] pathForResource:@"nameOfWav" ofType:@"wav"] to get the full path for a wav included in your application bundle. Then call UTF8String on that to get something you can pass to fopen.
Tommy
+1  A: 

Give me right approach for this.

there are routines in AudioToolbox.framework for reading audio files.

Justin
A: 

If the initial chunk ID returns 'caff' and not 'RIFF', then you are not reading a WAV file, so all the other parameters, including length and data are probably bogus.

hotpaw2