使用portaudio實現語音架構

來源:互聯網
上載者:User

標籤:style   class   blog   http   com   get   

在冰凍期間的主要工作就是使用portaudio實現了一套語音採集架構。

因為工作內容所限,所以一直不好談這塊的內容,現在已經離開冰凍了,終於可以和大家交流下這部分的內容了

const PaDeviceInfo * info = Pa_GetDeviceInfo(indexInputDevice);PaStreamParameters inputStreamParameters;inputStreamParameters.device = indexInputDevice;inputStreamParameters.channelCount = info->maxInputChannels;inputStreamParameters.hostApiSpecificStreamInfo = 0;inputStreamParameters.sampleFormat = paFloat32;inputStreamParameters.suggestedLatency = info->defaultLowInputLatency;info = Pa_GetDeviceInfo(indexOutputDevice);PaStreamParameters outputStreamParameters;outputStreamParameters.device = indexOutputDevice;outputStreamParameters.channelCount = info->maxOutputChannels;inputStreamParameters.hostApiSpecificStreamInfo = 0;inputStreamParameters.sampleFormat = paFloat32;inputStreamParameters.suggestedLatency = info->defaultLowInputLatency;PaError err = Pa_OpenStream(&input, &inputStreamParameters, &outputStreamParameters, info->defaultSampleRate, BUFF_LONG, paClipOff, 0, 0);if (err != paNoError){throw std::exception("open stream err=%d", err);}

初始化語音裝置

其中一個重要的參數是PaStreamCallback * streamCallback,這個函數是採集和播放的回呼函數,用於處理採集到的資料和為播放傳入緩衝資料(採集和播放的都是PCM資料),這裡和在冰凍不同,我採用了阻塞式的API所以回呼函數填寫了NULL。

另外2個重要的參數是const PaStreamParameters * inputParameters和const PaStreamParameters * outputParameters

分別是輸入裝置和輸出裝置

關於裝置的初始化

Pa_GetDeviceCount擷取裝置數, Pa_GetDeviceInfo擷取裝置資訊

Pa_GetHostApiCount擷取主機上可用的介面數目(windows上就是MME和DX Sound),Pa_GetHostApiInfo擷取API資訊。

PaError sound::read_buff(){PaError err = Pa_ReadStream(input, outputbuff, BUFF_LONG);if (err == paNoError){sigCapture(outputbuff, BUFF_LONG);}return err;}PaError sound::write_buff(char * inputbuff, int bufflong){return  Pa_WriteStream(input, inputbuff, BUFF_LONG);}

採樣和播放

和老式的回調不同,阻塞式的讀寫,具有優雅的可程式化性但是效能略低。

另在回調的模式中,和阻塞式不同的是,需要編寫2個冗長的回呼函數

typedef int PaStreamCallback(const void *input, void *output, unsigned long frameCount, const PaStreamCallbackTimeInfo *timeInfo, PaStreamCallbackFlags statusFlags, void *userData)

在input回調中向寫入input緩衝區寫入,在output回調中處理output緩衝區中採集到的資料。

  

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.