2016-01-29 01:17:19 +00:00
|
|
|
#include "boo/audiodev/IAudioVoiceAllocator.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>
|
|
|
|
|
|
|
|
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-01-30 01:45:40 +00:00
|
|
|
struct AQSAudioVoice : IAudioVoice
|
2016-01-29 01:17:19 +00:00
|
|
|
{
|
|
|
|
ChannelMap m_map;
|
|
|
|
IAudioVoiceCallback* m_cb;
|
|
|
|
AudioQueueRef m_queue = nullptr;
|
|
|
|
AudioQueueBufferRef m_buffers[3];
|
2016-03-08 21:18:38 +00:00
|
|
|
size_t m_bufferFrames = 1024;
|
2016-01-29 01:17:19 +00:00
|
|
|
size_t m_frameSize;
|
|
|
|
|
|
|
|
const ChannelMap& channelMap() const {return m_map;}
|
|
|
|
|
|
|
|
AudioQueueBufferRef m_callbackBuf = nullptr;
|
|
|
|
unsigned m_primeBuf;
|
|
|
|
static void Callback(void* inUserData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer)
|
|
|
|
{
|
2016-01-30 01:45:40 +00:00
|
|
|
AQSAudioVoice* voice = static_cast<AQSAudioVoice*>(inUserData);
|
2016-01-29 01:17:19 +00:00
|
|
|
voice->m_callbackBuf = inBuffer;
|
2016-03-08 21:18:38 +00:00
|
|
|
voice->m_cb->needsNextBuffer(*voice, voice->m_bufferFrames);
|
2016-01-29 01:17:19 +00:00
|
|
|
voice->m_callbackBuf = nullptr;
|
|
|
|
}
|
|
|
|
|
2016-01-30 01:45:40 +00:00
|
|
|
AQSAudioVoice(AudioChannelSet set, unsigned sampleRate, IAudioVoiceCallback* cb)
|
2016-01-29 01:17:19 +00:00
|
|
|
: m_cb(cb)
|
|
|
|
{
|
|
|
|
unsigned chCount = ChannelCount(set);
|
|
|
|
|
|
|
|
AudioStreamBasicDescription desc = {};
|
|
|
|
desc.mSampleRate = sampleRate;
|
|
|
|
desc.mFormatID = kAudioFormatLinearPCM;
|
|
|
|
desc.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger;
|
|
|
|
desc.mBytesPerPacket = chCount * 2;
|
|
|
|
desc.mFramesPerPacket = 1;
|
|
|
|
desc.mBytesPerFrame = chCount * 2;
|
|
|
|
desc.mChannelsPerFrame = chCount;
|
|
|
|
desc.mBitsPerChannel = 16;
|
|
|
|
|
|
|
|
OSStatus err;
|
2016-03-23 07:01:25 +00:00
|
|
|
while ((err = AudioQueueNewOutput(&desc, AudioQueueOutputCallback(Callback),
|
|
|
|
this, nullptr, nullptr, 0, &m_queue)))
|
2016-01-29 01:17:19 +00:00
|
|
|
{
|
|
|
|
if (set == AudioChannelSet::Stereo)
|
|
|
|
break;
|
|
|
|
set = AudioChannelSet(int(set) - 1);
|
|
|
|
chCount = ChannelCount(set);
|
|
|
|
desc.mBytesPerPacket = chCount * 2;
|
|
|
|
desc.mBytesPerFrame = chCount * 2;
|
|
|
|
desc.mChannelsPerFrame = chCount;
|
|
|
|
}
|
|
|
|
if (err)
|
|
|
|
{
|
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-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:
|
|
|
|
m_map.m_channelCount = layout.mNumberChannelDescriptions;
|
|
|
|
for (int i=0 ; i<layout.mNumberChannelDescriptions ; ++i)
|
|
|
|
{
|
|
|
|
AudioChannel ch = AQSChannelToBooChannel(layout.mChannelDescriptions[i].mChannelLabel);
|
|
|
|
m_map.m_channels[i] = ch;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kAudioChannelLayoutTag_UseChannelBitmap:
|
|
|
|
if ((layout.mChannelBitmap & kAudioChannelBit_Left) != 0)
|
|
|
|
m_map.m_channels[m_map.m_channelCount++] = AudioChannel::FrontLeft;
|
|
|
|
if ((layout.mChannelBitmap & kAudioChannelBit_Right) != 0)
|
|
|
|
m_map.m_channels[m_map.m_channelCount++] = AudioChannel::FrontRight;
|
|
|
|
if ((layout.mChannelBitmap & kAudioChannelBit_Center) != 0)
|
|
|
|
m_map.m_channels[m_map.m_channelCount++] = AudioChannel::FrontCenter;
|
|
|
|
if ((layout.mChannelBitmap & kAudioChannelBit_LFEScreen) != 0)
|
|
|
|
m_map.m_channels[m_map.m_channelCount++] = AudioChannel::LFE;
|
|
|
|
if ((layout.mChannelBitmap & kAudioChannelBit_LeftSurround) != 0)
|
|
|
|
m_map.m_channels[m_map.m_channelCount++] = AudioChannel::RearLeft;
|
|
|
|
if ((layout.mChannelBitmap & kAudioChannelBit_RightSurround) != 0)
|
|
|
|
m_map.m_channels[m_map.m_channelCount++] = AudioChannel::RearRight;
|
|
|
|
if ((layout.mChannelBitmap & kAudioChannelBit_LeftSurroundDirect) != 0)
|
|
|
|
m_map.m_channels[m_map.m_channelCount++] = AudioChannel::SideLeft;
|
|
|
|
if ((layout.mChannelBitmap & kAudioChannelBit_RightSurroundDirect) != 0)
|
|
|
|
m_map.m_channels[m_map.m_channelCount++] = AudioChannel::SideRight;
|
|
|
|
break;
|
|
|
|
case kAudioChannelLayoutTag_Stereo:
|
|
|
|
case kAudioChannelLayoutTag_StereoHeadphones:
|
|
|
|
m_map.m_channelCount = 2;
|
|
|
|
m_map.m_channels[0] = AudioChannel::FrontLeft;
|
|
|
|
m_map.m_channels[1] = AudioChannel::FrontRight;
|
|
|
|
break;
|
|
|
|
case kAudioChannelLayoutTag_Quadraphonic:
|
|
|
|
m_map.m_channelCount = 4;
|
|
|
|
m_map.m_channels[0] = AudioChannel::FrontLeft;
|
|
|
|
m_map.m_channels[1] = AudioChannel::FrontRight;
|
|
|
|
m_map.m_channels[2] = AudioChannel::RearLeft;
|
|
|
|
m_map.m_channels[3] = AudioChannel::RearRight;
|
|
|
|
break;
|
|
|
|
case kAudioChannelLayoutTag_Pentagonal:
|
|
|
|
m_map.m_channelCount = 5;
|
|
|
|
m_map.m_channels[0] = AudioChannel::FrontLeft;
|
|
|
|
m_map.m_channels[1] = AudioChannel::FrontRight;
|
|
|
|
m_map.m_channels[2] = AudioChannel::RearLeft;
|
|
|
|
m_map.m_channels[3] = AudioChannel::RearRight;
|
|
|
|
m_map.m_channels[4] = AudioChannel::FrontCenter;
|
|
|
|
break;
|
|
|
|
default:
|
2016-03-23 07:01:25 +00:00
|
|
|
Log.report(logvisor::Fatal,
|
|
|
|
"unknown channel layout %u; using stereo",
|
|
|
|
layout.mChannelLayoutTag);
|
2016-03-08 21:18:38 +00:00
|
|
|
m_map.m_channelCount = 2;
|
|
|
|
m_map.m_channels[0] = AudioChannel::FrontLeft;
|
|
|
|
m_map.m_channels[1] = AudioChannel::FrontRight;
|
|
|
|
break;
|
2016-01-29 01:17:19 +00:00
|
|
|
}
|
2016-03-08 21:18:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_map.m_channels[m_map.m_channelCount++] = AudioChannel::FrontLeft;
|
|
|
|
m_map.m_channels[m_map.m_channelCount++] = AudioChannel::FrontRight;
|
2016-01-29 01:17:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (m_map.m_channelCount < chCount)
|
|
|
|
m_map.m_channels[m_map.m_channelCount++] = AudioChannel::Unknown;
|
|
|
|
|
|
|
|
for (int i=0 ; i<3 ; ++i)
|
|
|
|
if (AudioQueueAllocateBuffer(m_queue, m_bufferFrames * chCount * 2, &m_buffers[i]))
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_frameSize = chCount * 2;
|
|
|
|
|
|
|
|
for (unsigned i=0 ; i<3 ; ++i)
|
|
|
|
{
|
|
|
|
m_primeBuf = i;
|
2016-03-08 21:18:38 +00:00
|
|
|
m_cb->needsNextBuffer(*this, m_bufferFrames);
|
2016-01-29 01:17:19 +00:00
|
|
|
}
|
|
|
|
AudioQueuePrime(m_queue, 0, nullptr);
|
|
|
|
}
|
|
|
|
|
2016-01-30 01:45:40 +00:00
|
|
|
~AQSAudioVoice()
|
2016-01-29 01:17:19 +00:00
|
|
|
{
|
|
|
|
AudioQueueDispose(m_queue, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void bufferSampleData(const int16_t* data, size_t frames)
|
|
|
|
{
|
|
|
|
if (m_callbackBuf)
|
|
|
|
{
|
2016-03-23 07:01:25 +00:00
|
|
|
m_callbackBuf->mAudioDataByteSize =
|
|
|
|
std::min(UInt32(frames * m_frameSize), m_callbackBuf->mAudioDataBytesCapacity);
|
2016-01-29 01:17:19 +00:00
|
|
|
memcpy(m_callbackBuf->mAudioData, data, m_callbackBuf->mAudioDataByteSize);
|
|
|
|
AudioQueueEnqueueBuffer(m_queue, m_callbackBuf, 0, nullptr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
AudioQueueBufferRef buf = m_buffers[m_primeBuf];
|
2016-03-23 07:01:25 +00:00
|
|
|
buf->mAudioDataByteSize =
|
|
|
|
std::min(UInt32(frames * m_frameSize), buf->mAudioDataBytesCapacity);
|
2016-01-29 01:17:19 +00:00
|
|
|
memcpy(buf->mAudioData, data, buf->mAudioDataByteSize);
|
|
|
|
AudioQueueEnqueueBuffer(m_queue, buf, 0, nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void start()
|
|
|
|
{
|
|
|
|
AudioQueueStart(m_queue, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void stop()
|
|
|
|
{
|
|
|
|
AudioQueueStop(m_queue, false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-03-24 00:01:57 +00:00
|
|
|
struct AQSAudioVoiceAllocator : IAudioVoiceEngine
|
2016-01-29 01:17:19 +00:00
|
|
|
{
|
2016-03-08 21:18:38 +00:00
|
|
|
static void DummyCallback(void* inUserData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer) {}
|
|
|
|
|
|
|
|
AudioChannelSet getAvailableSet()
|
|
|
|
{
|
|
|
|
const unsigned chCount = 8;
|
|
|
|
AudioStreamBasicDescription desc = {};
|
|
|
|
desc.mSampleRate = 32000;
|
|
|
|
desc.mFormatID = kAudioFormatLinearPCM;
|
|
|
|
desc.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger;
|
|
|
|
desc.mBytesPerPacket = chCount * 2;
|
|
|
|
desc.mFramesPerPacket = 1;
|
|
|
|
desc.mBytesPerFrame = chCount * 2;
|
|
|
|
desc.mChannelsPerFrame = chCount;
|
|
|
|
desc.mBitsPerChannel = 16;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-01-29 01:17:19 +00:00
|
|
|
std::unique_ptr<IAudioVoice> allocateNewVoice(AudioChannelSet layoutOut,
|
|
|
|
unsigned sampleRate,
|
|
|
|
IAudioVoiceCallback* cb)
|
|
|
|
{
|
2016-01-30 01:45:40 +00:00
|
|
|
AQSAudioVoice* newVoice = new AQSAudioVoice(layoutOut, sampleRate, cb);
|
2016-01-29 01:17:19 +00:00
|
|
|
std::unique_ptr<IAudioVoice> ret(newVoice);
|
|
|
|
if (!newVoice->m_queue)
|
|
|
|
return {};
|
|
|
|
return ret;
|
|
|
|
}
|
2016-03-08 21:18:38 +00:00
|
|
|
|
|
|
|
void pumpVoices() {}
|
2016-01-29 01:17:19 +00:00
|
|
|
};
|
|
|
|
|
2016-03-24 00:01:57 +00:00
|
|
|
std::unique_ptr<IAudioVoiceEngine> NewAudioVoiceAllocator()
|
2016-03-08 07:09:58 +00:00
|
|
|
{
|
|
|
|
return std::make_unique<AQSAudioVoiceAllocator>();
|
|
|
|
}
|
|
|
|
|
2016-01-29 01:17:19 +00:00
|
|
|
}
|