2016-03-24 02:50:36 +00:00
|
|
|
#include "AudioVoiceEngine.hpp"
|
2016-03-04 23:02:18 +00:00
|
|
|
#include "logvisor/logvisor.hpp"
|
2016-01-29 01:17:19 +00:00
|
|
|
|
|
|
|
#include <AudioToolbox/AudioToolbox.h>
|
|
|
|
|
2016-03-24 02:50:36 +00:00
|
|
|
#include <mutex>
|
|
|
|
#include <condition_variable>
|
|
|
|
|
2016-01-29 01:17:19 +00:00
|
|
|
namespace boo
|
|
|
|
{
|
2016-03-04 23:02:18 +00:00
|
|
|
static logvisor::Module Log("boo::AQS");
|
2016-01-29 01:17:19 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-03-24 02:50:36 +00:00
|
|
|
struct AQSAudioVoiceEngine : BaseAudioVoiceEngine
|
2016-01-29 01:17:19 +00:00
|
|
|
{
|
|
|
|
AudioQueueRef m_queue = nullptr;
|
|
|
|
AudioQueueBufferRef m_buffers[3];
|
2016-03-24 02:50:36 +00:00
|
|
|
size_t m_frameBytes;
|
2016-01-29 01:17:19 +00:00
|
|
|
|
2016-03-24 02:50:36 +00:00
|
|
|
std::mutex m_engineMutex;
|
|
|
|
std::condition_variable m_engineCv;
|
2016-01-29 01:17:19 +00:00
|
|
|
|
2016-03-24 08:05:19 +00:00
|
|
|
static void Callback(AQSAudioVoiceEngine* engine, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer)
|
2016-01-29 01:17:19 +00:00
|
|
|
{
|
2016-03-24 08:05:19 +00:00
|
|
|
std::unique_lock<std::mutex> lk(engine->m_engineMutex);
|
|
|
|
engine->m_engineCv.wait(lk);
|
2016-03-24 02:50:36 +00:00
|
|
|
|
2016-03-24 08:05:19 +00:00
|
|
|
engine->_pumpAndMixVoices(engine->m_mixInfo.m_periodFrames, reinterpret_cast<int32_t*>(inBuffer->mAudioData));
|
|
|
|
inBuffer->mAudioDataByteSize = engine->m_frameBytes;
|
2016-03-24 02:50:36 +00:00
|
|
|
AudioQueueEnqueueBuffer(inAQ, inBuffer, 0, nullptr);
|
2016-01-29 01:17:19 +00:00
|
|
|
}
|
|
|
|
|
2016-03-24 08:05:19 +00:00
|
|
|
static void DummyCallback(AQSAudioVoiceEngine* engine, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer) {}
|
2016-01-29 01:17:19 +00:00
|
|
|
|
2016-03-24 02:50:36 +00:00
|
|
|
AudioChannelSet _getAvailableSet()
|
|
|
|
{
|
|
|
|
const unsigned chCount = 8;
|
2016-01-29 01:17:19 +00:00
|
|
|
AudioStreamBasicDescription desc = {};
|
2016-03-24 02:50:36 +00:00
|
|
|
desc.mSampleRate = 96000;
|
2016-01-29 01:17:19 +00:00
|
|
|
desc.mFormatID = kAudioFormatLinearPCM;
|
|
|
|
desc.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger;
|
2016-03-24 02:50:36 +00:00
|
|
|
desc.mBytesPerPacket = chCount * 4;
|
2016-01-29 01:17:19 +00:00
|
|
|
desc.mFramesPerPacket = 1;
|
2016-03-24 02:50:36 +00:00
|
|
|
desc.mBytesPerFrame = chCount * 4;
|
2016-01-29 01:17:19 +00:00
|
|
|
desc.mChannelsPerFrame = chCount;
|
2016-03-24 02:50:36 +00:00
|
|
|
desc.mBitsPerChannel = 32;
|
2016-01-29 01:17:19 +00:00
|
|
|
|
2016-03-24 02:50:36 +00:00
|
|
|
AudioQueueRef queue;
|
|
|
|
if (AudioQueueNewOutput(&desc, AudioQueueOutputCallback(DummyCallback),
|
|
|
|
this, nullptr, nullptr, 0, &queue))
|
2016-01-29 01:17:19 +00:00
|
|
|
{
|
2016-03-24 02:50:36 +00:00
|
|
|
Log.report(logvisor::Error, "unable to create output audio queue");
|
|
|
|
return AudioChannelSet::Unknown;
|
2016-01-29 01:17:19 +00:00
|
|
|
}
|
2016-03-24 02:50:36 +00:00
|
|
|
|
|
|
|
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-01-29 01:17:19 +00:00
|
|
|
{
|
2016-03-08 21:18:38 +00:00
|
|
|
Log.report(logvisor::Fatal, "unable to create output audio queue");
|
2016-01-29 01:17:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-24 02:50:36 +00:00
|
|
|
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-01-29 01:17:19 +00:00
|
|
|
{
|
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-01-29 01:17:19 +00:00
|
|
|
|
2016-03-08 21:18:38 +00:00
|
|
|
switch (layout.mChannelLayoutTag)
|
2016-01-29 01:17:19 +00:00
|
|
|
{
|
2016-03-08 21:18:38 +00:00
|
|
|
case kAudioChannelLayoutTag_UseChannelDescriptions:
|
2016-03-24 02:50:36 +00:00
|
|
|
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);
|
2016-03-24 02:50:36 +00:00
|
|
|
chMapOut.m_channels[i] = ch;
|
2016-03-08 21:18:38 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kAudioChannelLayoutTag_UseChannelBitmap:
|
|
|
|
if ((layout.mChannelBitmap & kAudioChannelBit_Left) != 0)
|
2016-03-24 02:50:36 +00:00
|
|
|
chMapOut.m_channels[chMapOut.m_channelCount++] = AudioChannel::FrontLeft;
|
2016-03-08 21:18:38 +00:00
|
|
|
if ((layout.mChannelBitmap & kAudioChannelBit_Right) != 0)
|
2016-03-24 02:50:36 +00:00
|
|
|
chMapOut.m_channels[chMapOut.m_channelCount++] = AudioChannel::FrontRight;
|
2016-03-08 21:18:38 +00:00
|
|
|
if ((layout.mChannelBitmap & kAudioChannelBit_Center) != 0)
|
2016-03-24 02:50:36 +00:00
|
|
|
chMapOut.m_channels[chMapOut.m_channelCount++] = AudioChannel::FrontCenter;
|
2016-03-08 21:18:38 +00:00
|
|
|
if ((layout.mChannelBitmap & kAudioChannelBit_LFEScreen) != 0)
|
2016-03-24 02:50:36 +00:00
|
|
|
chMapOut.m_channels[chMapOut.m_channelCount++] = AudioChannel::LFE;
|
2016-03-08 21:18:38 +00:00
|
|
|
if ((layout.mChannelBitmap & kAudioChannelBit_LeftSurround) != 0)
|
2016-03-24 02:50:36 +00:00
|
|
|
chMapOut.m_channels[chMapOut.m_channelCount++] = AudioChannel::RearLeft;
|
2016-03-08 21:18:38 +00:00
|
|
|
if ((layout.mChannelBitmap & kAudioChannelBit_RightSurround) != 0)
|
2016-03-24 02:50:36 +00:00
|
|
|
chMapOut.m_channels[chMapOut.m_channelCount++] = AudioChannel::RearRight;
|
2016-03-08 21:18:38 +00:00
|
|
|
if ((layout.mChannelBitmap & kAudioChannelBit_LeftSurroundDirect) != 0)
|
2016-03-24 02:50:36 +00:00
|
|
|
chMapOut.m_channels[chMapOut.m_channelCount++] = AudioChannel::SideLeft;
|
2016-03-08 21:18:38 +00:00
|
|
|
if ((layout.mChannelBitmap & kAudioChannelBit_RightSurroundDirect) != 0)
|
2016-03-24 02:50:36 +00:00
|
|
|
chMapOut.m_channels[chMapOut.m_channelCount++] = AudioChannel::SideRight;
|
2016-03-08 21:18:38 +00:00
|
|
|
break;
|
|
|
|
case kAudioChannelLayoutTag_Stereo:
|
|
|
|
case kAudioChannelLayoutTag_StereoHeadphones:
|
2016-03-24 02:50:36 +00:00
|
|
|
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:
|
2016-03-24 02:50:36 +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;
|
2016-03-08 21:18:38 +00:00
|
|
|
break;
|
|
|
|
case kAudioChannelLayoutTag_Pentagonal:
|
2016-03-24 02:50:36 +00:00
|
|
|
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);
|
2016-03-24 02:50:36 +00:00
|
|
|
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-01-29 01:17:19 +00:00
|
|
|
}
|
2016-03-08 21:18:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-03-24 02:50:36 +00:00
|
|
|
chMapOut.m_channels[chMapOut.m_channelCount++] = AudioChannel::FrontLeft;
|
|
|
|
chMapOut.m_channels[chMapOut.m_channelCount++] = AudioChannel::FrontRight;
|
2016-01-29 01:17:19 +00:00
|
|
|
}
|
|
|
|
|
2016-03-24 02:50:36 +00:00
|
|
|
while (chMapOut.m_channelCount < chCount)
|
|
|
|
chMapOut.m_channels[chMapOut.m_channelCount++] = AudioChannel::Unknown;
|
2016-01-29 01:17:19 +00:00
|
|
|
|
2016-03-24 02:50:36 +00:00
|
|
|
m_mixInfo.m_periodFrames = 2400;
|
2016-01-29 01:17:19 +00:00
|
|
|
for (int i=0 ; i<3 ; ++i)
|
2016-03-24 02:50:36 +00:00
|
|
|
if (AudioQueueAllocateBuffer(m_queue, m_mixInfo.m_periodFrames * chCount * 4, &m_buffers[i]))
|
2016-01-29 01:17:19 +00:00
|
|
|
{
|
2016-03-08 21:18:38 +00:00
|
|
|
Log.report(logvisor::Fatal, "unable to create audio queue buffer");
|
2016-01-29 01:17:19 +00:00
|
|
|
AudioQueueDispose(m_queue, false);
|
|
|
|
m_queue = nullptr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-24 02:50:36 +00:00
|
|
|
m_frameBytes = m_mixInfo.m_periodFrames * m_mixInfo.m_channelMap.m_channelCount * 4;
|
2016-01-29 01:17:19 +00:00
|
|
|
|
|
|
|
for (unsigned i=0 ; i<3 ; ++i)
|
|
|
|
{
|
2016-03-24 02:50:36 +00:00
|
|
|
_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);
|
2016-01-29 01:17:19 +00:00
|
|
|
}
|
|
|
|
AudioQueuePrime(m_queue, 0, nullptr);
|
|
|
|
AudioQueueStart(m_queue, nullptr);
|
|
|
|
}
|
|
|
|
|
2016-03-24 02:50:36 +00:00
|
|
|
~AQSAudioVoiceEngine()
|
2016-03-08 21:18:38 +00:00
|
|
|
{
|
2016-03-24 02:50:36 +00:00
|
|
|
AudioQueueDispose(m_queue, false);
|
2016-03-08 21:18:38 +00:00
|
|
|
}
|
|
|
|
|
2016-03-24 02:50:36 +00:00
|
|
|
void pumpAndMixVoices()
|
2016-01-29 01:17:19 +00:00
|
|
|
{
|
2016-03-24 02:50:36 +00:00
|
|
|
std::unique_lock<std::mutex> lk(m_engineMutex);
|
|
|
|
m_engineCv.notify_one();
|
|
|
|
lk.unlock();
|
|
|
|
lk.lock();
|
2016-01-29 01:17:19 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-03-24 02:50:36 +00:00
|
|
|
std::unique_ptr<IAudioVoiceEngine> NewAudioVoiceEngine()
|
2016-03-08 07:09:58 +00:00
|
|
|
{
|
2016-03-24 02:50:36 +00:00
|
|
|
std::unique_ptr<IAudioVoiceEngine> ret = std::make_unique<AQSAudioVoiceEngine>();
|
|
|
|
if (!static_cast<AQSAudioVoiceEngine&>(*ret).m_queue)
|
|
|
|
return {};
|
|
|
|
return ret;
|
2016-03-08 07:09:58 +00:00
|
|
|
}
|
|
|
|
|
2016-01-29 01:17:19 +00:00
|
|
|
}
|