简易重采样resampler的实现

基于开源库speexdsp, 可以简单实现重采样。

 

简单实现的代码如下:

需要的三方库: speexdsp ,libsndfile

#include <stdlib.h>
#include <sndfile.h>
#include <speex_resampler.h>

#define BUFFER_SIZE 2048

int main(int argv, const char *args[]) {
    if (argv != 4) {
        printf("usage: %s <input file path> <to samplerate> <output file path> \n", args[0]);
        return 1;
    }

    const char *in_path = args[1];

    int output_sr = atoi(args[2]);
    const char *out_path = args[3];

    SF_INFO info;
    SNDFILE *in = sf_open(in_path, SFM_READ, &info);
    if (!in) {
        fprintf(stderr, "cannot open file %s\n", in_path);
        return 2;
    }
    SF_INFO onfo;
    onfo.channels = 1;
    onfo.samplerate = output_sr;
    onfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16 ;
    onfo.sections = 0;
    onfo.seekable = 1;
    SNDFILE *out = sf_open(out_path, SFM_WRITE, &onfo);
    if (!out) {
        fprintf(stderr, "cannot open file %s\n", out_path);
        sf_close(in);
        return 2;
    }


    int err = 0;
    SpeexResamplerState *resampler = speex_resampler_init(info.channels, info.samplerate, output_sr, 1, &err);

    printf("=====================\nchannels: %d\nsample rate:%d\n", info.channels, info.samplerate);
    if (info.channels != 1) {
        fprintf(stderr, "only support mono wav.\n");
        sf_close(in);
        sf_close(out);
        return 3;
    }

    short in_buffer[BUFFER_SIZE] = {0};
    uint32_t size = BUFFER_SIZE * output_sr/ info.samplerate;
    printf("new buffer size: %d\n", size);
    short *out_buffer = (short *) calloc(size,  sizeof(short));
    uint32_t read = 0;
    do {
        read = sf_read_short(in, in_buffer, BUFFER_SIZE);

        err = speex_resampler_process_int(resampler, 0, (const spx_int16_t *)in_buffer, (spx_uint32_t *)&read, (spx_int16_t *)out_buffer, &size);
        if (err) {
            fprintf(stderr, "err:%d\n", err);
        }
        sf_write_short(out, out_buffer, size);
    } while(read>0);
    free(out_buffer);
    sf_close(in);
    sf_close(out);
    return 0;
}

上述代码只在centos上编译测试,不过后来也有移植到windows下的。

如果需要成品的可以在这里下载windows版本,有需要可以自取。

https://download.csdn.net/download/mimiduck/12465600

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值