注意事项
音频重采样过程中使用有符号数,确保计算过程正确。 在48k->16k的下采样过程中,3个采样点数据相加取平均值不用担心溢出问题。已使用测试代码确认在Linux、Windows均正常。
重采样代码
static void prv_rtc_resample_16k_to_48k ( const uint8_t * src_data, int len,
int channel, uint8_t * dst_data) {
const int16_t * src_data_index = ( const int16_t * ) src_data;
int16_t * dst_data_index = ( int16_t * ) dst_data;
const int samples = len / ( channel * ( sizeof ( int16_t ) * sizeof ( uint8_t ) ) ) ;
for ( int i = 0 ; i < samples; i++ ) {
for ( int j = 0 ; j < channel; j++ ) {
dst_data_index[ i * 3 * channel + j * 3 + 0 ] =
src_data_index[ i * channel + j] ;
dst_data_index[ i * 3 * channel + j * 3 + 1 ] =
src_data_index[ i * channel + j] ;
dst_data_index[ i * 3 * channel + j * 3 + 2 ] =
src_data_index[ i * channel + j] ;
}
}
}
static void prv_rtc_resample_48k_to_16k ( const uint8_t * src_data, int len,
int channel, uint8_t * dst_data) {
const int16_t * src_data_index = ( const int16_t * ) src_data;
int16_t * dst_data_index = ( int16_t * ) dst_data;
const int samples = len / ( 3 * channel * ( sizeof ( int16_t ) * sizeof ( uint8_t ) ) ) ;
for ( int i = 0 ; i < samples; i++ ) {
for ( int j = 0 ; j < channel; j++ ) {
dst_data_index[ i * channel + j] =
( src_data_index[ i * 3 * channel + j * 3 + 0 ] +
src_data_index[ i * 3 * channel + j * 3 + 1 ] +
src_data_index[ i * 3 * channel + j * 3 + 2 ] ) /
3 ;
}
}
}
int16_t数据溢出测试
代码
# include <stdio.h>
# include <stdint.h>
int main ( ) {
int16_t tmp1 = 0x7fff , tmp2 = 0x7fff , tmp3 = 0x7fff ;
int16_t sum1 = tmp1 + tmp2 + tmp3;
int32_t sum2 = tmp1 + tmp2 + tmp3;
printf ( "sum1: %d, sum1/3: %d, %d\n" , sum1, sum1 / 3 , ( ( tmp1 + tmp2 + tmp3) / 3 ) ) ;
printf ( "sum1: %d, sum1/3: %d\n" , sum2, sum2 / 3 ) ;
return 0 ;
}
测试结果