jack2 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.

1115 lines
38KB

  1. /*
  2. Copyright (C) 2001-2003 Paul Davis
  3. Copyright (C) 2004-2006 Grame
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  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. */
  16. #include "JackClient.h"
  17. #include "JackError.h"
  18. #include "JackGraphManager.h"
  19. #include "JackEngineControl.h"
  20. #include "JackClientControl.h"
  21. #include "JackGlobals.h"
  22. #include "JackTime.h"
  23. #include "JackExports.h"
  24. #include <math.h>
  25. using namespace Jack;
  26. #ifdef __cplusplus
  27. extern "C"
  28. {
  29. #endif
  30. EXPORT int jack_client_name_size (void);
  31. EXPORT char* jack_get_client_name (jack_client_t *client);
  32. EXPORT int jack_internal_client_new (const char *client_name,
  33. const char *load_name,
  34. const char *load_init);
  35. EXPORT jack_client_t* my_jack_internal_client_new(const char* client_name);
  36. EXPORT void jack_internal_client_close (const char *client_name);
  37. EXPORT void my_jack_internal_client_close (jack_client_t* client);
  38. EXPORT int jack_is_realtime (jack_client_t *client);
  39. EXPORT void jack_on_shutdown (jack_client_t *client,
  40. void (*function)(void *arg), void *arg);
  41. EXPORT int jack_set_process_callback (jack_client_t *client,
  42. JackProcessCallback process_callback,
  43. void *arg);
  44. EXPORT int jack_set_thread_init_callback (jack_client_t *client,
  45. JackThreadInitCallback thread_init_callback,
  46. void *arg);
  47. EXPORT int jack_set_freewheel_callback (jack_client_t *client,
  48. JackFreewheelCallback freewheel_callback,
  49. void *arg);
  50. EXPORT int jack_set_freewheel(jack_client_t* client, int onoff);
  51. EXPORT int jack_set_buffer_size (jack_client_t *client, jack_nframes_t nframes);
  52. EXPORT int jack_set_buffer_size_callback (jack_client_t *client,
  53. JackBufferSizeCallback bufsize_callback,
  54. void *arg);
  55. EXPORT int jack_set_sample_rate_callback (jack_client_t *client,
  56. JackSampleRateCallback srate_callback,
  57. void *arg);
  58. EXPORT int jack_set_port_registration_callback (jack_client_t *,
  59. JackPortRegistrationCallback
  60. registration_callback, void *arg);
  61. EXPORT int jack_set_graph_order_callback (jack_client_t *,
  62. JackGraphOrderCallback graph_callback,
  63. void *);
  64. EXPORT int jack_set_xrun_callback (jack_client_t *,
  65. JackXRunCallback xrun_callback, void *arg);
  66. EXPORT int jack_activate (jack_client_t *client);
  67. EXPORT int jack_deactivate (jack_client_t *client);
  68. EXPORT jack_port_t * jack_port_register (jack_client_t *client,
  69. const char *port_name,
  70. const char *port_type,
  71. unsigned long flags,
  72. unsigned long buffer_size);
  73. EXPORT int jack_port_unregister (jack_client_t *, jack_port_t *);
  74. EXPORT void * jack_port_get_buffer (jack_port_t *, jack_nframes_t);
  75. EXPORT const char * jack_port_name (const jack_port_t *port);
  76. EXPORT const char * jack_port_short_name (const jack_port_t *port);
  77. EXPORT int jack_port_flags (const jack_port_t *port);
  78. EXPORT const char * jack_port_type (const jack_port_t *port);
  79. EXPORT int jack_port_is_mine (const jack_client_t *, const jack_port_t *port);
  80. EXPORT int jack_port_connected (const jack_port_t *port);
  81. EXPORT int jack_port_connected_to (const jack_port_t *port,
  82. const char *port_name);
  83. EXPORT const char ** jack_port_get_connections (const jack_port_t *port);
  84. EXPORT const char ** jack_port_get_all_connections (const jack_client_t *client,
  85. const jack_port_t *port);
  86. EXPORT int jack_port_tie (jack_port_t *src, jack_port_t *dst);
  87. EXPORT int jack_port_untie (jack_port_t *port);
  88. EXPORT int jack_port_lock (jack_client_t *, jack_port_t *);
  89. EXPORT int jack_port_unlock (jack_client_t *, jack_port_t *);
  90. EXPORT jack_nframes_t jack_port_get_latency (jack_port_t *port);
  91. EXPORT jack_nframes_t jack_port_get_total_latency (jack_client_t *,
  92. jack_port_t *port);
  93. EXPORT void jack_port_set_latency (jack_port_t *, jack_nframes_t);
  94. EXPORT int jack_recompute_total_latencies (jack_client_t*);
  95. EXPORT int jack_port_set_name (jack_port_t *port, const char *port_name);
  96. EXPORT int jack_port_request_monitor (jack_port_t *port, int onoff);
  97. EXPORT int jack_port_request_monitor_by_name (jack_client_t *client,
  98. const char *port_name, int onoff);
  99. EXPORT int jack_port_ensure_monitor (jack_port_t *port, int onoff);
  100. EXPORT int jack_port_monitoring_input (jack_port_t *port);
  101. EXPORT int jack_connect (jack_client_t *,
  102. const char *source_port,
  103. const char *destination_port);
  104. EXPORT int jack_disconnect (jack_client_t *,
  105. const char *source_port,
  106. const char *destination_port);
  107. EXPORT int jack_port_disconnect (jack_client_t *, jack_port_t *);
  108. EXPORT int jack_port_name_size(void);
  109. EXPORT int jack_port_type_size(void);
  110. EXPORT jack_nframes_t jack_get_sample_rate (jack_client_t *);
  111. EXPORT jack_nframes_t jack_get_buffer_size (jack_client_t *);
  112. EXPORT const char ** jack_get_ports (jack_client_t *,
  113. const char *port_name_pattern,
  114. const char *type_name_pattern,
  115. unsigned long flags);
  116. EXPORT jack_port_t * jack_port_by_name (jack_client_t *, const char *port_name);
  117. EXPORT jack_port_t * jack_port_by_id (jack_client_t *client,
  118. jack_port_id_t port_id);
  119. EXPORT int jack_engine_takeover_timebase (jack_client_t *);
  120. EXPORT jack_nframes_t jack_frames_since_cycle_start (const jack_client_t *);
  121. EXPORT jack_nframes_t jack_frame_time (const jack_client_t *);
  122. EXPORT jack_nframes_t jack_last_frame_time (const jack_client_t *client);
  123. EXPORT float jack_cpu_load (jack_client_t *client);
  124. EXPORT pthread_t jack_client_thread_id (jack_client_t *);
  125. EXPORT void jack_set_error_function (void (*func)(const char *));
  126. EXPORT float jack_get_max_delayed_usecs (jack_client_t *client);
  127. EXPORT float jack_get_xrun_delayed_usecs (jack_client_t *client);
  128. EXPORT void jack_reset_max_delayed_usecs (jack_client_t *client);
  129. EXPORT int jack_release_timebase (jack_client_t *client);
  130. EXPORT int jack_set_sync_callback (jack_client_t *client,
  131. JackSyncCallback sync_callback,
  132. void *arg);
  133. EXPORT int jack_set_sync_timeout (jack_client_t *client,
  134. jack_time_t timeout);
  135. EXPORT int jack_set_timebase_callback (jack_client_t *client,
  136. int conditional,
  137. JackTimebaseCallback timebase_callback,
  138. void *arg);
  139. EXPORT int jack_transport_locate (jack_client_t *client,
  140. jack_nframes_t frame);
  141. EXPORT jack_transport_state_t jack_transport_query (const jack_client_t *client,
  142. jack_position_t *pos);
  143. EXPORT jack_nframes_t jack_get_current_transport_frame (const jack_client_t *client);
  144. EXPORT int jack_transport_reposition (jack_client_t *client,
  145. jack_position_t *pos);
  146. EXPORT void jack_transport_start (jack_client_t *client);
  147. EXPORT void jack_transport_stop (jack_client_t *client);
  148. EXPORT void jack_get_transport_info (jack_client_t *client,
  149. jack_transport_info_t *tinfo);
  150. EXPORT void jack_set_transport_info (jack_client_t *client,
  151. jack_transport_info_t *tinfo);
  152. EXPORT int jack_acquire_real_time_scheduling (pthread_t thread, int priority);
  153. EXPORT int jack_client_create_thread (jack_client_t* client,
  154. pthread_t *thread,
  155. int priority,
  156. int realtime, // boolean
  157. void *(*start_routine)(void*),
  158. void *arg);
  159. EXPORT int jack_drop_real_time_scheduling (pthread_t thread);
  160. EXPORT char * jack_get_internal_client_name (jack_client_t *client,
  161. jack_intclient_t intclient);
  162. EXPORT jack_intclient_t jack_internal_client_handle (jack_client_t *client,
  163. const char *client_name,
  164. jack_status_t *status);
  165. EXPORT jack_intclient_t jack_internal_client_load (jack_client_t *client,
  166. const char *client_name,
  167. jack_options_t options,
  168. jack_status_t *status, ...);
  169. EXPORT jack_status_t jack_internal_client_unload (jack_client_t *client,
  170. jack_intclient_t intclient);
  171. #ifdef __cplusplus
  172. }
  173. #endif
  174. #ifdef WIN32
  175. /* missing on Windows : see http://bugs.mysql.com/bug.php?id=15936 */
  176. inline double rint(double nr)
  177. {
  178. double f = floor(nr);
  179. double c = ceil(nr);
  180. return (((c -nr) >= (nr - f)) ? f : c);
  181. }
  182. #endif
  183. static inline bool CheckPort(jack_port_id_t port_index)
  184. {
  185. return (port_index < PORT_NUM);
  186. }
  187. static inline void WaitGraphChange()
  188. {
  189. if (GetGraphManager()->IsPendingChange()) {
  190. JackLog("WaitGraphChange...\n");
  191. JackSleep(GetEngineControl()->fPeriodUsecs * 2);
  192. }
  193. }
  194. static void default_jack_error_callback (const char *desc)
  195. {
  196. fprintf(stderr, "%s\n", desc);
  197. }
  198. void (*jack_error_callback)(const char *desc) = &default_jack_error_callback;
  199. EXPORT void jack_set_error_function (void (*func)(const char *))
  200. {
  201. jack_error_callback = func;
  202. }
  203. EXPORT void* jack_port_get_buffer(jack_port_t* port, jack_nframes_t frames)
  204. {
  205. jack_port_id_t myport = (jack_port_id_t)port;
  206. if (!CheckPort(myport)) {
  207. jack_error("jack_port_get_buffer called with an incorrect port %ld", myport);
  208. return NULL;
  209. } else {
  210. return GetGraphManager()->GetBuffer(myport, frames);
  211. }
  212. }
  213. EXPORT const char* jack_port_name(const jack_port_t* port)
  214. {
  215. jack_port_id_t myport = (jack_port_id_t)port;
  216. if (!CheckPort(myport)) {
  217. jack_error("jack_port_name called with an incorrect port %ld", myport);
  218. return NULL;
  219. } else {
  220. return GetGraphManager()->GetPort(myport)->GetName();
  221. }
  222. }
  223. EXPORT const char* jack_port_short_name(const jack_port_t* port)
  224. {
  225. jack_port_id_t myport = (jack_port_id_t)port;
  226. if (!CheckPort(myport)) {
  227. jack_error("jack_port_short_name called with an incorrect port %ld", myport);
  228. return NULL;
  229. } else {
  230. return GetGraphManager()->GetPort(myport)->GetShortName();
  231. }
  232. }
  233. EXPORT int jack_port_flags(const jack_port_t* port)
  234. {
  235. jack_port_id_t myport = (jack_port_id_t)port;
  236. if (!CheckPort(myport)) {
  237. jack_error("jack_port_flags called with an incorrect port %ld", myport);
  238. return -1;
  239. } else {
  240. return GetGraphManager()->GetPort(myport)->Flags();
  241. }
  242. }
  243. EXPORT const char* jack_port_type(const jack_port_t* port)
  244. {
  245. jack_port_id_t myport = (jack_port_id_t)port;
  246. if (!CheckPort(myport)) {
  247. jack_error("jack_port_flags called an incorrect port %ld", myport);
  248. return NULL;
  249. } else {
  250. return GetGraphManager()->GetPort(myport)->Type();
  251. }
  252. }
  253. EXPORT int jack_port_connected(const jack_port_t* port)
  254. {
  255. jack_port_id_t myport = (jack_port_id_t)port;
  256. if (!CheckPort(myport)) {
  257. jack_error("jack_port_connected called with an incorrect port %ld", myport);
  258. return -1;
  259. } else {
  260. WaitGraphChange();
  261. return GetGraphManager()->GetConnectionsNum(myport);
  262. }
  263. }
  264. EXPORT int jack_port_connected_to(const jack_port_t* port, const char* portname)
  265. {
  266. jack_port_id_t myport = (jack_port_id_t)port;
  267. if (!CheckPort(myport)) {
  268. jack_error("jack_port_connected_to called with an incorrect port %ld", myport);
  269. return -1;
  270. } else if (portname == NULL) {
  271. jack_error("jack_port_connected_to called with a NULL port name");
  272. return -1;
  273. } else {
  274. WaitGraphChange();
  275. return GetGraphManager()->ConnectedTo(myport, portname);
  276. }
  277. }
  278. EXPORT int jack_port_tie(jack_port_t* src, jack_port_t* dst)
  279. {
  280. jack_port_id_t mysrc = (jack_port_id_t)src;
  281. if (!CheckPort(mysrc)) {
  282. jack_error("jack_port_tie called with a NULL src port");
  283. return -1;
  284. }
  285. jack_port_id_t mydst = (jack_port_id_t)dst;
  286. if (!CheckPort(mydst)) {
  287. jack_error("jack_port_tie called with a NULL dst port");
  288. return -1;
  289. }
  290. if (GetGraphManager()->GetPort(mysrc)->GetRefNum() != GetGraphManager()->GetPort(mydst)->GetRefNum()) {
  291. jack_error("jack_port_tie called with ports not belonging to the same client");
  292. return -1;
  293. }
  294. return GetGraphManager()->GetPort(mydst)->Tie(mysrc);
  295. }
  296. EXPORT int jack_port_untie(jack_port_t* port)
  297. {
  298. jack_port_id_t myport = (jack_port_id_t)port;
  299. if (!CheckPort(myport)) {
  300. jack_error("jack_port_untie called with an incorrect port %ld", myport);
  301. return -1;
  302. } else {
  303. return GetGraphManager()->GetPort(myport)->UnTie();
  304. }
  305. }
  306. EXPORT jack_nframes_t jack_port_get_latency(jack_port_t* port)
  307. {
  308. jack_port_id_t myport = (jack_port_id_t)port;
  309. if (!CheckPort(myport)) {
  310. jack_error("jack_port_get_latency called with an incorrect port %ld", myport);
  311. return 0;
  312. } else {
  313. WaitGraphChange();
  314. return GetGraphManager()->GetPort(myport)->GetLatency();
  315. }
  316. }
  317. EXPORT void jack_port_set_latency(jack_port_t* port, jack_nframes_t frames)
  318. {
  319. jack_port_id_t myport = (jack_port_id_t)port;
  320. if (!CheckPort(myport)) {
  321. jack_error("jack_port_set_latency called with an incorrect port %ld", myport);
  322. } else {
  323. GetGraphManager()->GetPort(myport)->SetLatency(frames);
  324. }
  325. }
  326. EXPORT int jack_recompute_total_latencies(jack_client_t* ext_client)
  327. {
  328. // The latency computation is done each time jack_port_get_total_latency is called
  329. return 0;
  330. }
  331. EXPORT int jack_port_set_name(jack_port_t* port, const char* name)
  332. {
  333. jack_port_id_t myport = (jack_port_id_t)port;
  334. if (!CheckPort(myport)) {
  335. jack_error("jack_port_set_name called with an incorrect port %ld", myport);
  336. return -1;
  337. } else if (name == NULL) {
  338. jack_error("jack_port_set_name called with a NULL port name");
  339. return -1;
  340. } else {
  341. return GetGraphManager()->GetPort(myport)->SetName(name);
  342. }
  343. }
  344. EXPORT int jack_port_request_monitor(jack_port_t* port, int onoff)
  345. {
  346. jack_port_id_t myport = (jack_port_id_t)port;
  347. if (!CheckPort(myport)) {
  348. jack_error("jack_port_request_monitor called with an incorrect port %ld", myport);
  349. return -1;
  350. } else {
  351. return GetGraphManager()->RequestMonitor(myport, onoff);
  352. }
  353. }
  354. EXPORT int jack_port_request_monitor_by_name(jack_client_t* ext_client, const char* port_name, int onoff)
  355. {
  356. JackClient* client = (JackClient*)ext_client;
  357. if (client == NULL) {
  358. jack_error("jack_port_request_monitor_by_name called with a NULL client");
  359. return -1;
  360. } else {
  361. jack_port_id_t myport = GetGraphManager()->GetPort(port_name);
  362. if (!CheckPort(myport)) {
  363. jack_error("jack_port_request_monitor_by_name called with an incorrect port %s", port_name);
  364. return -1;
  365. } else {
  366. return GetGraphManager()->RequestMonitor(myport, onoff);
  367. }
  368. }
  369. }
  370. EXPORT int jack_port_ensure_monitor(jack_port_t* port, int onoff)
  371. {
  372. jack_port_id_t myport = (jack_port_id_t)port;
  373. if (!CheckPort(myport)) {
  374. jack_error("jack_port_ensure_monitor called with an incorrect port %ld", myport);
  375. return -1;
  376. } else {
  377. return GetGraphManager()->GetPort(myport)->EnsureMonitor(onoff);
  378. }
  379. }
  380. EXPORT int jack_port_monitoring_input(jack_port_t* port)
  381. {
  382. jack_port_id_t myport = (jack_port_id_t)port;
  383. if (!CheckPort(myport)) {
  384. jack_error("jack_port_monitoring_input called with an incorrect port %ld", myport);
  385. return -1;
  386. } else {
  387. return GetGraphManager()->GetPort(myport)->MonitoringInput();
  388. }
  389. }
  390. EXPORT int jack_is_realtime(jack_client_t* ext_client)
  391. {
  392. JackClient* client = (JackClient*)ext_client;
  393. if (client == NULL) {
  394. jack_error("jack_is_realtime called with a NULL client");
  395. return -1;
  396. } else {
  397. return GetEngineControl()->fRealTime;
  398. }
  399. }
  400. EXPORT void jack_on_shutdown(jack_client_t* ext_client, void (*function)(void* arg), void* arg)
  401. {
  402. JackClient* client = (JackClient*)ext_client;
  403. if (client == NULL) {
  404. jack_error("jack_on_shutdown called with a NULL client");
  405. } else {
  406. client->OnShutdown(function, arg);
  407. }
  408. }
  409. EXPORT int jack_set_process_callback(jack_client_t* ext_client, JackProcessCallback callback, void* arg)
  410. {
  411. JackClient* client = (JackClient*)ext_client;
  412. if (client == NULL) {
  413. jack_error("jack_set_process_callback called with a NULL client");
  414. return -1;
  415. } else {
  416. return client->SetProcessCallback(callback, arg);
  417. }
  418. }
  419. EXPORT int jack_set_freewheel_callback(jack_client_t* ext_client, JackFreewheelCallback freewheel_callback, void* arg)
  420. {
  421. JackClient* client = (JackClient*)ext_client;
  422. if (client == NULL) {
  423. jack_error("jack_set_freewheel_callback called with a NULL client");
  424. return -1;
  425. } else {
  426. return client->SetFreewheelCallback(freewheel_callback, arg);
  427. }
  428. }
  429. EXPORT int jack_set_freewheel(jack_client_t* ext_client, int onoff)
  430. {
  431. JackClient* client = (JackClient*)ext_client;
  432. if (client == NULL) {
  433. jack_error("jack_set_freewheel called with a NULL client");
  434. return -1;
  435. } else {
  436. return client->SetFreeWheel(onoff);
  437. }
  438. }
  439. EXPORT int jack_set_buffer_size(jack_client_t* ext_client, jack_nframes_t nframes)
  440. {
  441. JackClient* client = (JackClient*)ext_client;
  442. if (client == NULL) {
  443. jack_error("jack_set_buffer_size called with a NULL client");
  444. return -1;
  445. } else {
  446. return client->SetBufferSize(nframes);
  447. }
  448. }
  449. EXPORT int jack_set_buffer_size_callback(jack_client_t* ext_client, JackBufferSizeCallback bufsize_callback, void* arg)
  450. {
  451. JackClient* client = (JackClient*)ext_client;
  452. if (client == NULL) {
  453. jack_error("jack_set_buffer_size_callback called with a NULL client");
  454. return -1;
  455. } else {
  456. return client->SetBufferSizeCallback(bufsize_callback, arg);
  457. }
  458. }
  459. EXPORT int jack_set_sample_rate_callback(jack_client_t* ext_client, JackSampleRateCallback srate_callback, void* arg)
  460. {
  461. JackClient* client = (JackClient*)ext_client;
  462. JackLog("jack_set_sample_rate_callback ext_client %x client %x \n", ext_client, client);
  463. if (client == NULL) {
  464. jack_error("jack_set_sample_rate_callback called with a NULL client");
  465. return -1;
  466. } else {
  467. JackLog("jack_set_sample_rate_callback: deprecated\n");
  468. return -1;
  469. }
  470. }
  471. EXPORT int jack_set_port_registration_callback(jack_client_t* ext_client, JackPortRegistrationCallback registration_callback, void* arg)
  472. {
  473. JackClient* client = (JackClient*)ext_client;
  474. if (client == NULL) {
  475. jack_error("jack_set_port_registration_callback called with a NULL client");
  476. return -1;
  477. } else {
  478. return client->SetPortRegistrationCallback(registration_callback, arg);
  479. }
  480. }
  481. EXPORT int jack_set_graph_order_callback(jack_client_t* ext_client, JackGraphOrderCallback graph_callback, void* arg)
  482. {
  483. JackClient* client = (JackClient*)ext_client;
  484. JackLog("jack_set_graph_order_callback ext_client %x client %x \n", ext_client, client);
  485. if (client == NULL) {
  486. jack_error("jack_set_graph_order_callback called with a NULL client");
  487. return -1;
  488. } else {
  489. return client->SetGraphOrderCallback(graph_callback, arg);
  490. }
  491. }
  492. EXPORT int jack_set_xrun_callback(jack_client_t* ext_client, JackXRunCallback callback, void* arg)
  493. {
  494. JackClient* client = (JackClient*)ext_client;
  495. if (client == NULL) {
  496. jack_error("jack_set_xrun_callback called with a NULL client");
  497. return -1;
  498. } else {
  499. return client->SetXRunCallback(callback, arg);
  500. }
  501. }
  502. EXPORT int jack_set_thread_init_callback(jack_client_t* ext_client, JackThreadInitCallback callback, void *arg)
  503. {
  504. JackClient* client = (JackClient*)ext_client;
  505. JackLog("jack_set_thread_init_callback ext_client %x client %x \n", ext_client, client);
  506. if (client == NULL) {
  507. jack_error("jack_set_thread_init_callback called with a NULL client");
  508. return -1;
  509. } else {
  510. return client->SetInitCallback(callback, arg);
  511. }
  512. }
  513. EXPORT int jack_activate(jack_client_t* ext_client)
  514. {
  515. JackClient* client = (JackClient*)ext_client;
  516. if (client == NULL) {
  517. jack_error("jack_activate called with a NULL client");
  518. return -1;
  519. } else {
  520. return client->Activate();
  521. }
  522. }
  523. EXPORT int jack_deactivate(jack_client_t* ext_client)
  524. {
  525. JackClient* client = (JackClient*)ext_client;
  526. if (client == NULL) {
  527. jack_error("jack_deactivate called with a NULL client");
  528. return -1;
  529. } else {
  530. return client->Deactivate();
  531. }
  532. }
  533. EXPORT jack_port_t* jack_port_register(jack_client_t* ext_client, const char* port_name, const char* port_type, unsigned long flags, unsigned long buffer_size)
  534. {
  535. JackClient* client = (JackClient*)ext_client;
  536. if (client == NULL) {
  537. jack_error("jack_port_register called with a NULL client");
  538. return NULL;
  539. } else if ((port_name == NULL) || (port_type == NULL)) {
  540. jack_error("jack_port_register called with a NULL port name or a NULL port_type");
  541. return NULL;
  542. } else {
  543. return (jack_port_t *)client->PortRegister(port_name, port_type, flags, buffer_size);
  544. }
  545. }
  546. EXPORT int jack_port_unregister(jack_client_t* ext_client, jack_port_t* port)
  547. {
  548. JackClient* client = (JackClient*)ext_client;
  549. if (client == NULL) {
  550. jack_error("jack_port_unregister called with a NULL client");
  551. return -1;
  552. }
  553. jack_port_id_t myport = (jack_port_id_t)port;
  554. if (!CheckPort(myport)) {
  555. jack_error("jack_port_unregister called with an incorrect port %ld", myport);
  556. return -1;
  557. }
  558. return client->PortUnRegister(myport);
  559. }
  560. EXPORT int jack_port_is_mine(const jack_client_t* ext_client, const jack_port_t* port)
  561. {
  562. JackClient* client = (JackClient*)ext_client;
  563. if (client == NULL) {
  564. jack_error("jack_port_is_mine called with a NULL client");
  565. return -1;
  566. }
  567. jack_port_id_t myport = (jack_port_id_t)port;
  568. if (!CheckPort(myport)) {
  569. jack_error("jack_port_is_mine called with an incorrect port %ld", myport);
  570. return -1;
  571. }
  572. return client->PortIsMine(myport);
  573. }
  574. EXPORT const char** jack_port_get_connections(const jack_port_t* port)
  575. {
  576. jack_port_id_t myport = (jack_port_id_t)port;
  577. if (!CheckPort(myport)) {
  578. jack_error("jack_port_get_connections called with an incorrect port %ld", myport);
  579. return NULL;
  580. } else {
  581. WaitGraphChange();
  582. return GetGraphManager()->GetConnections(myport);
  583. }
  584. }
  585. // Calling client does not need to "own" the port
  586. EXPORT const char** jack_port_get_all_connections(const jack_client_t* ext_client, const jack_port_t* port)
  587. {
  588. JackClient* client = (JackClient*)ext_client;
  589. if (client == NULL) {
  590. jack_error("jack_port_get_all_connections called with a NULL client");
  591. return NULL;
  592. }
  593. jack_port_id_t myport = (jack_port_id_t)port;
  594. if (!CheckPort(myport)) {
  595. jack_error("jack_port_get_all_connections called with an incorrect port %ld", myport);
  596. return NULL;
  597. } else {
  598. WaitGraphChange();
  599. return GetGraphManager()->GetConnections(myport);
  600. }
  601. }
  602. // Does not use the client parameter
  603. EXPORT int jack_port_lock(jack_client_t* ext_client, jack_port_t* port)
  604. {
  605. JackClient* client = (JackClient*)ext_client;
  606. if (client == NULL) {
  607. jack_error("jack_port_lock called with a NULL client");
  608. return -1;
  609. }
  610. jack_port_id_t myport = (jack_port_id_t)port;
  611. if (!CheckPort(myport)) {
  612. jack_error("jack_port_lock called with an incorrect port %ld", myport);
  613. return -1;
  614. } else {
  615. return (myport && client->PortIsMine(myport)) ? GetGraphManager()->GetPort(myport)->Lock() : -1;
  616. }
  617. }
  618. // Does not use the client parameter
  619. EXPORT int jack_port_unlock(jack_client_t* ext_client, jack_port_t* port)
  620. {
  621. JackClient* client = (JackClient*)ext_client;
  622. if (client == NULL) {
  623. jack_error("jack_port_unlock called with a NULL client");
  624. return -1;
  625. }
  626. jack_port_id_t myport = (jack_port_id_t)port;
  627. if (!CheckPort(myport)) {
  628. jack_error("jack_port_unlock called with an incorrect port %ld", myport);
  629. return -1;
  630. } else {
  631. return (myport && client->PortIsMine(myport)) ? GetGraphManager()->GetPort(myport)->Unlock() : -1;
  632. }
  633. }
  634. EXPORT jack_nframes_t jack_port_get_total_latency(jack_client_t* ext_client, jack_port_t* port)
  635. {
  636. JackClient* client = (JackClient*)ext_client;
  637. if (client == NULL) {
  638. jack_error("jack_port_get_total_latency called with a NULL client");
  639. return 0;
  640. }
  641. jack_port_id_t myport = (jack_port_id_t)port;
  642. if (!CheckPort(myport)) {
  643. jack_error("jack_port_get_total_latency called with an incorrect port %ld", myport);
  644. return 0;
  645. } else {
  646. // The latency computation is done each time
  647. WaitGraphChange();
  648. return GetGraphManager()->GetTotalLatency(myport);
  649. }
  650. }
  651. EXPORT int jack_connect(jack_client_t* ext_client, const char* src, const char* dst)
  652. {
  653. JackClient* client = (JackClient*)ext_client;
  654. if (client == NULL) {
  655. jack_error("jack_connect called with a NULL client");
  656. return -1;
  657. } else if ((src == NULL) || (dst == NULL)) {
  658. jack_error("jack_connect called with a NULL port name");
  659. return -1;
  660. } else {
  661. return client->PortConnect(src, dst);
  662. }
  663. }
  664. EXPORT int jack_disconnect(jack_client_t* ext_client, const char* src, const char* dst)
  665. {
  666. JackClient* client = (JackClient*)ext_client;
  667. if (client == NULL) {
  668. jack_error("jack_disconnect called with a NULL client");
  669. return -1;
  670. } else if ((src == NULL) || (dst == NULL)) {
  671. jack_error("jack_connect called with a NULL port name");
  672. return -1;
  673. } else {
  674. return client->PortDisconnect(src, dst);
  675. }
  676. }
  677. EXPORT int jack_port_connect(jack_client_t* ext_client, jack_port_t* src, jack_port_t* dst)
  678. {
  679. JackClient* client = (JackClient*)ext_client;
  680. if (client == NULL) {
  681. jack_error("jack_port_connect called with a NULL client");
  682. return -1;
  683. }
  684. jack_port_id_t mysrc = (jack_port_id_t)src;
  685. if (!CheckPort(mysrc)) {
  686. jack_error("jack_port_connect called with a NULL src port");
  687. return -1;
  688. }
  689. jack_port_id_t mydst = (jack_port_id_t)dst;
  690. if (!CheckPort(mydst)) {
  691. jack_error("jack_port_connect called with a NULL dst port");
  692. return -1;
  693. }
  694. return client->PortConnect(mysrc, mydst);
  695. }
  696. EXPORT int jack_port_disconnect(jack_client_t* ext_client, jack_port_t* src)
  697. {
  698. JackClient* client = (JackClient*)ext_client;
  699. if (client == NULL) {
  700. jack_error("jack_port_disconnect called with a NULL client");
  701. return -1;
  702. }
  703. jack_port_id_t myport = (jack_port_id_t)src;
  704. if (!CheckPort(myport)) {
  705. jack_error("jack_port_disconnect called with an incorrect port %ld", myport);
  706. return -1;
  707. }
  708. return client->PortDisconnect(myport);
  709. }
  710. EXPORT jack_nframes_t jack_get_sample_rate(jack_client_t* ext_client)
  711. {
  712. JackClient* client = (JackClient*)ext_client;
  713. if (client == NULL) {
  714. jack_error("jack_get_sample_rate called with a NULL client");
  715. return 0;
  716. } else {
  717. return GetEngineControl()->fSampleRate;
  718. }
  719. }
  720. EXPORT jack_nframes_t jack_get_buffer_size(jack_client_t* ext_client)
  721. {
  722. JackClient* client = (JackClient*)ext_client;
  723. if (client == NULL) {
  724. jack_error("jack_get_buffer_size called with a NULL client");
  725. return 0;
  726. } else {
  727. return GetEngineControl()->fBufferSize;
  728. }
  729. }
  730. EXPORT const char** jack_get_ports(jack_client_t* ext_client, const char* port_name_pattern, const char* type_name_pattern, unsigned long flags)
  731. {
  732. JackClient* client = (JackClient*)ext_client;
  733. if (client == NULL) {
  734. jack_error("jack_get_ports called with a NULL client");
  735. return NULL;
  736. }
  737. return GetGraphManager()->GetPorts(port_name_pattern, type_name_pattern, flags);
  738. }
  739. EXPORT jack_port_t* jack_port_by_name(jack_client_t* ext_client, const char* portname)
  740. {
  741. JackClient* client = (JackClient*)ext_client;
  742. if (client == NULL) {
  743. jack_error("jack_get_ports called with a NULL client");
  744. return 0;
  745. }
  746. if (portname == NULL) {
  747. jack_error("jack_port_by_name called with a NULL port name");
  748. return NULL;
  749. } else {
  750. int res = GetGraphManager()->GetPort(portname); // returns a port index at least > 1
  751. return (res == NO_PORT) ? NULL : (jack_port_t*)res;
  752. }
  753. }
  754. EXPORT jack_port_t* jack_port_by_id(const jack_client_t* ext_client, jack_port_id_t id)
  755. {
  756. /* jack_port_t* type is actually the port index */
  757. return (jack_port_t*)id;
  758. }
  759. EXPORT int jack_engine_takeover_timebase(jack_client_t* ext_client)
  760. {
  761. JackClient* client = (JackClient*)ext_client;
  762. if (client == NULL) {
  763. jack_error("jack_engine_takeover_timebase called with a NULL client");
  764. return -1;
  765. } else {
  766. jack_error("jack_engine_takeover_timebase : not yet implemented\n");
  767. return 0;
  768. }
  769. }
  770. EXPORT jack_nframes_t jack_frames_since_cycle_start(const jack_client_t* ext_client)
  771. {
  772. JackTimer timer;
  773. GetEngineControl()->fFrameTimer.ReadFrameTime(&timer);
  774. return (jack_nframes_t) floor((((float)GetEngineControl()->fSampleRate) / 1000000.0f) * (GetMicroSeconds() - timer.fCurrentCallback));
  775. }
  776. EXPORT jack_nframes_t jack_frame_time(const jack_client_t* ext_client)
  777. {
  778. JackClient* client = (JackClient*)ext_client;
  779. if (client == NULL) {
  780. jack_error("jack_frame_time called with a NULL client");
  781. return 0;
  782. } else {
  783. JackTimer timer;
  784. GetEngineControl()->fFrameTimer.ReadFrameTime(&timer);
  785. if (timer.fInitialized) {
  786. return timer.fFrames +
  787. (long) rint(((double) ((jack_time_t)(GetMicroSeconds() - timer.fCurrentWakeup)) /
  788. ((jack_time_t)(timer.fNextWakeUp - timer.fCurrentWakeup))) * GetEngineControl()->fBufferSize);
  789. } else {
  790. return 0;
  791. }
  792. }
  793. }
  794. EXPORT jack_nframes_t jack_last_frame_time(const jack_client_t* ext_client)
  795. {
  796. JackTimer timer;
  797. GetEngineControl()->fFrameTimer.ReadFrameTime(&timer);
  798. return timer.fFrames;
  799. }
  800. EXPORT float jack_cpu_load(jack_client_t* ext_client)
  801. {
  802. JackClient* client = (JackClient*)ext_client;
  803. if (client == NULL) {
  804. jack_error("jack_cpu_load called with a NULL client");
  805. return 0.0f;
  806. } else {
  807. return GetEngineControl()->fCPULoad;
  808. }
  809. }
  810. pthread_t EXPORT jack_client_thread_id(jack_client_t* ext_client)
  811. {
  812. JackClient* client = (JackClient*)ext_client;
  813. if (client == NULL) {
  814. jack_error("jack_client_thread_id called with a NULL client");
  815. return (pthread_t)NULL;
  816. } else {
  817. return client->GetThreadID();
  818. }
  819. }
  820. EXPORT char* jack_get_client_name (jack_client_t* ext_client)
  821. {
  822. JackClient* client = (JackClient*)ext_client;
  823. if (client == NULL) {
  824. jack_error("jack_get_client_name called with a NULL client");
  825. return NULL;
  826. } else {
  827. return client->GetClientControl()->fName;
  828. }
  829. }
  830. EXPORT int jack_client_name_size(void)
  831. {
  832. return JACK_CLIENT_NAME_SIZE;
  833. }
  834. EXPORT int jack_port_name_size(void)
  835. {
  836. return JACK_PORT_NAME_SIZE;
  837. }
  838. // transport.h
  839. EXPORT int jack_release_timebase(jack_client_t* ext_client)
  840. {
  841. JackClient* client = (JackClient*)ext_client;
  842. if (client == NULL) {
  843. jack_error("jack_release_timebase called with a NULL client");
  844. return -1;
  845. } else {
  846. return client->ReleaseTimebase();
  847. }
  848. }
  849. EXPORT int jack_set_sync_callback(jack_client_t* ext_client, JackSyncCallback sync_callback, void *arg)
  850. {
  851. JackClient* client = (JackClient*)ext_client;
  852. if (client == NULL) {
  853. jack_error("jack_set_sync_callback called with a NULL client");
  854. return -1;
  855. } else {
  856. return client->SetSyncCallback(sync_callback, arg);
  857. }
  858. }
  859. EXPORT int jack_set_sync_timeout(jack_client_t* ext_client, jack_time_t timeout)
  860. {
  861. JackClient* client = (JackClient*)ext_client;
  862. if (client == NULL) {
  863. jack_error("jack_set_sync_timeout called with a NULL client");
  864. return -1;
  865. } else {
  866. return client->SetSyncTimeout(timeout);
  867. }
  868. }
  869. EXPORT int jack_set_timebase_callback(jack_client_t* ext_client, int conditional, JackTimebaseCallback timebase_callback, void* arg)
  870. {
  871. JackClient* client = (JackClient*)ext_client;
  872. if (client == NULL) {
  873. jack_error("jack_set_timebase_callback called with a NULL client");
  874. return -1;
  875. } else {
  876. return client->SetTimebaseCallback(conditional, timebase_callback, arg);
  877. }
  878. }
  879. EXPORT int jack_transport_locate(jack_client_t* ext_client, jack_nframes_t frame)
  880. {
  881. JackClient* client = (JackClient*)ext_client;
  882. if (client == NULL) {
  883. jack_error("jack_transport_locate called with a NULL client");
  884. return -1;
  885. } else {
  886. return client->TransportLocate(frame);
  887. }
  888. }
  889. EXPORT jack_transport_state_t jack_transport_query(const jack_client_t* ext_client, jack_position_t* pos)
  890. {
  891. JackClient* client = (JackClient*)ext_client;
  892. if (client == NULL) {
  893. jack_error("jack_transport_query called with a NULL client");
  894. return JackTransportStopped;
  895. } else {
  896. return client->TransportQuery(pos);
  897. }
  898. }
  899. EXPORT jack_nframes_t jack_get_current_transport_frame(const jack_client_t* ext_client)
  900. {
  901. JackClient* client = (JackClient*)ext_client;
  902. if (client == NULL) {
  903. jack_error("jack_get_current_transport_frame called with a NULL client");
  904. return 0;
  905. } else {
  906. return client->GetCurrentTransportFrame();
  907. }
  908. }
  909. EXPORT int jack_transport_reposition(jack_client_t* ext_client, jack_position_t* pos)
  910. {
  911. JackClient* client = (JackClient*)ext_client;
  912. if (client == NULL) {
  913. jack_error("jack_transport_reposition called with a NULL client");
  914. return -1;
  915. } else {
  916. return client->TransportReposition(pos);
  917. }
  918. }
  919. EXPORT void jack_transport_start(jack_client_t* ext_client)
  920. {
  921. JackClient* client = (JackClient*)ext_client;
  922. if (client == NULL) {
  923. jack_error("jack_transport_start called with a NULL client");
  924. } else {
  925. client->TransportStart();
  926. }
  927. }
  928. EXPORT void jack_transport_stop(jack_client_t* ext_client)
  929. {
  930. JackClient* client = (JackClient*)ext_client;
  931. if (client == NULL) {
  932. jack_error("jack_transport_stop called with a NULL client");
  933. } else {
  934. client->TransportStop();
  935. }
  936. }
  937. // deprecated
  938. EXPORT void jack_get_transport_info(jack_client_t* ext_client, jack_transport_info_t* tinfo)
  939. {
  940. JackLog("jack_get_transport_info : deprecated");
  941. if (tinfo)
  942. memset(tinfo, 0, sizeof(jack_transport_info_t));
  943. }
  944. EXPORT void jack_set_transport_info(jack_client_t* ext_client, jack_transport_info_t* tinfo)
  945. {
  946. JackLog("jack_set_transport_info : deprecated");
  947. if (tinfo)
  948. memset(tinfo, 0, sizeof(jack_transport_info_t));
  949. }
  950. // statistics.h
  951. EXPORT float jack_get_max_delayed_usecs(jack_client_t* client)
  952. {
  953. JackLog("jack_get_max_delayed_usecs: not yet implemented\n");
  954. return 0.f;
  955. }
  956. EXPORT float jack_get_xrun_delayed_usecs(jack_client_t* client)
  957. {
  958. JackLog("jack_get_xrun_delayed_usecs: not yet implemented\n");
  959. return 0.f;
  960. }
  961. EXPORT void jack_reset_max_delayed_usecs(jack_client_t* client)
  962. {
  963. JackLog("jack_reset_max_delayed_usecs: not yet implemented\n");
  964. }
  965. // thread.h
  966. EXPORT int jack_acquire_real_time_scheduling(pthread_t thread, int priority)
  967. {
  968. JackLog("jack_acquire_real_time_scheduling: not yet implemented\n");
  969. return -1;
  970. }
  971. EXPORT int jack_client_create_thread(jack_client_t* client,
  972. pthread_t *thread,
  973. int priority,
  974. int realtime, /* boolean */
  975. void *(*start_routine)(void*),
  976. void *arg)
  977. {
  978. JackLog("jack_client_create_thread: not yet implemented\n");
  979. return -1;
  980. }
  981. EXPORT int jack_drop_real_time_scheduling(pthread_t thread)
  982. {
  983. JackLog("jack_drop_real_time_scheduling: not yet implemented\n");
  984. return -1;
  985. }
  986. // intclient.h
  987. EXPORT char* jack_get_internal_client_name(jack_client_t* client, jack_intclient_t intclient)
  988. {
  989. JackLog("jack_get_internal_client_name: not yet implemented\n");
  990. return "";
  991. }
  992. EXPORT jack_intclient_t jack_internal_client_handle(jack_client_t* client, const char* client_name, jack_status_t* status)
  993. {
  994. JackLog("jack_internal_client_handle: not yet implemented\n");
  995. return 0;
  996. }
  997. EXPORT jack_intclient_t jack_internal_client_load(jack_client_t* client, const char* client_name, jack_options_t options, jack_status_t* status, ...)
  998. {
  999. JackLog("jack_internal_client_load: not yet implemented\n");
  1000. return 0;
  1001. }
  1002. EXPORT jack_status_t jack_internal_client_unload(jack_client_t* client, jack_intclient_t intclient)
  1003. {
  1004. JackLog("jack_internal_client_unload: not yet implemented\n");
  1005. return JackFailure;
  1006. }