分析 .WAV 文件
analyzing .WAV files
我目前正在处理 .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 文件并按照我将其放入 [=22= 中的方式进行操作? ]很好("file.txt"是否包含文件函数的"y-axis"值,其中"x-axis"是时间)
您的代码大部分是正确的。您没有检查 wav header 来验证 wave 文件是否实际包含 16 位样本。
您计算的 16 位值是错误的,因为 buff
是 short int
的数组。如果 buff
是一个 char
数组,那么您使用的计算是正确的(但是您必须将 i
增加 2)。
对于 short int
数组,您可以只说 int c = buff[i];
除非您的系统是 big-endian 系统。
检查 abs(t) > 1
是不必要的,因为 -1.0 <= c
< 1.0.
我目前正在处理 .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 文件并按照我将其放入 [=22= 中的方式进行操作? ]很好("file.txt"是否包含文件函数的"y-axis"值,其中"x-axis"是时间)
您的代码大部分是正确的。您没有检查 wav header 来验证 wave 文件是否实际包含 16 位样本。
您计算的 16 位值是错误的,因为 buff
是 short int
的数组。如果 buff
是一个 char
数组,那么您使用的计算是正确的(但是您必须将 i
增加 2)。
对于 short int
数组,您可以只说 int c = buff[i];
除非您的系统是 big-endian 系统。
检查 abs(t) > 1
是不必要的,因为 -1.0 <= c
< 1.0.