Linux音频编码的采样率模拟实现

    音频采样率一般来说都是由硬件决定的,但是某些操作系统的内核只提供最大某些固定的采样率,比如最大只有16KHz,这样我们需要用软件模拟的方式将采样率升高成原来的一般,但是这种方式可能并不能提高音质或者声音的精细度,不过工程师的任务就是尽可能的完成一些技术指标或者客户需求。这里简单记录以下:

 

16K采样率转8K采样率,即降采样处理:

Linux音频编码的就是把/dev/dsp下的音频数据采集到应用层来,拷贝到一块内存里,然后进行音频编码,G722,G711,MPEG layer 1/2/3等等。这里用一个简单的代码例子完成以上操作:#include int main(int argc, char **argv) { int fd = 0; int num_bytes = 0; int i,j; unsigned char *inputbuffer = NULL; unsigned char *outputbuffer = NULL; unsigned char *resamples_buffer = NULL; fd = open("dev/dsp",O_RDWR); if (fd == -1) { printf("open dev/dsp error/n"); return -1; } inputbuffer = (unsigned char *)malloc(640); if(inputbuffer == NULL) { printf("alloc inputbuffer error/n"); return -1; } outputbuffer = (unsigned char *)malloc(320); if (inputbuffer == NULL) { printf("alloc outputbuffer error/n"); return -1; } resamples_buffer = (unsigned char *)malloc(160); if (resamples_buffer == NULL) { printf("alloc resamples_buffer error/n"); return -1; } while (1) { num_bytes = read(fd,inputbuffer,640); if (num_bytes != 640) { printf("read error,num_bytes = %d/n",num_bytes); continue ; } //采样率翻倍 for (i = 0;i < 640; i++) { *(resamples_buffer + j) = *(inputbuffer + i); *(resamples_buffer + j + 1) = 0; j += 2; } //编码,编码后的数据送到outputbuffer G711_encode(...); G722_encode(...); MPL2_encode(...); //降采样率,例如把16K降到8K for(i = 0;i < 640; i+=2) { *(resamples_buffer + j) = *(outputbuffer + i); j++; } } }  

 

你可能感兴趣的:(codec,Linux编程,linux,buffer,null,layer,任务)