multimedia/client/qt_gl_/yuvgl/media/AudioCapture.cpp

134 lines
3.0 KiB
C++
Raw Normal View History

2020-06-04 15:28:45 +00:00
#include "AudioCapture.h"
#include "Debuger.h"
PaStream *gStreamOut = nullptr;
CaptureAudio::CaptureAudio(uint16_t rate, uint8_t channel) {
this->mChanel = channel;
this->mSampleRate = rate;
this->mSize = 0;
this->mStatus = FAIL;
this->observer = nullptr;
}
2020-06-21 17:13:44 +00:00
/**
* @brief <EFBFBD><EFBFBD><EFBFBD>ò<EFBFBD><EFBFBD>񵽵Ļص<EFBFBD>֪ͨ<EFBFBD><EFBFBD>
*
* @param CaptureAudioObserver <EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><EFBFBD>
* @return <EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><EFBFBD>
* @retval -1 <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
* @retval 0<EFBFBD>ɹ<EFBFBD>
*/
2020-06-04 15:28:45 +00:00
int CaptureAudio::SetObserver(CaptureAudioObserver* ob) {
if (nullptr == ob) return -1;
this->observer = ob;
return 0;
}
int paOutStreamBkss(const void* input, void* output, unsigned long frameCount,
const PaStreamCallbackTimeInfo* timeInfo, PaStreamCallbackFlags statusFlags, void * userData)
{
CaptureAudio *pCap;
Debuger::Debug(L"%d\r\n", frameCount);
if (userData != nullptr) {
pCap = (CaptureAudio *)userData;
pCap->OnCallBack(input,output,frameCount);
}
pCap->AddCnt(4 * frameCount);
return 0;
}
int CaptureAudio::OnCallBack(const void* input, void* output, unsigned long frameCount) {
if(nullptr != this->observer)
this->observer->OnAudioData(input, frameCount);
return 0;
}
CaptureAudio::~CaptureAudio() {
if(mInStream != nullptr)
Pa_CloseStream(mInStream);
}
int CaptureAudio::StartCapture()
{
PaError err = paNoError;
if (this->mStatus == RUNNING) {
err = Pa_StartStream(mInStream);
if (err != paNoError) {
this->mStatus = FAIL;
}
}
else
return -1;
return 0;
}
vector<CaptureAudio::MICInfo> CaptureAudio::EnumSpeakers()
{
vector<CaptureAudio::MICInfo> ret;
PaError err = Pa_Initialize();
if (err != paNoError) {
Debuger::Debug(L"init stream error\r\n");
mStatus = FAIL;
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E8B1B8><EFBFBD><EFBFBD>
PaDeviceIndex iNumDevices = Pa_GetDeviceCount();
if (iNumDevices <= 0)
{
return ret;
}
for (int i = 0; i < iNumDevices; i++)
{
MICInfo ins;
ins.index = i;
const PaDeviceInfo *deviceInfo = Pa_GetDeviceInfo(i);
if (nullptr != deviceInfo)
if (deviceInfo->maxInputChannels > 0) {
ins.name = deviceInfo->name;
ret.push_back(ins);
}
}
return ret;
}
int CaptureAudio::InitCapture(int index,uint16_t rate, uint8_t channel) {
PaStreamParameters intputParameters;
PaError err = paNoError;
err = Pa_Initialize();
if (err != paNoError) goto error;
if (index < 0)
{
index = Pa_GetDefaultInputDevice();
}
if (paNoDevice == index) {
mStatus = FAIL;
return -1;
}
intputParameters.device = index;
intputParameters.channelCount = 2;
intputParameters.sampleFormat = paInt16;
intputParameters.suggestedLatency = Pa_GetDeviceInfo(intputParameters.device)->defaultLowInputLatency;
intputParameters.hostApiSpecificStreamInfo = NULL;
err = Pa_OpenStream(&mInStream, &intputParameters, NULL, 44100, 1024,
paFramesPerBufferUnspecified, paOutStreamBkss, this);
if (err != paNoError) {
this->mStatus = FAIL;
return -1;
}
this->mStatus = RUNNING;
return 0;
error:
Pa_Terminate();
return -1;
}
void CaptureAudio::StopCapture()
{
if (this->mStatus == RUNNING) {
Pa_StopStream(mInStream);
this->mStatus = STOP;
}
}