从 iphone 中读取 .wav 文件时数据为空?

发布于 2024-09-29 06:10:36 字数 1105 浏览 7 评论 0原文

我正在尝试读取 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

想念有你 2024-10-06 06:10:37

AudioToolbox.framework 中有用于读取音频文件的例程。

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

生寂 2024-10-06 06:10:37

我看不到数据的任何价值。

具体来说,这是什么意思?您是说 Data 的长度为零吗?

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

sizeof(short)*Subchunk2Size/(BitsPerSample/8) 的实际值是多少?

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

您确定 var 有正确的值吗?我假设它是一个 NSString 变量,表示 .wav 声音文件的路径。该文件存在吗?它有合适的长度吗?

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);

您在 Subchunk2ID 中的读取方式与您以相同方式声明(然后设置)的其他三个变量不同是否有原因?这对我来说似乎是一个错字。

最后,不要以大写字母开头来命名 Objective-C 中的变量。您应该只对类名这样做。这是在 Cocoa 中编程时应该遵循的风格考虑/约定。

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.

や莫失莫忘 2024-10-06 06:10:37

如果初始块 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文