標籤: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緩衝區中採集到的資料。