127 lines
2.8 KiB
C++
127 lines
2.8 KiB
C++
#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;
|
|
|
|
}
|
|
|
|
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;
|
|
}
|
|
//»ñµÃÉ豸ÊýÁ¿
|
|
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;
|
|
}
|
|
}
|