boo/lib/audiodev/WASAPI.cpp

264 lines
9.3 KiB
C++
Raw Normal View History

#include "../win/Win32Common.hpp"
2016-03-24 01:50:34 +00:00
#include "AudioVoiceEngine.hpp"
#include "logvisor/logvisor.hpp"
#include <Mmdeviceapi.h>
#include <Audioclient.h>
const CLSID CLSID_MMDeviceEnumerator = __uuidof(MMDeviceEnumerator);
const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator);
const IID IID_IAudioClient = __uuidof(IAudioClient);
const IID IID_IAudioRenderClient = __uuidof(IAudioRenderClient);
namespace boo
{
static logvisor::Module Log("boo::WASAPI");
2016-03-24 01:50:34 +00:00
struct WASAPIAudioVoiceEngine : BaseAudioVoiceEngine
{
2016-03-24 01:50:34 +00:00
ComPtr<IMMDevice> m_device;
ComPtr<IAudioClient> m_audClient;
ComPtr<IAudioRenderClient> m_renderClient;
2016-03-24 01:50:34 +00:00
WASAPIAudioVoiceEngine()
{
2016-03-24 01:50:34 +00:00
/* Enumerate default audio device */
ComPtr<IMMDeviceEnumerator> pEnumerator;
if (FAILED(CoCreateInstance(CLSID_MMDeviceEnumerator, nullptr,
CLSCTX_ALL, IID_IMMDeviceEnumerator,
&pEnumerator)))
{
2016-03-24 01:50:34 +00:00
Log.report(logvisor::Fatal, L"unable to create MMDeviceEnumerator instance");
return;
}
2016-03-24 01:50:34 +00:00
if (FAILED(pEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &m_device)))
{
2016-03-24 01:50:34 +00:00
Log.report(logvisor::Fatal, L"unable to obtain default audio device");
m_device.Reset();
return;
}
2016-03-24 01:50:34 +00:00
if (FAILED(m_device->Activate(IID_IAudioClient, CLSCTX_ALL, nullptr, &m_audClient)))
{
2016-03-24 01:50:34 +00:00
Log.report(logvisor::Fatal, L"unable to create audio client from device");
m_device.Reset();
return;
}
2016-03-24 01:50:34 +00:00
WAVEFORMATEXTENSIBLE* pwfx;
if (FAILED(m_audClient->GetMixFormat((WAVEFORMATEX**)&pwfx)))
{
2016-03-24 01:50:34 +00:00
Log.report(logvisor::Fatal, L"unable to obtain audio mix format from device");
m_device.Reset();
return;
}
2016-03-24 01:50:34 +00:00
/* Get channel information */
if ((pwfx->dwChannelMask & (SPEAKER_FRONT_LEFT|SPEAKER_FRONT_RIGHT)) == (SPEAKER_FRONT_LEFT|SPEAKER_FRONT_RIGHT))
{
m_mixInfo.m_channels = AudioChannelSet::Stereo;
if ((pwfx->dwChannelMask & (SPEAKER_BACK_LEFT|SPEAKER_BACK_RIGHT)) == (SPEAKER_BACK_LEFT|SPEAKER_BACK_RIGHT))
{
m_mixInfo.m_channels = AudioChannelSet::Quad;
if ((pwfx->dwChannelMask & (SPEAKER_FRONT_CENTER|SPEAKER_LOW_FREQUENCY)) == (SPEAKER_FRONT_CENTER|SPEAKER_LOW_FREQUENCY))
{
m_mixInfo.m_channels = AudioChannelSet::Surround51;
if ((pwfx->dwChannelMask & (SPEAKER_SIDE_LEFT|SPEAKER_SIDE_RIGHT)) == (SPEAKER_SIDE_LEFT|SPEAKER_SIDE_RIGHT))
{
m_mixInfo.m_channels = AudioChannelSet::Surround71;
}
}
}
}
ChannelMap& chMapOut = m_mixInfo.m_channelMap;
switch (pwfx->Format.nChannels)
{
case 2:
2016-03-24 01:50:34 +00:00
chMapOut.m_channelCount = 2;
chMapOut.m_channels[0] = AudioChannel::FrontLeft;
chMapOut.m_channels[1] = AudioChannel::FrontRight;
break;
case 4:
2016-03-24 01:50:34 +00:00
chMapOut.m_channelCount = 4;
chMapOut.m_channels[0] = AudioChannel::FrontLeft;
chMapOut.m_channels[1] = AudioChannel::FrontRight;
chMapOut.m_channels[2] = AudioChannel::RearLeft;
chMapOut.m_channels[3] = AudioChannel::RearRight;
break;
case 5:
2016-03-24 01:50:34 +00:00
chMapOut.m_channelCount = 5;
chMapOut.m_channels[0] = AudioChannel::FrontLeft;
chMapOut.m_channels[1] = AudioChannel::FrontRight;
chMapOut.m_channels[2] = AudioChannel::FrontCenter;
chMapOut.m_channels[3] = AudioChannel::RearLeft;
chMapOut.m_channels[4] = AudioChannel::RearRight;
break;
case 6:
2016-03-24 01:50:34 +00:00
chMapOut.m_channelCount = 6;
chMapOut.m_channels[0] = AudioChannel::FrontLeft;
chMapOut.m_channels[1] = AudioChannel::FrontRight;
chMapOut.m_channels[2] = AudioChannel::FrontCenter;
chMapOut.m_channels[3] = AudioChannel::LFE;
chMapOut.m_channels[4] = AudioChannel::RearLeft;
chMapOut.m_channels[5] = AudioChannel::RearRight;
break;
case 8:
2016-03-24 01:50:34 +00:00
chMapOut.m_channelCount = 8;
chMapOut.m_channels[0] = AudioChannel::FrontLeft;
chMapOut.m_channels[1] = AudioChannel::FrontRight;
chMapOut.m_channels[2] = AudioChannel::FrontCenter;
chMapOut.m_channels[3] = AudioChannel::LFE;
chMapOut.m_channels[4] = AudioChannel::RearLeft;
chMapOut.m_channels[5] = AudioChannel::RearRight;
chMapOut.m_channels[6] = AudioChannel::SideLeft;
chMapOut.m_channels[7] = AudioChannel::SideRight;
break;
default:
2016-03-24 01:50:34 +00:00
Log.report(logvisor::Warning, "unknown channel layout %u; using stereo", pwfx->Format.nChannels);
chMapOut.m_channelCount = 2;
chMapOut.m_channels[0] = AudioChannel::FrontLeft;
chMapOut.m_channels[1] = AudioChannel::FrontRight;
break;
}
2016-03-24 01:50:34 +00:00
/* Initialize audio client */
if (FAILED(m_audClient->Initialize(
AUDCLNT_SHAREMODE_SHARED,
0,
1000000,
0,
(WAVEFORMATEX*)pwfx,
nullptr)))
{
2016-03-24 01:50:34 +00:00
Log.report(logvisor::Fatal, L"unable to initialize audio client");
m_device.Reset();
CoTaskMemFree(pwfx);
return;
}
2016-03-24 01:50:34 +00:00
m_mixInfo.m_sampleRate = pwfx->Format.nSamplesPerSec;
2016-03-24 01:50:34 +00:00
if (pwfx->Format.wFormatTag == WAVE_FORMAT_PCM ||
(pwfx->Format.wFormatTag == WAVE_FORMAT_EXTENSIBLE && pwfx->SubFormat == KSDATAFORMAT_SUBTYPE_PCM))
{
2016-03-24 01:50:34 +00:00
if (pwfx->Format.wBitsPerSample == 16)
{
m_mixInfo.m_sampleFormat = SOXR_INT16_I;
m_mixInfo.m_bitsPerSample = 16;
}
else if (pwfx->Format.wBitsPerSample == 32)
{
m_mixInfo.m_sampleFormat = SOXR_INT32_I;
m_mixInfo.m_bitsPerSample = 32;
}
else
{
Log.report(logvisor::Fatal, L"unsupported bits-per-sample %d", pwfx->Format.wBitsPerSample);
m_device.Reset();
return;
}
}
else if (pwfx->Format.wFormatTag == WAVE_FORMAT_IEEE_FLOAT ||
(pwfx->Format.wFormatTag == WAVE_FORMAT_EXTENSIBLE && pwfx->SubFormat == KSDATAFORMAT_SUBTYPE_IEEE_FLOAT))
{
if (pwfx->Format.wBitsPerSample == 32)
{
m_mixInfo.m_sampleFormat = SOXR_FLOAT32_I;
m_mixInfo.m_bitsPerSample = 32;
}
else
{
Log.report(logvisor::Fatal, L"unsupported floating-point bits-per-sample %d", pwfx->Format.wBitsPerSample);
m_device.Reset();
return;
}
}
2016-03-24 01:50:34 +00:00
CoTaskMemFree(pwfx);
2016-03-24 01:50:34 +00:00
UINT32 bufferFrameCount;
if (FAILED(m_audClient->GetBufferSize(&bufferFrameCount)))
{
Log.report(logvisor::Fatal, L"unable to get audio buffer frame count");
m_device.Reset();
return;
}
m_mixInfo.m_periodFrames = bufferFrameCount;
2016-03-24 01:50:34 +00:00
if (FAILED(m_audClient->GetService(IID_IAudioRenderClient, &m_renderClient)))
{
2016-03-24 01:50:34 +00:00
Log.report(logvisor::Fatal, L"unable to create audio render client");
m_device.Reset();
return;
}
}
2016-03-24 01:50:34 +00:00
bool m_started = false;
2016-03-24 01:50:34 +00:00
void pumpAndMixVoices()
{
2016-03-24 01:50:34 +00:00
UINT32 numFramesPadding;
if (FAILED(m_audClient->GetCurrentPadding(&numFramesPadding)))
{
2016-03-24 01:50:34 +00:00
Log.report(logvisor::Fatal, L"unable to get available buffer frames");
return;
}
2016-03-24 01:50:34 +00:00
size_t frames = m_mixInfo.m_periodFrames - numFramesPadding;
if (frames <= 0)
return;
BYTE* bufOut;
if (FAILED(m_renderClient->GetBuffer(frames, &bufOut)))
{
2016-03-24 01:50:34 +00:00
Log.report(logvisor::Fatal, L"unable to map audio buffer");
return;
}
2016-03-24 01:50:34 +00:00
DWORD flags = 0;
switch (m_mixInfo.m_sampleFormat)
{
2016-03-24 01:50:34 +00:00
case SOXR_INT16_I:
_pumpAndMixVoices(frames, reinterpret_cast<int16_t*>(bufOut));
break;
case SOXR_INT32_I:
_pumpAndMixVoices(frames, reinterpret_cast<int32_t*>(bufOut));
break;
case SOXR_FLOAT32_I:
_pumpAndMixVoices(frames, reinterpret_cast<float*>(bufOut));
break;
default:
flags = AUDCLNT_BUFFERFLAGS_SILENT;
break;
}
2016-03-24 01:50:34 +00:00
if (FAILED(m_renderClient->ReleaseBuffer(frames, flags)))
{
2016-03-24 01:50:34 +00:00
Log.report(logvisor::Fatal, L"unable to unmap audio buffer");
return;
}
2016-03-24 01:50:34 +00:00
if (!m_started)
{
2016-03-24 01:50:34 +00:00
if (FAILED(m_audClient->Start()))
{
2016-03-24 01:50:34 +00:00
Log.report(logvisor::Fatal, L"unable to start audio client");
m_device.Reset();
return;
}
2016-03-24 01:50:34 +00:00
m_started = true;
}
}
};
2016-03-24 01:50:34 +00:00
std::unique_ptr<IAudioVoiceEngine> NewAudioVoiceEngine()
{
2016-03-24 01:50:34 +00:00
std::unique_ptr<IAudioVoiceEngine> ret = std::make_unique<WASAPIAudioVoiceEngine>();
if (!static_cast<WASAPIAudioVoiceEngine&>(*ret).m_device)
return {};
return ret;
}
}