开发者

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

开发者 https://www.devze.com 2023-01-21 05:01 出处:网络
I am trying to read the .wav file and give the raw data as input to the F开发者_开发百科FT algorithm. I have used the following code to read the .wav file.

I am trying to read the .wav file and give the raw data as input to the F开发者_开发百科FT 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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号