SPEEX重采樣分析(一)
簡介
- 算法速度快
- SIMD(SSE)指令支持
- 低內存
- 高質量
該算法是基于最原始的重采樣算法:
Smith, Julius O. Digital Audio ResamplingHome Page
Center for Computer Research in Music and Acoustics (CCRMA),
Stanford University, 2007.
Web published at http://www-ccrma.stanford.edu/~jos/resample/.
這里使用cubic interpolation代替linear interpolation。減少CPU時間,能更好使用SIDM算法。
接口
speex_resampler_init
功能 |
創建實例 |
函數 |
SpeexResamplerState *speex_resampler_init(spx_uint32_t nb_channels, spx_uint32_t in_rate, spx_uint32_t out_rate, int quality, int *err); |
參數 |
nb_channels [in] 通道數 in_rate [in] 輸入音頻的采樣率 out_rate [in] 輸出音頻的采樣率 quality [in] 重采樣質量 err [out] 錯誤碼 |
返回值 |
|
簡介 |
|
|
|
speex_resampler_init_frac
功能 |
根據input/output比例來創建實例 |
函數 |
SpeexResamplerState *speex_resampler_init_frac(spx_uint32_t nb_channels, spx_uint32_t ratio_num, spx_uint32_t ratio_den, spx_uint32_t in_rate, spx_uint32_t out_rate, int quality, int *err); |
參數 |
nb_channels [in]通道數 ratio_num [in]比例分子 ratio_den [in]比例分母 in_rate [in]輸入的采樣率 out_rate [in]輸出的采樣率 quality [in]重采樣質量 err [out]錯誤碼 |
返回值 |
成功返回實例句柄,失敗返回NULL |
簡介 |
|
|
|
speex_resampler_destroy
功能 |
銷毀實例 |
函數 |
void speex_resampler_destroy(SpeexResamplerState *st); |
參數 |
St [in] 實例句柄 |
返回值 |
void |
簡介 |
釋放資源 |
|
|
speex_resampler_process_float
功能 |
重采樣浮點序列 |
函數 |
int speex_resampler_process_float(SpeexResamplerState *st, spx_uint32_t channel_index, const float *in, spx_uint32_t *in_len, float *out, spx_uint32_t *out_len); |
參數 |
St [in]句柄 channel_index [in]通道號 in [in]輸入緩存 in_len [in]輸入長度(返回處理的采樣數) out [in]輸出緩存 out_len [in]輸出長度(返回寫入的采樣數) |
返回值 |
錯誤碼 |
簡介 |
輸入和輸出緩存不能重疊,外部準備緩存 |
|
|
speex_resampler_process_interleaved_float
功能 |
重采樣交叉的多通道浮點數據(例如:雙通道PCM) |
函數 |
int speex_resampler_process_interleaved_float(SpeexResamplerState *st, const float *in, spx_uint32_t *in_len, float *out, spx_uint32_t *out_len); |
參數 |
St [in]句柄 in [in]輸入緩存 in_len [in]輸入長度(返回處理的采樣數) out [in]輸出緩存 out_len [in]輸出長度(返回寫入的采樣數) |
返回值 |
錯誤碼 |
簡介 |
|
|
|
注:以上是主要的接口函數
示例
下面把16K的音頻轉換為8k的音頻
st = speex_resampler_init(1, 16000, 8000, 10, &err);
do{ readlen = fread(in, sizeof(short), 1024, fin); if (readlen > 0) { inlen = readlen; outlen = 1024; ret = speex_resampler_process_int(st, 0, in, &inlen, out, &outlen); if (ret == RESAMPLER_ERR_SUCCESS) { fwrite(out, sizeof(short), outlen, fout); } }
}while(readlen == 1024);
speex_resampler_destroy(st); |