data is null when reading from the .wav file in iphone?

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 can't see any value in data. Have I missed something? I want to give the raw sound data as input to FFt. Please help me out. Give me the right approach for this.

Answers


Give me right approach for this.

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


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.


Need Your Help

Sessions are lost when reloading partial pages

php mysql session session-state sha512

I have an issue where some of my pages take an extended period to load due to communications with various external APIs. I noticed that when I change to a different page or reload the current page ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.