简单的音频重采样

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 (< 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 (- Ystart); /* Return number of output samples */
}


Java音频重采样是指将音频信号的采样率进行修改或调整的过程。采样率是指在单位时间内对音频信号进行采样的次数,通常以赫兹(Hz)为单位。例如,CD音频采样率为44.1kHz,即每秒对音频信号采样44,100次。 音频重采样的目的是为了改变音频信号的采样率,以满足特定需求或要求。例如,当两个不同采样率的音频需要进行混合时,就需要进行重采样。另外,有些设备或平台只支持特定的采样率,那么我们也需要将音频重采样到符合其要求的采样率。 在Java中,可以使用一些库或框架来进行音频重采样。例如,可以使用Java Sound API提供的功能来进行音频重采样。通过Java Sound API,可以获取音频数据流的采样率,并使用线性插值或其他算法将其转换为目标采样率。 要进行音频重采样,我们需要注意一些关键点。首先,要选择合适的重采样算法,以保证音质不受损。其次,要平衡采样率变化对音频信号的影响,避免出现混叠或伪音等问题。最后,要对重采样后的音频进行适当的处理和校正,以确保音质的准确性和真实性。 总之,Java音频重采样是一项重要的音频处理技术,可以用于解决不同采样音频的兼容性问题,同时也能为我们提供更好的音频体验。通过选择合适的算法和处理方法,我们可以有效地进行音频重采样,并获得满足需求的音频信号。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值