还是音频重采样的问题,https://segmentfault.com/a/11... 的后续,应用场景是对已解码的8K采样率、16bit采样深度、单声道、无文件头的raw格式语音数据进行重采样,将采样率变为16K,其余参数不变。之前试过libsox,Release版本始终无法正确运行,原因也没有找到,FFmpeg做出来的采样结果又和SoX不同,跟模型对不上。根据官方的说法soxr和SoX应该是一样的,只不过是专门做快速高精度的一维声采样。对于我这么简单的应用场景,理论上它俩效果应该可以达到完全一样,但是我用libsoxr做出来的采样结果和SoX就不一样,肉眼看上去波形没有区别,但是看二进制文件差别还挺大的……我尝试改变了一些参数,包括dither和quality的各项参数,但是都没有得到相同结果。也搜了讨论区和StackOverflow,都没有得到解答。
下面是代码,希望有人能指出我的问题。

#include "soxr.h"
bool Resample (short* pWavBuf, int wavLen, short* pWav16k, int wavLen16k) {
//pWavBuf为输入的raw格式语音数据,采样率8K,采样深度16bit;
//wavLen为输入数据的长度,单位为样点数,并非数据长度的字节数
//pWav16k为输出的缓存
//wavLen16k为输出数据的长度,单位依然为样点数

    size_t ilen = wavLen;       //input samples per channel
    size_t olen = ilen * 2;     //output samples per channel
    wavLen16k = olen;
    double irate = 8000;        //input sample reate
    double orate = 16000;       //output sample rate
    
    soxr_io_spec_t io_spec;     //io format
    io_spec.itype = SOXR_INT16; //input datatype
    io_spec.otype = SOXR_INT16; //output datatype
    io_spec.scale = 1;          //linear
    io_spec.e = NULL;           //reserved
    io_spec.flags = SOXR_TPDF;  //SOXR_NO_DITHER也试过,都不行
    
    soxr_quality_spec_t qua_spec;   //quality
    qua_spec = soxr_quality_spec(SOXR_LQ, 0);   //not sure
    /*//此处尝试了单独指定一些参数,例如:
    qua_spec.precision = 16;
    qua_spec.phase_response = 50;   //linear
    qua_spec.passband_end = 0.95;
    qua_spec.stopband_begin = 1;
    qua_spec.e = NULL;
    qua_spec.flags = SOXR_ROLLOFF_SMALL;
    //但均未获得期望结果
    */
    
    size_t idone, odone;
    soxr_error_t error;
    
    //single call to resample one wav
    error = soxr_oneshot(irate, orate, 1, pWavBuf, ilen, &idone, pWav16k, olen, &odone, &io_spec, &qua_spec, NULL);
    
    return true;
}

locking
4 声望1 粉丝

引用和评论

0 条评论