从 iphone 中读取 .wav 文件时数据为空?
我正在尝试读取 .wav 文件并将原始数据作为 FFT 算法的输入。我使用以下代码来读取 .wav 文件。
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);
ChunkID 给出值“caff”,格式给出“desc”。我看不到数据有任何价值。我错过了什么吗?我想将原始声音数据作为 FFT 的输入。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
AudioToolbox.framework 中有用于读取音频文件的例程。
There are routines in AudioToolbox.framework for reading audio files.
具体来说,这是什么意思?您是说
Data
的长度为零吗?sizeof(short)*Subchunk2Size/(BitsPerSample/8)
的实际值是多少?您确定
var
有正确的值吗?我假设它是一个NSString
变量,表示 .wav 声音文件的路径。该文件存在吗?它有合适的长度吗?您在
Subchunk2ID
中的读取方式与您以相同方式声明(然后设置)的其他三个变量不同是否有原因?这对我来说似乎是一个错字。最后,不要以大写字母开头来命名 Objective-C 中的变量。您应该只对类名这样做。这是在 Cocoa 中编程时应该遵循的风格考虑/约定。
What does this mean, specifically? Are you saying
Data
has zero length?What is the actual value of
sizeof(short)*Subchunk2Size/(BitsPerSample/8)
?Are you sure
var
has a correct value? I assume it's anNSString
variable that represents a path to a .wav sound file. Does that file exist? Does it have an appropriate length?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.
如果初始块 ID 返回“caff”而不是“RIFF”,则您读取的不是 WAV 文件,因此所有其他参数(包括长度和数据)可能都是伪造的。
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.