boo/lib/audiodev/AQS.cpp

257 lines
9.8 KiB
C++
Raw Normal View History

#include "AudioVoiceEngine.hpp"
2016-03-04 23:02:18 +00:00
#include "logvisor/logvisor.hpp"
#include <AudioToolbox/AudioToolbox.h>
#include <mutex>
#include <condition_variable>
namespace boo
{
2016-03-04 23:02:18 +00:00
static logvisor::Module Log("boo::AQS");
static AudioChannel AQSChannelToBooChannel(AudioChannelLabel ch)
{
switch (ch)
{
case kAudioChannelLabel_Left:
return AudioChannel::FrontLeft;
case kAudioChannelLabel_Right:
return AudioChannel::FrontRight;
case kAudioChannelLabel_LeftSurround:
return AudioChannel::RearLeft;
case kAudioChannelLabel_RightSurround:
return AudioChannel::RearRight;
case kAudioChannelLabel_Center:
return AudioChannel::FrontCenter;
case kAudioChannelLabel_LFEScreen:
return AudioChannel::LFE;
case kAudioChannelLabel_LeftSurroundDirect:
return AudioChannel::RearLeft;
case kAudioChannelLabel_RightSurroundDirect:
return AudioChannel::SideRight;
}
return AudioChannel::Unknown;
}
struct AQSAudioVoiceEngine : BaseAudioVoiceEngine
{
AudioQueueRef m_queue = nullptr;
AudioQueueBufferRef m_buffers[3];
size_t m_frameBytes;
std::mutex m_engineMutex;
std::condition_variable m_engineCv;
static void Callback(AQSAudioVoiceEngine* engine, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer)
{
std::unique_lock<std::mutex> lk(engine->m_engineMutex);
engine->m_engineCv.wait(lk);
engine->_pumpAndMixVoices(engine->m_mixInfo.m_periodFrames, reinterpret_cast<int32_t*>(inBuffer->mAudioData));
inBuffer->mAudioDataByteSize = engine->m_frameBytes;
AudioQueueEnqueueBuffer(inAQ, inBuffer, 0, nullptr);
}
static void DummyCallback(AQSAudioVoiceEngine* engine, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer) {}
AudioChannelSet _getAvailableSet()
{
const unsigned chCount = 8;
AudioStreamBasicDescription desc = {};
desc.mSampleRate = 96000;
desc.mFormatID = kAudioFormatLinearPCM;
desc.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger;
desc.mBytesPerPacket = chCount * 4;
desc.mFramesPerPacket = 1;
desc.mBytesPerFrame = chCount * 4;
desc.mChannelsPerFrame = chCount;
desc.mBitsPerChannel = 32;
AudioQueueRef queue;
if (AudioQueueNewOutput(&desc, AudioQueueOutputCallback(DummyCallback),
this, nullptr, nullptr, 0, &queue))
{
Log.report(logvisor::Error, "unable to create output audio queue");
return AudioChannelSet::Unknown;
}
UInt32 hwChannels;
UInt32 channelsSz = sizeof(UInt32);
if (AudioQueueGetProperty(queue, kAudioQueueDeviceProperty_NumberChannels, &hwChannels, &channelsSz))
{
Log.report(logvisor::Error, "unable to get channel count from audio queue");
AudioQueueDispose(queue, true);
return AudioChannelSet::Unknown;
}
AudioQueueDispose(queue, true);
switch (hwChannels)
{
case 2:
return AudioChannelSet::Stereo;
case 4:
return AudioChannelSet::Quad;
case 6:
return AudioChannelSet::Surround51;
case 8:
return AudioChannelSet::Surround71;
default: break;
}
return AudioChannelSet::Unknown;
}
AQSAudioVoiceEngine()
{
m_mixInfo.m_channels = _getAvailableSet();
unsigned chCount = ChannelCount(m_mixInfo.m_channels);
AudioStreamBasicDescription desc = {};
desc.mSampleRate = 96000;
desc.mFormatID = kAudioFormatLinearPCM;
desc.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger;
desc.mBytesPerPacket = chCount * 4;
desc.mFramesPerPacket = 1;
desc.mBytesPerFrame = chCount * 4;
desc.mChannelsPerFrame = chCount;
desc.mBitsPerChannel = 32;
OSStatus err;
if ((err = AudioQueueNewOutput(&desc, AudioQueueOutputCallback(Callback),
this, nullptr, nullptr, 0, &m_queue)))
{
2016-03-08 21:18:38 +00:00
Log.report(logvisor::Fatal, "unable to create output audio queue");
return;
}
m_mixInfo.m_sampleRate = 96000.0;
m_mixInfo.m_sampleFormat = SOXR_INT32_I;
m_mixInfo.m_bitsPerSample = 32;
ChannelMap& chMapOut = m_mixInfo.m_channelMap;
2016-03-08 21:18:38 +00:00
if (chCount > 2)
{
2016-03-08 21:18:38 +00:00
AudioChannelLayout layout;
UInt32 layoutSz = sizeof(layout);
if (AudioQueueGetProperty(m_queue, kAudioQueueProperty_ChannelLayout, &layout, &layoutSz))
{
Log.report(logvisor::Fatal, "unable to get channel layout from audio queue");
return;
}
2016-03-08 21:18:38 +00:00
switch (layout.mChannelLayoutTag)
{
2016-03-08 21:18:38 +00:00
case kAudioChannelLayoutTag_UseChannelDescriptions:
chMapOut.m_channelCount = layout.mNumberChannelDescriptions;
2016-03-08 21:18:38 +00:00
for (int i=0 ; i<layout.mNumberChannelDescriptions ; ++i)
{
AudioChannel ch = AQSChannelToBooChannel(layout.mChannelDescriptions[i].mChannelLabel);
chMapOut.m_channels[i] = ch;
2016-03-08 21:18:38 +00:00
}
break;
case kAudioChannelLayoutTag_UseChannelBitmap:
if ((layout.mChannelBitmap & kAudioChannelBit_Left) != 0)
chMapOut.m_channels[chMapOut.m_channelCount++] = AudioChannel::FrontLeft;
2016-03-08 21:18:38 +00:00
if ((layout.mChannelBitmap & kAudioChannelBit_Right) != 0)
chMapOut.m_channels[chMapOut.m_channelCount++] = AudioChannel::FrontRight;
2016-03-08 21:18:38 +00:00
if ((layout.mChannelBitmap & kAudioChannelBit_Center) != 0)
chMapOut.m_channels[chMapOut.m_channelCount++] = AudioChannel::FrontCenter;
2016-03-08 21:18:38 +00:00
if ((layout.mChannelBitmap & kAudioChannelBit_LFEScreen) != 0)
chMapOut.m_channels[chMapOut.m_channelCount++] = AudioChannel::LFE;
2016-03-08 21:18:38 +00:00
if ((layout.mChannelBitmap & kAudioChannelBit_LeftSurround) != 0)
chMapOut.m_channels[chMapOut.m_channelCount++] = AudioChannel::RearLeft;
2016-03-08 21:18:38 +00:00
if ((layout.mChannelBitmap & kAudioChannelBit_RightSurround) != 0)
chMapOut.m_channels[chMapOut.m_channelCount++] = AudioChannel::RearRight;
2016-03-08 21:18:38 +00:00
if ((layout.mChannelBitmap & kAudioChannelBit_LeftSurroundDirect) != 0)
chMapOut.m_channels[chMapOut.m_channelCount++] = AudioChannel::SideLeft;
2016-03-08 21:18:38 +00:00
if ((layout.mChannelBitmap & kAudioChannelBit_RightSurroundDirect) != 0)
chMapOut.m_channels[chMapOut.m_channelCount++] = AudioChannel::SideRight;
2016-03-08 21:18:38 +00:00
break;
case kAudioChannelLayoutTag_Stereo:
case kAudioChannelLayoutTag_StereoHeadphones:
chMapOut.m_channelCount = 2;
chMapOut.m_channels[0] = AudioChannel::FrontLeft;
chMapOut.m_channels[1] = AudioChannel::FrontRight;
2016-03-08 21:18:38 +00:00
break;
case kAudioChannelLayoutTag_Quadraphonic:
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;
2016-03-08 21:18:38 +00:00
break;
case kAudioChannelLayoutTag_Pentagonal:
chMapOut.m_channelCount = 5;
chMapOut.m_channels[0] = AudioChannel::FrontLeft;
chMapOut.m_channels[1] = AudioChannel::FrontRight;
chMapOut.m_channels[2] = AudioChannel::RearLeft;
chMapOut.m_channels[3] = AudioChannel::RearRight;
chMapOut.m_channels[4] = AudioChannel::FrontCenter;
2016-03-08 21:18:38 +00:00
break;
default:
2016-03-23 07:01:25 +00:00
Log.report(logvisor::Fatal,
"unknown channel layout %u; using stereo",
layout.mChannelLayoutTag);
chMapOut.m_channelCount = 2;
chMapOut.m_channels[0] = AudioChannel::FrontLeft;
chMapOut.m_channels[1] = AudioChannel::FrontRight;
2016-03-08 21:18:38 +00:00
break;
}
2016-03-08 21:18:38 +00:00
}
else
{
chMapOut.m_channels[chMapOut.m_channelCount++] = AudioChannel::FrontLeft;
chMapOut.m_channels[chMapOut.m_channelCount++] = AudioChannel::FrontRight;
}
while (chMapOut.m_channelCount < chCount)
chMapOut.m_channels[chMapOut.m_channelCount++] = AudioChannel::Unknown;
m_mixInfo.m_periodFrames = 2400;
for (int i=0 ; i<3 ; ++i)
if (AudioQueueAllocateBuffer(m_queue, m_mixInfo.m_periodFrames * chCount * 4, &m_buffers[i]))
{
2016-03-08 21:18:38 +00:00
Log.report(logvisor::Fatal, "unable to create audio queue buffer");
AudioQueueDispose(m_queue, false);
m_queue = nullptr;
return;
}
m_frameBytes = m_mixInfo.m_periodFrames * m_mixInfo.m_channelMap.m_channelCount * 4;
for (unsigned i=0 ; i<3 ; ++i)
{
_pumpAndMixVoices(m_mixInfo.m_periodFrames, reinterpret_cast<int32_t*>(m_buffers[i]->mAudioData));
m_buffers[i]->mAudioDataByteSize = m_frameBytes;
AudioQueueEnqueueBuffer(m_queue, m_buffers[i], 0, nullptr);
}
AudioQueuePrime(m_queue, 0, nullptr);
AudioQueueStart(m_queue, nullptr);
}
~AQSAudioVoiceEngine()
2016-03-08 21:18:38 +00:00
{
AudioQueueDispose(m_queue, false);
2016-03-08 21:18:38 +00:00
}
void pumpAndMixVoices()
{
std::unique_lock<std::mutex> lk(m_engineMutex);
m_engineCv.notify_one();
lk.unlock();
lk.lock();
}
};
std::unique_ptr<IAudioVoiceEngine> NewAudioVoiceEngine()
{
std::unique_ptr<IAudioVoiceEngine> ret = std::make_unique<AQSAudioVoiceEngine>();
if (!static_cast<AQSAudioVoiceEngine&>(*ret).m_queue)
return {};
return ret;
}
}