| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
#ifndef __coreaudio_dsp__ |
| 37 |
#define __coreaudio_dsp__ |
| 38 |
|
| 39 |
#include <math.h> |
| 40 |
#include <stdlib.h> |
| 41 |
#include <stdio.h> |
| 42 |
|
| 43 |
#include <vector> |
| 44 |
#include <iostream> |
| 45 |
#include <libgen.h> |
| 46 |
|
| 47 |
#include <AudioToolbox/AudioConverter.h> |
| 48 |
#include <CoreAudio/CoreAudio.h> |
| 49 |
#include <AudioUnit/AudioUnit.h> |
| 50 |
#include <CoreServices/CoreServices.h> |
| 51 |
|
| 52 |
#include "faust/audio/audio.h" |
| 53 |
#include "faust/audio/dsp.h" |
| 54 |
|
| 55 |
using namespace std; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
#define OPEN_ERR -1 |
| 66 |
#define CLOSE_ERR -1 |
| 67 |
#define NO_ERR 0 |
| 68 |
|
| 69 |
#define WAIT_COUNTER 60 |
| 70 |
|
| 71 |
typedef UInt8 CAAudioHardwareDeviceSectionID; |
| 72 |
#define kAudioDeviceSectionInput ((CAAudioHardwareDeviceSectionID)0x01) |
| 73 |
#define kAudioDeviceSectionOutput ((CAAudioHardwareDeviceSectionID)0x00) |
| 74 |
#define kAudioDeviceSectionGlobal ((CAAudioHardwareDeviceSectionID)0x00) |
| 75 |
#define kAudioDeviceSectionWildcard ((CAAudioHardwareDeviceSectionID)0xFF) |
| 76 |
|
| 77 |
class TCoreAudioRenderer |
| 78 |
{ |
| 79 |
private: |
| 80 |
|
| 81 |
int fDevNumInChans; |
| 82 |
int fDevNumOutChans; |
| 83 |
|
| 84 |
float** fInChannel; |
| 85 |
float** fOutChannel; |
| 86 |
|
| 87 |
dsp* fDSP; |
| 88 |
|
| 89 |
AudioBufferList* fInputData; |
| 90 |
AudioDeviceID fDeviceID; |
| 91 |
AudioUnit fAUHAL; |
| 92 |
AudioObjectID fPluginID; |
| 93 |
bool fState; |
| 94 |
|
| 95 |
OSStatus GetDefaultDevice(int inChan, int outChan, int samplerate, AudioDeviceID* id); |
| 96 |
|
| 97 |
OSStatus CreateAggregateDevice(AudioDeviceID captureDeviceID, AudioDeviceID playbackDeviceID, int samplerate, AudioDeviceID* outAggregateDevice); |
| 98 |
OSStatus CreateAggregateDeviceAux(vector<AudioDeviceID> captureDeviceID, vector<AudioDeviceID> playbackDeviceID, int samplerate, AudioDeviceID* outAggregateDevice); |
| 99 |
OSStatus DestroyAggregateDevice(); |
| 100 |
|
| 101 |
OSStatus GetDeviceNameFromID(AudioDeviceID id, char* name); |
| 102 |
|
| 103 |
int SetupSampleRateAux(AudioDeviceID inDevice, int samplerate); |
| 104 |
|
| 105 |
static OSStatus Render(void *inRefCon, |
| 106 |
AudioUnitRenderActionFlags *ioActionFlags, |
| 107 |
const AudioTimeStamp *inTimeStamp, |
| 108 |
UInt32 inBusNumber, |
| 109 |
UInt32 inNumberFrames, |
| 110 |
AudioBufferList *ioData); |
| 111 |
|
| 112 |
static OSStatus SRNotificationCallback(AudioDeviceID inDevice, |
| 113 |
UInt32 inChannel, |
| 114 |
Boolean isInput, |
| 115 |
AudioDevicePropertyID inPropertyID, |
| 116 |
void* inClientData); |
| 117 |
|
| 118 |
void compute(int inNumberFrames) |
| 119 |
{ |
| 120 |
fDSP->compute(inNumberFrames, fInChannel, fOutChannel); |
| 121 |
} |
| 122 |
|
| 123 |
public: |
| 124 |
|
| 125 |
TCoreAudioRenderer() |
| 126 |
:fInputData(0),fDeviceID(0),fAUHAL(0),fPluginID(0),fState(false),fDevNumInChans(0),fDevNumOutChans(0),fDSP(0) |
| 127 |
{} |
| 128 |
virtual ~TCoreAudioRenderer() |
| 129 |
{} |
| 130 |
|
| 131 |
long OpenDefault(dsp* dsp, long inChan, long outChan, long bufferSize, long sampleRate); |
| 132 |
long Close(); |
| 133 |
|
| 134 |
long Start(); |
| 135 |
long Stop(); |
| 136 |
|
| 137 |
}; |
| 138 |
|
| 139 |
typedef TCoreAudioRenderer * TCoreAudioRendererPtr; |
| 140 |
|
| 141 |
static void PrintStreamDesc(AudioStreamBasicDescription *inDesc) |
| 142 |
{ |
| 143 |
cout << "- - - - - - - - - - - - - - - - - - - -" << endl; |
| 144 |
cout << " Sample Rate: " << inDesc->mSampleRate << endl; |
| 145 |
cout << " Format ID:%.*s\n" << sizeof(inDesc->mFormatID) << (char*)&inDesc->mFormatID << endl; |
| 146 |
cout << " Format Flags " << inDesc->mFormatFlags << endl; |
| 147 |
cout << " Bytes per Packet: " << inDesc->mBytesPerPacket << endl; |
| 148 |
cout << " Frames per Packet: " << inDesc->mFramesPerPacket << endl; |
| 149 |
cout << " Bytes per Frame: " << inDesc->mBytesPerFrame << endl; |
| 150 |
cout << " Channels per Frame: "<< inDesc->mChannelsPerFrame << endl; |
| 151 |
cout << " Bits per Channel: " << inDesc->mBitsPerChannel << endl; |
| 152 |
cout << "- - - - - - - - - - - - - - - - - - - -" << endl; |
| 153 |
} |
| 154 |
|
| 155 |
static void printError(OSStatus err) |
| 156 |
{ |
| 157 |
switch (err) { |
| 158 |
case kAudioHardwareNoError: |
| 159 |
printf("error code : kAudioHardwareNoError\n"); |
| 160 |
break; |
| 161 |
case kAudioConverterErr_FormatNotSupported: |
| 162 |
printf("error code : kAudioConverterErr_FormatNotSupported\n"); |
| 163 |
break; |
| 164 |
case kAudioConverterErr_OperationNotSupported: |
| 165 |
printf("error code : kAudioConverterErr_OperationNotSupported\n"); |
| 166 |
break; |
| 167 |
case kAudioConverterErr_PropertyNotSupported: |
| 168 |
printf("error code : kAudioConverterErr_PropertyNotSupported\n"); |
| 169 |
break; |
| 170 |
case kAudioConverterErr_InvalidInputSize: |
| 171 |
printf("error code : kAudioConverterErr_InvalidInputSize\n"); |
| 172 |
break; |
| 173 |
case kAudioConverterErr_InvalidOutputSize: |
| 174 |
printf("error code : kAudioConverterErr_InvalidOutputSize\n"); |
| 175 |
break; |
| 176 |
case kAudioConverterErr_UnspecifiedError: |
| 177 |
printf("error code : kAudioConverterErr_UnspecifiedError\n"); |
| 178 |
break; |
| 179 |
case kAudioConverterErr_BadPropertySizeError: |
| 180 |
printf("error code : kAudioConverterErr_BadPropertySizeError\n"); |
| 181 |
break; |
| 182 |
case kAudioConverterErr_RequiresPacketDescriptionsError: |
| 183 |
printf("error code : kAudioConverterErr_RequiresPacketDescriptionsError\n"); |
| 184 |
break; |
| 185 |
case kAudioConverterErr_InputSampleRateOutOfRange: |
| 186 |
printf("error code : kAudioConverterErr_InputSampleRateOutOfRange\n"); |
| 187 |
break; |
| 188 |
case kAudioConverterErr_OutputSampleRateOutOfRange: |
| 189 |
printf("error code : kAudioConverterErr_OutputSampleRateOutOfRange\n"); |
| 190 |
break; |
| 191 |
case kAudioHardwareNotRunningError: |
| 192 |
printf("error code : kAudioHardwareNotRunningError\n"); |
| 193 |
break; |
| 194 |
case kAudioHardwareUnknownPropertyError: |
| 195 |
printf("error code : kAudioHardwareUnknownPropertyError\n"); |
| 196 |
break; |
| 197 |
case kAudioHardwareIllegalOperationError: |
| 198 |
printf("error code : kAudioHardwareIllegalOperationError\n"); |
| 199 |
break; |
| 200 |
case kAudioHardwareBadDeviceError: |
| 201 |
printf("error code : kAudioHardwareBadDeviceError\n"); |
| 202 |
break; |
| 203 |
case kAudioHardwareBadStreamError: |
| 204 |
printf("error code : kAudioHardwareBadStreamError\n"); |
| 205 |
break; |
| 206 |
case kAudioDeviceUnsupportedFormatError: |
| 207 |
printf("error code : kAudioDeviceUnsupportedFormatError\n"); |
| 208 |
break; |
| 209 |
case kAudioDevicePermissionsError: |
| 210 |
printf("error code : kAudioDevicePermissionsError\n"); |
| 211 |
break; |
| 212 |
default: |
| 213 |
printf("error code : unknown\n"); |
| 214 |
break; |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
OSStatus TCoreAudioRenderer::Render(void *inRefCon, |
| 219 |
AudioUnitRenderActionFlags *ioActionFlags, |
| 220 |
const AudioTimeStamp *inTimeStamp, |
| 221 |
UInt32, |
| 222 |
UInt32 inNumberFrames, |
| 223 |
AudioBufferList *ioData) |
| 224 |
{ |
| 225 |
TCoreAudioRendererPtr renderer = (TCoreAudioRendererPtr)inRefCon; |
| 226 |
AudioUnitRender(renderer->fAUHAL, ioActionFlags, inTimeStamp, 1, inNumberFrames, renderer->fInputData); |
| 227 |
for (int i = 0; i < renderer->fDevNumInChans; i++) { |
| 228 |
renderer->fInChannel[i] = (float*)renderer->fInputData->mBuffers[i].mData; |
| 229 |
} |
| 230 |
for (int i = 0; i < renderer->fDevNumOutChans; i++) { |
| 231 |
renderer->fOutChannel[i] = (float*)ioData->mBuffers[i].mData; |
| 232 |
} |
| 233 |
renderer->compute((int)inNumberFrames); |
| 234 |
return 0; |
| 235 |
} |
| 236 |
|
| 237 |
static CFStringRef GetDeviceName(AudioDeviceID id) |
| 238 |
{ |
| 239 |
UInt32 size = sizeof(CFStringRef); |
| 240 |
CFStringRef UIname; |
| 241 |
OSStatus err = AudioDeviceGetProperty(id, 0, false, kAudioDevicePropertyDeviceUID, &size, &UIname); |
| 242 |
return (err == noErr) ? UIname : NULL; |
| 243 |
} |
| 244 |
|
| 245 |
OSStatus TCoreAudioRenderer::GetDeviceNameFromID(AudioDeviceID id, char* name) |
| 246 |
{ |
| 247 |
UInt32 size = 256; |
| 248 |
return AudioDeviceGetProperty(id, 0, false, kAudioDevicePropertyDeviceName, &size, name); |
| 249 |
} |
| 250 |
|
| 251 |
OSStatus TCoreAudioRenderer::GetDefaultDevice(int inChan, int outChan, int samplerate, AudioDeviceID* id) |
| 252 |
{ |
| 253 |
UInt32 theSize = sizeof(UInt32); |
| 254 |
AudioDeviceID inDefault; |
| 255 |
AudioDeviceID outDefault; |
| 256 |
OSStatus res; |
| 257 |
|
| 258 |
if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice, |
| 259 |
&theSize, &inDefault)) != noErr) |
| 260 |
return res; |
| 261 |
|
| 262 |
if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, |
| 263 |
&theSize, &outDefault)) != noErr) |
| 264 |
return res; |
| 265 |
|
| 266 |
|
| 267 |
if (inChan > 0 && outChan > 0) { |
| 268 |
|
| 269 |
if (inDefault == outDefault) { |
| 270 |
*id = inDefault; |
| 271 |
return noErr; |
| 272 |
} else { |
| 273 |
printf("GetDefaultDevice : input = %uld and output = %uld are not the same, create aggregate device...\n", inDefault, outDefault); |
| 274 |
if (CreateAggregateDevice(inDefault, outDefault, samplerate, id) != noErr) |
| 275 |
return kAudioHardwareBadDeviceError; |
| 276 |
} |
| 277 |
} else if (inChan > 0) { |
| 278 |
*id = inDefault; |
| 279 |
return noErr; |
| 280 |
} else if (outChan > 0) { |
| 281 |
*id = outDefault; |
| 282 |
return noErr; |
| 283 |
} else { |
| 284 |
return kAudioHardwareBadDeviceError; |
| 285 |
} |
| 286 |
|
| 287 |
return noErr; |
| 288 |
} |
| 289 |
|
| 290 |
OSStatus TCoreAudioRenderer::CreateAggregateDevice(AudioDeviceID captureDeviceID, AudioDeviceID playbackDeviceID, int samplerate, AudioDeviceID* outAggregateDevice) |
| 291 |
{ |
| 292 |
OSStatus err = noErr; |
| 293 |
AudioObjectID sub_device[32]; |
| 294 |
UInt32 outSize = sizeof(sub_device); |
| 295 |
|
| 296 |
err = AudioDeviceGetProperty(captureDeviceID, 0, kAudioDeviceSectionGlobal, kAudioAggregateDevicePropertyActiveSubDeviceList, &outSize, sub_device); |
| 297 |
vector<AudioDeviceID> captureDeviceIDArray; |
| 298 |
|
| 299 |
if (err != noErr) { |
| 300 |
printf("Input device does not have subdevices\n"); |
| 301 |
captureDeviceIDArray.push_back(captureDeviceID); |
| 302 |
} else { |
| 303 |
int num_devices = outSize / sizeof(AudioObjectID); |
| 304 |
printf("Input device has %d subdevices\n", num_devices); |
| 305 |
for (int i = 0; i < num_devices; i++) { |
| 306 |
captureDeviceIDArray.push_back(sub_device[i]); |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
err = AudioDeviceGetProperty(playbackDeviceID, 0, kAudioDeviceSectionGlobal, kAudioAggregateDevicePropertyActiveSubDeviceList, &outSize, sub_device); |
| 311 |
vector<AudioDeviceID> playbackDeviceIDArray; |
| 312 |
|
| 313 |
if (err != noErr) { |
| 314 |
printf("Output device does not have subdevices\n"); |
| 315 |
playbackDeviceIDArray.push_back(playbackDeviceID); |
| 316 |
} else { |
| 317 |
int num_devices = outSize / sizeof(AudioObjectID); |
| 318 |
printf("Output device has %d subdevices\n", num_devices); |
| 319 |
for (int i = 0; i < num_devices; i++) { |
| 320 |
playbackDeviceIDArray.push_back(sub_device[i]); |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
return CreateAggregateDeviceAux(captureDeviceIDArray, playbackDeviceIDArray, samplerate, outAggregateDevice); |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
OSStatus TCoreAudioRenderer::SRNotificationCallback(AudioDeviceID inDevice, |
| 329 |
UInt32 , |
| 330 |
Boolean , |
| 331 |
AudioDevicePropertyID inPropertyID, |
| 332 |
void* inClientData) |
| 333 |
{ |
| 334 |
TCoreAudioRenderer* driver = (TCoreAudioRenderer*)inClientData; |
| 335 |
|
| 336 |
switch (inPropertyID) { |
| 337 |
|
| 338 |
case kAudioDevicePropertyNominalSampleRate: { |
| 339 |
printf("JackCoreAudioDriver::SRNotificationCallback kAudioDevicePropertyNominalSampleRate\n"); |
| 340 |
driver->fState = true; |
| 341 |
|
| 342 |
Float64 sampleRate; |
| 343 |
UInt32 outSize = sizeof(Float64); |
| 344 |
OSStatus err = AudioDeviceGetProperty(inDevice, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyNominalSampleRate, &outSize, &sampleRate); |
| 345 |
if (err != noErr) { |
| 346 |
printf("Cannot get current sample rate\n"); |
| 347 |
printError(err); |
| 348 |
} else { |
| 349 |
printf("SRNotificationCallback : checked sample rate = %f\n", sampleRate); |
| 350 |
} |
| 351 |
break; |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
return noErr; |
| 356 |
} |
| 357 |
|
| 358 |
int TCoreAudioRenderer::SetupSampleRateAux(AudioDeviceID inDevice, int samplerate) |
| 359 |
{ |
| 360 |
OSStatus err = noErr; |
| 361 |
UInt32 outSize; |
| 362 |
Float64 sampleRate; |
| 363 |
|
| 364 |
|
| 365 |
outSize = sizeof(Float64); |
| 366 |
err = AudioDeviceGetProperty(inDevice, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyNominalSampleRate, &outSize, &sampleRate); |
| 367 |
if (err != noErr) { |
| 368 |
printf("Cannot get current sample rate\n"); |
| 369 |
printError(err); |
| 370 |
return -1; |
| 371 |
} else { |
| 372 |
printf("Current sample rate = %f\n", sampleRate); |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
if (samplerate != (int)sampleRate) { |
| 377 |
sampleRate = (Float64)samplerate; |
| 378 |
|
| 379 |
|
| 380 |
err = AudioDeviceAddPropertyListener(inDevice, 0, true, kAudioDevicePropertyNominalSampleRate, SRNotificationCallback, this); |
| 381 |
if (err != noErr) { |
| 382 |
printf("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyNominalSampleRate\n"); |
| 383 |
printError(err); |
| 384 |
return -1; |
| 385 |
} |
| 386 |
err = AudioDeviceSetProperty(inDevice, NULL, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyNominalSampleRate, outSize, &sampleRate); |
| 387 |
if (err != noErr) { |
| 388 |
printf("Cannot set sample rate = %d\n", samplerate); |
| 389 |
printError(err); |
| 390 |
return -1; |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
int count = 0; |
| 395 |
while (!fState && count++ < WAIT_COUNTER) { |
| 396 |
usleep(100000); |
| 397 |
printf("Wait count = %d\n", count); |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
outSize = sizeof(Float64); |
| 402 |
err = AudioDeviceGetProperty(inDevice, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyNominalSampleRate, &outSize, &sampleRate); |
| 403 |
if (err != noErr) { |
| 404 |
printf("Cannot get current sample rate\n"); |
| 405 |
printError(err); |
| 406 |
} else { |
| 407 |
printf("Checked sample rate = %f\n", sampleRate); |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
AudioDeviceRemovePropertyListener(inDevice, 0, true, kAudioDevicePropertyNominalSampleRate, SRNotificationCallback); |
| 412 |
} |
| 413 |
|
| 414 |
return 0; |
| 415 |
} |
| 416 |
|
| 417 |
OSStatus TCoreAudioRenderer::CreateAggregateDeviceAux(vector<AudioDeviceID> captureDeviceID, vector<AudioDeviceID> playbackDeviceID, int samplerate, AudioDeviceID* outAggregateDevice) |
| 418 |
{ |
| 419 |
OSStatus osErr = noErr; |
| 420 |
UInt32 outSize; |
| 421 |
Boolean outWritable; |
| 422 |
|
| 423 |
bool fClockDriftCompensate = true; |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
AudioObjectPropertyAddress theAddressOwned = { kAudioObjectPropertyOwnedObjects, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster }; |
| 428 |
AudioObjectPropertyAddress theAddressDrift = { kAudioSubDevicePropertyDriftCompensation, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster }; |
| 429 |
UInt32 theQualifierDataSize = sizeof(AudioObjectID); |
| 430 |
AudioClassID inClass = kAudioSubDeviceClassID; |
| 431 |
void* theQualifierData = &inClass; |
| 432 |
UInt32 subDevicesNum = 0; |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
UInt32 keptclockdomain = 0; |
| 438 |
UInt32 clockdomain = 0; |
| 439 |
outSize = sizeof(UInt32); |
| 440 |
bool need_clock_drift_compensation = false; |
| 441 |
|
| 442 |
for (UInt32 i = 0; i < captureDeviceID.size(); i++) { |
| 443 |
if (SetupSampleRateAux(captureDeviceID[i], samplerate) < 0) { |
| 444 |
printf("TCoreAudioRenderer::CreateAggregateDevice : cannot set SR of input device\n"); |
| 445 |
} else { |
| 446 |
|
| 447 |
osErr = AudioDeviceGetProperty(captureDeviceID[i], 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyClockDomain, &outSize, &clockdomain); |
| 448 |
if (osErr != 0) { |
| 449 |
printf("TCoreAudioRenderer::CreateAggregateDevice : kAudioDevicePropertyClockDomain error\n"); |
| 450 |
printError(osErr); |
| 451 |
} else { |
| 452 |
keptclockdomain = (keptclockdomain == 0) ? clockdomain : keptclockdomain; |
| 453 |
printf("TCoreAudioRenderer::CreateAggregateDevice : input clockdomain = %d\n", clockdomain); |
| 454 |
if (clockdomain != 0 && clockdomain != keptclockdomain) { |
| 455 |
printf("TCoreAudioRenderer::CreateAggregateDevice : devices do not share the same clock!! clock drift compensation would be needed...\n"); |
| 456 |
need_clock_drift_compensation = true; |
| 457 |
} |
| 458 |
} |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
for (UInt32 i = 0; i < playbackDeviceID.size(); i++) { |
| 463 |
if (SetupSampleRateAux(playbackDeviceID[i], samplerate) < 0) { |
| 464 |
printf("TCoreAudioRenderer::CreateAggregateDevice : cannot set SR of output device\n"); |
| 465 |
} else { |
| 466 |
|
| 467 |
osErr = AudioDeviceGetProperty(playbackDeviceID[i], 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyClockDomain, &outSize, &clockdomain); |
| 468 |
if (osErr != 0) { |
| 469 |
printf("TCoreAudioRenderer::CreateAggregateDevice : kAudioDevicePropertyClockDomain error\n"); |
| 470 |
printError(osErr); |
| 471 |
} else { |
| 472 |
keptclockdomain = (keptclockdomain == 0) ? clockdomain : keptclockdomain; |
| 473 |
printf("TCoreAudioRenderer::CreateAggregateDevice : output clockdomain = %d", clockdomain); |
| 474 |
if (clockdomain != 0 && clockdomain != keptclockdomain) { |
| 475 |
printf("TCoreAudioRenderer::CreateAggregateDevice : devices do not share the same clock!! clock drift compensation would be needed...\n"); |
| 476 |
need_clock_drift_compensation = true; |
| 477 |
} |
| 478 |
} |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
|
| 483 |
if (keptclockdomain == 0) { |
| 484 |
need_clock_drift_compensation = true; |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
char device_name[256]; |
| 492 |
for (UInt32 i = 0; i < captureDeviceID.size(); i++) { |
| 493 |
GetDeviceNameFromID(captureDeviceID[i], device_name); |
| 494 |
printf("Separated input = '%s' \n", device_name); |
| 495 |
} |
| 496 |
|
| 497 |
for (UInt32 i = 0; i < playbackDeviceID.size(); i++) { |
| 498 |
GetDeviceNameFromID(playbackDeviceID[i], device_name); |
| 499 |
printf("Separated output = '%s' \n", device_name); |
| 500 |
} |
| 501 |
|
| 502 |
osErr = AudioHardwareGetPropertyInfo(kAudioHardwarePropertyPlugInForBundleID, &outSize, &outWritable); |
| 503 |
if (osErr != noErr) { |
| 504 |
printf("TCoreAudioRenderer::CreateAggregateDevice : AudioHardwareGetPropertyInfo kAudioHardwarePropertyPlugInForBundleID error\n"); |
| 505 |
printError(osErr); |
| 506 |
return osErr; |
| 507 |
} |
| 508 |
|
| 509 |
AudioValueTranslation pluginAVT; |
| 510 |
|
| 511 |
CFStringRef inBundleRef = CFSTR("com.apple.audio.CoreAudio"); |
| 512 |
|
| 513 |
pluginAVT.mInputData = &inBundleRef; |
| 514 |
pluginAVT.mInputDataSize = sizeof(inBundleRef); |
| 515 |
pluginAVT.mOutputData = &fPluginID; |
| 516 |
pluginAVT.mOutputDataSize = sizeof(fPluginID); |
| 517 |
|
| 518 |
osErr = AudioHardwareGetProperty(kAudioHardwarePropertyPlugInForBundleID, &outSize, &pluginAVT); |
| 519 |
if (osErr != noErr) { |
| 520 |
printf("TCoreAudioRenderer::CreateAggregateDevice : AudioHardwareGetProperty kAudioHardwarePropertyPlugInForBundleID error\n"); |
| 521 |
printError(osErr); |
| 522 |
return osErr; |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
CFMutableDictionaryRef aggDeviceDict = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); |
| 530 |
|
| 531 |
CFStringRef AggregateDeviceNameRef = CFSTR("JackDuplex"); |
| 532 |
CFStringRef AggregateDeviceUIDRef = CFSTR("com.grame.JackDuplex"); |
| 533 |
|
| 534 |
|
| 535 |
CFDictionaryAddValue(aggDeviceDict, CFSTR(kAudioAggregateDeviceNameKey), AggregateDeviceNameRef); |
| 536 |
|
| 537 |
|
| 538 |
CFDictionaryAddValue(aggDeviceDict, CFSTR(kAudioAggregateDeviceUIDKey), AggregateDeviceUIDRef); |
| 539 |
|
| 540 |
|
| 541 |
int value = 1; |
| 542 |
CFNumberRef AggregateDeviceNumberRef = CFNumberCreate(NULL, kCFNumberIntType, &value); |
| 543 |
|
| 544 |
SInt32 system; |
| 545 |
Gestalt(gestaltSystemVersion, &system); |
| 546 |
|
| 547 |
printf("TCoreAudioRenderer::CreateAggregateDevice : system version = %x limit = %x\n", system, 0x00001054); |
| 548 |
|
| 549 |
|
| 550 |
if (system < 0x00001054) { |
| 551 |
printf("TCoreAudioRenderer::CreateAggregateDevice : public aggregate device....\n"); |
| 552 |
} else { |
| 553 |
printf("TCoreAudioRenderer::CreateAggregateDevice : private aggregate device....\n"); |
| 554 |
CFDictionaryAddValue(aggDeviceDict, CFSTR(kAudioAggregateDeviceIsPrivateKey), AggregateDeviceNumberRef); |
| 555 |
} |
| 556 |
|
| 557 |
|
| 558 |
CFMutableArrayRef subDevicesArrayClock = NULL; |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
CFMutableArrayRef subDevicesArray = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks); |
| 602 |
|
| 603 |
vector<CFStringRef> captureDeviceUID; |
| 604 |
for (UInt32 i = 0; i < captureDeviceID.size(); i++) { |
| 605 |
CFStringRef ref = GetDeviceName(captureDeviceID[i]); |
| 606 |
if (ref == NULL) |
| 607 |
return -1; |
| 608 |
captureDeviceUID.push_back(ref); |
| 609 |
|
| 610 |
CFArrayAppendValue(subDevicesArray, ref); |
| 611 |
} |
| 612 |
|
| 613 |
vector<CFStringRef> playbackDeviceUID; |
| 614 |
for (UInt32 i = 0; i < playbackDeviceID.size(); i++) { |
| 615 |
CFStringRef ref = GetDeviceName(playbackDeviceID[i]); |
| 616 |
if (ref == NULL) |
| 617 |
return -1; |
| 618 |
playbackDeviceUID.push_back(ref); |
| 619 |
|
| 620 |
CFArrayAppendValue(subDevicesArray, ref); |
| 621 |
} |
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
AudioObjectPropertyAddress pluginAOPA; |
| 628 |
pluginAOPA.mSelector = kAudioPlugInCreateAggregateDevice; |
| 629 |
pluginAOPA.mScope = kAudioObjectPropertyScopeGlobal; |
| 630 |
pluginAOPA.mElement = kAudioObjectPropertyElementMaster; |
| 631 |
UInt32 outDataSize; |
| 632 |
|
| 633 |
osErr = AudioObjectGetPropertyDataSize(fPluginID, &pluginAOPA, 0, NULL, &outDataSize); |
| 634 |
if (osErr != noErr) { |
| 635 |
printf("TCoreAudioRenderer::CreateAggregateDevice : AudioObjectGetPropertyDataSize error\n"); |
| 636 |
printError(osErr); |
| 637 |
goto error; |
| 638 |
} |
| 639 |
|
| 640 |
osErr = AudioObjectGetPropertyData(fPluginID, &pluginAOPA, sizeof(aggDeviceDict), &aggDeviceDict, &outDataSize, outAggregateDevice); |
| 641 |
if (osErr != noErr) { |
| 642 |
printf("TCoreAudioRenderer::CreateAggregateDevice : AudioObjectGetPropertyData error\n"); |
| 643 |
printError(osErr); |
| 644 |
goto error; |
| 645 |
} |
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false); |
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
pluginAOPA.mSelector = kAudioAggregateDevicePropertyFullSubDeviceList; |
| 656 |
pluginAOPA.mScope = kAudioObjectPropertyScopeGlobal; |
| 657 |
pluginAOPA.mElement = kAudioObjectPropertyElementMaster; |
| 658 |
outDataSize = sizeof(CFMutableArrayRef); |
| 659 |
osErr = AudioObjectSetPropertyData(*outAggregateDevice, &pluginAOPA, 0, NULL, outDataSize, &subDevicesArray); |
| 660 |
if (osErr != noErr) { |
| 661 |
printf("TCoreAudioRenderer::CreateAggregateDevice : AudioObjectSetPropertyData for sub-device list error\n"); |
| 662 |
printError(osErr); |
| 663 |
goto error; |
| 664 |
} |
| 665 |
|
| 666 |
|
| 667 |
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false); |
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
pluginAOPA.mSelector = kAudioAggregateDevicePropertyMasterSubDevice; |
| 676 |
pluginAOPA.mScope = kAudioObjectPropertyScopeGlobal; |
| 677 |
pluginAOPA.mElement = kAudioObjectPropertyElementMaster; |
| 678 |
outDataSize = sizeof(CFStringRef); |
| 679 |
osErr = AudioObjectSetPropertyData(*outAggregateDevice, &pluginAOPA, 0, NULL, outDataSize, &captureDeviceUID[0]); |
| 680 |
if (osErr != noErr) { |
| 681 |
printf("TCoreAudioRenderer::CreateAggregateDevice : AudioObjectSetPropertyData for master device error\n"); |
| 682 |
printError(osErr); |
| 683 |
goto error; |
| 684 |
} |
| 685 |
|
| 686 |
|
| 687 |
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false); |
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
if (fClockDriftCompensate) { |
| 693 |
if (need_clock_drift_compensation) { |
| 694 |
printf("Clock drift compensation activated...\n"); |
| 695 |
|
| 696 |
|
| 697 |
osErr = AudioObjectGetPropertyDataSize(*outAggregateDevice, &theAddressOwned, theQualifierDataSize, theQualifierData, &outSize); |
| 698 |
if (osErr != noErr) { |
| 699 |
printf("TCoreAudioRenderer::CreateAggregateDevice kAudioObjectPropertyOwnedObjects error\n"); |
| 700 |
printError(osErr); |
| 701 |
} |
| 702 |
|
| 703 |
|
| 704 |
subDevicesNum = outSize / sizeof(AudioObjectID); |
| 705 |
printf("TCoreAudioRenderer::CreateAggregateDevice clock drift compensation, number of sub-devices = %d\n", subDevicesNum); |
| 706 |
AudioObjectID subDevices[subDevicesNum]; |
| 707 |
outSize = sizeof(subDevices); |
| 708 |
|
| 709 |
osErr = AudioObjectGetPropertyData(*outAggregateDevice, &theAddressOwned, theQualifierDataSize, theQualifierData, &outSize, subDevices); |
| 710 |
if (osErr != noErr) { |
| 711 |
printf("TCoreAudioRenderer::CreateAggregateDevice kAudioObjectPropertyOwnedObjects error\n"); |
| 712 |
printError(osErr); |
| 713 |
} |
| 714 |
|
| 715 |
|
| 716 |
for (UInt32 index = 0; index < subDevicesNum; ++index) { |
| 717 |
UInt32 theDriftCompensationValue = 1; |
| 718 |
osErr = AudioObjectSetPropertyData(subDevices[index], &theAddressDrift, 0, NULL, sizeof(UInt32), &theDriftCompensationValue); |
| 719 |
if (osErr != noErr) { |
| 720 |
printf("TCoreAudioRenderer::CreateAggregateDevice kAudioSubDevicePropertyDriftCompensation error\n"); |
| 721 |
printError(osErr); |
| 722 |
} |
| 723 |
} |
| 724 |
} else { |
| 725 |
printf("Clock drift compensation was asked but is not needed (devices use the same clock domain)\n"); |
| 726 |
} |
| 727 |
} |
| 728 |
|
| 729 |
|
| 730 |
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false); |
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
CFRelease(AggregateDeviceNumberRef); |
| 738 |
|
| 739 |
|
| 740 |
CFRelease(aggDeviceDict); |
| 741 |
CFRelease(subDevicesArray); |
| 742 |
|
| 743 |
if (subDevicesArrayClock) |
| 744 |
CFRelease(subDevicesArrayClock); |
| 745 |
|
| 746 |
|
| 747 |
for (UInt32 i = 0; i < captureDeviceUID.size(); i++) { |
| 748 |
CFRelease(captureDeviceUID[i]); |
| 749 |
} |
| 750 |
|
| 751 |
for (UInt32 i = 0; i < playbackDeviceUID.size(); i++) { |
| 752 |
CFRelease(playbackDeviceUID[i]); |
| 753 |
} |
| 754 |
|
| 755 |
printf("New aggregate device %d\n", *outAggregateDevice); |
| 756 |
return noErr; |
| 757 |
|
| 758 |
error: |
| 759 |
DestroyAggregateDevice(); |
| 760 |
return -1; |
| 761 |
} |
| 762 |
|
| 763 |
OSStatus TCoreAudioRenderer::DestroyAggregateDevice() |
| 764 |
{ |
| 765 |
OSStatus osErr = noErr; |
| 766 |
AudioObjectPropertyAddress pluginAOPA; |
| 767 |
pluginAOPA.mSelector = kAudioPlugInDestroyAggregateDevice; |
| 768 |
pluginAOPA.mScope = kAudioObjectPropertyScopeGlobal; |
| 769 |
pluginAOPA.mElement = kAudioObjectPropertyElementMaster; |
| 770 |
UInt32 outDataSize; |
| 771 |
|
| 772 |
if (fPluginID > 0) { |
| 773 |
|
| 774 |
osErr = AudioObjectGetPropertyDataSize(fPluginID, &pluginAOPA, 0, NULL, &outDataSize); |
| 775 |
if (osErr != noErr) { |
| 776 |
printf("TCoreAudioRenderer::DestroyAggregateDevice : AudioObjectGetPropertyDataSize error\n"); |
| 777 |
printError(osErr); |
| 778 |
return osErr; |
| 779 |
} |
| 780 |
|
| 781 |
osErr = AudioObjectGetPropertyData(fPluginID, &pluginAOPA, 0, NULL, &outDataSize, &fDeviceID); |
| 782 |
if (osErr != noErr) { |
| 783 |
printf("TCoreAudioRenderer::DestroyAggregateDevice : AudioObjectGetPropertyData error\n"); |
| 784 |
printError(osErr); |
| 785 |
return osErr; |
| 786 |
} |
| 787 |
|
| 788 |
} |
| 789 |
|
| 790 |
return noErr; |
| 791 |
} |
| 792 |
|
| 793 |
|
| 794 |
long TCoreAudioRenderer::OpenDefault(dsp* dsp, long inChan, long outChan, long bufferSize, long samplerate) |
| 795 |
{ |
| 796 |
OSStatus err = noErr; |
| 797 |
ComponentResult err1; |
| 798 |
UInt32 outSize; |
| 799 |
UInt32 enableIO; |
| 800 |
Boolean isWritable; |
| 801 |
AudioStreamBasicDescription srcFormat, dstFormat, sampleRate; |
| 802 |
long in_nChannels, out_nChannels; |
| 803 |
|
| 804 |
fDSP = dsp; |
| 805 |
fDevNumInChans = inChan; |
| 806 |
fDevNumOutChans = outChan; |
| 807 |
|
| 808 |
fInChannel = new float*[fDevNumInChans]; |
| 809 |
fOutChannel = new float*[fDevNumOutChans]; |
| 810 |
|
| 811 |
printf("OpenDefault inChan = %ld outChan = %ld bufferSize = %ld samplerate = %ld\n", inChan, outChan, bufferSize, samplerate); |
| 812 |
|
| 813 |
SInt32 major; |
| 814 |
SInt32 minor; |
| 815 |
Gestalt(gestaltSystemVersionMajor, &major); |
| 816 |
Gestalt(gestaltSystemVersionMinor, &minor); |
| 817 |
|
| 818 |
|
| 819 |
if (major == 10 && minor >= 6) { |
| 820 |
CFRunLoopRef theRunLoop = NULL; |
| 821 |
AudioObjectPropertyAddress theAddress = { kAudioHardwarePropertyRunLoop, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster }; |
| 822 |
OSStatus osErr = AudioObjectSetPropertyData (kAudioObjectSystemObject, &theAddress, 0, NULL, sizeof(CFRunLoopRef), &theRunLoop); |
| 823 |
if (osErr != noErr) { |
| 824 |
printf("TCoreAudioRenderer::Open kAudioHardwarePropertyRunLoop error\n"); |
| 825 |
printError(osErr); |
| 826 |
} |
| 827 |
} |
| 828 |
|
| 829 |
if (GetDefaultDevice(inChan, outChan, samplerate,&fDeviceID) != noErr) { |
| 830 |
printf("Cannot open default device\n"); |
| 831 |
return OPEN_ERR; |
| 832 |
} |
| 833 |
|
| 834 |
|
| 835 |
outSize = sizeof(UInt32); |
| 836 |
err = AudioDeviceSetProperty(fDeviceID, NULL, 0, false, kAudioDevicePropertyBufferFrameSize, outSize, &bufferSize); |
| 837 |
if (err != noErr) { |
| 838 |
printf("Cannot set buffer size %ld\n", bufferSize); |
| 839 |
err = AudioDeviceGetProperty(fDeviceID, 0, false, kAudioDevicePropertyBufferFrameSize, &outSize, &bufferSize); |
| 840 |
if (err != noErr) { |
| 841 |
printf("Cannot get buffer size %ld\n", bufferSize); |
| 842 |
printError(err); |
| 843 |
return OPEN_ERR; |
| 844 |
} else { |
| 845 |
printf("Use fixed buffer size %ld\n", bufferSize); |
| 846 |
} |
| 847 |
} |
| 848 |
|
| 849 |
|
| 850 |
outSize = sizeof(AudioStreamBasicDescription); |
| 851 |
err = AudioDeviceGetProperty(fDeviceID, 0, false, kAudioDevicePropertyStreamFormat, &outSize, &sampleRate); |
| 852 |
if (err != noErr) { |
| 853 |
printf("Cannot get current sample rate\n"); |
| 854 |
printError(err); |
| 855 |
return OPEN_ERR; |
| 856 |
} |
| 857 |
|
| 858 |
if (samplerate != long(sampleRate.mSampleRate)) { |
| 859 |
sampleRate.mSampleRate = (Float64)(samplerate); |
| 860 |
err = AudioDeviceSetProperty(fDeviceID, NULL, 0, false, kAudioDevicePropertyStreamFormat, outSize, &sampleRate); |
| 861 |
if (err != noErr) { |
| 862 |
printf("Cannot set sample rate = %ld\n", samplerate); |
| 863 |
printError(err); |
| 864 |
return OPEN_ERR; |
| 865 |
} |
| 866 |
} |
| 867 |
|
| 868 |
|
| 869 |
ComponentDescription cd = {kAudioUnitType_Output, kAudioUnitSubType_HALOutput, kAudioUnitManufacturer_Apple, 0, 0}; |
| 870 |
Component HALOutput = FindNextComponent(NULL, &cd); |
| 871 |
|
| 872 |
err1 = OpenAComponent(HALOutput, &fAUHAL); |
| 873 |
if (err1 != noErr) { |
| 874 |
printf("Error calling OpenAComponent\n"); |
| 875 |
printError(err1); |
| 876 |
goto error; |
| 877 |
} |
| 878 |
|
| 879 |
err1 = AudioUnitInitialize(fAUHAL); |
| 880 |
if (err1 != noErr) { |
| 881 |
printf("Cannot initialize AUHAL unit\n"); |
| 882 |
printError(err1); |
| 883 |
goto error; |
| 884 |
} |
| 885 |
|
| 886 |
enableIO = 1; |
| 887 |
err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, 0, &enableIO, sizeof(enableIO)); |
| 888 |
if (err1 != noErr) { |
| 889 |
printf("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output\n"); |
| 890 |
printError(err1); |
| 891 |
goto error; |
| 892 |
} |
| 893 |
|
| 894 |
enableIO = 1; |
| 895 |
err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, 1, &enableIO, sizeof(enableIO)); |
| 896 |
if (err1 != noErr) { |
| 897 |
printf("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input\n"); |
| 898 |
printError(err1); |
| 899 |
goto error; |
| 900 |
} |
| 901 |
|
| 902 |
err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &fDeviceID, sizeof(AudioDeviceID)); |
| 903 |
if (err1 != noErr) { |
| 904 |
printf("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_CurrentDevice\n"); |
| 905 |
printError(err1); |
| 906 |
goto error; |
| 907 |
} |
| 908 |
|
| 909 |
err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 1, (UInt32*)&bufferSize, sizeof(UInt32)); |
| 910 |
if (err1 != noErr) { |
| 911 |
printf("Error calling AudioUnitSetProperty - kAudioUnitProperty_MaximumFramesPerSlice\n"); |
| 912 |
printError(err1); |
| 913 |
goto error; |
| 914 |
} |
| 915 |
|
| 916 |
err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 0, (UInt32*)&bufferSize, sizeof(UInt32)); |
| 917 |
if (err1 != noErr) { |
| 918 |
printf("Error calling AudioUnitSetProperty - kAudioUnitProperty_MaximumFramesPerSlice\n"); |
| 919 |
printError(err1); |
| 920 |
goto error; |
| 921 |
} |
| 922 |
|
| 923 |
err1 = AudioUnitGetPropertyInfo(fAUHAL, kAudioOutputUnitProperty_ChannelMap, kAudioUnitScope_Input, 1, &outSize, &isWritable); |
| 924 |
if (err1 != noErr) { |
| 925 |
printf("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_ChannelMap-INFO 1\n"); |
| 926 |
printError(err1); |
| 927 |
} |
| 928 |
|
| 929 |
in_nChannels = (err1 == noErr) ? outSize / sizeof(SInt32) : 0; |
| 930 |
printf("in_nChannels = %ld\n", in_nChannels); |
| 931 |
|
| 932 |
err1 = AudioUnitGetPropertyInfo(fAUHAL, kAudioOutputUnitProperty_ChannelMap, kAudioUnitScope_Output, 0, &outSize, &isWritable); |
| 933 |
if (err1 != noErr) { |
| 934 |
printf("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_ChannelMap-INFO 0\n"); |
| 935 |
printError(err1); |
| 936 |
} |
| 937 |
|
| 938 |
out_nChannels = (err1 == noErr) ? outSize / sizeof(SInt32) : 0; |
| 939 |
printf("out_nChannels = %ld\n", out_nChannels); |
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
if (outChan < out_nChannels) { |
| 955 |
SInt32 chanArr[out_nChannels]; |
| 956 |
for (int i = 0; i < out_nChannels; i++) { |
| 957 |
chanArr[i] = -1; |
| 958 |
} |
| 959 |
for (int i = 0; i < outChan; i++) { |
| 960 |
chanArr[i] = i; |
| 961 |
} |
| 962 |
err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_ChannelMap, kAudioUnitScope_Output, 0, chanArr, sizeof(SInt32) * out_nChannels); |
| 963 |
if (err1 != noErr) { |
| 964 |
printf("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_ChannelMap 0\n"); |
| 965 |
printError(err1); |
| 966 |
} |
| 967 |
} |
| 968 |
|
| 969 |
if (inChan < in_nChannels) { |
| 970 |
SInt32 chanArr[in_nChannels]; |
| 971 |
for (int i = 0; i < in_nChannels; i++) { |
| 972 |
chanArr[i] = -1; |
| 973 |
} |
| 974 |
for (int i = 0; i < inChan; i++) { |
| 975 |
chanArr[i] = i; |
| 976 |
} |
| 977 |
AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_ChannelMap , kAudioUnitScope_Input, 1, chanArr, sizeof(SInt32) * in_nChannels); |
| 978 |
if (err1 != noErr) { |
| 979 |
printf("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_ChannelMap 1\n"); |
| 980 |
printError(err1); |
| 981 |
} |
| 982 |
} |
| 983 |
|
| 984 |
if (inChan > 0) { |
| 985 |
outSize = sizeof(AudioStreamBasicDescription); |
| 986 |
err1 = AudioUnitGetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &srcFormat, &outSize); |
| 987 |
if (err1 != noErr) { |
| 988 |
printf("Error calling AudioUnitGetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Output\n"); |
| 989 |
printError(err1); |
| 990 |
} |
| 991 |
PrintStreamDesc(&srcFormat); |
| 992 |
|
| 993 |
srcFormat.mSampleRate = samplerate; |
| 994 |
srcFormat.mFormatID = kAudioFormatLinearPCM; |
| 995 |
srcFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kLinearPCMFormatFlagIsNonInterleaved; |
| 996 |
srcFormat.mBytesPerPacket = sizeof(float); |
| 997 |
srcFormat.mFramesPerPacket = 1; |
| 998 |
srcFormat.mBytesPerFrame = sizeof(float); |
| 999 |
srcFormat.mChannelsPerFrame = inChan; |
| 1000 |
srcFormat.mBitsPerChannel = 32; |
| 1001 |
|
| 1002 |
PrintStreamDesc(&srcFormat); |
| 1003 |
|
| 1004 |
err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &srcFormat, sizeof(AudioStreamBasicDescription)); |
| 1005 |
if (err1 != noErr) { |
| 1006 |
printf("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Output\n"); |
| 1007 |
printError(err1); |
| 1008 |
} |
| 1009 |
} |
| 1010 |
|
| 1011 |
if (outChan > 0) { |
| 1012 |
outSize = sizeof(AudioStreamBasicDescription); |
| 1013 |
err1 = AudioUnitGetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &dstFormat, &outSize); |
| 1014 |
if (err1 != noErr) { |
| 1015 |
printf("Error calling AudioUnitGetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Output\n"); |
| 1016 |
printError(err1); |
| 1017 |
} |
| 1018 |
PrintStreamDesc(&dstFormat); |
| 1019 |
|
| 1020 |
dstFormat.mSampleRate = samplerate; |
| 1021 |
dstFormat.mFormatID = kAudioFormatLinearPCM; |
| 1022 |
dstFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kLinearPCMFormatFlagIsNonInterleaved; |
| 1023 |
dstFormat.mBytesPerPacket = sizeof(float); |
| 1024 |
dstFormat.mFramesPerPacket = 1; |
| 1025 |
dstFormat.mBytesPerFrame = sizeof(float); |
| 1026 |
dstFormat.mChannelsPerFrame = outChan; |
| 1027 |
dstFormat.mBitsPerChannel = 32; |
| 1028 |
|
| 1029 |
PrintStreamDesc(&dstFormat); |
| 1030 |
|
| 1031 |
err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &dstFormat, sizeof(AudioStreamBasicDescription)); |
| 1032 |
if (err1 != noErr) { |
| 1033 |
printf("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Output\n"); |
| 1034 |
printError(err1); |
| 1035 |
} |
| 1036 |
} |
| 1037 |
|
| 1038 |
if (inChan > 0 && outChan == 0) { |
| 1039 |
AURenderCallbackStruct output; |
| 1040 |
output.inputProc = Render; |
| 1041 |
output.inputProcRefCon = this; |
| 1042 |
err1 = AudioUnitSetProperty(fAUHAL, kAudioOutputUnitProperty_SetInputCallback, kAudioUnitScope_Global, 0, &output, sizeof(output)); |
| 1043 |
if (err1 != noErr) { |
| 1044 |
printf("Error calling AudioUnitSetProperty - kAudioUnitProperty_SetRenderCallback 1\n"); |
| 1045 |
printError(err1); |
| 1046 |
goto error; |
| 1047 |
} |
| 1048 |
} else { |
| 1049 |
AURenderCallbackStruct output; |
| 1050 |
output.inputProc = Render; |
| 1051 |
output.inputProcRefCon = this; |
| 1052 |
err1 = AudioUnitSetProperty(fAUHAL, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &output, sizeof(output)); |
| 1053 |
if (err1 != noErr) { |
| 1054 |
printf("Error calling AudioUnitSetProperty - kAudioUnitProperty_SetRenderCallback 0\n"); |
| 1055 |
printError(err1); |
| 1056 |
goto error; |
| 1057 |
} |
| 1058 |
} |
| 1059 |
|
| 1060 |
fInputData = (AudioBufferList*)malloc(sizeof(UInt32) + inChan * sizeof(AudioBuffer)); |
| 1061 |
if (fInputData == 0) { |
| 1062 |
printf("Cannot allocate memory for input buffers\n"); |
| 1063 |
goto error; |
| 1064 |
} |
| 1065 |
fInputData->mNumberBuffers = inChan; |
| 1066 |
|
| 1067 |
|
| 1068 |
for (int i = 0; i < inChan; i++) { |
| 1069 |
fInputData->mBuffers[i].mNumberChannels = 1; |
| 1070 |
fInputData->mBuffers[i].mData = malloc(bufferSize * sizeof(float)); |
| 1071 |
fInputData->mBuffers[i].mDataByteSize = bufferSize * sizeof(float); |
| 1072 |
} |
| 1073 |
|
| 1074 |
return NO_ERR; |
| 1075 |
|
| 1076 |
error: |
| 1077 |
AudioUnitUninitialize(fAUHAL); |
| 1078 |
CloseComponent(fAUHAL); |
| 1079 |
return OPEN_ERR; |
| 1080 |
} |
| 1081 |
|
| 1082 |
long TCoreAudioRenderer::Close() |
| 1083 |
{ |
| 1084 |
if (!fAUHAL) { |
| 1085 |
return CLOSE_ERR; |
| 1086 |
} |
| 1087 |
|
| 1088 |
for (int i = 0; i < fDevNumInChans; i++) { |
| 1089 |
free(fInputData->mBuffers[i].mData); |
| 1090 |
} |
| 1091 |
free(fInputData); |
| 1092 |
AudioUnitUninitialize(fAUHAL); |
| 1093 |
CloseComponent(fAUHAL); |
| 1094 |
DestroyAggregateDevice(); |
| 1095 |
|
| 1096 |
delete[] fInChannel; |
| 1097 |
delete[] fOutChannel; |
| 1098 |
return NO_ERR; |
| 1099 |
} |
| 1100 |
|
| 1101 |
long TCoreAudioRenderer::Start() |
| 1102 |
{ |
| 1103 |
if (!fAUHAL) { |
| 1104 |
return OPEN_ERR; |
| 1105 |
} |
| 1106 |
|
| 1107 |
OSStatus err = AudioOutputUnitStart(fAUHAL); |
| 1108 |
|
| 1109 |
if (err != noErr) { |
| 1110 |
printf("Error while opening device : device open error \n"); |
| 1111 |
return OPEN_ERR; |
| 1112 |
} else { |
| 1113 |
return NO_ERR; |
| 1114 |
} |
| 1115 |
} |
| 1116 |
|
| 1117 |
long TCoreAudioRenderer::Stop() |
| 1118 |
{ |
| 1119 |
if (!fAUHAL) { |
| 1120 |
return OPEN_ERR; |
| 1121 |
} |
| 1122 |
|
| 1123 |
OSStatus err = AudioOutputUnitStop(fAUHAL); |
| 1124 |
|
| 1125 |
if (err != noErr) { |
| 1126 |
printf("Error while closing device : device close error \n"); |
| 1127 |
return OPEN_ERR; |
| 1128 |
} else { |
| 1129 |
return NO_ERR; |
| 1130 |
} |
| 1131 |
} |
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
class coreaudio : public audio { |
| 1143 |
|
| 1144 |
TCoreAudioRenderer audio_device; |
| 1145 |
long fSampleRate, fFramesPerBuf; |
| 1146 |
|
| 1147 |
public: |
| 1148 |
coreaudio(long srate, long fpb) : fSampleRate(srate), fFramesPerBuf(fpb) {} |
| 1149 |
virtual ~coreaudio() {} |
| 1150 |
|
| 1151 |
virtual bool init(const char* , dsp* DSP) { |
| 1152 |
DSP->init(fSampleRate); |
| 1153 |
if (audio_device.OpenDefault(DSP, DSP->getNumInputs(), DSP->getNumOutputs(), fFramesPerBuf, fSampleRate) < 0) { |
| 1154 |
printf("Cannot open CoreAudio device\n"); |
| 1155 |
return false; |
| 1156 |
} |
| 1157 |
return true; |
| 1158 |
} |
| 1159 |
|
| 1160 |
virtual bool start() { |
| 1161 |
if (audio_device.Start() < 0) { |
| 1162 |
printf("Cannot start CoreAudio device\n"); |
| 1163 |
return false; |
| 1164 |
} |
| 1165 |
return true; |
| 1166 |
} |
| 1167 |
|
| 1168 |
virtual void stop() { |
| 1169 |
audio_device.Stop(); |
| 1170 |
audio_device.Close(); |
| 1171 |
} |
| 1172 |
|
| 1173 |
}; |
| 1174 |
|
| 1175 |
#endif |
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|