2 Copyright (C) 2004-2008 Grame
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "JackCoreAudioDriver.h"
21 #include "JackEngineControl.h"
22 #include "JackMachThread.h"
23 #include "JackGraphManager.h"
24 #include "JackError.h"
25 #include "JackClientControl.h"
26 #include "JackDriverLoader.h"
27 #include "JackGlobals.h"
28 #include "JackTools.h"
29 #include "JackCompilerDeps.h"
32 #include <CoreServices/CoreServices.h>
33 #include <CoreFoundation/CFNumber.h>
38 static void Print4CharCode(const char* msg
, long c
)
40 UInt32 __4CC_number
= (c
);
42 *((SInt32
*)__4CC_string
) = EndianU32_NtoB(__4CC_number
);
44 jack_log("%s'%s'", (msg
), __4CC_string
);
47 static void printError(OSStatus err
)
50 case kAudioHardwareNoError
:
51 jack_log("error code : kAudioHardwareNoError");
53 case kAudioConverterErr_FormatNotSupported
:
54 jack_log("error code : kAudioConverterErr_FormatNotSupported");
56 case kAudioConverterErr_OperationNotSupported
:
57 jack_log("error code : kAudioConverterErr_OperationNotSupported");
59 case kAudioConverterErr_PropertyNotSupported
:
60 jack_log("error code : kAudioConverterErr_PropertyNotSupported");
62 case kAudioConverterErr_InvalidInputSize
:
63 jack_log("error code : kAudioConverterErr_InvalidInputSize");
65 case kAudioConverterErr_InvalidOutputSize
:
66 jack_log("error code : kAudioConverterErr_InvalidOutputSize");
68 case kAudioConverterErr_UnspecifiedError
:
69 jack_log("error code : kAudioConverterErr_UnspecifiedError");
71 case kAudioConverterErr_BadPropertySizeError
:
72 jack_log("error code : kAudioConverterErr_BadPropertySizeError");
74 case kAudioConverterErr_RequiresPacketDescriptionsError
:
75 jack_log("error code : kAudioConverterErr_RequiresPacketDescriptionsError");
77 case kAudioConverterErr_InputSampleRateOutOfRange
:
78 jack_log("error code : kAudioConverterErr_InputSampleRateOutOfRange");
80 case kAudioConverterErr_OutputSampleRateOutOfRange
:
81 jack_log("error code : kAudioConverterErr_OutputSampleRateOutOfRange");
83 case kAudioHardwareNotRunningError
:
84 jack_log("error code : kAudioHardwareNotRunningError");
86 case kAudioHardwareUnknownPropertyError
:
87 jack_log("error code : kAudioHardwareUnknownPropertyError");
89 case kAudioHardwareIllegalOperationError
:
90 jack_log("error code : kAudioHardwareIllegalOperationError");
92 case kAudioHardwareBadDeviceError
:
93 jack_log("error code : kAudioHardwareBadDeviceError");
95 case kAudioHardwareBadStreamError
:
96 jack_log("error code : kAudioHardwareBadStreamError");
98 case kAudioDeviceUnsupportedFormatError
:
99 jack_log("error code : kAudioDeviceUnsupportedFormatError");
101 case kAudioDevicePermissionsError
:
102 jack_log("error code : kAudioDevicePermissionsError");
104 case kAudioHardwareBadObjectError
:
105 jack_log("error code : kAudioHardwareBadObjectError");
107 case kAudioHardwareUnsupportedOperationError
:
108 jack_log("error code : kAudioHardwareUnsupportedOperationError");
111 Print4CharCode("error code : unknown", err
);
116 static OSStatus
DisplayDeviceNames()
124 err
= AudioHardwareGetPropertyInfo(kAudioHardwarePropertyDevices
, &size
, &isWritable
);
128 deviceNum
= size
/ sizeof(AudioDeviceID
);
129 AudioDeviceID devices
[deviceNum
];
131 err
= AudioHardwareGetProperty(kAudioHardwarePropertyDevices
, &size
, devices
);
135 for (i
= 0; i
< deviceNum
; i
++) {
136 char device_name
[256];
137 char internal_name
[256];
139 size
= sizeof(CFStringRef
);
141 err
= AudioDeviceGetProperty(devices
[i
], 0, false, kAudioDevicePropertyDeviceUID
, &size
, &UIname
);
143 CFStringGetCString(UIname
, internal_name
, 256, CFStringGetSystemEncoding());
149 err
= AudioDeviceGetProperty(devices
[i
], 0, false, kAudioDevicePropertyDeviceName
, &size
, device_name
);
153 jack_info("Device name = \'%s\', internal_name = \'%s\' (to be used as -C, -P, or -d parameter)", device_name
, internal_name
);
164 static CFStringRef
GetDeviceName(AudioDeviceID id
)
166 UInt32 size
= sizeof(CFStringRef
);
168 OSStatus err
= AudioDeviceGetProperty(id
, 0, false, kAudioDevicePropertyDeviceUID
, &size
, &UIname
);
169 return (err
== noErr
) ? UIname
: NULL
;
172 OSStatus
JackCoreAudioDriver::Render(void *inRefCon
,
173 AudioUnitRenderActionFlags
*ioActionFlags
,
174 const AudioTimeStamp
*inTimeStamp
,
176 UInt32 inNumberFrames
,
177 AudioBufferList
*ioData
)
179 JackCoreAudioDriver
* driver
= (JackCoreAudioDriver
*)inRefCon
;
180 driver
->fActionFags
= ioActionFlags
;
181 driver
->fCurrentTime
= (AudioTimeStamp
*)inTimeStamp
;
182 driver
->fDriverOutputData
= ioData
;
183 driver
->CycleTakeBeginTime();
184 return driver
->Process();
187 int JackCoreAudioDriver::Read()
189 AudioUnitRender(fAUHAL
, fActionFags
, fCurrentTime
, 1, fEngineControl
->fBufferSize
, fJackInputData
);
193 int JackCoreAudioDriver::Write()
195 for (int i
= 0; i
< fPlaybackChannels
; i
++) {
196 if (fGraphManager
->GetConnectionsNum(fPlaybackPortList
[i
]) > 0) {
197 float* buffer
= GetOutputBuffer(i
);
198 int size
= sizeof(float) * fEngineControl
->fBufferSize
;
199 memcpy((float*)fDriverOutputData
->mBuffers
[i
].mData
, buffer
, size
);
201 if (fWithMonitorPorts
&& fGraphManager
->GetConnectionsNum(fMonitorPortList
[i
]) > 0)
202 memcpy(GetMonitorBuffer(i
), buffer
, size
);
204 memset((float*)fDriverOutputData
->mBuffers
[i
].mData
, 0, sizeof(float) * fEngineControl
->fBufferSize
);
210 // Will run only once
211 OSStatus
JackCoreAudioDriver::MeasureCallback(AudioDeviceID inDevice
,
212 const AudioTimeStamp
* inNow
,
213 const AudioBufferList
* inInputData
,
214 const AudioTimeStamp
* inInputTime
,
215 AudioBufferList
* outOutputData
,
216 const AudioTimeStamp
* inOutputTime
,
219 JackCoreAudioDriver
* driver
= (JackCoreAudioDriver
*)inClientData
;
220 AudioDeviceStop(driver
->fDeviceID
, MeasureCallback
);
222 jack_log("JackCoreAudioDriver::MeasureCallback called");
223 JackMachThread::GetParams(pthread_self(), &driver
->fEngineControl
->fPeriod
, &driver
->fEngineControl
->fComputation
, &driver
->fEngineControl
->fConstraint
);
225 if (driver
->fComputationGrain
> 0) {
226 jack_log("JackCoreAudioDriver::MeasureCallback : RT thread computation setup to %d percent of period", int(driver
->fComputationGrain
* 100));
227 driver
->fEngineControl
->fComputation
= driver
->fEngineControl
->fPeriod
* driver
->fComputationGrain
;
230 // Setup threadded based log function
231 set_threaded_log_function();
235 OSStatus
JackCoreAudioDriver::SRNotificationCallback(AudioDeviceID inDevice
,
238 AudioDevicePropertyID inPropertyID
,
241 JackCoreAudioDriver
* driver
= (JackCoreAudioDriver
*)inClientData
;
243 switch (inPropertyID
) {
245 case kAudioDevicePropertyNominalSampleRate
: {
246 jack_log("JackCoreAudioDriver::SRNotificationCallback kAudioDevicePropertyNominalSampleRate");
247 driver
->fState
= true;
255 // A better implementation would possibly try to recover in case of hardware device change (see HALLAB HLFilePlayerWindowControllerAudioDevicePropertyListenerProc code)
256 OSStatus
JackCoreAudioDriver::DeviceNotificationCallback(AudioDeviceID inDevice
,
259 AudioDevicePropertyID inPropertyID
,
262 JackCoreAudioDriver
* driver
= (JackCoreAudioDriver
*)inClientData
;
264 switch (inPropertyID
) {
266 case kAudioDeviceProcessorOverload
: {
267 jack_error("JackCoreAudioDriver::DeviceNotificationCallback kAudioDeviceProcessorOverload");
268 jack_time_t cur_time
= GetMicroSeconds();
269 driver
->NotifyXRun(cur_time
, float(cur_time
- driver
->fBeginDateUst
)); // Better this value than nothing...
273 case kAudioDevicePropertyStreamConfiguration
: {
274 jack_error("Cannot handle kAudioDevicePropertyStreamConfiguration : server will quit...");
275 driver
->NotifyFailure(JackBackendError
, "Another application has changed the device configuration."); // Message length limited to JACK_MESSAGE_SIZE
276 driver
->CloseAUHAL();
277 kill(JackTools::GetPID(), SIGINT
);
278 return kAudioHardwareUnsupportedOperationError
;
281 case kAudioDevicePropertyNominalSampleRate
: {
282 jack_error("Cannot handle kAudioDevicePropertyNominalSampleRate : server will quit...");
283 driver
->NotifyFailure(JackBackendError
, "Another application has changed the sample rate."); // Message length limited to JACK_MESSAGE_SIZE
284 driver
->CloseAUHAL();
285 kill(JackTools::GetPID(), SIGINT
);
286 return kAudioHardwareUnsupportedOperationError
;
293 OSStatus
JackCoreAudioDriver::GetDeviceIDFromUID(const char* UID
, AudioDeviceID
* id
)
295 UInt32 size
= sizeof(AudioValueTranslation
);
296 CFStringRef inIUD
= CFStringCreateWithCString(NULL
, UID
, CFStringGetSystemEncoding());
297 AudioValueTranslation value
= { &inIUD
, sizeof(CFStringRef
), id
, sizeof(AudioDeviceID
) };
300 return kAudioHardwareUnspecifiedError
;
302 OSStatus res
= AudioHardwareGetProperty(kAudioHardwarePropertyDeviceForUID
, &size
, &value
);
304 jack_log("GetDeviceIDFromUID %s %ld", UID
, *id
);
305 return (*id
== kAudioDeviceUnknown
) ? kAudioHardwareBadDeviceError
: res
;
309 OSStatus
JackCoreAudioDriver::GetDefaultDevice(AudioDeviceID
* id
)
312 UInt32 theSize
= sizeof(UInt32
);
313 AudioDeviceID inDefault
;
314 AudioDeviceID outDefault
;
316 if ((res
= AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice
, &theSize
, &inDefault
)) != noErr
)
319 if ((res
= AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice
, &theSize
, &outDefault
)) != noErr
)
322 jack_log("GetDefaultDevice: input = %ld output = %ld", inDefault
, outDefault
);
324 // Get the device only if default input and ouput are the same
325 if (inDefault
== outDefault
) {
329 jack_error("Default input and output devices are not the same !!");
330 return kAudioHardwareBadDeviceError
;
334 OSStatus
JackCoreAudioDriver::GetDefaultInputDevice(AudioDeviceID
* id
)
337 UInt32 theSize
= sizeof(UInt32
);
338 AudioDeviceID inDefault
;
340 if ((res
= AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice
, &theSize
, &inDefault
)) != noErr
)
343 jack_log("GetDefaultInputDevice: input = %ld ", inDefault
);
348 OSStatus
JackCoreAudioDriver::GetDefaultOutputDevice(AudioDeviceID
* id
)
351 UInt32 theSize
= sizeof(UInt32
);
352 AudioDeviceID outDefault
;
354 if ((res
= AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice
, &theSize
, &outDefault
)) != noErr
)
357 jack_log("GetDefaultOutputDevice: output = %ld", outDefault
);
362 OSStatus
JackCoreAudioDriver::GetDeviceNameFromID(AudioDeviceID id
, char* name
)
365 return AudioDeviceGetProperty(id
, 0, false, kAudioDevicePropertyDeviceName
, &size
, name
);
368 OSStatus
JackCoreAudioDriver::GetTotalChannels(AudioDeviceID device
, int& channelCount
, bool isInput
)
370 OSStatus err
= noErr
;
373 AudioBufferList
* bufferList
= 0;
376 err
= AudioDeviceGetPropertyInfo(device
, 0, isInput
, kAudioDevicePropertyStreamConfiguration
, &outSize
, &outWritable
);
378 bufferList
= (AudioBufferList
*)malloc(outSize
);
379 err
= AudioDeviceGetProperty(device
, 0, isInput
, kAudioDevicePropertyStreamConfiguration
, &outSize
, bufferList
);
381 for (unsigned int i
= 0; i
< bufferList
->mNumberBuffers
; i
++)
382 channelCount
+= bufferList
->mBuffers
[i
].mNumberChannels
;
392 JackCoreAudioDriver::JackCoreAudioDriver(const char* name
, const char* alias
, JackLockedEngine
* engine
, JackSynchro
* table
)
393 : JackAudioDriver(name
, alias
, engine
, table
), fJackInputData(NULL
), fDriverOutputData(NULL
), fPluginID(0), fState(false), fIOUsage(1.f
),fComputationGrain(-1.f
)
396 JackCoreAudioDriver::~JackCoreAudioDriver()
399 OSStatus
JackCoreAudioDriver::DestroyAggregateDevice()
401 OSStatus osErr
= noErr
;
402 AudioObjectPropertyAddress pluginAOPA
;
403 pluginAOPA
.mSelector
= kAudioPlugInDestroyAggregateDevice
;
404 pluginAOPA
.mScope
= kAudioObjectPropertyScopeGlobal
;
405 pluginAOPA
.mElement
= kAudioObjectPropertyElementMaster
;
408 osErr
= AudioObjectGetPropertyDataSize(fPluginID
, &pluginAOPA
, 0, NULL
, &outDataSize
);
409 if (osErr
!= noErr
) {
410 jack_error("JackCoreAudioDriver::DestroyAggregateDevice : AudioObjectGetPropertyDataSize error");
415 osErr
= AudioObjectGetPropertyData(fPluginID
, &pluginAOPA
, 0, NULL
, &outDataSize
, &fDeviceID
);
416 if (osErr
!= noErr
) {
417 jack_error("JackCoreAudioDriver::DestroyAggregateDevice : AudioObjectGetPropertyData error");
425 OSStatus
JackCoreAudioDriver::CreateAggregateDevice(AudioDeviceID captureDeviceID
, AudioDeviceID playbackDeviceID
, AudioDeviceID
* outAggregateDevice
)
427 OSStatus osErr
= noErr
;
431 //---------------------------------------------------------------------------
432 // Start to create a new aggregate by getting the base audio hardware plugin
433 //---------------------------------------------------------------------------
435 jack_info("Separated input and output devices, so create a private aggregate device to handle them...");
437 osErr
= AudioHardwareGetPropertyInfo(kAudioHardwarePropertyPlugInForBundleID
, &outSize
, &outWritable
);
441 AudioValueTranslation pluginAVT
;
443 CFStringRef inBundleRef
= CFSTR("com.apple.audio.CoreAudio");
445 pluginAVT
.mInputData
= &inBundleRef
;
446 pluginAVT
.mInputDataSize
= sizeof(inBundleRef
);
447 pluginAVT
.mOutputData
= &fPluginID
;
448 pluginAVT
.mOutputDataSize
= sizeof(fPluginID
);
450 osErr
= AudioHardwareGetProperty(kAudioHardwarePropertyPlugInForBundleID
, &outSize
, &pluginAVT
);
454 //-------------------------------------------------
455 // Create a CFDictionary for our aggregate device
456 //-------------------------------------------------
458 CFMutableDictionaryRef aggDeviceDict
= CFDictionaryCreateMutable(NULL
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
460 CFStringRef AggregateDeviceNameRef
= CFSTR("JackDuplex");
461 CFStringRef AggregateDeviceUIDRef
= CFSTR("com.grame.JackDuplex");
463 // add the name of the device to the dictionary
464 CFDictionaryAddValue(aggDeviceDict
, CFSTR(kAudioAggregateDeviceNameKey
), AggregateDeviceNameRef
);
466 // add our choice of UID for the aggregate device to the dictionary
467 CFDictionaryAddValue(aggDeviceDict
, CFSTR(kAudioAggregateDeviceUIDKey
), AggregateDeviceUIDRef
);
469 // add a "private aggregate key" to the dictionary
471 CFNumberRef AggregateDeviceNumberRef
= CFNumberCreate(NULL
, kCFNumberIntType
, &value
);
472 CFDictionaryAddValue(aggDeviceDict
, CFSTR(kAudioAggregateDeviceIsPrivateKey
), AggregateDeviceNumberRef
);
474 //-------------------------------------------------
475 // Create a CFMutableArray for our sub-device list
476 //-------------------------------------------------
478 CFStringRef captureDeviceUID
= GetDeviceName(captureDeviceID
);
479 CFStringRef playbackDeviceUID
= GetDeviceName(playbackDeviceID
);
481 if (captureDeviceUID
== NULL
|| playbackDeviceUID
== NULL
)
484 // we need to append the UID for each device to a CFMutableArray, so create one here
485 CFMutableArrayRef subDevicesArray
= CFArrayCreateMutable(NULL
, 0, &kCFTypeArrayCallBacks
);
487 // two sub-devices in this example, so append the sub-device's UID to the CFArray
488 CFArrayAppendValue(subDevicesArray
, captureDeviceUID
);
489 CFArrayAppendValue(subDevicesArray
, playbackDeviceUID
);
491 //-----------------------------------------------------------------------
492 // Feed the dictionary to the plugin, to create a blank aggregate device
493 //-----------------------------------------------------------------------
495 AudioObjectPropertyAddress pluginAOPA
;
496 pluginAOPA
.mSelector
= kAudioPlugInCreateAggregateDevice
;
497 pluginAOPA
.mScope
= kAudioObjectPropertyScopeGlobal
;
498 pluginAOPA
.mElement
= kAudioObjectPropertyElementMaster
;
501 osErr
= AudioObjectGetPropertyDataSize(fPluginID
, &pluginAOPA
, 0, NULL
, &outDataSize
);
505 osErr
= AudioObjectGetPropertyData(fPluginID
, &pluginAOPA
, sizeof(aggDeviceDict
), &aggDeviceDict
, &outDataSize
, outAggregateDevice
);
509 // pause for a bit to make sure that everything completed correctly
510 // this is to work around a bug in the HAL where a new aggregate device seems to disappear briefly after it is created
511 CFRunLoopRunInMode(kCFRunLoopDefaultMode
, 0.1, false);
513 //-------------------------
514 // Set the sub-device list
515 //-------------------------
517 pluginAOPA
.mSelector
= kAudioAggregateDevicePropertyFullSubDeviceList
;
518 pluginAOPA
.mScope
= kAudioObjectPropertyScopeGlobal
;
519 pluginAOPA
.mElement
= kAudioObjectPropertyElementMaster
;
520 outDataSize
= sizeof(CFMutableArrayRef
);
521 osErr
= AudioObjectSetPropertyData(*outAggregateDevice
, &pluginAOPA
, 0, NULL
, outDataSize
, &subDevicesArray
);
525 // pause again to give the changes time to take effect
526 CFRunLoopRunInMode(kCFRunLoopDefaultMode
, 0.1, false);
528 //-----------------------
529 // Set the master device
530 //-----------------------
532 // set the master device manually (this is the device which will act as the master clock for the aggregate device)
533 // pass in the UID of the device you want to use
534 pluginAOPA
.mSelector
= kAudioAggregateDevicePropertyMasterSubDevice
;
535 pluginAOPA
.mScope
= kAudioObjectPropertyScopeGlobal
;
536 pluginAOPA
.mElement
= kAudioObjectPropertyElementMaster
;
537 outDataSize
= sizeof(CFStringRef
);
538 osErr
= AudioObjectSetPropertyData(*outAggregateDevice
, &pluginAOPA
, 0, NULL
, outDataSize
, &captureDeviceUID
); // capture is master...
542 // pause again to give the changes time to take effect
543 CFRunLoopRunInMode(kCFRunLoopDefaultMode
, 0.1, false);
549 CFRelease(AggregateDeviceNumberRef
);
551 // release the CF objects we have created - we don't need them any more
552 CFRelease(aggDeviceDict
);
553 CFRelease(subDevicesArray
);
555 // release the device UID
556 CFRelease(captureDeviceUID
);
557 CFRelease(playbackDeviceUID
);
559 jack_log("New aggregate device %ld", *outAggregateDevice
);
563 int JackCoreAudioDriver::SetupDevices(const char* capture_driver_uid
, const char* playback_driver_uid
, char* capture_driver_name
, char* playback_driver_name
)
565 capture_driver_name
[0] = 0;
566 playback_driver_name
[0] = 0;
569 if (strcmp(capture_driver_uid
, "") != 0 && strcmp(playback_driver_uid
, "") != 0) {
570 jack_log("JackCoreAudioDriver::Open duplex");
572 // Same device for capture and playback...
573 if (strcmp(capture_driver_uid
, playback_driver_uid
) == 0) {
575 if (GetDeviceIDFromUID(playback_driver_uid
, &fDeviceID
) != noErr
) {
576 jack_log("Will take default in/out");
577 if (GetDefaultDevice(&fDeviceID
) != noErr
) {
578 jack_error("Cannot open default device");
582 if (GetDeviceNameFromID(fDeviceID
, capture_driver_name
) != noErr
|| GetDeviceNameFromID(fDeviceID
, playback_driver_name
) != noErr
) {
583 jack_error("Cannot get device name from device ID");
589 // Creates aggregate device
590 AudioDeviceID captureID
, playbackID
;
591 if (GetDeviceIDFromUID(capture_driver_uid
, &captureID
) != noErr
)
593 if (GetDeviceIDFromUID(playback_driver_uid
, &playbackID
) != noErr
)
595 if (CreateAggregateDevice(captureID
, playbackID
, &fDeviceID
) != noErr
)
600 } else if (strcmp(capture_driver_uid
, "") != 0) {
601 jack_log("JackCoreAudioDriver::Open capture only");
602 if (GetDeviceIDFromUID(capture_driver_uid
, &fDeviceID
) != noErr
) {
603 jack_log("Will take default input");
604 if (GetDefaultInputDevice(&fDeviceID
) != noErr
) {
605 jack_error("Cannot open default device");
609 if (GetDeviceNameFromID(fDeviceID
, capture_driver_name
) != noErr
) {
610 jack_error("Cannot get device name from device ID");
615 } else if (strcmp(playback_driver_uid
, "") != 0) {
616 jack_log("JackCoreAudioDriver::Open playback only");
617 if (GetDeviceIDFromUID(playback_driver_uid
, &fDeviceID
) != noErr
) {
618 jack_log("Will take default output");
619 if (GetDefaultOutputDevice(&fDeviceID
) != noErr
) {
620 jack_error("Cannot open default device");
624 if (GetDeviceNameFromID(fDeviceID
, playback_driver_name
) != noErr
) {
625 jack_error("Cannot get device name from device ID");
629 // Use default driver in duplex mode
631 jack_log("JackCoreAudioDriver::Open default driver");
632 if (GetDefaultDevice(&fDeviceID
) != noErr
) {
633 jack_error("Cannot open default device");
636 if (GetDeviceNameFromID(fDeviceID
, capture_driver_name
) != noErr
|| GetDeviceNameFromID(fDeviceID
, playback_driver_name
) != noErr
) {
637 jack_error("Cannot get device name from device ID");
646 Return the max possible input channels in in_nChannels and output channels in out_nChannels.
648 int JackCoreAudioDriver::SetupChannels(bool capturing
, bool playing
, int& inchannels
, int& outchannels
, int& in_nChannels
, int& out_nChannels
, bool strict
)
650 OSStatus err
= noErr
;
653 err
= GetTotalChannels(fDeviceID
, in_nChannels
, true);
655 jack_error("Cannot get input channel number");
659 jack_log("Max input channels : %d", in_nChannels
);
664 err
= GetTotalChannels(fDeviceID
, out_nChannels
, false);
666 jack_error("Cannot get output channel number");
670 jack_log("Max output channels : %d", out_nChannels
);
674 if (inchannels
> in_nChannels
) {
675 jack_error("This device hasn't required input channels inchannels = %d in_nChannels = %d", inchannels
, in_nChannels
);
680 if (outchannels
> out_nChannels
) {
681 jack_error("This device hasn't required output channels outchannels = %d out_nChannels = %d", outchannels
, out_nChannels
);
686 if (inchannels
== 0) {
687 jack_log("Setup max in channels = %d", in_nChannels
);
688 inchannels
= in_nChannels
;
691 if (outchannels
== 0) {
692 jack_log("Setup max out channels = %d", out_nChannels
);
693 outchannels
= out_nChannels
;
699 int JackCoreAudioDriver::SetupBufferSizeAndSampleRate(jack_nframes_t buffer_size
, jack_nframes_t samplerate
)
701 OSStatus err
= noErr
;
705 // Setting buffer size
706 outSize
= sizeof(UInt32
);
707 err
= AudioDeviceSetProperty(fDeviceID
, NULL
, 0, false, kAudioDevicePropertyBufferFrameSize
, outSize
, &buffer_size
);
709 jack_error("Cannot set buffer size %ld", buffer_size
);
715 outSize
= sizeof(Float64
);
716 err
= AudioDeviceGetProperty(fDeviceID
, 0, kAudioDeviceSectionGlobal
, kAudioDevicePropertyNominalSampleRate
, &outSize
, &sampleRate
);
718 jack_error("Cannot get current sample rate");
723 // If needed, set new sample rate
724 if (samplerate
!= (jack_nframes_t
)sampleRate
) {
725 sampleRate
= (Float64
)samplerate
;
727 // To get SR change notification
728 err
= AudioDeviceAddPropertyListener(fDeviceID
, 0, true, kAudioDevicePropertyNominalSampleRate
, SRNotificationCallback
, this);
730 jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyNominalSampleRate");
734 err
= AudioDeviceSetProperty(fDeviceID
, NULL
, 0, kAudioDeviceSectionGlobal
, kAudioDevicePropertyNominalSampleRate
, outSize
, &sampleRate
);
736 jack_error("Cannot set sample rate = %ld", samplerate
);
741 // Waiting for SR change notification
743 while (!fState
&& count
++ < 100) {
745 jack_log("Wait count = %d", count
);
748 // Remove SR change notification
749 AudioDeviceRemovePropertyListener(fDeviceID
, 0, true, kAudioDevicePropertyNominalSampleRate
, SRNotificationCallback
);
755 int JackCoreAudioDriver::OpenAUHAL(bool capturing
,
761 jack_nframes_t buffer_size
,
762 jack_nframes_t samplerate
,
765 ComponentResult err1
;
767 AudioStreamBasicDescription srcFormat
, dstFormat
;
769 jack_log("OpenAUHAL capturing = %d playing = %d inchannels = %d outchannels = %d in_nChannels = %d out_nChannels = %d", capturing
, playing
, inchannels
, outchannels
, in_nChannels
, out_nChannels
);
771 if (inchannels
== 0 && outchannels
== 0) {
772 jack_error("No input and output channels...");
777 ComponentDescription cd
= {kAudioUnitType_Output
, kAudioUnitSubType_HALOutput
, kAudioUnitManufacturer_Apple
, 0, 0};
778 Component HALOutput
= FindNextComponent(NULL
, &cd
);
780 err1
= OpenAComponent(HALOutput
, &fAUHAL
);
782 jack_error("Error calling OpenAComponent");
787 err1
= AudioUnitInitialize(fAUHAL
);
789 jack_error("Cannot initialize AUHAL unit");
795 if (capturing
&& inchannels
> 0) {
797 jack_log("Setup AUHAL input on");
800 jack_log("Setup AUHAL input off");
803 err1
= AudioUnitSetProperty(fAUHAL
, kAudioOutputUnitProperty_EnableIO
, kAudioUnitScope_Input
, 1, &enableIO
, sizeof(enableIO
));
805 jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input");
811 if (playing
&& outchannels
> 0) {
813 jack_log("Setup AUHAL output on");
816 jack_log("Setup AUHAL output off");
819 err1
= AudioUnitSetProperty(fAUHAL
, kAudioOutputUnitProperty_EnableIO
, kAudioUnitScope_Output
, 0, &enableIO
, sizeof(enableIO
));
821 jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_EnableIO,kAudioUnitScope_Output");
827 AudioDeviceID currAudioDeviceID
;
828 UInt32 size
= sizeof(AudioDeviceID
);
829 err1
= AudioUnitGetProperty(fAUHAL
, kAudioOutputUnitProperty_CurrentDevice
, kAudioUnitScope_Global
, 0, &currAudioDeviceID
, &size
);
831 jack_error("Error calling AudioUnitGetProperty - kAudioOutputUnitProperty_CurrentDevice");
836 jack_log("AudioUnitGetPropertyCurrentDevice = %d", currAudioDeviceID
);
839 // Setup up choosen device, in both input and output cases
840 err1
= AudioUnitSetProperty(fAUHAL
, kAudioOutputUnitProperty_CurrentDevice
, kAudioUnitScope_Global
, 0, &fDeviceID
, sizeof(AudioDeviceID
));
842 jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_CurrentDevice");
849 if (capturing
&& inchannels
> 0) {
850 err1
= AudioUnitSetProperty(fAUHAL
, kAudioUnitProperty_MaximumFramesPerSlice
, kAudioUnitScope_Global
, 1, (UInt32
*)&buffer_size
, sizeof(UInt32
));
852 jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_MaximumFramesPerSlice");
859 if (playing
&& outchannels
> 0) {
860 err1
= AudioUnitSetProperty(fAUHAL
, kAudioUnitProperty_MaximumFramesPerSlice
, kAudioUnitScope_Global
, 0, (UInt32
*)&buffer_size
, sizeof(UInt32
));
862 jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_MaximumFramesPerSlice");
870 if (capturing
&& inchannels
> 0 && inchannels
< in_nChannels
) {
871 SInt32 chanArr
[in_nChannels
];
872 for (int i
= 0; i
< in_nChannels
; i
++) {
875 for (int i
= 0; i
< inchannels
; i
++) {
878 AudioUnitSetProperty(fAUHAL
, kAudioOutputUnitProperty_ChannelMap
, kAudioUnitScope_Input
, 1, chanArr
, sizeof(SInt32
) * in_nChannels
);
880 jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_ChannelMap 1");
885 if (playing
&& outchannels
> 0 && outchannels
< out_nChannels
) {
886 SInt32 chanArr
[out_nChannels
];
887 for (int i
= 0; i
< out_nChannels
; i
++) {
890 for (int i
= 0; i
< outchannels
; i
++) {
893 err1
= AudioUnitSetProperty(fAUHAL
, kAudioOutputUnitProperty_ChannelMap
, kAudioUnitScope_Output
, 0, chanArr
, sizeof(SInt32
) * out_nChannels
);
895 jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_ChannelMap 0");
900 // Setup stream converters
901 jack_log("Setup AUHAL input stream converter SR = %ld", samplerate
);
902 srcFormat
.mSampleRate
= samplerate
;
903 srcFormat
.mFormatID
= kAudioFormatLinearPCM
;
904 srcFormat
.mFormatFlags
= kAudioFormatFlagsNativeFloatPacked
| kLinearPCMFormatFlagIsNonInterleaved
;
905 srcFormat
.mBytesPerPacket
= sizeof(float);
906 srcFormat
.mFramesPerPacket
= 1;
907 srcFormat
.mBytesPerFrame
= sizeof(float);
908 srcFormat
.mChannelsPerFrame
= outchannels
;
909 srcFormat
.mBitsPerChannel
= 32;
911 err1
= AudioUnitSetProperty(fAUHAL
, kAudioUnitProperty_StreamFormat
, kAudioUnitScope_Input
, 0, &srcFormat
, sizeof(AudioStreamBasicDescription
));
913 jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Input");
917 jack_log("Setup AUHAL output stream converter SR = %ld", samplerate
);
918 dstFormat
.mSampleRate
= samplerate
;
919 dstFormat
.mFormatID
= kAudioFormatLinearPCM
;
920 dstFormat
.mFormatFlags
= kAudioFormatFlagsNativeFloatPacked
| kLinearPCMFormatFlagIsNonInterleaved
;
921 dstFormat
.mBytesPerPacket
= sizeof(float);
922 dstFormat
.mFramesPerPacket
= 1;
923 dstFormat
.mBytesPerFrame
= sizeof(float);
924 dstFormat
.mChannelsPerFrame
= inchannels
;
925 dstFormat
.mBitsPerChannel
= 32;
927 err1
= AudioUnitSetProperty(fAUHAL
, kAudioUnitProperty_StreamFormat
, kAudioUnitScope_Output
, 1, &dstFormat
, sizeof(AudioStreamBasicDescription
));
929 jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Output");
934 if (inchannels
> 0 && outchannels
== 0) {
935 AURenderCallbackStruct output
;
936 output
.inputProc
= Render
;
937 output
.inputProcRefCon
= this;
938 err1
= AudioUnitSetProperty(fAUHAL
, kAudioOutputUnitProperty_SetInputCallback
, kAudioUnitScope_Global
, 0, &output
, sizeof(output
));
940 jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_SetRenderCallback 1");
945 AURenderCallbackStruct output
;
946 output
.inputProc
= Render
;
947 output
.inputProcRefCon
= this;
948 err1
= AudioUnitSetProperty(fAUHAL
, kAudioUnitProperty_SetRenderCallback
, kAudioUnitScope_Input
, 0, &output
, sizeof(output
));
950 jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_SetRenderCallback 0");
959 int JackCoreAudioDriver::SetupBuffers(int inchannels
)
962 fJackInputData
= (AudioBufferList
*)malloc(sizeof(UInt32
) + inchannels
* sizeof(AudioBuffer
));
963 if (fJackInputData
== 0) {
964 jack_error("Cannot allocate memory for input buffers");
967 fJackInputData
->mNumberBuffers
= inchannels
;
968 for (int i
= 0; i
< fCaptureChannels
; i
++) {
969 fJackInputData
->mBuffers
[i
].mNumberChannels
= 1;
970 fJackInputData
->mBuffers
[i
].mDataByteSize
= fEngineControl
->fBufferSize
* sizeof(float);
975 void JackCoreAudioDriver::DisposeBuffers()
977 if (fJackInputData
) {
978 free(fJackInputData
);
983 void JackCoreAudioDriver::CloseAUHAL()
985 AudioUnitUninitialize(fAUHAL
);
986 CloseComponent(fAUHAL
);
989 int JackCoreAudioDriver::AddListeners()
991 OSStatus err
= noErr
;
994 err
= AudioDeviceAddPropertyListener(fDeviceID
, 0, true, kAudioDeviceProcessorOverload
, DeviceNotificationCallback
, this);
996 jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDeviceProcessorOverload");
1001 err
= AudioDeviceAddPropertyListener(fDeviceID
, 0, true, kAudioHardwarePropertyDevices
, DeviceNotificationCallback
, this);
1003 jack_error("Error calling AudioDeviceAddPropertyListener with kAudioHardwarePropertyDevices");
1008 err
= AudioDeviceAddPropertyListener(fDeviceID
, 0, true, kAudioDevicePropertyNominalSampleRate
, DeviceNotificationCallback
, this);
1010 jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyNominalSampleRate");
1015 err
= AudioDeviceAddPropertyListener(fDeviceID
, 0, true, kAudioDevicePropertyDeviceIsRunning
, DeviceNotificationCallback
, this);
1017 jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyDeviceIsRunning");
1022 err
= AudioDeviceAddPropertyListener(fDeviceID
, 0, true, kAudioDevicePropertyStreamConfiguration
, DeviceNotificationCallback
, this);
1024 jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyStreamConfiguration");
1029 err
= AudioDeviceAddPropertyListener(fDeviceID
, 0, false, kAudioDevicePropertyStreamConfiguration
, DeviceNotificationCallback
, this);
1031 jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyStreamConfiguration");
1036 if (!fEngineControl
->fSyncMode
&& fIOUsage
!= 1.f
) {
1037 UInt32 outSize
= sizeof(float);
1038 err
= AudioDeviceSetProperty(fDeviceID
, NULL
, 0, false, kAudioDevicePropertyIOCycleUsage
, outSize
, &fIOUsage
);
1040 jack_error("Error calling AudioDeviceSetProperty kAudioDevicePropertyIOCycleUsage");
1048 void JackCoreAudioDriver::RemoveListeners()
1050 AudioDeviceRemovePropertyListener(fDeviceID
, 0, true, kAudioDeviceProcessorOverload
, DeviceNotificationCallback
);
1051 AudioDeviceRemovePropertyListener(fDeviceID
, 0, true, kAudioHardwarePropertyDevices
, DeviceNotificationCallback
);
1052 AudioDeviceRemovePropertyListener(fDeviceID
, 0, true, kAudioDevicePropertyNominalSampleRate
, DeviceNotificationCallback
);
1053 AudioDeviceRemovePropertyListener(fDeviceID
, 0, true, kAudioDevicePropertyDeviceIsRunning
, DeviceNotificationCallback
);
1054 AudioDeviceRemovePropertyListener(fDeviceID
, 0, true, kAudioDevicePropertyStreamConfiguration
, DeviceNotificationCallback
);
1055 AudioDeviceRemovePropertyListener(fDeviceID
, 0, false, kAudioDevicePropertyStreamConfiguration
, DeviceNotificationCallback
);
1058 int JackCoreAudioDriver::Open(jack_nframes_t buffer_size
,
1059 jack_nframes_t samplerate
,
1065 const char* capture_driver_uid
,
1066 const char* playback_driver_uid
,
1067 jack_nframes_t capture_latency
,
1068 jack_nframes_t playback_latency
,
1069 int async_output_latency
,
1070 int computation_grain
)
1072 int in_nChannels
= 0;
1073 int out_nChannels
= 0;
1074 char capture_driver_name
[256];
1075 char playback_driver_name
[256];
1077 // Keep initial state
1078 fCapturing
= capturing
;
1080 fInChannels
= inchannels
;
1081 fOutChannels
= outchannels
;
1083 strcpy(fCaptureUID
, capture_driver_uid
);
1084 strcpy(fPlaybackUID
, playback_driver_uid
);
1085 fCaptureLatency
= capture_latency
;
1086 fPlaybackLatency
= playback_latency
;
1087 fIOUsage
= float(async_output_latency
) / 100.f
;
1088 fComputationGrain
= float(computation_grain
) / 100.f
;
1092 Gestalt(gestaltSystemVersionMajor
, &major
);
1093 Gestalt(gestaltSystemVersionMinor
, &minor
);
1095 // Starting with 10.6 systems, the HAL notification thread is created internally
1096 if (major
== 10 && minor
>=6) {
1097 CFRunLoopRef theRunLoop
= NULL
;
1098 AudioObjectPropertyAddress theAddress
= { kAudioHardwarePropertyRunLoop
, kAudioObjectPropertyScopeGlobal
, kAudioObjectPropertyElementMaster
};
1099 OSStatus theError
= AudioObjectSetPropertyData (kAudioObjectSystemObject
, &theAddress
, 0, NULL
, sizeof(CFRunLoopRef
), &theRunLoop
);
1100 if (theError
!= noErr
) {
1101 jack_error("JackCoreAudioDriver::Open kAudioHardwarePropertyRunLoop error");
1105 if (SetupDevices(capture_driver_uid
, playback_driver_uid
, capture_driver_name
, playback_driver_name
) < 0)
1108 // Generic JackAudioDriver Open
1109 if (JackAudioDriver::Open(buffer_size
, samplerate
, capturing
, playing
, inchannels
, outchannels
, monitor
, capture_driver_name
, playback_driver_name
, capture_latency
, playback_latency
) != 0)
1112 if (SetupChannels(capturing
, playing
, inchannels
, outchannels
, in_nChannels
, out_nChannels
, true) < 0)
1115 if (SetupBufferSizeAndSampleRate(buffer_size
, samplerate
) < 0)
1118 if (OpenAUHAL(capturing
, playing
, inchannels
, outchannels
, in_nChannels
, out_nChannels
, buffer_size
, samplerate
, true) < 0)
1121 if (capturing
&& inchannels
> 0)
1122 if (SetupBuffers(inchannels
) < 0)
1125 if (AddListeners() < 0)
1128 // Core driver may have changed the in/out values
1129 fCaptureChannels
= inchannels
;
1130 fPlaybackChannels
= outchannels
;
1138 int JackCoreAudioDriver::Close()
1140 jack_log("JackCoreAudioDriver::Close");
1142 JackAudioDriver::Close();
1147 DestroyAggregateDevice();
1151 int JackCoreAudioDriver::Attach()
1155 jack_port_id_t port_index
;
1158 char channel_name
[64];
1159 char name
[JACK_CLIENT_NAME_SIZE
+ JACK_PORT_NAME_SIZE
];
1160 char alias
[JACK_CLIENT_NAME_SIZE
+ JACK_PORT_NAME_SIZE
];
1161 unsigned long port_flags
= JackPortIsOutput
| JackPortIsPhysical
| JackPortIsTerminal
;
1163 jack_log("JackCoreAudioDriver::Attach fBufferSize %ld fSampleRate %ld", fEngineControl
->fBufferSize
, fEngineControl
->fSampleRate
);
1165 for (int i
= 0; i
< fCaptureChannels
; i
++) {
1167 err
= AudioDeviceGetPropertyInfo(fDeviceID
, i
+ 1, true, kAudioDevicePropertyChannelName
, &size
, &isWritable
);
1169 jack_log("AudioDeviceGetPropertyInfo kAudioDevicePropertyChannelName error ");
1170 if (err
== noErr
&& size
> 0) {
1171 err
= AudioDeviceGetProperty(fDeviceID
, i
+ 1, true, kAudioDevicePropertyChannelName
, &size
, channel_name
);
1173 jack_log("AudioDeviceGetProperty kAudioDevicePropertyChannelName error ");
1174 snprintf(alias
, sizeof(alias
) - 1, "%s:%s:out_%s%u", fAliasName
, fCaptureDriverName
, channel_name
, i
+ 1);
1176 snprintf(alias
, sizeof(alias
) - 1, "%s:%s:out%u", fAliasName
, fCaptureDriverName
, i
+ 1);
1179 snprintf(name
, sizeof(name
) - 1, "%s:capture_%d", fClientControl
.fName
, i
+ 1);
1181 if ((port_index
= fGraphManager
->AllocatePort(fClientControl
.fRefNum
, name
, JACK_DEFAULT_AUDIO_TYPE
, (JackPortFlags
)port_flags
, fEngineControl
->fBufferSize
)) == NO_PORT
) {
1182 jack_error("Cannot register port for %s", name
);
1186 size
= sizeof(UInt32
);
1189 err
= AudioDeviceGetProperty(fDeviceID
, 0, true, kAudioDevicePropertyLatency
, &size
, &value1
);
1191 jack_log("AudioDeviceGetProperty kAudioDevicePropertyLatency error ");
1192 err
= AudioDeviceGetProperty(fDeviceID
, 0, true, kAudioDevicePropertySafetyOffset
, &size
, &value2
);
1194 jack_log("AudioDeviceGetProperty kAudioDevicePropertySafetyOffset error ");
1196 port
= fGraphManager
->GetPort(port_index
);
1197 port
->SetAlias(alias
);
1198 port
->SetLatency(fEngineControl
->fBufferSize
+ value1
+ value2
+ fCaptureLatency
);
1199 fCapturePortList
[i
] = port_index
;
1202 port_flags
= JackPortIsInput
| JackPortIsPhysical
| JackPortIsTerminal
;
1204 for (int i
= 0; i
< fPlaybackChannels
; i
++) {
1206 err
= AudioDeviceGetPropertyInfo(fDeviceID
, i
+ 1, false, kAudioDevicePropertyChannelName
, &size
, &isWritable
);
1208 jack_log("AudioDeviceGetPropertyInfo kAudioDevicePropertyChannelName error ");
1209 if (err
== noErr
&& size
> 0) {
1210 err
= AudioDeviceGetProperty(fDeviceID
, i
+ 1, false, kAudioDevicePropertyChannelName
, &size
, channel_name
);
1212 jack_log("AudioDeviceGetProperty kAudioDevicePropertyChannelName error ");
1213 snprintf(alias
, sizeof(alias
) - 1, "%s:%s:in_%s%u", fAliasName
, fPlaybackDriverName
, channel_name
, i
+ 1);
1215 snprintf(alias
, sizeof(alias
) - 1, "%s:%s:in%u", fAliasName
, fPlaybackDriverName
, i
+ 1);
1218 snprintf(name
, sizeof(name
) - 1, "%s:playback_%d", fClientControl
.fName
, i
+ 1);
1220 if ((port_index
= fGraphManager
->AllocatePort(fClientControl
.fRefNum
, name
, JACK_DEFAULT_AUDIO_TYPE
, (JackPortFlags
)port_flags
, fEngineControl
->fBufferSize
)) == NO_PORT
) {
1221 jack_error("Cannot register port for %s", name
);
1225 size
= sizeof(UInt32
);
1228 err
= AudioDeviceGetProperty(fDeviceID
, 0, false, kAudioDevicePropertyLatency
, &size
, &value1
);
1230 jack_log("AudioDeviceGetProperty kAudioDevicePropertyLatency error ");
1231 err
= AudioDeviceGetProperty(fDeviceID
, 0, false, kAudioDevicePropertySafetyOffset
, &size
, &value2
);
1233 jack_log("AudioDeviceGetProperty kAudioDevicePropertySafetyOffset error ");
1235 port
= fGraphManager
->GetPort(port_index
);
1236 port
->SetAlias(alias
);
1237 // Add more latency if "async" mode is used...
1238 port
->SetLatency(fEngineControl
->fBufferSize
+ ((fEngineControl
->fSyncMode
) ? 0 : fEngineControl
->fBufferSize
* fIOUsage
) + value1
+ value2
+ fPlaybackLatency
);
1239 fPlaybackPortList
[i
] = port_index
;
1242 if (fWithMonitorPorts
) {
1243 jack_log("Create monitor port ");
1244 snprintf(name
, sizeof(name
) - 1, "%s:monitor_%u", fClientControl
.fName
, i
+ 1);
1245 if ((port_index
= fGraphManager
->AllocatePort(fClientControl
.fRefNum
, name
, JACK_DEFAULT_AUDIO_TYPE
, JackPortIsOutput
, fEngineControl
->fBufferSize
)) == NO_PORT
) {
1246 jack_error("Cannot register monitor port for %s", name
);
1249 port
= fGraphManager
->GetPort(port_index
);
1250 port
->SetAlias(alias
);
1251 port
->SetLatency(fEngineControl
->fBufferSize
);
1252 fMonitorPortList
[i
] = port_index
;
1257 // Input buffers do no change : prepare them only once
1258 for (int i
= 0; i
< fCaptureChannels
; i
++) {
1259 fJackInputData
->mBuffers
[i
].mData
= GetInputBuffer(i
);
1265 int JackCoreAudioDriver::Start()
1267 jack_log("JackCoreAudioDriver::Start");
1268 JackAudioDriver::Start();
1270 #ifdef MAC_OS_X_VERSION_10_5
1271 OSStatus err = AudioDeviceCreateIOProcID(fDeviceID, MeasureCallback, this, &fMesureCallbackID);
1273 OSStatus err = AudioDeviceAddIOProc(fDeviceID, MeasureCallback, this);
1276 OSStatus err
= AudioDeviceAddIOProc(fDeviceID
, MeasureCallback
, this);
1281 err
= AudioOutputUnitStart(fAUHAL
);
1285 if ((err
= AudioDeviceStart(fDeviceID
, MeasureCallback
)) != noErr
) {
1286 jack_error("Cannot start MeasureCallback");
1294 int JackCoreAudioDriver::Stop()
1296 jack_log("JackCoreAudioDriver::Stop");
1297 AudioDeviceStop(fDeviceID
, MeasureCallback
);
1299 #ifdef MAC_OS_X_VERSION_10_5
1300 AudioDeviceDestroyIOProcID(fDeviceID, fMesureCallbackID);
1302 AudioDeviceRemoveIOProc(fDeviceID, MeasureCallback);
1305 AudioDeviceRemoveIOProc(fDeviceID
, MeasureCallback
);
1306 return (AudioOutputUnitStop(fAUHAL
) == noErr
) ? 0 : -1;
1309 int JackCoreAudioDriver::SetBufferSize(jack_nframes_t buffer_size
)
1312 UInt32 outSize
= sizeof(UInt32
);
1314 err
= AudioDeviceSetProperty(fDeviceID
, NULL
, 0, false, kAudioDevicePropertyBufferFrameSize
, outSize
, &buffer_size
);
1316 jack_error("Cannot set buffer size %ld", buffer_size
);
1321 JackAudioDriver::SetBufferSize(buffer_size
); // never fails
1323 // Input buffers do no change : prepare them only once
1324 for (int i
= 0; i
< fCaptureChannels
; i
++) {
1325 fJackInputData
->mBuffers
[i
].mNumberChannels
= 1;
1326 fJackInputData
->mBuffers
[i
].mDataByteSize
= fEngineControl
->fBufferSize
* sizeof(float);
1327 fJackInputData
->mBuffers
[i
].mData
= GetInputBuffer(i
);
1333 } // end of namespace
1341 SERVER_EXPORT jack_driver_desc_t
* driver_get_descriptor()
1343 jack_driver_desc_t
*desc
;
1345 desc
= (jack_driver_desc_t
*)calloc(1, sizeof(jack_driver_desc_t
));
1347 strcpy(desc
->name
, "coreaudio"); // size MUST be less then JACK_DRIVER_NAME_MAX + 1
1348 strcpy(desc
->desc
, "Apple CoreAudio API based audio backend"); // size MUST be less then JACK_DRIVER_PARAM_DESC + 1
1351 desc
->params
= (jack_driver_param_desc_t
*)calloc(desc
->nparams
, sizeof(jack_driver_param_desc_t
));
1354 strcpy(desc
->params
[i
].name
, "channels");
1355 desc
->params
[i
].character
= 'c';
1356 desc
->params
[i
].type
= JackDriverParamInt
;
1357 desc
->params
[i
].value
.ui
= 0;
1358 strcpy(desc
->params
[i
].short_desc
, "Maximum number of channels");
1359 strcpy(desc
->params
[i
].long_desc
, desc
->params
[i
].short_desc
);
1362 strcpy(desc
->params
[i
].name
, "inchannels");
1363 desc
->params
[i
].character
= 'i';
1364 desc
->params
[i
].type
= JackDriverParamInt
;
1365 desc
->params
[i
].value
.ui
= 0;
1366 strcpy(desc
->params
[i
].short_desc
, "Maximum number of input channels");
1367 strcpy(desc
->params
[i
].long_desc
, desc
->params
[i
].short_desc
);
1370 strcpy(desc
->params
[i
].name
, "outchannels");
1371 desc
->params
[i
].character
= 'o';
1372 desc
->params
[i
].type
= JackDriverParamInt
;
1373 desc
->params
[i
].value
.ui
= 0;
1374 strcpy(desc
->params
[i
].short_desc
, "Maximum number of output channels");
1375 strcpy(desc
->params
[i
].long_desc
, desc
->params
[i
].short_desc
);
1378 strcpy(desc
->params
[i
].name
, "capture");
1379 desc
->params
[i
].character
= 'C';
1380 desc
->params
[i
].type
= JackDriverParamString
;
1381 strcpy(desc
->params
[i
].value
.str
, "will take default CoreAudio input device");
1382 strcpy(desc
->params
[i
].short_desc
, "Provide capture ports. Optionally set CoreAudio device name");
1383 strcpy(desc
->params
[i
].long_desc
, desc
->params
[i
].short_desc
);
1386 strcpy(desc
->params
[i
].name
, "playback");
1387 desc
->params
[i
].character
= 'P';
1388 desc
->params
[i
].type
= JackDriverParamString
;
1389 strcpy(desc
->params
[i
].value
.str
, "will take default CoreAudio output device");
1390 strcpy(desc
->params
[i
].short_desc
, "Provide playback ports. Optionally set CoreAudio device name");
1391 strcpy(desc
->params
[i
].long_desc
, desc
->params
[i
].short_desc
);
1394 strcpy (desc
->params
[i
].name
, "monitor");
1395 desc
->params
[i
].character
= 'm';
1396 desc
->params
[i
].type
= JackDriverParamBool
;
1397 desc
->params
[i
].value
.i
= 0;
1398 strcpy(desc
->params
[i
].short_desc
, "Provide monitor ports for the output");
1399 strcpy(desc
->params
[i
].long_desc
, desc
->params
[i
].short_desc
);
1402 strcpy(desc
->params
[i
].name
, "duplex");
1403 desc
->params
[i
].character
= 'D';
1404 desc
->params
[i
].type
= JackDriverParamBool
;
1405 desc
->params
[i
].value
.i
= TRUE
;
1406 strcpy(desc
->params
[i
].short_desc
, "Provide both capture and playback ports");
1407 strcpy(desc
->params
[i
].long_desc
, desc
->params
[i
].short_desc
);
1410 strcpy(desc
->params
[i
].name
, "rate");
1411 desc
->params
[i
].character
= 'r';
1412 desc
->params
[i
].type
= JackDriverParamUInt
;
1413 desc
->params
[i
].value
.ui
= 44100U;
1414 strcpy(desc
->params
[i
].short_desc
, "Sample rate");
1415 strcpy(desc
->params
[i
].long_desc
, desc
->params
[i
].short_desc
);
1418 strcpy(desc
->params
[i
].name
, "period");
1419 desc
->params
[i
].character
= 'p';
1420 desc
->params
[i
].type
= JackDriverParamUInt
;
1421 desc
->params
[i
].value
.ui
= 128U;
1422 strcpy(desc
->params
[i
].short_desc
, "Frames per period");
1423 strcpy(desc
->params
[i
].long_desc
, desc
->params
[i
].short_desc
);
1426 strcpy(desc
->params
[i
].name
, "device");
1427 desc
->params
[i
].character
= 'd';
1428 desc
->params
[i
].type
= JackDriverParamString
;
1429 strcpy(desc
->params
[i
].value
.str
, "will take default CoreAudio device name");
1430 strcpy(desc
->params
[i
].short_desc
, "CoreAudio device name");
1431 strcpy(desc
->params
[i
].long_desc
, desc
->params
[i
].short_desc
);
1434 strcpy(desc
->params
[i
].name
, "input-latency");
1435 desc
->params
[i
].character
= 'I';
1436 desc
->params
[i
].type
= JackDriverParamUInt
;
1437 desc
->params
[i
].value
.i
= 0;
1438 strcpy(desc
->params
[i
].short_desc
, "Extra input latency (frames)");
1439 strcpy(desc
->params
[i
].long_desc
, desc
->params
[i
].short_desc
);
1442 strcpy(desc
->params
[i
].name
, "output-latency");
1443 desc
->params
[i
].character
= 'O';
1444 desc
->params
[i
].type
= JackDriverParamUInt
;
1445 desc
->params
[i
].value
.i
= 0;
1446 strcpy(desc
->params
[i
].short_desc
, "Extra output latency (frames)");
1447 strcpy(desc
->params
[i
].long_desc
, desc
->params
[i
].short_desc
);
1450 strcpy(desc
->params
[i
].name
, "list-devices");
1451 desc
->params
[i
].character
= 'l';
1452 desc
->params
[i
].type
= JackDriverParamBool
;
1453 desc
->params
[i
].value
.i
= TRUE
;
1454 strcpy(desc
->params
[i
].short_desc
, "Display available CoreAudio devices");
1455 strcpy(desc
->params
[i
].long_desc
, desc
->params
[i
].short_desc
);
1458 strcpy(desc
->params
[i
].name
, "async-latency");
1459 desc
->params
[i
].character
= 'L';
1460 desc
->params
[i
].type
= JackDriverParamUInt
;
1461 desc
->params
[i
].value
.i
= 100;
1462 strcpy(desc
->params
[i
].short_desc
, "Extra output latency in aynchronous mode (percent)");
1463 strcpy(desc
->params
[i
].long_desc
, desc
->params
[i
].short_desc
);
1466 strcpy(desc
->params
[i
].name
, "grain");
1467 desc
->params
[i
].character
= 'G';
1468 desc
->params
[i
].type
= JackDriverParamUInt
;
1469 desc
->params
[i
].value
.i
= 100;
1470 strcpy(desc
->params
[i
].short_desc
, "Computation grain in RT thread (percent)");
1471 strcpy(desc
->params
[i
].long_desc
, desc
->params
[i
].short_desc
);
1476 SERVER_EXPORT
Jack::JackDriverClientInterface
* driver_initialize(Jack::JackLockedEngine
* engine
, Jack::JackSynchro
* table
, const JSList
* params
)
1478 jack_nframes_t srate
= 44100;
1479 jack_nframes_t frames_per_interrupt
= 128;
1480 int capture
= FALSE
;
1481 int playback
= FALSE
;
1484 bool monitor
= false;
1485 const char* capture_driver_uid
= "";
1486 const char* playback_driver_uid
= "";
1488 const jack_driver_param_t
*param
;
1489 jack_nframes_t systemic_input_latency
= 0;
1490 jack_nframes_t systemic_output_latency
= 0;
1491 int async_output_latency
= 100;
1492 int computation_grain
= -1;
1494 for (node
= params
; node
; node
= jack_slist_next(node
)) {
1495 param
= (const jack_driver_param_t
*) node
->data
;
1497 switch (param
->character
) {
1500 capture_driver_uid
= strdup(param
->value
.str
);
1501 playback_driver_uid
= strdup(param
->value
.str
);
1510 chan_in
= chan_out
= (int) param
->value
.ui
;
1514 chan_in
= (int) param
->value
.ui
;
1518 chan_out
= (int) param
->value
.ui
;
1523 if (strcmp(param
->value
.str
, "none") != 0) {
1524 capture_driver_uid
= strdup(param
->value
.str
);
1530 if (strcmp(param
->value
.str
, "none") != 0) {
1531 playback_driver_uid
= strdup(param
->value
.str
);
1536 monitor
= param
->value
.i
;
1540 srate
= param
->value
.ui
;
1544 frames_per_interrupt
= (unsigned int) param
->value
.ui
;
1548 systemic_input_latency
= param
->value
.ui
;
1552 systemic_output_latency
= param
->value
.ui
;
1556 Jack::DisplayDeviceNames();
1560 async_output_latency
= param
->value
.ui
;
1564 computation_grain
= param
->value
.ui
;
1569 /* duplex is the default */
1570 if (!capture
&& !playback
) {
1575 Jack::JackCoreAudioDriver
* driver
= new Jack::JackCoreAudioDriver("system", "coreaudio", engine
, table
);
1576 if (driver
->Open(frames_per_interrupt
, srate
, capture
, playback
, chan_in
, chan_out
, monitor
, capture_driver_uid
,
1577 playback_driver_uid
, systemic_input_latency
, systemic_output_latency
, async_output_latency
, computation_grain
) == 0) {