jack1 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1208 lines
42KB

  1. /*
  2. Copyright � Grame, 2003.
  3. Copyright � Johnny Petrantoni, 2003.
  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.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. Grame Research Laboratory, 9, rue du Garet 69001 Lyon - France
  16. grame@rd.grame.fr
  17. Johnny Petrantoni, johnny@lato-b.com - Italy, Rome.
  18. Jan 30, 2004: Johnny Petrantoni: first code of the coreaudio driver, based on portaudio driver by Stephane Letz.
  19. Feb 02, 2004: Johnny Petrantoni: fixed null cycle, removed double copy of buffers in AudioRender, the driver works fine (tested with Built-in Audio and Hammerfall RME), but no cpu load is displayed.
  20. Feb 03, 2004: Johnny Petrantoni: some little fix.
  21. Feb 03, 2004: Stephane Letz: some fix in AudioRender.cpp code.
  22. Feb 03, 2004: Johnny Petrantoni: removed the default device stuff (useless, in jackosx, because JackPilot manages this behavior), the device must be specified. and all parameter must be correct.
  23. Feb 04, 2004: Johnny Petrantoni: now the driver supports interfaces with multiple interleaved streams (such as the MOTU 828).
  24. Nov 05, 2004: S.Letz: correct management of -I option for use with JackPilot.
  25. Nov 15, 2004: S.Letz: Set a default value for deviceID.
  26. Nov 30, 2004: S.Letz: In coreaudio_driver_write : clear to avoid playing dirty buffers when the client does not produce output anymore.
  27. Dec 05, 2004: S.Letz: XRun detection
  28. Dec 09, 2004: S.Letz: Dynamic buffer size change
  29. Dec 23, 2004: S.Letz: Correct bug in dynamic buffer size change : update period_usecs
  30. Jan 20, 2005: S.Letz: Almost complete rewrite using AUHAL.
  31. May 20, 2005: S.Letz: Add "systemic" latencies management.
  32. Jun 06, 2005: S.Letz: Remove the "-I" parameter, change the semantic of "-n" parameter : -n (driver name) now correctly uses the PropertyDeviceUID
  33. (persistent accross reboot...) as the identifier for the used coreaudio driver.
  34. Jun 14, 2005: S.Letz: Since the "-I" parameter is not used anymore, rename the "systemic" latencies management parametes "-I" and "-O" like for the ALSA driver.
  35. Aug 16, 2005: S.Letz: Remove get_device_id_from_num, use get_default_device instead. If the -n option is not used or the device name cannot
  36. be found, the default device is used. Note: the default device can be used only if both default input and default output are the same.
  37. Dec 19, 2005: S.Letz: Add -d option (display_device_names).
  38. Apri 7, 2006: S.Letz: Synchronization with the jackdmp coreaudio driver version: improve half-duplex management.
  39. May 17, 2006: S.Letz: Minor fix in driver_initialize.
  40. May 18, 2006: S.Letz: Document sample rate default value.
  41. May 31, 2006: S.Letz: Apply Rui patch for more consistent driver parameter naming.
  42. Dec 04, 2007: S.Letz: Fix a bug in sample rate management (occuring in particular with "aggregate" devices).
  43. Dec 05, 2007: S.Letz: Correct sample_rate management in Open. Better handling in sample_rate change listener.
  44. */
  45. #include <stdio.h>
  46. #include <string.h>
  47. #include <jack/engine.h>
  48. #include "coreaudio_driver.h"
  49. const int CAVersion = 3;
  50. //#define PRINTDEBUG 1
  51. static void JCALog(char *fmt, ...)
  52. {
  53. #ifdef PRINTDEBUG
  54. va_list ap;
  55. va_start(ap, fmt);
  56. fprintf(stderr, "JCA: ");
  57. vfprintf(stderr, fmt, ap);
  58. va_end(ap);
  59. #endif
  60. }
  61. static void printError(OSStatus err)
  62. {
  63. #ifdef DEBUG
  64. switch (err) {
  65. case kAudioHardwareNoError:
  66. JCALog("error code : kAudioHardwareNoError\n");
  67. break;
  68. case kAudioHardwareNotRunningError:
  69. JCALog("error code : kAudioHardwareNotRunningError\n");
  70. break;
  71. case kAudioHardwareUnspecifiedError:
  72. JCALog("error code : kAudioHardwareUnspecifiedError\n");
  73. break;
  74. case kAudioHardwareUnknownPropertyError:
  75. JCALog("error code : kAudioHardwareUnknownPropertyError\n");
  76. break;
  77. case kAudioHardwareBadPropertySizeError:
  78. JCALog("error code : kAudioHardwareBadPropertySizeError\n");
  79. break;
  80. case kAudioHardwareIllegalOperationError:
  81. JCALog("error code : kAudioHardwareIllegalOperationError\n");
  82. break;
  83. case kAudioHardwareBadDeviceError:
  84. JCALog("error code : kAudioHardwareBadDeviceError\n");
  85. break;
  86. case kAudioHardwareBadStreamError:
  87. JCALog("error code : kAudioHardwareBadStreamError\n");
  88. break;
  89. case kAudioDeviceUnsupportedFormatError:
  90. JCALog("error code : kAudioDeviceUnsupportedFormatError\n");
  91. break;
  92. case kAudioDevicePermissionsError:
  93. JCALog("error code : kAudioDevicePermissionsError\n");
  94. break;
  95. default:
  96. JCALog("error code : unknown %ld\n", err);
  97. break;
  98. }
  99. #endif
  100. }
  101. static OSStatus get_device_name_from_id(AudioDeviceID id, char name[256])
  102. {
  103. UInt32 size = sizeof(char) * 256;
  104. OSStatus res = AudioDeviceGetProperty(id, 0, false,
  105. kAudioDevicePropertyDeviceName,
  106. &size,
  107. &name[0]);
  108. return res;
  109. }
  110. static OSStatus get_device_id_from_uid(char* UID, AudioDeviceID* id)
  111. {
  112. UInt32 size = sizeof(AudioValueTranslation);
  113. CFStringRef inIUD = CFStringCreateWithCString(NULL, UID, CFStringGetSystemEncoding());
  114. AudioValueTranslation value = { &inIUD, sizeof(CFStringRef), id, sizeof(AudioDeviceID) };
  115. if (inIUD == NULL) {
  116. return kAudioHardwareUnspecifiedError;
  117. } else {
  118. OSStatus res = AudioHardwareGetProperty(kAudioHardwarePropertyDeviceForUID, &size, &value);
  119. CFRelease(inIUD);
  120. JCALog("get_device_id_from_uid %s %ld \n", UID, *id);
  121. return (*id == kAudioDeviceUnknown) ? kAudioHardwareBadDeviceError : res;
  122. }
  123. }
  124. static OSStatus get_default_device(AudioDeviceID * id)
  125. {
  126. OSStatus res;
  127. UInt32 theSize = sizeof(UInt32);
  128. AudioDeviceID inDefault;
  129. AudioDeviceID outDefault;
  130. if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice,
  131. &theSize, &inDefault)) != noErr)
  132. return res;
  133. if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice,
  134. &theSize, &outDefault)) != noErr)
  135. return res;
  136. JCALog("get_default_device: input %ld output %ld\n", inDefault, outDefault);
  137. // Get the device only if default input and ouput are the same
  138. if (inDefault == outDefault) {
  139. *id = inDefault;
  140. return noErr;
  141. } else {
  142. jack_error("Default input and output devices are not the same !!");
  143. return kAudioHardwareBadDeviceError;
  144. }
  145. }
  146. static OSStatus get_default_input_device(AudioDeviceID* id)
  147. {
  148. OSStatus res;
  149. UInt32 theSize = sizeof(UInt32);
  150. AudioDeviceID inDefault;
  151. if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultInputDevice,
  152. &theSize, &inDefault)) != noErr)
  153. return res;
  154. JCALog("get_default_input_device: input = %ld \n", inDefault);
  155. *id = inDefault;
  156. return noErr;
  157. }
  158. static OSStatus get_default_output_device(AudioDeviceID* id)
  159. {
  160. OSStatus res;
  161. UInt32 theSize = sizeof(UInt32);
  162. AudioDeviceID outDefault;
  163. if ((res = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice,
  164. &theSize, &outDefault)) != noErr)
  165. return res;
  166. JCALog("get_default_output_device: output = %ld\n", outDefault);
  167. *id = outDefault;
  168. return noErr;
  169. }
  170. OSStatus get_total_channels(AudioDeviceID device, int* channelCount, bool isInput)
  171. {
  172. OSStatus err = noErr;
  173. UInt32 outSize;
  174. Boolean outWritable;
  175. AudioBufferList* bufferList = 0;
  176. AudioStreamID* streamList = 0;
  177. int i, numStream;
  178. err = AudioDeviceGetPropertyInfo(device, 0, isInput, kAudioDevicePropertyStreams, &outSize, &outWritable);
  179. if (err == noErr) {
  180. streamList = (AudioStreamID*)malloc(outSize);
  181. numStream = outSize/sizeof(AudioStreamID);
  182. JCALog("get_total_channels device stream number = %ld numStream = %ld\n", device, numStream);
  183. err = AudioDeviceGetProperty(device, 0, isInput, kAudioDevicePropertyStreams, &outSize, streamList);
  184. if (err == noErr) {
  185. AudioStreamBasicDescription streamDesc;
  186. outSize = sizeof(AudioStreamBasicDescription);
  187. for (i = 0; i < numStream; i++) {
  188. err = AudioStreamGetProperty(streamList[i], 0, kAudioDevicePropertyStreamFormat, &outSize, &streamDesc);
  189. JCALog("get_total_channels streamDesc mFormatFlags = %ld mChannelsPerFrame = %ld\n", streamDesc.mFormatFlags, streamDesc.mChannelsPerFrame);
  190. }
  191. }
  192. }
  193. *channelCount = 0;
  194. err = AudioDeviceGetPropertyInfo(device, 0, isInput, kAudioDevicePropertyStreamConfiguration, &outSize, &outWritable);
  195. if (err == noErr) {
  196. bufferList = (AudioBufferList*)malloc(outSize);
  197. err = AudioDeviceGetProperty(device, 0, isInput, kAudioDevicePropertyStreamConfiguration, &outSize, bufferList);
  198. if (err == noErr) {
  199. for (i = 0; i < bufferList->mNumberBuffers; i++)
  200. *channelCount += bufferList->mBuffers[i].mNumberChannels;
  201. }
  202. }
  203. if (streamList)
  204. free(streamList);
  205. if (bufferList)
  206. free(bufferList);
  207. return err;
  208. }
  209. static OSStatus display_device_names()
  210. {
  211. UInt32 size;
  212. Boolean isWritable;
  213. int i, deviceNum;
  214. OSStatus err;
  215. CFStringRef UIname;
  216. err = AudioHardwareGetPropertyInfo(kAudioHardwarePropertyDevices, &size, &isWritable);
  217. if (err != noErr)
  218. return err;
  219. deviceNum = size/sizeof(AudioDeviceID);
  220. AudioDeviceID devices[deviceNum];
  221. err = AudioHardwareGetProperty(kAudioHardwarePropertyDevices, &size, devices);
  222. if (err != noErr)
  223. return err;
  224. for (i = 0; i < deviceNum; i++) {
  225. char device_name[256];
  226. char internal_name[256];
  227. size = sizeof(CFStringRef);
  228. UIname = NULL;
  229. err = AudioDeviceGetProperty(devices[i], 0, false, kAudioDevicePropertyDeviceUID, &size, &UIname);
  230. if (err == noErr) {
  231. CFStringGetCString(UIname, internal_name, 256, CFStringGetSystemEncoding());
  232. } else {
  233. goto error;
  234. }
  235. size = 256;
  236. err = AudioDeviceGetProperty(devices[i], 0, false, kAudioDevicePropertyDeviceName, &size, device_name);
  237. if (err != noErr)
  238. return err;
  239. jack_info("ICI");
  240. jack_info("Device name = \'%s\', internal_name = \'%s\' (to be used as -d parameter)", device_name, internal_name);
  241. }
  242. return noErr;
  243. error:
  244. if (UIname != NULL)
  245. CFRelease(UIname);
  246. return err;
  247. }
  248. static OSStatus render(void *inRefCon,
  249. AudioUnitRenderActionFlags *ioActionFlags,
  250. const AudioTimeStamp *inTimeStamp,
  251. UInt32 inBusNumber,
  252. UInt32 inNumberFrames,
  253. AudioBufferList *ioData)
  254. {
  255. int res, i;
  256. JSList *node;
  257. coreaudio_driver_t* ca_driver = (coreaudio_driver_t*)inRefCon;
  258. AudioUnitRender(ca_driver->au_hal, ioActionFlags, inTimeStamp, 1, inNumberFrames, ca_driver->input_list);
  259. if (ca_driver->xrun_detected > 0) { /* XRun was detected */
  260. jack_time_t current_time = jack_get_microseconds();
  261. ca_driver->engine->delay(ca_driver->engine, current_time -
  262. (ca_driver->last_wait_ust + ca_driver->period_usecs));
  263. ca_driver->last_wait_ust = current_time;
  264. ca_driver->xrun_detected = 0;
  265. return 0;
  266. } else {
  267. ca_driver->last_wait_ust = jack_get_microseconds();
  268. ca_driver->engine->transport_cycle_start(ca_driver->engine,
  269. jack_get_microseconds());
  270. res = ca_driver->engine->run_cycle(ca_driver->engine, inNumberFrames, 0);
  271. }
  272. if (ca_driver->null_cycle_occured) {
  273. ca_driver->null_cycle_occured = 0;
  274. for (i = 0; i < ca_driver->playback_nchannels; i++) {
  275. memset((float*)ioData->mBuffers[i].mData, 0, sizeof(float) * inNumberFrames);
  276. }
  277. } else {
  278. for (i = 0, node = ca_driver->playback_ports; i < ca_driver->playback_nchannels; i++, node = jack_slist_next(node)) {
  279. memcpy((float*)ioData->mBuffers[i].mData,
  280. (jack_default_audio_sample_t*)jack_port_get_buffer(((jack_port_t *) node->data), inNumberFrames),
  281. sizeof(float) * inNumberFrames);
  282. }
  283. }
  284. return res;
  285. }
  286. static OSStatus render_input(void *inRefCon,
  287. AudioUnitRenderActionFlags *ioActionFlags,
  288. const AudioTimeStamp *inTimeStamp,
  289. UInt32 inBusNumber,
  290. UInt32 inNumberFrames,
  291. AudioBufferList *ioData)
  292. {
  293. coreaudio_driver_t* ca_driver = (coreaudio_driver_t*)inRefCon;
  294. AudioUnitRender(ca_driver->au_hal, ioActionFlags, inTimeStamp, 1, inNumberFrames, ca_driver->input_list);
  295. if (ca_driver->xrun_detected > 0) { /* XRun was detected */
  296. jack_time_t current_time = jack_get_microseconds();
  297. ca_driver->engine->delay(ca_driver->engine, current_time -
  298. (ca_driver->last_wait_ust + ca_driver->period_usecs));
  299. ca_driver->last_wait_ust = current_time;
  300. ca_driver->xrun_detected = 0;
  301. return 0;
  302. } else {
  303. ca_driver->last_wait_ust = jack_get_microseconds();
  304. ca_driver->engine->transport_cycle_start(ca_driver->engine,
  305. jack_get_microseconds());
  306. return ca_driver->engine->run_cycle(ca_driver->engine, inNumberFrames, 0);
  307. }
  308. }
  309. static OSStatus sr_notification(AudioDeviceID inDevice,
  310. UInt32 inChannel,
  311. Boolean isInput,
  312. AudioDevicePropertyID inPropertyID,
  313. void* inClientData)
  314. {
  315. coreaudio_driver_t* driver = (coreaudio_driver_t*)inClientData;
  316. switch (inPropertyID) {
  317. case kAudioDevicePropertyNominalSampleRate: {
  318. JCALog("JackCoreAudioDriver::SRNotificationCallback kAudioDevicePropertyNominalSampleRate \n");
  319. driver->state = 1;
  320. break;
  321. }
  322. }
  323. return noErr;
  324. }
  325. static OSStatus notification(AudioDeviceID inDevice,
  326. UInt32 inChannel,
  327. Boolean isInput,
  328. AudioDevicePropertyID inPropertyID,
  329. void* inClientData)
  330. {
  331. coreaudio_driver_t* driver = (coreaudio_driver_t*)inClientData;
  332. switch (inPropertyID) {
  333. case kAudioDeviceProcessorOverload:
  334. driver->xrun_detected = 1;
  335. break;
  336. case kAudioDevicePropertyNominalSampleRate: {
  337. UInt32 outSize = sizeof(Float64);
  338. Float64 sampleRate;
  339. AudioStreamBasicDescription srcFormat, dstFormat;
  340. OSStatus err = AudioDeviceGetProperty(driver->device_id, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyNominalSampleRate, &outSize, &sampleRate);
  341. if (err != noErr) {
  342. jack_error("Cannot get current sample rate");
  343. return kAudioHardwareUnsupportedOperationError;
  344. }
  345. JCALog("JackCoreAudioDriver::NotificationCallback kAudioDevicePropertyNominalSampleRate %ld\n", (long)sampleRate);
  346. outSize = sizeof(AudioStreamBasicDescription);
  347. // Update SR for input
  348. err = AudioUnitGetProperty(driver->au_hal, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &srcFormat, &outSize);
  349. if (err != noErr) {
  350. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Input");
  351. }
  352. srcFormat.mSampleRate = sampleRate;
  353. err = AudioUnitSetProperty(driver->au_hal, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &srcFormat, outSize);
  354. if (err != noErr) {
  355. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Input");
  356. }
  357. // Update SR for output
  358. err = AudioUnitGetProperty(driver->au_hal, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &dstFormat, &outSize);
  359. if (err != noErr) {
  360. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Output");
  361. }
  362. dstFormat.mSampleRate = sampleRate;
  363. err = AudioUnitSetProperty(driver->au_hal, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &dstFormat, outSize);
  364. if (err != noErr) {
  365. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Output");
  366. }
  367. break;
  368. }
  369. }
  370. return noErr;
  371. }
  372. static int
  373. coreaudio_driver_attach(coreaudio_driver_t * driver, jack_engine_t * engine)
  374. {
  375. jack_port_t *port;
  376. JSList *node;
  377. int port_flags;
  378. channel_t chn;
  379. char buf[JACK_PORT_NAME_SIZE];
  380. char channel_name[64];
  381. OSStatus err;
  382. UInt32 size;
  383. UInt32 value1,value2;
  384. Boolean isWritable;
  385. driver->engine = engine;
  386. driver->engine->set_buffer_size(engine, driver->frames_per_cycle);
  387. driver->engine->set_sample_rate(engine, driver->frame_rate);
  388. port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
  389. /*
  390. if (driver->has_hw_monitoring) {
  391. port_flags |= JackPortCanMonitor;
  392. }
  393. */
  394. for (chn = 0; chn < driver->capture_nchannels; chn++) {
  395. err = AudioDeviceGetPropertyInfo(driver->device_id, chn + 1, true, kAudioDevicePropertyChannelName, &size, &isWritable);
  396. if (err == noErr && size > 0) {
  397. err = AudioDeviceGetProperty(driver->device_id, chn + 1, true, kAudioDevicePropertyChannelName, &size, channel_name);
  398. if (err != noErr)
  399. JCALog("AudioDeviceGetProperty kAudioDevicePropertyChannelName error \n");
  400. snprintf(buf, sizeof(buf) - 1, "%s:out_%s%lu", driver->capture_driver_name, channel_name, chn + 1);
  401. } else {
  402. snprintf(buf, sizeof(buf) - 1, "%s:out%lu", driver->capture_driver_name, chn + 1);
  403. }
  404. if ((port = jack_port_register(driver->client, buf,
  405. JACK_DEFAULT_AUDIO_TYPE, port_flags,
  406. 0)) == NULL) {
  407. jack_error("coreaudio: cannot register port for %s", buf);
  408. break;
  409. }
  410. size = sizeof(UInt32);
  411. value1 = value2 = 0;
  412. err = AudioDeviceGetProperty(driver->device_id, 0, true, kAudioDevicePropertyLatency, &size, &value1);
  413. if (err != noErr)
  414. JCALog("AudioDeviceGetProperty kAudioDevicePropertyLatency error \n");
  415. err = AudioDeviceGetProperty(driver->device_id, 0, true, kAudioDevicePropertySafetyOffset, &size, &value2);
  416. if (err != noErr)
  417. JCALog("AudioDeviceGetProperty kAudioDevicePropertySafetyOffset error \n");
  418. jack_port_set_latency(port, driver->frames_per_cycle + value1 + value2 + driver->capture_frame_latency);
  419. driver->capture_ports =
  420. jack_slist_append(driver->capture_ports, port);
  421. }
  422. port_flags = JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal;
  423. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  424. err = AudioDeviceGetPropertyInfo(driver->device_id, chn + 1, false, kAudioDevicePropertyChannelName, &size, &isWritable);
  425. if (err == noErr && size > 0) {
  426. err = AudioDeviceGetProperty(driver->device_id, chn + 1, false, kAudioDevicePropertyChannelName, &size, channel_name);
  427. if (err != noErr)
  428. JCALog("AudioDeviceGetProperty kAudioDevicePropertyChannelName error \n");
  429. snprintf(buf, sizeof(buf) - 1, "%s:in_%s%lu", driver->playback_driver_name, channel_name, chn + 1);
  430. } else {
  431. snprintf(buf, sizeof(buf) - 1, "%s:in%lu", driver->playback_driver_name, chn + 1);
  432. }
  433. if ((port = jack_port_register(driver->client, buf,
  434. JACK_DEFAULT_AUDIO_TYPE, port_flags,
  435. 0)) == NULL) {
  436. jack_error("coreaudio: cannot register port for %s", buf);
  437. break;
  438. }
  439. size = sizeof(UInt32);
  440. value1 = value2 = 0;
  441. err = AudioDeviceGetProperty(driver->device_id, 0, false, kAudioDevicePropertyLatency, &size, &value1);
  442. if (err != noErr)
  443. JCALog("AudioDeviceGetProperty kAudioDevicePropertyLatency error \n");
  444. err = AudioDeviceGetProperty(driver->device_id, 0, false, kAudioDevicePropertySafetyOffset, &size, &value2);
  445. if (err != noErr)
  446. JCALog("AudioDeviceGetProperty kAudioDevicePropertySafetyOffset error \n");
  447. jack_port_set_latency(port, driver->frames_per_cycle + value1 + value2 + driver->playback_frame_latency);
  448. driver->playback_ports =
  449. jack_slist_append(driver->playback_ports, port);
  450. }
  451. // Input buffers do no change : prepare them only once
  452. for (chn = 0, node = driver->capture_ports; chn < driver->capture_nchannels; chn++, node = jack_slist_next(node)) {
  453. driver->input_list->mBuffers[chn].mData
  454. = (jack_default_audio_sample_t*)jack_port_get_buffer(((jack_port_t *) node->data), driver->frames_per_cycle);
  455. }
  456. jack_activate(driver->client);
  457. return 0;
  458. }
  459. static int
  460. coreaudio_driver_detach(coreaudio_driver_t * driver, jack_engine_t * engine)
  461. {
  462. JSList *node;
  463. if (driver->engine == 0) {
  464. return -1;
  465. }
  466. for (node = driver->capture_ports; node; node = jack_slist_next(node)) {
  467. jack_port_unregister(driver->client, ((jack_port_t *) node->data));
  468. }
  469. jack_slist_free(driver->capture_ports);
  470. driver->capture_ports = 0;
  471. for (node = driver->playback_ports; node; node = jack_slist_next(node)) {
  472. jack_port_unregister(driver->client, ((jack_port_t *) node->data));
  473. }
  474. jack_slist_free(driver->playback_ports);
  475. driver->playback_ports = 0;
  476. driver->engine = 0;
  477. return 0;
  478. }
  479. static int
  480. coreaudio_driver_null_cycle(coreaudio_driver_t * driver, jack_nframes_t nframes)
  481. {
  482. driver->null_cycle_occured = 1;
  483. return 0;
  484. }
  485. static int
  486. coreaudio_driver_read(coreaudio_driver_t * driver, jack_nframes_t nframes)
  487. {
  488. return 0;
  489. }
  490. static int
  491. coreaudio_driver_write(coreaudio_driver_t * driver, jack_nframes_t nframes)
  492. {
  493. return 0;
  494. }
  495. static int coreaudio_driver_audio_start(coreaudio_driver_t * driver)
  496. {
  497. return (AudioOutputUnitStart(driver->au_hal) == noErr) ? 0 : -1;
  498. }
  499. static int coreaudio_driver_audio_stop(coreaudio_driver_t * driver)
  500. {
  501. return (AudioOutputUnitStop(driver->au_hal) == noErr) ? 0 : -1;
  502. }
  503. static int
  504. coreaudio_driver_bufsize(coreaudio_driver_t * driver,
  505. jack_nframes_t nframes)
  506. {
  507. /* This gets called from the engine server thread, so it must
  508. * be serialized with the driver thread. Stopping the audio
  509. * also stops that thread. */
  510. /*
  511. TO DO
  512. */
  513. return 0;
  514. }
  515. /** create a new driver instance
  516. */
  517. static jack_driver_t *coreaudio_driver_new(char* name,
  518. jack_client_t* client,
  519. jack_nframes_t nframes,
  520. jack_nframes_t samplerate,
  521. int capturing,
  522. int playing,
  523. int inchannels,
  524. int outchannels,
  525. char* capture_driver_uid,
  526. char* playback_driver_uid,
  527. jack_nframes_t capture_latency,
  528. jack_nframes_t playback_latency)
  529. {
  530. coreaudio_driver_t *driver;
  531. OSStatus err = noErr;
  532. ComponentResult err1;
  533. UInt32 outSize;
  534. UInt32 enableIO;
  535. AudioStreamBasicDescription srcFormat, dstFormat;
  536. Float64 sampleRate;
  537. int in_nChannels = 0;
  538. int out_nChannels = 0;
  539. int i;
  540. driver = (coreaudio_driver_t *) calloc(1, sizeof(coreaudio_driver_t));
  541. jack_driver_init((jack_driver_t *) driver);
  542. if (!jack_power_of_two(nframes)) {
  543. jack_error("CA: -p must be a power of two.");
  544. goto error;
  545. }
  546. driver->state = 0;
  547. driver->frames_per_cycle = nframes;
  548. driver->frame_rate = samplerate;
  549. driver->capturing = capturing;
  550. driver->playing = playing;
  551. driver->xrun_detected = 0;
  552. driver->null_cycle = 0;
  553. driver->attach = (JackDriverAttachFunction) coreaudio_driver_attach;
  554. driver->detach = (JackDriverDetachFunction) coreaudio_driver_detach;
  555. driver->read = (JackDriverReadFunction) coreaudio_driver_read;
  556. driver->write = (JackDriverReadFunction) coreaudio_driver_write;
  557. driver->null_cycle =
  558. (JackDriverNullCycleFunction) coreaudio_driver_null_cycle;
  559. driver->bufsize = (JackDriverBufSizeFunction) coreaudio_driver_bufsize;
  560. driver->start = (JackDriverStartFunction) coreaudio_driver_audio_start;
  561. driver->stop = (JackDriverStopFunction) coreaudio_driver_audio_stop;
  562. driver->capture_frame_latency = capture_latency;
  563. driver->playback_frame_latency = playback_latency;
  564. // Duplex
  565. if (strcmp(capture_driver_uid, "") != 0 && strcmp(playback_driver_uid, "") != 0) {
  566. JCALog("Open duplex \n");
  567. if (get_device_id_from_uid(playback_driver_uid, &driver->device_id) != noErr) {
  568. if (get_default_device(&driver->device_id) != noErr) {
  569. jack_error("Cannot open default device");
  570. goto error;
  571. }
  572. }
  573. if (get_device_name_from_id(driver->device_id, driver->capture_driver_name) != noErr || get_device_name_from_id(driver->device_id, driver->playback_driver_name) != noErr) {
  574. jack_error("Cannot get device name from device ID");
  575. goto error;
  576. }
  577. // Capture only
  578. } else if (strcmp(capture_driver_uid, "") != 0) {
  579. JCALog("Open capture only \n");
  580. if (get_device_id_from_uid(capture_driver_uid, &driver->device_id) != noErr) {
  581. if (get_default_input_device(&driver->device_id) != noErr) {
  582. jack_error("Cannot open default device");
  583. goto error;
  584. }
  585. }
  586. if (get_device_name_from_id(driver->device_id, driver->capture_driver_name) != noErr) {
  587. jack_error("Cannot get device name from device ID");
  588. goto error;
  589. }
  590. // Playback only
  591. } else if (playback_driver_uid != NULL) {
  592. JCALog("Open playback only \n");
  593. if (get_device_id_from_uid(playback_driver_uid, &driver->device_id) != noErr) {
  594. if (get_default_output_device(&driver->device_id) != noErr) {
  595. jack_error("Cannot open default device");
  596. goto error;
  597. }
  598. }
  599. if (get_device_name_from_id(driver->device_id, driver->playback_driver_name) != noErr) {
  600. jack_error("Cannot get device name from device ID");
  601. goto error;
  602. }
  603. // Use default driver in duplex mode
  604. } else {
  605. JCALog("Open default driver \n");
  606. if (get_default_device(&driver->device_id) != noErr) {
  607. jack_error("Cannot open default device");
  608. goto error;
  609. }
  610. if (get_device_name_from_id(driver->device_id, driver->capture_driver_name) != noErr || get_device_name_from_id(driver->device_id, driver->playback_driver_name) != noErr) {
  611. jack_error("Cannot get device name from device ID");
  612. goto error;
  613. }
  614. }
  615. driver->client = client;
  616. driver->period_usecs =
  617. (((float) driver->frames_per_cycle) / driver->frame_rate) *
  618. 1000000.0f;
  619. if (capturing) {
  620. err = get_total_channels(driver->device_id, &in_nChannels, true);
  621. if (err != noErr) {
  622. jack_error("Cannot get input channel number");
  623. printError(err);
  624. goto error;
  625. }
  626. }
  627. if (playing) {
  628. err = get_total_channels(driver->device_id, &out_nChannels, false);
  629. if (err != noErr) {
  630. jack_error("Cannot get output channel number");
  631. printError(err);
  632. goto error;
  633. }
  634. }
  635. if (inchannels > in_nChannels) {
  636. jack_error("This device hasn't required input channels inchannels = %ld in_nChannels = %ld", inchannels, in_nChannels);
  637. goto error;
  638. }
  639. if (outchannels > out_nChannels) {
  640. jack_error("This device hasn't required output channels outchannels = %ld out_nChannels = %ld", outchannels, out_nChannels);
  641. goto error;
  642. }
  643. if (inchannels == 0) {
  644. JCALog("Setup max in channels = %ld\n", in_nChannels);
  645. inchannels = in_nChannels;
  646. }
  647. if (outchannels == 0) {
  648. JCALog("Setup max out channels = %ld\n", out_nChannels);
  649. outchannels = out_nChannels;
  650. }
  651. // Setting buffer size
  652. outSize = sizeof(UInt32);
  653. err = AudioDeviceSetProperty(driver->device_id, NULL, 0, false, kAudioDevicePropertyBufferFrameSize, outSize, &nframes);
  654. if (err != noErr) {
  655. jack_error("Cannot set buffer size %ld", nframes);
  656. printError(err);
  657. goto error;
  658. }
  659. // Set sample rate
  660. outSize = sizeof(Float64);
  661. err = AudioDeviceGetProperty(driver->device_id, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyNominalSampleRate, &outSize, &sampleRate);
  662. if (err != noErr) {
  663. jack_error("Cannot get current sample rate");
  664. printError(err);
  665. goto error;
  666. }
  667. if (samplerate != (jack_nframes_t)sampleRate) {
  668. sampleRate = (Float64)samplerate;
  669. // To get SR change notification
  670. err = AudioDeviceAddPropertyListener(driver->device_id, 0, true, kAudioDevicePropertyNominalSampleRate, sr_notification, driver);
  671. if (err != noErr) {
  672. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyNominalSampleRate");
  673. printError(err);
  674. return -1;
  675. }
  676. err = AudioDeviceSetProperty(driver->device_id, NULL, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyNominalSampleRate, outSize, &sampleRate);
  677. if (err != noErr) {
  678. jack_error("Cannot set sample rate = %ld", samplerate);
  679. printError(err);
  680. return -1;
  681. }
  682. // Waiting for SR change notification
  683. int count = 0;
  684. while (!driver->state && count++ < 100) {
  685. usleep(100000);
  686. JCALog("Wait count = %ld\n", count);
  687. }
  688. // Remove SR change notification
  689. AudioDeviceRemovePropertyListener(driver->device_id, 0, true, kAudioDevicePropertyNominalSampleRate, sr_notification);
  690. }
  691. // AUHAL
  692. ComponentDescription cd = {kAudioUnitType_Output, kAudioUnitSubType_HALOutput, kAudioUnitManufacturer_Apple, 0, 0};
  693. Component HALOutput = FindNextComponent(NULL, &cd);
  694. err1 = OpenAComponent(HALOutput, &driver->au_hal);
  695. if (err1 != noErr) {
  696. jack_error("Error calling OpenAComponent");
  697. printError(err1);
  698. goto error;
  699. }
  700. err1 = AudioUnitInitialize(driver->au_hal);
  701. if (err1 != noErr) {
  702. jack_error("Cannot initialize AUHAL unit");
  703. printError(err1);
  704. goto error;
  705. }
  706. // Start I/O
  707. enableIO = 1;
  708. if (capturing && inchannels > 0) {
  709. JCALog("Setup AUHAL input\n");
  710. err1 = AudioUnitSetProperty(driver->au_hal, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, 1, &enableIO, sizeof(enableIO));
  711. if (err1 != noErr) {
  712. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input");
  713. printError(err1);
  714. goto error;
  715. }
  716. }
  717. if (playing && outchannels > 0) {
  718. JCALog("Setup AUHAL output\n");
  719. err1 = AudioUnitSetProperty(driver->au_hal, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, 0, &enableIO, sizeof(enableIO));
  720. if (err1 != noErr) {
  721. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_EnableIO,kAudioUnitScope_Output");
  722. printError(err1);
  723. goto error;
  724. }
  725. }
  726. // Setup up choosen device, in both input and output cases
  727. err1 = AudioUnitSetProperty(driver->au_hal, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &driver->device_id, sizeof(AudioDeviceID));
  728. if (err1 != noErr) {
  729. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_CurrentDevice");
  730. printError(err1);
  731. goto error;
  732. }
  733. // Set buffer size
  734. if (capturing && inchannels > 0) {
  735. err1 = AudioUnitSetProperty(driver->au_hal, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 1, (UInt32*)&nframes, sizeof(UInt32));
  736. if (err1 != noErr) {
  737. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_MaximumFramesPerSlice");
  738. printError(err1);
  739. goto error;
  740. }
  741. }
  742. if (playing && outchannels > 0) {
  743. err1 = AudioUnitSetProperty(driver->au_hal, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 0, (UInt32*)&nframes, sizeof(UInt32));
  744. if (err1 != noErr) {
  745. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_MaximumFramesPerSlice");
  746. printError(err1);
  747. goto error;
  748. }
  749. }
  750. // Setup channel map
  751. if (capturing && inchannels > 0 && inchannels < in_nChannels) {
  752. SInt32 chanArr[in_nChannels];
  753. for (i = 0; i < in_nChannels; i++) {
  754. chanArr[i] = -1;
  755. }
  756. for (i = 0; i < inchannels; i++) {
  757. chanArr[i] = i;
  758. }
  759. AudioUnitSetProperty(driver->au_hal, kAudioOutputUnitProperty_ChannelMap , kAudioUnitScope_Input, 1, chanArr, sizeof(SInt32) * in_nChannels);
  760. if (err1 != noErr) {
  761. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_ChannelMap 1");
  762. printError(err1);
  763. }
  764. }
  765. if (playing && outchannels > 0 && outchannels < out_nChannels) {
  766. SInt32 chanArr[out_nChannels];
  767. for (i = 0; i < out_nChannels; i++) {
  768. chanArr[i] = -1;
  769. }
  770. for (i = 0; i < outchannels; i++) {
  771. chanArr[i] = i;
  772. }
  773. err1 = AudioUnitSetProperty(driver->au_hal, kAudioOutputUnitProperty_ChannelMap, kAudioUnitScope_Output, 0, chanArr, sizeof(SInt32) * out_nChannels);
  774. if (err1 != noErr) {
  775. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_ChannelMap 0");
  776. printError(err1);
  777. }
  778. }
  779. // Setup stream converters
  780. srcFormat.mSampleRate = samplerate;
  781. srcFormat.mFormatID = kAudioFormatLinearPCM;
  782. srcFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kLinearPCMFormatFlagIsNonInterleaved;
  783. srcFormat.mBytesPerPacket = sizeof(float);
  784. srcFormat.mFramesPerPacket = 1;
  785. srcFormat.mBytesPerFrame = sizeof(float);
  786. srcFormat.mChannelsPerFrame = outchannels;
  787. srcFormat.mBitsPerChannel = 32;
  788. err1 = AudioUnitSetProperty(driver->au_hal, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &srcFormat, sizeof(AudioStreamBasicDescription));
  789. if (err1 != noErr) {
  790. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Input");
  791. printError(err1);
  792. }
  793. dstFormat.mSampleRate = samplerate;
  794. dstFormat.mFormatID = kAudioFormatLinearPCM;
  795. dstFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kLinearPCMFormatFlagIsNonInterleaved;
  796. dstFormat.mBytesPerPacket = sizeof(float);
  797. dstFormat.mFramesPerPacket = 1;
  798. dstFormat.mBytesPerFrame = sizeof(float);
  799. dstFormat.mChannelsPerFrame = inchannels;
  800. dstFormat.mBitsPerChannel = 32;
  801. err1 = AudioUnitSetProperty(driver->au_hal, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &dstFormat, sizeof(AudioStreamBasicDescription));
  802. if (err1 != noErr) {
  803. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Output");
  804. printError(err1);
  805. }
  806. // Setup callbacks
  807. if (inchannels > 0 && outchannels == 0) {
  808. AURenderCallbackStruct output;
  809. output.inputProc = render_input;
  810. output.inputProcRefCon = driver;
  811. err1 = AudioUnitSetProperty(driver->au_hal, kAudioOutputUnitProperty_SetInputCallback, kAudioUnitScope_Global, 0, &output, sizeof(output));
  812. if (err1 != noErr) {
  813. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_SetRenderCallback 1");
  814. printError(err1);
  815. goto error;
  816. }
  817. } else {
  818. AURenderCallbackStruct output;
  819. output.inputProc = render;
  820. output.inputProcRefCon = driver;
  821. err1 = AudioUnitSetProperty(driver->au_hal, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &output, sizeof(output));
  822. if (err1 != noErr) {
  823. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_SetRenderCallback 0");
  824. printError(err1);
  825. goto error;
  826. }
  827. }
  828. if (capturing && inchannels > 0) {
  829. driver->input_list = (AudioBufferList*)malloc(sizeof(UInt32) + inchannels * sizeof(AudioBuffer));
  830. if (driver->input_list == 0)
  831. goto error;
  832. driver->input_list->mNumberBuffers = inchannels;
  833. // Prepare buffers
  834. for (i = 0; i < driver->capture_nchannels; i++) {
  835. driver->input_list->mBuffers[i].mNumberChannels = 1;
  836. driver->input_list->mBuffers[i].mDataByteSize = nframes * sizeof(float);
  837. }
  838. }
  839. err = AudioDeviceAddPropertyListener(driver->device_id, 0, true, kAudioDeviceProcessorOverload, notification, driver);
  840. if (err != noErr) {
  841. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDeviceProcessorOverload");
  842. goto error;
  843. }
  844. err = AudioDeviceAddPropertyListener(driver->device_id, 0, true, kAudioDevicePropertyNominalSampleRate, notification, driver);
  845. if (err != noErr) {
  846. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyNominalSampleRate");
  847. goto error;
  848. }
  849. driver->playback_nchannels = outchannels;
  850. driver->capture_nchannels = inchannels;
  851. return ((jack_driver_t *) driver);
  852. error:
  853. AudioUnitUninitialize(driver->au_hal);
  854. CloseComponent(driver->au_hal);
  855. jack_error("Cannot open the coreaudio driver");
  856. free(driver);
  857. return NULL;
  858. }
  859. /** free all memory allocated by a driver instance
  860. */
  861. static void coreaudio_driver_delete(coreaudio_driver_t * driver)
  862. {
  863. AudioDeviceRemovePropertyListener(driver->device_id, 0, true, kAudioDeviceProcessorOverload, notification);
  864. free(driver->input_list);
  865. AudioUnitUninitialize(driver->au_hal);
  866. CloseComponent(driver->au_hal);
  867. free(driver);
  868. }
  869. //== driver "plugin" interface =================================================
  870. /* DRIVER "PLUGIN" INTERFACE */
  871. const char driver_client_name[] = "coreaudio";
  872. jack_driver_desc_t *driver_get_descriptor()
  873. {
  874. jack_driver_desc_t *desc;
  875. unsigned int i;
  876. desc = calloc(1, sizeof(jack_driver_desc_t));
  877. strcpy(desc->name, "coreaudio");
  878. desc->nparams = 12;
  879. desc->params = calloc(desc->nparams, sizeof(jack_driver_param_desc_t));
  880. i = 0;
  881. strcpy(desc->params[i].name, "channels");
  882. desc->params[i].character = 'c';
  883. desc->params[i].type = JackDriverParamInt;
  884. desc->params[i].value.ui = 2;
  885. strcpy(desc->params[i].short_desc, "Maximum number of channels");
  886. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  887. i++;
  888. strcpy(desc->params[i].name, "inchannels");
  889. desc->params[i].character = 'i';
  890. desc->params[i].type = JackDriverParamInt;
  891. desc->params[i].value.ui = 2;
  892. strcpy(desc->params[i].short_desc, "Maximum number of input channels");
  893. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  894. i++;
  895. strcpy(desc->params[i].name, "outchannels");
  896. desc->params[i].character = 'o';
  897. desc->params[i].type = JackDriverParamInt;
  898. desc->params[i].value.ui = 2;
  899. strcpy(desc->params[i].short_desc, "Maximum number of output channels");
  900. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  901. i++;
  902. strcpy(desc->params[i].name, "capture");
  903. desc->params[i].character = 'C';
  904. desc->params[i].type = JackDriverParamString;
  905. strcpy(desc->params[i].value.str, "will take default CoreAudio input device");
  906. strcpy(desc->params[i].short_desc, "Provide capture ports. Optionally set CoreAudio device name");
  907. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  908. i++;
  909. strcpy(desc->params[i].name, "playback");
  910. desc->params[i].character = 'P';
  911. desc->params[i].type = JackDriverParamString;
  912. strcpy(desc->params[i].value.str, "will take default CoreAudio output device");
  913. strcpy(desc->params[i].short_desc, "Provide playback ports. Optionally set CoreAudio device name");
  914. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  915. i++;
  916. strcpy(desc->params[i].name, "duplex");
  917. desc->params[i].character = 'D';
  918. desc->params[i].type = JackDriverParamBool;
  919. desc->params[i].value.i = TRUE;
  920. strcpy(desc->params[i].short_desc, "Capture and playback");
  921. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  922. i++;
  923. strcpy(desc->params[i].name, "rate");
  924. desc->params[i].character = 'r';
  925. desc->params[i].type = JackDriverParamUInt;
  926. desc->params[i].value.ui = 44100U;
  927. strcpy(desc->params[i].short_desc, "Sample rate");
  928. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  929. i++;
  930. strcpy(desc->params[i].name, "period");
  931. desc->params[i].character = 'p';
  932. desc->params[i].type = JackDriverParamUInt;
  933. desc->params[i].value.ui = 128U;
  934. strcpy(desc->params[i].short_desc, "Frames per period");
  935. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  936. i++;
  937. strcpy(desc->params[i].name, "device");
  938. desc->params[i].character = 'd';
  939. desc->params[i].type = JackDriverParamString;
  940. desc->params[i].value.ui = 128U;
  941. strcpy(desc->params[i].value.str, "will take default CoreAudio device name");
  942. strcpy(desc->params[i].short_desc, "CoreAudio device name");
  943. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  944. i++;
  945. strcpy(desc->params[i].name, "input-latency");
  946. desc->params[i].character = 'I';
  947. desc->params[i].type = JackDriverParamUInt;
  948. desc->params[i].value.i = 0;
  949. strcpy(desc->params[i].short_desc, "Extra input latency");
  950. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  951. i++;
  952. strcpy(desc->params[i].name, "output-latency");
  953. desc->params[i].character = 'O';
  954. desc->params[i].type = JackDriverParamUInt;
  955. desc->params[i].value.i = 0;
  956. strcpy(desc->params[i].short_desc, "Extra output latency");
  957. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  958. i++;
  959. strcpy(desc->params[i].name, "list-devices");
  960. desc->params[i].character = 'l';
  961. desc->params[i].type = JackDriverParamBool;
  962. desc->params[i].value.i = FALSE;
  963. strcpy(desc->params[i].short_desc, "Display available CoreAudio devices");
  964. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  965. return desc;
  966. }
  967. jack_driver_t *driver_initialize(jack_client_t * client,
  968. const JSList * params)
  969. {
  970. jack_nframes_t srate = 44100; /* Some older Mac models only support this value */
  971. jack_nframes_t frames_per_interrupt = 128;
  972. int capture = FALSE;
  973. int playback = FALSE;
  974. int chan_in = 0;
  975. int chan_out = 0;
  976. char* capture_pcm_name = "";
  977. char* playback_pcm_name = "";
  978. const JSList *node;
  979. const jack_driver_param_t *param;
  980. jack_nframes_t systemic_input_latency = 0;
  981. jack_nframes_t systemic_output_latency = 0;
  982. for (node = params; node; node = jack_slist_next(node)) {
  983. param = (const jack_driver_param_t *) node->data;
  984. switch (param->character) {
  985. case 'd':
  986. capture_pcm_name = strdup(param->value.str);
  987. playback_pcm_name = strdup(param->value.str);
  988. break;
  989. case 'D':
  990. capture = TRUE;
  991. playback = TRUE;
  992. break;
  993. case 'c':
  994. chan_in = chan_out = (int) param->value.ui;
  995. break;
  996. case 'i':
  997. chan_in = (int) param->value.ui;
  998. break;
  999. case 'o':
  1000. chan_out = (int) param->value.ui;
  1001. break;
  1002. case 'C':
  1003. capture = TRUE;
  1004. if (strcmp(param->value.str, "none") != 0) {
  1005. capture_pcm_name = strdup(param->value.str);
  1006. }
  1007. break;
  1008. case 'P':
  1009. playback = TRUE;
  1010. if (strcmp(param->value.str, "none") != 0) {
  1011. playback_pcm_name = strdup(param->value.str);
  1012. }
  1013. break;
  1014. case 'r':
  1015. srate = param->value.ui;
  1016. break;
  1017. case 'p':
  1018. frames_per_interrupt = (unsigned int) param->value.ui;
  1019. break;
  1020. case 'I':
  1021. systemic_input_latency = param->value.ui;
  1022. break;
  1023. case 'O':
  1024. systemic_output_latency = param->value.ui;
  1025. break;
  1026. case 'l':
  1027. display_device_names();
  1028. break;
  1029. }
  1030. }
  1031. /* duplex is the default */
  1032. if (!capture && !playback) {
  1033. capture = TRUE;
  1034. playback = TRUE;
  1035. }
  1036. return coreaudio_driver_new("coreaudio", client, frames_per_interrupt,
  1037. srate, capture, playback, chan_in,
  1038. chan_out, capture_pcm_name, playback_pcm_name, systemic_input_latency, systemic_output_latency);
  1039. }
  1040. void driver_finish(jack_driver_t * driver)
  1041. {
  1042. coreaudio_driver_delete((coreaudio_driver_t *) driver);
  1043. }