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.

1211 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. if (driver->engine->set_buffer_size(engine, driver->frames_per_cycle)) {
  387. jack_error ("coreaudio: cannot set engine buffer size to %d (check MIDI)", driver->frames_per_cycle);
  388. return -1;
  389. }
  390. driver->engine->set_sample_rate(engine, driver->frame_rate);
  391. port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
  392. /*
  393. if (driver->has_hw_monitoring) {
  394. port_flags |= JackPortCanMonitor;
  395. }
  396. */
  397. for (chn = 0; chn < driver->capture_nchannels; chn++) {
  398. err = AudioDeviceGetPropertyInfo(driver->device_id, chn + 1, true, kAudioDevicePropertyChannelName, &size, &isWritable);
  399. if (err == noErr && size > 0) {
  400. err = AudioDeviceGetProperty(driver->device_id, chn + 1, true, kAudioDevicePropertyChannelName, &size, channel_name);
  401. if (err != noErr)
  402. JCALog("AudioDeviceGetProperty kAudioDevicePropertyChannelName error \n");
  403. snprintf(buf, sizeof(buf) - 1, "%s:out_%s%lu", driver->capture_driver_name, channel_name, chn + 1);
  404. } else {
  405. snprintf(buf, sizeof(buf) - 1, "%s:out%lu", driver->capture_driver_name, chn + 1);
  406. }
  407. if ((port = jack_port_register(driver->client, buf,
  408. JACK_DEFAULT_AUDIO_TYPE, port_flags,
  409. 0)) == NULL) {
  410. jack_error("coreaudio: cannot register port for %s", buf);
  411. break;
  412. }
  413. size = sizeof(UInt32);
  414. value1 = value2 = 0;
  415. err = AudioDeviceGetProperty(driver->device_id, 0, true, kAudioDevicePropertyLatency, &size, &value1);
  416. if (err != noErr)
  417. JCALog("AudioDeviceGetProperty kAudioDevicePropertyLatency error \n");
  418. err = AudioDeviceGetProperty(driver->device_id, 0, true, kAudioDevicePropertySafetyOffset, &size, &value2);
  419. if (err != noErr)
  420. JCALog("AudioDeviceGetProperty kAudioDevicePropertySafetyOffset error \n");
  421. jack_port_set_latency(port, driver->frames_per_cycle + value1 + value2 + driver->capture_frame_latency);
  422. driver->capture_ports =
  423. jack_slist_append(driver->capture_ports, port);
  424. }
  425. port_flags = JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal;
  426. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  427. err = AudioDeviceGetPropertyInfo(driver->device_id, chn + 1, false, kAudioDevicePropertyChannelName, &size, &isWritable);
  428. if (err == noErr && size > 0) {
  429. err = AudioDeviceGetProperty(driver->device_id, chn + 1, false, kAudioDevicePropertyChannelName, &size, channel_name);
  430. if (err != noErr)
  431. JCALog("AudioDeviceGetProperty kAudioDevicePropertyChannelName error \n");
  432. snprintf(buf, sizeof(buf) - 1, "%s:in_%s%lu", driver->playback_driver_name, channel_name, chn + 1);
  433. } else {
  434. snprintf(buf, sizeof(buf) - 1, "%s:in%lu", driver->playback_driver_name, chn + 1);
  435. }
  436. if ((port = jack_port_register(driver->client, buf,
  437. JACK_DEFAULT_AUDIO_TYPE, port_flags,
  438. 0)) == NULL) {
  439. jack_error("coreaudio: cannot register port for %s", buf);
  440. break;
  441. }
  442. size = sizeof(UInt32);
  443. value1 = value2 = 0;
  444. err = AudioDeviceGetProperty(driver->device_id, 0, false, kAudioDevicePropertyLatency, &size, &value1);
  445. if (err != noErr)
  446. JCALog("AudioDeviceGetProperty kAudioDevicePropertyLatency error \n");
  447. err = AudioDeviceGetProperty(driver->device_id, 0, false, kAudioDevicePropertySafetyOffset, &size, &value2);
  448. if (err != noErr)
  449. JCALog("AudioDeviceGetProperty kAudioDevicePropertySafetyOffset error \n");
  450. jack_port_set_latency(port, driver->frames_per_cycle + value1 + value2 + driver->playback_frame_latency);
  451. driver->playback_ports =
  452. jack_slist_append(driver->playback_ports, port);
  453. }
  454. // Input buffers do no change : prepare them only once
  455. for (chn = 0, node = driver->capture_ports; chn < driver->capture_nchannels; chn++, node = jack_slist_next(node)) {
  456. driver->input_list->mBuffers[chn].mData
  457. = (jack_default_audio_sample_t*)jack_port_get_buffer(((jack_port_t *) node->data), driver->frames_per_cycle);
  458. }
  459. jack_activate(driver->client);
  460. return 0;
  461. }
  462. static int
  463. coreaudio_driver_detach(coreaudio_driver_t * driver, jack_engine_t * engine)
  464. {
  465. JSList *node;
  466. if (driver->engine == 0) {
  467. return -1;
  468. }
  469. for (node = driver->capture_ports; node; node = jack_slist_next(node)) {
  470. jack_port_unregister(driver->client, ((jack_port_t *) node->data));
  471. }
  472. jack_slist_free(driver->capture_ports);
  473. driver->capture_ports = 0;
  474. for (node = driver->playback_ports; node; node = jack_slist_next(node)) {
  475. jack_port_unregister(driver->client, ((jack_port_t *) node->data));
  476. }
  477. jack_slist_free(driver->playback_ports);
  478. driver->playback_ports = 0;
  479. driver->engine = 0;
  480. return 0;
  481. }
  482. static int
  483. coreaudio_driver_null_cycle(coreaudio_driver_t * driver, jack_nframes_t nframes)
  484. {
  485. driver->null_cycle_occured = 1;
  486. return 0;
  487. }
  488. static int
  489. coreaudio_driver_read(coreaudio_driver_t * driver, jack_nframes_t nframes)
  490. {
  491. return 0;
  492. }
  493. static int
  494. coreaudio_driver_write(coreaudio_driver_t * driver, jack_nframes_t nframes)
  495. {
  496. return 0;
  497. }
  498. static int coreaudio_driver_audio_start(coreaudio_driver_t * driver)
  499. {
  500. return (AudioOutputUnitStart(driver->au_hal) == noErr) ? 0 : -1;
  501. }
  502. static int coreaudio_driver_audio_stop(coreaudio_driver_t * driver)
  503. {
  504. return (AudioOutputUnitStop(driver->au_hal) == noErr) ? 0 : -1;
  505. }
  506. static int
  507. coreaudio_driver_bufsize(coreaudio_driver_t * driver,
  508. jack_nframes_t nframes)
  509. {
  510. /* This gets called from the engine server thread, so it must
  511. * be serialized with the driver thread. Stopping the audio
  512. * also stops that thread. */
  513. /*
  514. TO DO
  515. */
  516. return 0;
  517. }
  518. /** create a new driver instance
  519. */
  520. static jack_driver_t *coreaudio_driver_new(char* name,
  521. jack_client_t* client,
  522. jack_nframes_t nframes,
  523. jack_nframes_t samplerate,
  524. int capturing,
  525. int playing,
  526. int inchannels,
  527. int outchannels,
  528. char* capture_driver_uid,
  529. char* playback_driver_uid,
  530. jack_nframes_t capture_latency,
  531. jack_nframes_t playback_latency)
  532. {
  533. coreaudio_driver_t *driver;
  534. OSStatus err = noErr;
  535. ComponentResult err1;
  536. UInt32 outSize;
  537. UInt32 enableIO;
  538. AudioStreamBasicDescription srcFormat, dstFormat;
  539. Float64 sampleRate;
  540. int in_nChannels = 0;
  541. int out_nChannels = 0;
  542. int i;
  543. driver = (coreaudio_driver_t *) calloc(1, sizeof(coreaudio_driver_t));
  544. jack_driver_init((jack_driver_t *) driver);
  545. if (!jack_power_of_two(nframes)) {
  546. jack_error("CA: -p must be a power of two.");
  547. goto error;
  548. }
  549. driver->state = 0;
  550. driver->frames_per_cycle = nframes;
  551. driver->frame_rate = samplerate;
  552. driver->capturing = capturing;
  553. driver->playing = playing;
  554. driver->xrun_detected = 0;
  555. driver->null_cycle = 0;
  556. driver->attach = (JackDriverAttachFunction) coreaudio_driver_attach;
  557. driver->detach = (JackDriverDetachFunction) coreaudio_driver_detach;
  558. driver->read = (JackDriverReadFunction) coreaudio_driver_read;
  559. driver->write = (JackDriverReadFunction) coreaudio_driver_write;
  560. driver->null_cycle =
  561. (JackDriverNullCycleFunction) coreaudio_driver_null_cycle;
  562. driver->bufsize = (JackDriverBufSizeFunction) coreaudio_driver_bufsize;
  563. driver->start = (JackDriverStartFunction) coreaudio_driver_audio_start;
  564. driver->stop = (JackDriverStopFunction) coreaudio_driver_audio_stop;
  565. driver->capture_frame_latency = capture_latency;
  566. driver->playback_frame_latency = playback_latency;
  567. // Duplex
  568. if (strcmp(capture_driver_uid, "") != 0 && strcmp(playback_driver_uid, "") != 0) {
  569. JCALog("Open duplex \n");
  570. if (get_device_id_from_uid(playback_driver_uid, &driver->device_id) != noErr) {
  571. if (get_default_device(&driver->device_id) != noErr) {
  572. jack_error("Cannot open default device");
  573. goto error;
  574. }
  575. }
  576. 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) {
  577. jack_error("Cannot get device name from device ID");
  578. goto error;
  579. }
  580. // Capture only
  581. } else if (strcmp(capture_driver_uid, "") != 0) {
  582. JCALog("Open capture only \n");
  583. if (get_device_id_from_uid(capture_driver_uid, &driver->device_id) != noErr) {
  584. if (get_default_input_device(&driver->device_id) != noErr) {
  585. jack_error("Cannot open default device");
  586. goto error;
  587. }
  588. }
  589. if (get_device_name_from_id(driver->device_id, driver->capture_driver_name) != noErr) {
  590. jack_error("Cannot get device name from device ID");
  591. goto error;
  592. }
  593. // Playback only
  594. } else if (playback_driver_uid != NULL) {
  595. JCALog("Open playback only \n");
  596. if (get_device_id_from_uid(playback_driver_uid, &driver->device_id) != noErr) {
  597. if (get_default_output_device(&driver->device_id) != noErr) {
  598. jack_error("Cannot open default device");
  599. goto error;
  600. }
  601. }
  602. if (get_device_name_from_id(driver->device_id, driver->playback_driver_name) != noErr) {
  603. jack_error("Cannot get device name from device ID");
  604. goto error;
  605. }
  606. // Use default driver in duplex mode
  607. } else {
  608. JCALog("Open default driver \n");
  609. if (get_default_device(&driver->device_id) != noErr) {
  610. jack_error("Cannot open default device");
  611. goto error;
  612. }
  613. 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) {
  614. jack_error("Cannot get device name from device ID");
  615. goto error;
  616. }
  617. }
  618. driver->client = client;
  619. driver->period_usecs =
  620. (((float) driver->frames_per_cycle) / driver->frame_rate) *
  621. 1000000.0f;
  622. if (capturing) {
  623. err = get_total_channels(driver->device_id, &in_nChannels, true);
  624. if (err != noErr) {
  625. jack_error("Cannot get input channel number");
  626. printError(err);
  627. goto error;
  628. }
  629. }
  630. if (playing) {
  631. err = get_total_channels(driver->device_id, &out_nChannels, false);
  632. if (err != noErr) {
  633. jack_error("Cannot get output channel number");
  634. printError(err);
  635. goto error;
  636. }
  637. }
  638. if (inchannels > in_nChannels) {
  639. jack_error("This device hasn't required input channels inchannels = %ld in_nChannels = %ld", inchannels, in_nChannels);
  640. goto error;
  641. }
  642. if (outchannels > out_nChannels) {
  643. jack_error("This device hasn't required output channels outchannels = %ld out_nChannels = %ld", outchannels, out_nChannels);
  644. goto error;
  645. }
  646. if (inchannels == 0) {
  647. JCALog("Setup max in channels = %ld\n", in_nChannels);
  648. inchannels = in_nChannels;
  649. }
  650. if (outchannels == 0) {
  651. JCALog("Setup max out channels = %ld\n", out_nChannels);
  652. outchannels = out_nChannels;
  653. }
  654. // Setting buffer size
  655. outSize = sizeof(UInt32);
  656. err = AudioDeviceSetProperty(driver->device_id, NULL, 0, false, kAudioDevicePropertyBufferFrameSize, outSize, &nframes);
  657. if (err != noErr) {
  658. jack_error("Cannot set buffer size %ld", nframes);
  659. printError(err);
  660. goto error;
  661. }
  662. // Set sample rate
  663. outSize = sizeof(Float64);
  664. err = AudioDeviceGetProperty(driver->device_id, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyNominalSampleRate, &outSize, &sampleRate);
  665. if (err != noErr) {
  666. jack_error("Cannot get current sample rate");
  667. printError(err);
  668. goto error;
  669. }
  670. if (samplerate != (jack_nframes_t)sampleRate) {
  671. sampleRate = (Float64)samplerate;
  672. // To get SR change notification
  673. err = AudioDeviceAddPropertyListener(driver->device_id, 0, true, kAudioDevicePropertyNominalSampleRate, sr_notification, driver);
  674. if (err != noErr) {
  675. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyNominalSampleRate");
  676. printError(err);
  677. return -1;
  678. }
  679. err = AudioDeviceSetProperty(driver->device_id, NULL, 0, kAudioDeviceSectionGlobal, kAudioDevicePropertyNominalSampleRate, outSize, &sampleRate);
  680. if (err != noErr) {
  681. jack_error("Cannot set sample rate = %ld", samplerate);
  682. printError(err);
  683. return -1;
  684. }
  685. // Waiting for SR change notification
  686. int count = 0;
  687. while (!driver->state && count++ < 100) {
  688. usleep(100000);
  689. JCALog("Wait count = %ld\n", count);
  690. }
  691. // Remove SR change notification
  692. AudioDeviceRemovePropertyListener(driver->device_id, 0, true, kAudioDevicePropertyNominalSampleRate, sr_notification);
  693. }
  694. // AUHAL
  695. ComponentDescription cd = {kAudioUnitType_Output, kAudioUnitSubType_HALOutput, kAudioUnitManufacturer_Apple, 0, 0};
  696. Component HALOutput = FindNextComponent(NULL, &cd);
  697. err1 = OpenAComponent(HALOutput, &driver->au_hal);
  698. if (err1 != noErr) {
  699. jack_error("Error calling OpenAComponent");
  700. printError(err1);
  701. goto error;
  702. }
  703. err1 = AudioUnitInitialize(driver->au_hal);
  704. if (err1 != noErr) {
  705. jack_error("Cannot initialize AUHAL unit");
  706. printError(err1);
  707. goto error;
  708. }
  709. // Start I/O
  710. enableIO = 1;
  711. if (capturing && inchannels > 0) {
  712. JCALog("Setup AUHAL input\n");
  713. err1 = AudioUnitSetProperty(driver->au_hal, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, 1, &enableIO, sizeof(enableIO));
  714. if (err1 != noErr) {
  715. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input");
  716. printError(err1);
  717. goto error;
  718. }
  719. }
  720. if (playing && outchannels > 0) {
  721. JCALog("Setup AUHAL output\n");
  722. err1 = AudioUnitSetProperty(driver->au_hal, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, 0, &enableIO, sizeof(enableIO));
  723. if (err1 != noErr) {
  724. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_EnableIO,kAudioUnitScope_Output");
  725. printError(err1);
  726. goto error;
  727. }
  728. }
  729. // Setup up choosen device, in both input and output cases
  730. err1 = AudioUnitSetProperty(driver->au_hal, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &driver->device_id, sizeof(AudioDeviceID));
  731. if (err1 != noErr) {
  732. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_CurrentDevice");
  733. printError(err1);
  734. goto error;
  735. }
  736. // Set buffer size
  737. if (capturing && inchannels > 0) {
  738. err1 = AudioUnitSetProperty(driver->au_hal, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 1, (UInt32*)&nframes, sizeof(UInt32));
  739. if (err1 != noErr) {
  740. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_MaximumFramesPerSlice");
  741. printError(err1);
  742. goto error;
  743. }
  744. }
  745. if (playing && outchannels > 0) {
  746. err1 = AudioUnitSetProperty(driver->au_hal, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 0, (UInt32*)&nframes, sizeof(UInt32));
  747. if (err1 != noErr) {
  748. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_MaximumFramesPerSlice");
  749. printError(err1);
  750. goto error;
  751. }
  752. }
  753. // Setup channel map
  754. if (capturing && inchannels > 0 && inchannels < in_nChannels) {
  755. SInt32 chanArr[in_nChannels];
  756. for (i = 0; i < in_nChannels; i++) {
  757. chanArr[i] = -1;
  758. }
  759. for (i = 0; i < inchannels; i++) {
  760. chanArr[i] = i;
  761. }
  762. AudioUnitSetProperty(driver->au_hal, kAudioOutputUnitProperty_ChannelMap , kAudioUnitScope_Input, 1, chanArr, sizeof(SInt32) * in_nChannels);
  763. if (err1 != noErr) {
  764. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_ChannelMap 1");
  765. printError(err1);
  766. }
  767. }
  768. if (playing && outchannels > 0 && outchannels < out_nChannels) {
  769. SInt32 chanArr[out_nChannels];
  770. for (i = 0; i < out_nChannels; i++) {
  771. chanArr[i] = -1;
  772. }
  773. for (i = 0; i < outchannels; i++) {
  774. chanArr[i] = i;
  775. }
  776. err1 = AudioUnitSetProperty(driver->au_hal, kAudioOutputUnitProperty_ChannelMap, kAudioUnitScope_Output, 0, chanArr, sizeof(SInt32) * out_nChannels);
  777. if (err1 != noErr) {
  778. jack_error("Error calling AudioUnitSetProperty - kAudioOutputUnitProperty_ChannelMap 0");
  779. printError(err1);
  780. }
  781. }
  782. // Setup stream converters
  783. srcFormat.mSampleRate = samplerate;
  784. srcFormat.mFormatID = kAudioFormatLinearPCM;
  785. srcFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kLinearPCMFormatFlagIsNonInterleaved;
  786. srcFormat.mBytesPerPacket = sizeof(float);
  787. srcFormat.mFramesPerPacket = 1;
  788. srcFormat.mBytesPerFrame = sizeof(float);
  789. srcFormat.mChannelsPerFrame = outchannels;
  790. srcFormat.mBitsPerChannel = 32;
  791. err1 = AudioUnitSetProperty(driver->au_hal, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &srcFormat, sizeof(AudioStreamBasicDescription));
  792. if (err1 != noErr) {
  793. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Input");
  794. printError(err1);
  795. }
  796. dstFormat.mSampleRate = samplerate;
  797. dstFormat.mFormatID = kAudioFormatLinearPCM;
  798. dstFormat.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kLinearPCMFormatFlagIsNonInterleaved;
  799. dstFormat.mBytesPerPacket = sizeof(float);
  800. dstFormat.mFramesPerPacket = 1;
  801. dstFormat.mBytesPerFrame = sizeof(float);
  802. dstFormat.mChannelsPerFrame = inchannels;
  803. dstFormat.mBitsPerChannel = 32;
  804. err1 = AudioUnitSetProperty(driver->au_hal, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &dstFormat, sizeof(AudioStreamBasicDescription));
  805. if (err1 != noErr) {
  806. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_StreamFormat kAudioUnitScope_Output");
  807. printError(err1);
  808. }
  809. // Setup callbacks
  810. if (inchannels > 0 && outchannels == 0) {
  811. AURenderCallbackStruct output;
  812. output.inputProc = render_input;
  813. output.inputProcRefCon = driver;
  814. err1 = AudioUnitSetProperty(driver->au_hal, kAudioOutputUnitProperty_SetInputCallback, kAudioUnitScope_Global, 0, &output, sizeof(output));
  815. if (err1 != noErr) {
  816. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_SetRenderCallback 1");
  817. printError(err1);
  818. goto error;
  819. }
  820. } else {
  821. AURenderCallbackStruct output;
  822. output.inputProc = render;
  823. output.inputProcRefCon = driver;
  824. err1 = AudioUnitSetProperty(driver->au_hal, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &output, sizeof(output));
  825. if (err1 != noErr) {
  826. jack_error("Error calling AudioUnitSetProperty - kAudioUnitProperty_SetRenderCallback 0");
  827. printError(err1);
  828. goto error;
  829. }
  830. }
  831. if (capturing && inchannels > 0) {
  832. driver->input_list = (AudioBufferList*)malloc(sizeof(UInt32) + inchannels * sizeof(AudioBuffer));
  833. if (driver->input_list == 0)
  834. goto error;
  835. driver->input_list->mNumberBuffers = inchannels;
  836. // Prepare buffers
  837. for (i = 0; i < driver->capture_nchannels; i++) {
  838. driver->input_list->mBuffers[i].mNumberChannels = 1;
  839. driver->input_list->mBuffers[i].mDataByteSize = nframes * sizeof(float);
  840. }
  841. }
  842. err = AudioDeviceAddPropertyListener(driver->device_id, 0, true, kAudioDeviceProcessorOverload, notification, driver);
  843. if (err != noErr) {
  844. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDeviceProcessorOverload");
  845. goto error;
  846. }
  847. err = AudioDeviceAddPropertyListener(driver->device_id, 0, true, kAudioDevicePropertyNominalSampleRate, notification, driver);
  848. if (err != noErr) {
  849. jack_error("Error calling AudioDeviceAddPropertyListener with kAudioDevicePropertyNominalSampleRate");
  850. goto error;
  851. }
  852. driver->playback_nchannels = outchannels;
  853. driver->capture_nchannels = inchannels;
  854. return ((jack_driver_t *) driver);
  855. error:
  856. AudioUnitUninitialize(driver->au_hal);
  857. CloseComponent(driver->au_hal);
  858. jack_error("Cannot open the coreaudio driver");
  859. free(driver);
  860. return NULL;
  861. }
  862. /** free all memory allocated by a driver instance
  863. */
  864. static void coreaudio_driver_delete(coreaudio_driver_t * driver)
  865. {
  866. AudioDeviceRemovePropertyListener(driver->device_id, 0, true, kAudioDeviceProcessorOverload, notification);
  867. free(driver->input_list);
  868. AudioUnitUninitialize(driver->au_hal);
  869. CloseComponent(driver->au_hal);
  870. free(driver);
  871. }
  872. //== driver "plugin" interface =================================================
  873. /* DRIVER "PLUGIN" INTERFACE */
  874. const char driver_client_name[] = "coreaudio";
  875. jack_driver_desc_t *driver_get_descriptor()
  876. {
  877. jack_driver_desc_t *desc;
  878. unsigned int i;
  879. desc = calloc(1, sizeof(jack_driver_desc_t));
  880. strcpy(desc->name, "coreaudio");
  881. desc->nparams = 12;
  882. desc->params = calloc(desc->nparams, sizeof(jack_driver_param_desc_t));
  883. i = 0;
  884. strcpy(desc->params[i].name, "channels");
  885. desc->params[i].character = 'c';
  886. desc->params[i].type = JackDriverParamInt;
  887. desc->params[i].value.ui = 2;
  888. strcpy(desc->params[i].short_desc, "Maximum number of channels");
  889. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  890. i++;
  891. strcpy(desc->params[i].name, "inchannels");
  892. desc->params[i].character = 'i';
  893. desc->params[i].type = JackDriverParamInt;
  894. desc->params[i].value.ui = 2;
  895. strcpy(desc->params[i].short_desc, "Maximum number of input channels");
  896. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  897. i++;
  898. strcpy(desc->params[i].name, "outchannels");
  899. desc->params[i].character = 'o';
  900. desc->params[i].type = JackDriverParamInt;
  901. desc->params[i].value.ui = 2;
  902. strcpy(desc->params[i].short_desc, "Maximum number of output channels");
  903. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  904. i++;
  905. strcpy(desc->params[i].name, "capture");
  906. desc->params[i].character = 'C';
  907. desc->params[i].type = JackDriverParamString;
  908. strcpy(desc->params[i].value.str, "will take default CoreAudio input device");
  909. strcpy(desc->params[i].short_desc, "Provide capture ports. Optionally set CoreAudio device name");
  910. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  911. i++;
  912. strcpy(desc->params[i].name, "playback");
  913. desc->params[i].character = 'P';
  914. desc->params[i].type = JackDriverParamString;
  915. strcpy(desc->params[i].value.str, "will take default CoreAudio output device");
  916. strcpy(desc->params[i].short_desc, "Provide playback ports. Optionally set CoreAudio device name");
  917. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  918. i++;
  919. strcpy(desc->params[i].name, "duplex");
  920. desc->params[i].character = 'D';
  921. desc->params[i].type = JackDriverParamBool;
  922. desc->params[i].value.i = TRUE;
  923. strcpy(desc->params[i].short_desc, "Capture and playback");
  924. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  925. i++;
  926. strcpy(desc->params[i].name, "rate");
  927. desc->params[i].character = 'r';
  928. desc->params[i].type = JackDriverParamUInt;
  929. desc->params[i].value.ui = 44100U;
  930. strcpy(desc->params[i].short_desc, "Sample rate");
  931. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  932. i++;
  933. strcpy(desc->params[i].name, "period");
  934. desc->params[i].character = 'p';
  935. desc->params[i].type = JackDriverParamUInt;
  936. desc->params[i].value.ui = 128U;
  937. strcpy(desc->params[i].short_desc, "Frames per period");
  938. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  939. i++;
  940. strcpy(desc->params[i].name, "device");
  941. desc->params[i].character = 'd';
  942. desc->params[i].type = JackDriverParamString;
  943. desc->params[i].value.ui = 128U;
  944. strcpy(desc->params[i].value.str, "will take default CoreAudio device name");
  945. strcpy(desc->params[i].short_desc, "CoreAudio device name");
  946. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  947. i++;
  948. strcpy(desc->params[i].name, "input-latency");
  949. desc->params[i].character = 'I';
  950. desc->params[i].type = JackDriverParamUInt;
  951. desc->params[i].value.i = 0;
  952. strcpy(desc->params[i].short_desc, "Extra input latency");
  953. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  954. i++;
  955. strcpy(desc->params[i].name, "output-latency");
  956. desc->params[i].character = 'O';
  957. desc->params[i].type = JackDriverParamUInt;
  958. desc->params[i].value.i = 0;
  959. strcpy(desc->params[i].short_desc, "Extra output latency");
  960. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  961. i++;
  962. strcpy(desc->params[i].name, "list-devices");
  963. desc->params[i].character = 'l';
  964. desc->params[i].type = JackDriverParamBool;
  965. desc->params[i].value.i = FALSE;
  966. strcpy(desc->params[i].short_desc, "Display available CoreAudio devices");
  967. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  968. return desc;
  969. }
  970. jack_driver_t *driver_initialize(jack_client_t * client,
  971. const JSList * params)
  972. {
  973. jack_nframes_t srate = 44100; /* Some older Mac models only support this value */
  974. jack_nframes_t frames_per_interrupt = 128;
  975. int capture = FALSE;
  976. int playback = FALSE;
  977. int chan_in = 0;
  978. int chan_out = 0;
  979. char* capture_pcm_name = "";
  980. char* playback_pcm_name = "";
  981. const JSList *node;
  982. const jack_driver_param_t *param;
  983. jack_nframes_t systemic_input_latency = 0;
  984. jack_nframes_t systemic_output_latency = 0;
  985. for (node = params; node; node = jack_slist_next(node)) {
  986. param = (const jack_driver_param_t *) node->data;
  987. switch (param->character) {
  988. case 'd':
  989. capture_pcm_name = strdup(param->value.str);
  990. playback_pcm_name = strdup(param->value.str);
  991. break;
  992. case 'D':
  993. capture = TRUE;
  994. playback = TRUE;
  995. break;
  996. case 'c':
  997. chan_in = chan_out = (int) param->value.ui;
  998. break;
  999. case 'i':
  1000. chan_in = (int) param->value.ui;
  1001. break;
  1002. case 'o':
  1003. chan_out = (int) param->value.ui;
  1004. break;
  1005. case 'C':
  1006. capture = TRUE;
  1007. if (strcmp(param->value.str, "none") != 0) {
  1008. capture_pcm_name = strdup(param->value.str);
  1009. }
  1010. break;
  1011. case 'P':
  1012. playback = TRUE;
  1013. if (strcmp(param->value.str, "none") != 0) {
  1014. playback_pcm_name = strdup(param->value.str);
  1015. }
  1016. break;
  1017. case 'r':
  1018. srate = param->value.ui;
  1019. break;
  1020. case 'p':
  1021. frames_per_interrupt = (unsigned int) param->value.ui;
  1022. break;
  1023. case 'I':
  1024. systemic_input_latency = param->value.ui;
  1025. break;
  1026. case 'O':
  1027. systemic_output_latency = param->value.ui;
  1028. break;
  1029. case 'l':
  1030. display_device_names();
  1031. break;
  1032. }
  1033. }
  1034. /* duplex is the default */
  1035. if (!capture && !playback) {
  1036. capture = TRUE;
  1037. playback = TRUE;
  1038. }
  1039. return coreaudio_driver_new("coreaudio", client, frames_per_interrupt,
  1040. srate, capture, playback, chan_in,
  1041. chan_out, capture_pcm_name, playback_pcm_name, systemic_input_latency, systemic_output_latency);
  1042. }
  1043. void driver_finish(jack_driver_t * driver)
  1044. {
  1045. coreaudio_driver_delete((coreaudio_driver_t *) driver);
  1046. }