92 lines
2.2 KiB
C++
92 lines
2.2 KiB
C++
#include "AudioPlayer.h"
|
|
#include "Debuger.h"
|
|
#include "utils.h"
|
|
AudioPlayer::AudioPlayer(int index)
|
|
{
|
|
mStatus = RUNNING;
|
|
|
|
PaError err = Pa_Initialize();
|
|
if (err != paNoError) {
|
|
Debuger::Debug(L"init stream error\r\n");
|
|
mStatus = FAIL;
|
|
}
|
|
//获得设备数量
|
|
PaDeviceIndex iNumDevices = Pa_GetDeviceCount();
|
|
if (iNumDevices < 0)
|
|
{
|
|
}
|
|
for (int i = 0; i < iNumDevices; i++)
|
|
{
|
|
const PaDeviceInfo *deviceInfo = Pa_GetDeviceInfo(i);
|
|
Debuger::Debug(L"index %d %d %d \r\n",i,
|
|
deviceInfo->maxInputChannels, deviceInfo->maxOutputChannels); //打印设备名
|
|
}
|
|
mOutputParameters.device = index;
|
|
mOutputParameters.channelCount = 2; //输出采用双声道,左声道在前
|
|
mOutputParameters.sampleFormat = paInt16;
|
|
mOutputParameters.suggestedLatency = Pa_GetDeviceInfo(mOutputParameters.device)->defaultLowOutputLatency;
|
|
mOutputParameters.hostApiSpecificStreamInfo = NULL;
|
|
|
|
err = Pa_OpenStream(&mOutStream, NULL, &mOutputParameters, 44100, 1024,
|
|
paFramesPerBufferUnspecified, NULL, NULL);
|
|
if (err != paNoError) {
|
|
Debuger::Debug(L"open output stream error\r\n");
|
|
mStatus = FAIL;
|
|
goto end;
|
|
}
|
|
err = Pa_StartStream(mOutStream);
|
|
if (err != paNoError) {
|
|
Debuger::Debug(L"start stream error\r\n");
|
|
mStatus = FAIL;
|
|
}
|
|
end:
|
|
return;
|
|
}
|
|
|
|
vector<AudioPlayer::SpeakerInfo> AudioPlayer::EnumSpeakers()
|
|
{
|
|
vector<AudioPlayer::SpeakerInfo> 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++)
|
|
{
|
|
SpeakerInfo ins;
|
|
ins.index = i;
|
|
const PaDeviceInfo *deviceInfo = Pa_GetDeviceInfo(i);
|
|
if(nullptr != deviceInfo)
|
|
if (deviceInfo->maxOutputChannels > 0) {
|
|
//ins.name = char2wchar(deviceInfo->name);
|
|
ret.push_back(ins);
|
|
}
|
|
}
|
|
return ret;
|
|
}
|
|
int AudioPlayer::Play(uint8_t * data, uint16_t num)
|
|
{
|
|
PaError err;
|
|
if (mStatus == RUNNING) {
|
|
err = Pa_WriteStream(mOutStream, data, num);
|
|
if (paNoError != err) {
|
|
return -1;
|
|
}
|
|
}
|
|
else {
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
int AudioPlayer::OnAudioDecode(uint8_t * dat, uint16_t size)
|
|
{
|
|
return this->Play(dat, 1024);
|
|
return 0;
|
|
}
|