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-05-22 23:09:32 +00:00
|
|
|
#include <CoreMIDI/CoreMIDI.h>
|
|
|
|
#include <CoreAudio/HostTime.h>
|
2016-01-29 01:17:19 +00:00
|
|
|
|
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-05-22 23:09:32 +00:00
|
|
|
MIDIClientRef m_midiClient;
|
|
|
|
|
2016-03-24 02:50:36 +00:00
|
|
|
std::mutex m_engineMutex;
|
2016-05-22 23:09:32 +00:00
|
|
|
std::condition_variable m_engineEnterCv;
|
|
|
|
std::condition_variable m_engineLeaveCv;
|
|
|
|
bool m_cbWaiting = false;
|
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);
|
2016-05-22 23:09:32 +00:00
|
|
|
engine->m_cbWaiting = true;
|
|
|
|
engine->m_engineEnterCv.wait(lk);
|
|
|
|
engine->m_cbWaiting = false;
|
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-05-23 19:15:06 +00:00
|
|
|
|
2016-05-22 23:09:32 +00:00
|
|
|
engine->m_engineLeaveCv.notify_one();
|
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;
|
|
|
|
}
|
|
|
|
|
2016-05-22 23:09:32 +00:00
|
|
|
std::vector<std::pair<std::string, std::string>> enumerateMIDIDevices() const
|
|
|
|
{
|
|
|
|
std::vector<std::pair<std::string, std::string>> ret;
|
|
|
|
|
|
|
|
ItemCount numDevices = MIDIGetNumberOfDevices();
|
|
|
|
ret.reserve(numDevices);
|
|
|
|
for (ItemCount i=0 ; i<numDevices ; ++i)
|
|
|
|
{
|
|
|
|
MIDIDeviceRef dev = MIDIGetDevice(i);
|
|
|
|
if (!dev)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
CFStringRef idstr;
|
|
|
|
if (MIDIObjectGetStringProperty(dev, kMIDIPropertyDeviceID, &idstr))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
CFStringRef namestr;
|
|
|
|
if (MIDIObjectGetStringProperty(dev, kMIDIPropertyDisplayName, &namestr))
|
|
|
|
{
|
|
|
|
CFRelease(idstr);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret.push_back(std::make_pair(std::string(CFStringGetCStringPtr(idstr, kCFStringEncodingUTF8)),
|
|
|
|
std::string(CFStringGetCStringPtr(namestr, kCFStringEncodingUTF8))));
|
|
|
|
|
|
|
|
CFRelease(idstr);
|
|
|
|
CFRelease(namestr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static MIDIDeviceRef LookupMIDIDevice(const char* name)
|
|
|
|
{
|
|
|
|
ItemCount numDevices = MIDIGetNumberOfDevices();
|
|
|
|
for (ItemCount i=0 ; i<numDevices ; ++i)
|
|
|
|
{
|
|
|
|
MIDIDeviceRef dev = MIDIGetDevice(i);
|
|
|
|
if (!dev)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
CFStringRef idstr;
|
|
|
|
if (MIDIObjectGetStringProperty(dev, kMIDIPropertyDeviceID, &idstr))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!strcmp(CFStringGetCStringPtr(idstr, kCFStringEncodingUTF8), name))
|
|
|
|
{
|
|
|
|
CFRelease(idstr);
|
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
CFRelease(idstr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
static MIDIEndpointRef LookupMIDISource(const char* name)
|
|
|
|
{
|
|
|
|
MIDIDeviceRef dev = LookupMIDIDevice(name);
|
|
|
|
if (!dev)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
ItemCount numEnt = MIDIDeviceGetNumberOfEntities(dev);
|
|
|
|
for (ItemCount i=0 ; i<numEnt ; ++i)
|
|
|
|
{
|
|
|
|
MIDIEntityRef ent = MIDIDeviceGetEntity(dev, i);
|
|
|
|
if (ent)
|
|
|
|
{
|
|
|
|
ItemCount numSrc = MIDIEntityGetNumberOfSources(ent);
|
|
|
|
for (ItemCount s=0 ; s<numSrc ; ++s)
|
|
|
|
{
|
|
|
|
MIDIEndpointRef src = MIDIEntityGetSource(ent, s);
|
|
|
|
if (src)
|
|
|
|
return src;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
static MIDIEndpointRef LookupMIDIDest(const char* name)
|
|
|
|
{
|
|
|
|
MIDIDeviceRef dev = LookupMIDIDevice(name);
|
|
|
|
if (!dev)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
ItemCount numEnt = MIDIDeviceGetNumberOfEntities(dev);
|
|
|
|
for (ItemCount i=0 ; i<numEnt ; ++i)
|
|
|
|
{
|
|
|
|
MIDIEntityRef ent = MIDIDeviceGetEntity(dev, i);
|
|
|
|
if (ent)
|
|
|
|
{
|
|
|
|
ItemCount numDest = MIDIEntityGetNumberOfDestinations(ent);
|
|
|
|
for (ItemCount d=0 ; d<numDest ; ++d)
|
|
|
|
{
|
|
|
|
MIDIEndpointRef dst = MIDIEntityGetDestination(ent, d);
|
|
|
|
if (dst)
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
static void MIDIReceiveProc(const MIDIPacketList* pktlist,
|
|
|
|
IMIDIReceiver* readProcRefCon,
|
|
|
|
void*)
|
|
|
|
{
|
|
|
|
const MIDIPacket* packet = &pktlist->packet[0];
|
|
|
|
for (int i=0 ; i<pktlist->numPackets ; ++i)
|
|
|
|
{
|
|
|
|
std::vector<uint8_t> bytes(std::cbegin(packet->data), std::cbegin(packet->data) + packet->length);
|
|
|
|
readProcRefCon->m_receiver(std::move(bytes));
|
|
|
|
packet = MIDIPacketNext(packet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct MIDIIn : public IMIDIIn
|
|
|
|
{
|
|
|
|
MIDIEndpointRef m_midi = 0;
|
|
|
|
MIDIPortRef m_midiPort = 0;
|
|
|
|
|
|
|
|
MIDIIn(bool virt, ReceiveFunctor&& receiver)
|
|
|
|
: IMIDIIn(virt, std::move(receiver)) {}
|
|
|
|
|
|
|
|
~MIDIIn()
|
|
|
|
{
|
|
|
|
if (m_midi)
|
|
|
|
MIDIEndpointDispose(m_midi);
|
|
|
|
if (m_midiPort)
|
|
|
|
MIDIPortDispose(m_midiPort);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string description() const
|
|
|
|
{
|
|
|
|
CFStringRef namestr;
|
|
|
|
if (MIDIObjectGetStringProperty(m_midi, kMIDIPropertyDisplayName, &namestr))
|
|
|
|
return {};
|
|
|
|
|
|
|
|
std::string ret(CFStringGetCStringPtr(namestr, kCFStringEncodingUTF8));
|
|
|
|
CFRelease(namestr);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct MIDIOut : public IMIDIOut
|
|
|
|
{
|
|
|
|
MIDIEndpointRef m_midi = 0;
|
|
|
|
MIDIPortRef m_midiPort = 0;
|
|
|
|
|
|
|
|
MIDIOut(bool virt)
|
|
|
|
: IMIDIOut(virt) {}
|
|
|
|
|
|
|
|
~MIDIOut()
|
|
|
|
{
|
|
|
|
if (m_midi)
|
|
|
|
MIDIEndpointDispose(m_midi);
|
|
|
|
if (m_midiPort)
|
|
|
|
MIDIPortDispose(m_midiPort);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string description() const
|
|
|
|
{
|
|
|
|
CFStringRef namestr;
|
|
|
|
if (MIDIObjectGetStringProperty(m_midi, kMIDIPropertyDisplayName, &namestr))
|
|
|
|
return {};
|
|
|
|
|
|
|
|
std::string ret(CFStringGetCStringPtr(namestr, kCFStringEncodingUTF8));
|
|
|
|
CFRelease(namestr);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t send(const void* buf, size_t len) const
|
|
|
|
{
|
|
|
|
union
|
|
|
|
{
|
|
|
|
MIDIPacketList head;
|
|
|
|
Byte storage[512];
|
|
|
|
} list;
|
|
|
|
MIDIPacket* curPacket = MIDIPacketListInit(&list.head);
|
|
|
|
if (MIDIPacketListAdd(&list.head, sizeof(list), curPacket, AudioGetCurrentHostTime(),
|
|
|
|
len, reinterpret_cast<const Byte*>(buf)))
|
|
|
|
{
|
|
|
|
if (m_midiPort)
|
|
|
|
MIDISend(m_midiPort, m_midi, &list.head);
|
|
|
|
else
|
|
|
|
MIDIReceived(m_midi, &list.head);
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct MIDIInOut : public IMIDIInOut
|
|
|
|
{
|
|
|
|
MIDIEndpointRef m_midiIn = 0;
|
|
|
|
MIDIPortRef m_midiPortIn = 0;
|
|
|
|
MIDIEndpointRef m_midiOut = 0;
|
|
|
|
MIDIPortRef m_midiPortOut = 0;
|
|
|
|
|
|
|
|
MIDIInOut(bool virt, ReceiveFunctor&& receiver)
|
|
|
|
: IMIDIInOut(virt, std::move(receiver)) {}
|
|
|
|
|
|
|
|
~MIDIInOut()
|
|
|
|
{
|
|
|
|
if (m_midiIn)
|
|
|
|
MIDIEndpointDispose(m_midiIn);
|
|
|
|
if (m_midiPortIn)
|
|
|
|
MIDIPortDispose(m_midiPortIn);
|
|
|
|
if (m_midiOut)
|
|
|
|
MIDIEndpointDispose(m_midiOut);
|
|
|
|
if (m_midiPortOut)
|
|
|
|
MIDIPortDispose(m_midiPortOut);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string description() const
|
|
|
|
{
|
|
|
|
CFStringRef namestr;
|
|
|
|
if (MIDIObjectGetStringProperty(m_midiIn, kMIDIPropertyDisplayName, &namestr))
|
|
|
|
return {};
|
|
|
|
|
|
|
|
std::string ret(CFStringGetCStringPtr(namestr, kCFStringEncodingUTF8));
|
|
|
|
CFRelease(namestr);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t send(const void* buf, size_t len) const
|
|
|
|
{
|
|
|
|
union
|
|
|
|
{
|
|
|
|
MIDIPacketList head;
|
|
|
|
Byte storage[512];
|
|
|
|
} list;
|
|
|
|
MIDIPacket* curPacket = MIDIPacketListInit(&list.head);
|
|
|
|
if (MIDIPacketListAdd(&list.head, sizeof(list), curPacket, AudioGetCurrentHostTime(),
|
|
|
|
len, reinterpret_cast<const Byte*>(buf)))
|
|
|
|
{
|
|
|
|
if (m_midiPortOut)
|
|
|
|
MIDISend(m_midiPortOut, m_midiOut, &list.head);
|
|
|
|
else
|
|
|
|
MIDIReceived(m_midiOut, &list.head);
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
unsigned m_midiInCounter = 0;
|
|
|
|
unsigned m_midiOutCounter = 0;
|
|
|
|
|
|
|
|
std::unique_ptr<IMIDIIn> newVirtualMIDIIn(ReceiveFunctor&& receiver)
|
|
|
|
{
|
|
|
|
std::unique_ptr<IMIDIIn> ret = std::make_unique<MIDIIn>(true, std::move(receiver));
|
|
|
|
if (!ret)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
char name[256];
|
2016-05-23 19:15:06 +00:00
|
|
|
snprintf(name, 256, "Boo MIDI Virtual In %u", m_midiInCounter++);
|
2016-05-22 23:09:32 +00:00
|
|
|
CFStringRef midiName = CFStringCreateWithCStringNoCopy(nullptr, name, kCFStringEncodingUTF8, kCFAllocatorNull);
|
|
|
|
OSStatus stat;
|
|
|
|
if ((stat = MIDIDestinationCreate(m_midiClient, midiName, MIDIReadProc(MIDIReceiveProc),
|
|
|
|
static_cast<IMIDIReceiver*>(ret.get()),
|
|
|
|
&static_cast<MIDIIn&>(*ret).m_midi)))
|
|
|
|
ret.reset();
|
|
|
|
CFRelease(midiName);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<IMIDIOut> newVirtualMIDIOut()
|
|
|
|
{
|
|
|
|
std::unique_ptr<IMIDIOut> ret = std::make_unique<MIDIOut>(true);
|
|
|
|
if (!ret)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
char name[256];
|
2016-05-23 19:15:06 +00:00
|
|
|
snprintf(name, 256, "Boo MIDI Virtual Out %u", m_midiOutCounter++);
|
2016-05-22 23:09:32 +00:00
|
|
|
CFStringRef midiName = CFStringCreateWithCStringNoCopy(nullptr, name, kCFStringEncodingUTF8, kCFAllocatorNull);
|
|
|
|
if (MIDISourceCreate(m_midiClient, midiName, &static_cast<MIDIOut&>(*ret).m_midi))
|
|
|
|
ret.reset();
|
|
|
|
CFRelease(midiName);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<IMIDIInOut> newVirtualMIDIInOut(ReceiveFunctor&& receiver)
|
|
|
|
{
|
|
|
|
std::unique_ptr<IMIDIInOut> ret = std::make_unique<MIDIInOut>(true, std::move(receiver));
|
|
|
|
if (!ret)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
char name[256];
|
2016-05-23 19:15:06 +00:00
|
|
|
snprintf(name, 256, "Boo MIDI Virtual In %u", m_midiInCounter++);
|
2016-05-22 23:09:32 +00:00
|
|
|
CFStringRef midiName = CFStringCreateWithCStringNoCopy(nullptr, name, kCFStringEncodingUTF8, kCFAllocatorNull);
|
|
|
|
if (MIDIDestinationCreate(m_midiClient, midiName, MIDIReadProc(MIDIReceiveProc),
|
|
|
|
static_cast<IMIDIReceiver*>(ret.get()),
|
|
|
|
&static_cast<MIDIInOut&>(*ret).m_midiIn))
|
|
|
|
ret.reset();
|
|
|
|
CFRelease(midiName);
|
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
return {};
|
|
|
|
|
2016-05-23 19:15:06 +00:00
|
|
|
snprintf(name, 256, "Boo MIDI Virtual Out %u", m_midiOutCounter++);
|
2016-05-22 23:09:32 +00:00
|
|
|
midiName = CFStringCreateWithCStringNoCopy(nullptr, name, kCFStringEncodingUTF8, kCFAllocatorNull);
|
|
|
|
if (MIDISourceCreate(m_midiClient, midiName, &static_cast<MIDIInOut&>(*ret).m_midiOut))
|
|
|
|
ret.reset();
|
|
|
|
CFRelease(midiName);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<IMIDIIn> newRealMIDIIn(const char* name, ReceiveFunctor&& receiver)
|
|
|
|
{
|
|
|
|
MIDIEndpointRef src = LookupMIDISource(name);
|
|
|
|
if (!src)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
std::unique_ptr<IMIDIIn> ret = std::make_unique<MIDIIn>(false, std::move(receiver));
|
|
|
|
if (!ret)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
char mname[256];
|
2016-05-23 19:15:06 +00:00
|
|
|
snprintf(mname, 256, "Boo MIDI Real In %u", m_midiInCounter++);
|
2016-05-22 23:09:32 +00:00
|
|
|
CFStringRef midiName = CFStringCreateWithCStringNoCopy(nullptr, mname, kCFStringEncodingUTF8, kCFAllocatorNull);
|
|
|
|
if (MIDIInputPortCreate(m_midiClient, midiName, MIDIReadProc(MIDIReceiveProc),
|
|
|
|
static_cast<IMIDIReceiver*>(ret.get()),
|
|
|
|
&static_cast<MIDIIn&>(*ret).m_midiPort))
|
|
|
|
ret.reset();
|
|
|
|
else
|
|
|
|
MIDIPortConnectSource(static_cast<MIDIIn&>(*ret).m_midiPort, src, nullptr);
|
|
|
|
CFRelease(midiName);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<IMIDIOut> newRealMIDIOut(const char* name)
|
|
|
|
{
|
|
|
|
MIDIEndpointRef dst = LookupMIDIDest(name);
|
|
|
|
if (!dst)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
std::unique_ptr<IMIDIOut> ret = std::make_unique<MIDIOut>(false);
|
|
|
|
if (!ret)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
char mname[256];
|
2016-05-23 19:15:06 +00:00
|
|
|
snprintf(mname, 256, "Boo MIDI Real Out %u", m_midiOutCounter++);
|
2016-05-22 23:09:32 +00:00
|
|
|
CFStringRef midiName = CFStringCreateWithCStringNoCopy(nullptr, mname, kCFStringEncodingUTF8, kCFAllocatorNull);
|
|
|
|
if (MIDIOutputPortCreate(m_midiClient, midiName, &static_cast<MIDIOut&>(*ret).m_midiPort))
|
|
|
|
ret.reset();
|
|
|
|
else
|
|
|
|
static_cast<MIDIOut&>(*ret).m_midi = dst;
|
|
|
|
CFRelease(midiName);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<IMIDIInOut> newRealMIDIInOut(const char* name, ReceiveFunctor&& receiver)
|
|
|
|
{
|
|
|
|
MIDIEndpointRef src = LookupMIDISource(name);
|
|
|
|
if (!src)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
MIDIEndpointRef dst = LookupMIDIDest(name);
|
|
|
|
if (!dst)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
std::unique_ptr<IMIDIInOut> ret = std::make_unique<MIDIInOut>(false, std::move(receiver));
|
|
|
|
if (!ret)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
char mname[256];
|
2016-05-23 19:15:06 +00:00
|
|
|
snprintf(mname, 256, "Boo MIDI Real In %u", m_midiInCounter++);
|
2016-05-22 23:09:32 +00:00
|
|
|
CFStringRef midiName = CFStringCreateWithCStringNoCopy(nullptr, mname, kCFStringEncodingUTF8, kCFAllocatorNull);
|
|
|
|
if (MIDIInputPortCreate(m_midiClient, midiName, MIDIReadProc(MIDIReceiveProc),
|
|
|
|
static_cast<IMIDIReceiver*>(ret.get()),
|
|
|
|
&static_cast<MIDIInOut&>(*ret).m_midiPortIn))
|
|
|
|
ret.reset();
|
|
|
|
else
|
|
|
|
MIDIPortConnectSource(static_cast<MIDIInOut&>(*ret).m_midiPortIn, src, nullptr);
|
|
|
|
CFRelease(midiName);
|
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
return {};
|
|
|
|
|
2016-05-23 19:15:06 +00:00
|
|
|
snprintf(mname, 256, "Boo MIDI Real Out %u", m_midiOutCounter++);
|
2016-05-22 23:09:32 +00:00
|
|
|
midiName = CFStringCreateWithCStringNoCopy(nullptr, mname, kCFStringEncodingUTF8, kCFAllocatorNull);
|
|
|
|
if (MIDIOutputPortCreate(m_midiClient, midiName, &static_cast<MIDIInOut&>(*ret).m_midiPortOut))
|
|
|
|
ret.reset();
|
|
|
|
else
|
|
|
|
static_cast<MIDIInOut&>(*ret).m_midiOut = dst;
|
|
|
|
CFRelease(midiName);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-03-24 02:50:36 +00:00
|
|
|
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;
|
2016-05-22 23:09:32 +00:00
|
|
|
m_5msFrames = 96000 * 5 / 1000;
|
2016-03-24 02:50:36 +00:00
|
|
|
|
|
|
|
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-05-22 23:09:32 +00:00
|
|
|
|
|
|
|
/* Also create shared MIDI client */
|
|
|
|
MIDIClientCreate(CFSTR("Boo MIDI"), nullptr, nullptr, &m_midiClient);
|
2016-01-29 01:17:19 +00:00
|
|
|
}
|
|
|
|
|
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-05-22 23:09:32 +00:00
|
|
|
MIDIClientDispose(m_midiClient);
|
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);
|
2016-05-22 23:09:32 +00:00
|
|
|
if (m_cbWaiting)
|
|
|
|
{
|
|
|
|
m_engineEnterCv.notify_one();
|
|
|
|
m_engineLeaveCv.wait(lk);
|
|
|
|
}
|
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
|
|
|
}
|