3

我正在尝试使用带有这个 11 秒 44kHz .wav 样本文件的KissFFT 库作为测试输入。

但是,当我处理窗口大小为 512 的文件时,我只得到 1 个输出值。这很奇怪,44kHz 的 11 秒 .wav 文件不应该给出 1 个值作为窗口大小为 512 的输出。像 16 这样的较小窗口会给我 5 个值,这仍然是一个低计数。

有谁知道我做错了什么?

这是我的代码:

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <math.h>
#include "kiss_fft.h"

#define WIN 512

int main()
{
    char *music_file = "C:/MSin44W16-13.wav";
    FILE *in;
    char buf[WIN * 2];
    int nfft = WIN, i, fx;
    double intensity = 0;
    kiss_fft_cfg cfg;
    kiss_fft_cpx cx_in[WIN];
    kiss_fft_cpx cx_out[WIN];
    short *sh;

    cfg = kiss_fft_alloc(nfft, 0, 0, 0);
    in = fopen(music_file, "r");
    if (!in) {
        printf("unable to open file: %s\n", music_file);
        perror("Error");
        return 1;
    }
    fx = 0;
    while (fread(buf, 1, WIN * 2, in)) 
    {
        for (i = 0;i<WIN;i++) {
            sh = (short *)&buf[i * 2];
            cx_in[i].r = (float) (((double)*sh) / 32768.0);
            cx_in[i].i = 0.0;
        }

        kiss_fft(cfg, cx_in, cx_out);
        //Display the value of a position
        int position = 511;
        intensity = sqrt(pow(cx_out[position].r, 2) + pow(cx_out[position].i, 2));
        printf("%9.4f\n", intensity);

        //Display all values
        /*
        for (i = 0;i<WIN;i++) {
            //printf("Joe: cx_out[i].r:%f\n", cx_out[i].r);
            //printf("Joe: cx_out[i].i:%f\n", cx_out[i].i);
            intensity = sqrt(pow(cx_out[i].r,2) + pow(cx_out[i].i,2));
            printf("%d - %9.4f\n", i, intensity);
        }
        */

    }
    free(cfg);
    scanf("%d");

    return 0;
}

这是我得到的输出:

 42.7577

这是更新的代码版本,但我在编译时遇到错误:

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <math.h>
#include "kiss_fft.h"
#include "sndfile.h"

#define WIN 512

int main()
{
    char *music_file = "C:/voice.wav";
    SNDFILE *infile;
    SF_INFO      sfinfo;
    //int          readcount;

    short buf[WIN * 2];
    int nfft = WIN;
    double intensity = 0;
    kiss_fft_cfg cfg;
    kiss_fft_cpx cx_in[WIN];
    kiss_fft_cpx cx_out[WIN];
    short *sh;

    cfg = kiss_fft_alloc(nfft, 0, 0, 0);


    if (!( infile = sf_open(music_file, SFM_READ, &sfinfo) ))
    {   /* Open failed so print an error message. */
        printf("Not able to open input file %s.\n", "input.wav");
        /* Print the error message fron libsndfile. */
        sf_perror(NULL);
        return  1;
    }

    while ((sf_read_short(infile, buf, WIN)))//fread(buf, 1, WIN * 2, in)
    {
        //system("cls");

        for (int i = 0;i<WIN;i++) {
            sh = (short *)&buf[i * 2];
            cx_in[i].r = (float) (((double)*sh) / 32768.0);
            cx_in[i].i = 0.0;
        }

        kiss_fft(cfg, cx_in, cx_out);
        //Display the value of a position
        int position = 511;
        intensity = sqrt(pow(cx_out[position].r, 2) + pow(cx_out[position].i, 2));
        printf("%9.4f\n", intensity);

        //Display all values
        /*
        for (i = 0;i<WIN;i++) {
            //printf("Joe: cx_out[i].r:%f\n", cx_out[i].r);
            //printf("Joe: cx_out[i].i:%f\n", cx_out[i].i);
            intensity = sqrt(pow(cx_out[i].r,2) + pow(cx_out[i].i,2));
            printf("%d - %9.4f\n", i, intensity);
        }
        */

    }
    sf_close(infile);
    free(cfg);
    int temp;
    scanf_s("%d", &temp);

    return 0;
}

我按照这篇文章的步骤操作:

Visual Studio 2010 中的“错误 LNK2019:无法解析的外部符号”错误

我仍然得到这些错误:

在此处输入图像描述

4

1 回答 1

4

问题不是来自 KissFFT,而是来自您试图读取以 ASCII 模式打开的二进制波形文件这一事实:

in = fopen(music_file, "r");

当您稍后尝试与fread您一起读取数据时,最终会遇到一个无效字符。在您的特定示例文件中,读取的第 215个字符是Substitute Character (hex value 0x1A),它被您的 C 运行时库解释为文件结束标记。相应地,fread停止填充更多数据并最终返回 0(在第二次迭代中WIN设置为 512,稍后WIN设置为 16)。

要解决此问题,您应该使用以下命令以二进制文件打开文件:

in = fopen(music_file, "rb");

请注意,这将确保二进制数据按原样读取到您的输入缓冲区中,但不会为您解码波形文件头。要正确读取和解码波形文件并获取有意义的数据,您应该考虑使用音频库(例如libsndfile来命名)。如果您必须推出自己的波形文件阅读器,您应该阅读规范和/或查看有关该主题的许多教程之一。

于 2016-03-02T02:08:11.063 回答