音頻採樣率一般來說都是由硬體決定的,但是某些作業系統的核心只提供最大某些固定的採樣率,比如最大隻有16KHz,這樣我們需要用軟體類比的方式將採樣率升高成原來的一般,但是這種方式可能並不能提高音質或者聲音的精細度,不過工程師的任務就是儘可能的完成一些技術指標或者客戶需求。這裡簡單記錄以下:
16K採樣率轉8K採樣率,即降採樣處理:
Linux音頻編碼的就是把/dev/dsp下的音頻資料擷取到應用程式層來,拷貝到一塊記憶體裡,然後進行音頻編碼,G722,G711,MPEG layer 1/2/3等等。這裡用一個簡單的代碼例子完成以上操作:#include <XXXX.h><br />int main(int argc, char **argv)<br />{<br />int fd = 0;<br />int num_bytes = 0;<br />int i,j;<br />unsigned char *inputbuffer = NULL;<br />unsigned char *outputbuffer = NULL;<br />unsigned char *resamples_buffer = NULL;<br />fd = open("dev/dsp",O_RDWR);<br />if (fd == -1)<br />{<br />printf("open dev/dsp error/n");<br />return -1;<br />}</p><p>inputbuffer = (unsigned char *)malloc(640);<br />if(inputbuffer == NULL)<br />{<br />printf("alloc inputbuffer error/n");<br />return -1;<br />}</p><p>outputbuffer = (unsigned char *)malloc(320);<br />if (inputbuffer == NULL)<br />{<br />printf("alloc outputbuffer error/n");<br />return -1;<br />}</p><p>resamples_buffer = (unsigned char *)malloc(160);<br />if (resamples_buffer == NULL)<br />{<br />printf("alloc resamples_buffer error/n");<br />return -1;<br />}</p><p>while (1)<br />{<br />num_bytes = read(fd,inputbuffer,640);<br />if (num_bytes != 640)<br />{<br />printf("read error,num_bytes = %d/n",num_bytes);<br />continue ;<br />}</p><p>//採樣率翻倍<br />for (i = 0;i < 640; i++)<br />{<br />*(resamples_buffer + j) = *(inputbuffer + i);<br />*(resamples_buffer + j + 1) = 0;<br />j += 2;<br />}</p><p>//編碼,編碼後的資料送到outputbuffer<br />G711_encode(...);<br />G722_encode(...);<br />MPL2_encode(...);</p><p>//降採樣率,例如把16K降到8K<br />for(i = 0;i < 640; i+=2)<br />{<br />*(resamples_buffer + j) = *(outputbuffer + i);<br />j++;<br />}<br />}</p><p>}