【发布时间】:2017-11-19 17:06:04
【问题描述】:
我目前正在处理 .WAV 文件,我有一个问题。
我解决这个问题的方法是创建一个包含信息的结构:
typedef struct header_file{
char chunk_id[4];
int chunk_size;
char format[4];
char subchunk1_id[4];
int subchunk1_size;
short int audio_format;
short int num_channels;
int sample_rate;
int byte_rate;
short int block_align;
short int bits_per_sample;
char subchunk2_id[4];
int subchunk2_size;
}header;
typedef struct header_file* wav_p;
现在,我尝试运行 WAV 文件,如下所示:
ofstream myFile;
myFile.open("file.txt");
FILE * file = fopen("file.wav", "rb");
const int BUFFSIZE = 256;
int count = 0;
short int buff[BUFFSIZE];
wav_p wav = (wav_p)malloc(sizeof(header));
int nb;
if (file)
{
fread(wav, 1, sizeof(header), file);
while (!feof(file)){
nb = fread(buff, 1, BUFFSIZE, file);
count++;
for (int i = 0; i<BUFFSIZE; i += 1){
//the following part i found on the internet so i'm not sure if it is good
int h = (signed char)buff[i + 1];
int c = (h << 8) | buff[i];
double t = c / 32768.0;
myFile << t << endl;
if(abs(t)>1){
//checking that a value is between -1 to 1
}
}
}
}
fclose(file);
myFile.close();
我的问题是:内部for 是否正确?在file.txt 中,我所有的值都在-1 到1 之间,所以我认为这很好,但我不确定,我是否正确地检查了.wav 文件并且我把它放在“file.txt”中的方式是好(“file.txt”是否包含文件函数的“y轴”值,其中“x轴”是时间)
【问题讨论】:
-
这里发布的代码中有一个错误 - 有一个未声明的变量
hi。此外,如果您能说出出现了什么错误,让您认为您的程序有问题,那将会很有帮助。 -
我写了
hi而不是h,我编辑了它。我没有收到任何错误,但是因为我从互联网上获取了int h = (signed char)buff[i + 1]; int c = (h << 8) | buff[i]; double t = c / 32768.0;,所以我不知道它是否是正确的计算方法t