Chinaunix首页 | 论坛 | 博客
  • 博客访问: 158925
  • 博文数量: 56
  • 博客积分: 2510
  • 博客等级: 少校
  • 技术积分: 502
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-18 14:21
文章分类

全部博文(56)

文章存档

2010年(39)

2009年(17)

我的朋友

分类: C/C++

2010-06-14 23:54:01

Performance of audio resampling software

void _x_audio_out_resample_mono(short* input_samples, int in_samples,
    short* output_samples, int out_samples)
{
  int osample;
  /* 16+16 fixed point math */
  uint32_t isample = 0;
  uint32_t istep = ((in_samples-2) << 16)/(out_samples-2);

#ifdef VERBOSE
  printf ("Audio : resample %d samples to %d\n",
          in_samples, out_samples);
#endif

  for (osample = 0; osample < out_samples - 1; osample++) {
    int s1;
    int s2;
    int16_t os;
    uint32_t t = isample&0xffff;
   
    /* don't "optimize" the (isample >> 16)*2 to (isample >> 15) */
    s1 = input_samples[(isample >> 16)];
    s2 = input_samples[(isample >> 16)+1];
   
    os = (s1 * (0x10000-t)+ s2 * t) >> 16;
    output_samples[osample] = os;

    isample += istep;
  }
  output_samples[out_samples-1] = input_samples[in_samples-1];
}



使用方法:
如22050Hz-->8000Hz,参数为
_x_audio_out_resample_mono(inbuf, in_sample_num, outbuf, in_sample_num*8000/(22050*2));
inbuf为待采样音频,outbuf为输出缓存,保证尺寸不小于
in_sample_num*8000/(22050*2)


另一个函数,比上面的效果要好。

typedef short HWORD;
typedef int WORD;
typedef unsigned int UWORD;
typedef unsigned short UHWORD;
#define MAX_HWORD (32767)
#define MIN_HWORD (-32767)
#define Np 15
#define Pmask ((1<<Np)-1)

static inline HWORD WordToHword(WORD v, int scl)
{
    HWORD out;
    WORD llsb = (1<<(scl-1));
    v += llsb; /* round */
    v >>= scl;
    if (v>MAX_HWORD) {
#ifdef DEBUG
        if (pof == 0)
          fprintf(stderr, "*** resample: sound sample overflow\n");
        else if ((pof % 10000) == 0)
          fprintf(stderr, "*** resample: another ten thousand overflows\n");
        pof++;
#endif
        v = MAX_HWORD;
    } else if (v < MIN_HWORD) {
#ifdef DEBUG
        if (nof == 0)
          fprintf(stderr, "*** resample: sound sample (-) overflow\n");
        else if ((nof % 1000) == 0)
          fprintf(stderr, "*** resample: another thousand (-) overflows\n");
        nof++;
#endif
        v = MIN_HWORD;
    }
    out = (HWORD) v;
    return out;
}


static int
  SrcLinear(HWORD X[], HWORD Y[], double factor, UWORD *Time, UHWORD Nx)
{
    HWORD iconst;
    HWORD *Xp, *Ystart;
    WORD v,x1,x2;
    
    double dt; /* Step through input signal */
    UWORD dtb; /* Fixed-point version of Dt */
    UWORD endTime; /* When Time reaches EndTime, return to user */
    
    dt = 1.0/factor; /* Output sampling period */
    dtb = dt*(1<<Np) + 0.5; /* Fixed-point representation */
    
    Ystart = Y;
    endTime = *Time + (1<<Np)*(WORD)Nx;
    while (*Time < endTime)
    {
        iconst = (*Time) & Pmask;
        Xp = &X[(*Time)>>Np]; /* Ptr to current input sample */
        x1 = *Xp++;
        x2 = *Xp;
        x1 *= ((1<<Np)-iconst);
        x2 *= iconst;
        v = x1 + x2;
        *Y++ = WordToHword(v,Np); /* Deposit output */
        *Time += dtb; /* Move to next sample by time increment */
    }
    return (Y - Ystart); /* Return number of output samples */
}



阅读(4882) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~