我必须正常化.wav
音频文件。成功地从文件(ChunkID
,ChunkSize
,Format
,fmt
等)获得的元数据(前44个字节),所以我可以找出有多少渠道在那里(NumChannels
)或BitPerSaple
等如何查找.wav文件的数据(样本)的大小?
现在我有将所有样本的数据复制到动态分配的数组中但我不知道如何获取文件的大小(在malloc()
函数中使用)。
下面是代码(如果它会帮助):
#include <stdio.h>
#include <stdlib.h>
#define hdr_SIZE 44
typedef struct FMT
{
char SubChunk1ID[4];
int SubChunk1Size;
short int AudioFormat;
short int NumChannels;
int SampleRate;
int ByteRate;
short int BlockAlign;
short int BitsPerSample;
} fmt;
typedef struct DATA
{
char Subchunk2ID[4];
int Subchunk2Size;
int Data[441000];
} data;
typedef struct HEADER
{
char ChunkID[4];
int ChunkSize;
char Format[4];
fmt S1;
data S2;
} header;
int main()
{
char nameIn[255], nameOut[255];
printf("Enter the names of input and output files including file extension:\n");
scanf ("%s", nameIn);
//printf("%s\n", nameIn);
scanf ("%s", nameOut);
//printf("%s\n", nameOut);
FILE *input = fopen(nameIn, "rb");
FILE *output = fopen(nameOut, "wb");
header hdr;
if(input == NULL)
{
printf("Unable to open wave file (input)\n");
exit(EXIT_FAILURE);
}
fread(&hdr, sizeof(char), hdr_SIZE, input);
/* Displaying file's metadata (chunks). */
printf("\n*********************************\n");
printf("WAVE file's metadata:\n\n");
printf("%4.4s\n", hdr.ChunkID);
printf("%d\n", hdr.ChunkSize);
printf("%4.4s\n", hdr.Format);
printf("%4.4s\n", hdr.S1.SubChunk1ID);
printf("%d\n", hdr.S1.SubChunk1Size);
printf("%d\n", hdr.S1.AudioFormat);
printf("%d\n", hdr.S1.NumChannels);
printf("%d\n", hdr.S1.SampleRate);
printf("%d\n", hdr.S1.ByteRate);
printf("%d\n", hdr.S1.BlockAlign);
printf("%d\n", hdr.S1.BitsPerSample);
printf("%4.4s\n", hdr.S2.Subchunk2ID);
printf("%d\n", hdr.S2.Subchunk2Size);
printf("\n*********************************\n");
/* Dead end... =(*/
fclose(input);
fclose(output);
return 0;
}
操作系统Windows 7; Code :: Blocks IDE。
UPDATE(解决了!): 事实证明,我已经有样本的大小值(Subchunk2Size
)。所以在我的情况下,我只需要使用hdr.S2.Subchunk2Size
作为malloc()
函数。
你想解释一下吗? **我想知道它是如何工作的。** – yulian 2013-05-08 19:38:22
'fstat'是一个检索文件元数据的便携式函数。其中元数据是文件的大小。 – 2013-05-08 19:41:47
当然:如果你在Unix上,理由是'man stat';如果没有,请阅读http://linux.die.net/man/2/fstat。在其他系统上(我不知道你的是什么),请参阅文档.. – 2013-05-08 19:44:33