重采样库示例

功能:对音频数据重采样,适用于单声道16位音频数据。

​#include <fcntl.h>
#define BUFFER_LEN30*48000*2
static void simple_test (int converter, double ratio) ;
int  main(int argc ,char ** argv)
{ 
     printf("usage : ./a.out type file\n");
     SRC_DATA src_data ;
     static float input [BUFFER_LEN], output [BUFFER_LEN] ;
     int converter  = atoi(argv[1]) ;
     double src_ratio = 0.25;  // src_ratio = out_rate /input_rate
     int input_len, output_len, error, terminate ;
     int fd_input ,fd_output;
     int ret = 0;
     printf ("\tsimple_test      (SRC ratio = %6.4f) ........... ", src_ratio) ;
     fflush (stdout) ;
     float * p = input;
     /* Calculate maximun input and output lengths. */
     if (src_ratio >= 1.0) {
         output_len = BUFFER_LEN ;
         input_len = (int) floor (BUFFER_LEN / src_ratio) ;
      } else { 
                 input_len = BUFFER_LEN ;
                 output_len = (int) floor (BUFFER_LEN * src_ratio) ;
      } ;

    /* Reduce input_len by 10 so output is longer than necessary. */
    input_len -= 10 ;
    if (output_len > BUFFER_LEN) { 
        printf ("\n\nLine %d : output_len > BUFFER_LEN\n\n", __LINE__) ;
        exit (1) ;
    };
    fd_input = open(argv[2],O_RDONLY);
    fd_output= open("aec_near.pcm", O_RDWR|O_CREAT|O_TRUNC,0777 );
    int read_len = BUFFER_LEN;
    short tmpp = 0;
    while(read_len>0) {
        ret = read(fd_input,&tmpp,2); 
        if(ret ==0)
            break;
         *p= (float)tmpp;
          p++;
          read_len -= ret;
    }
    memset (&src_data, 0, sizeof (src_data)) ;
    src_data.src_ratio = src_ratio ;
    src_data.data_out = output ;
    src_data.output_frames = output_len ;
    src_data.data_in = input ;
    src_data.input_frames = input_len ;
    // printf("input_len :%d output_len :%d \n",input_len,output_len);
    if ((error = src_simple (&src_data, converter, 1))) { 
        printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ;
        exit (1) ;
    } ;
    int write_len = src_data.output_frames_gen; 
    float r_tmpp ;
    short res[BUFFER_LEN]; 
    p = output;   
    short * pf = res;
    while(write_len>0) {
        *pf = (short)(*p);
        pf++;
        p++;
        write_len --;
    }
    write(fd_output,res,src_data.output_frames_gen);
    terminate = (int) ceil ((src_ratio >= 1.0) ? src_ratio : 1.0 / src_ratio) ;
    if (fabs (src_data.output_frames_gen - src_ratio * input_len) > 2 * terminate) { 
        printf ("\n\nLine %d : bad output data length %ld should be %d.\n", __LINE__,
        src_data.output_frames_gen, (int) floor (src_ratio * input_len)) ;
        printf ("\tsrc_ratio  : %.4f\n", src_ratio) ;
        printf ("\tinput_len  : %d\n\toutput_len : %d\n\n", input_len, output_len) ;
        exit (1) ;
    } ;
    puts ("ok") ;
    close(fd_input);
    close(fd_output);
    return  0 ;
} /* simple_test */

[Click and drag to move]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值