分析.WAV 文件

analyzing .WAV files

本文关键字:文件 WAV 分析      更新时间:2023-10-16

我目前正在使用 .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 轴"是时间(

你的代码基本上是正确的。 您没有检查 wav 标头以验证波形文件是否实际包含 16 位样本。

您对 16 位值的计算是错误的,因为buff是一个short int数组。 如果buff是一个char数组,你使用的计算是正确的(但你必须i递增 2(。

使用short int阵列,您可以说int c = buff[i];,除非您的系统是大端系统。

检查

abs(t) > 1是不必要的,因为 -1.0 <= c <1.0。