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.

1612 lines
51KB

  1. /*
  2. Copyright (C) 2001-2003 Paul Davis
  3. Copyright (C) 2004-2008 Grame
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  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 "JackAPI.h"
  25. #ifdef __APPLE__
  26. #include "JackMachThread.h"
  27. #elif WIN32
  28. #include "JackWinThread.h"
  29. #else
  30. #include "JackPosixThread.h"
  31. #endif
  32. #include <math.h>
  33. #ifdef __CLIENTDEBUG__
  34. #include "JackLibGlobals.h"
  35. #endif
  36. using namespace Jack;
  37. #ifdef __cplusplus
  38. extern "C"
  39. {
  40. #endif
  41. EXPORT jack_client_t * jack_client_open (const char *client_name,
  42. jack_options_t options,
  43. jack_status_t *status, ...);
  44. EXPORT jack_client_t * jack_client_new (const char *client_name);
  45. EXPORT int jack_client_name_size (void);
  46. EXPORT char* jack_get_client_name (jack_client_t *client);
  47. EXPORT int jack_internal_client_new (const char *client_name,
  48. const char *load_name,
  49. const char *load_init);
  50. EXPORT void jack_internal_client_close (const char *client_name);
  51. EXPORT int jack_is_realtime (jack_client_t *client);
  52. EXPORT void jack_on_shutdown (jack_client_t *client,
  53. void (*function)(void *arg), void *arg);
  54. EXPORT int jack_set_process_callback (jack_client_t *client,
  55. JackProcessCallback process_callback,
  56. void *arg);
  57. EXPORT jack_nframes_t jack_thread_wait(jack_client_t *client, int status);
  58. EXPORT int jack_set_thread_init_callback (jack_client_t *client,
  59. JackThreadInitCallback thread_init_callback,
  60. void *arg);
  61. EXPORT int jack_set_freewheel_callback (jack_client_t *client,
  62. JackFreewheelCallback freewheel_callback,
  63. void *arg);
  64. EXPORT int jack_set_freewheel(jack_client_t* client, int onoff);
  65. EXPORT int jack_set_buffer_size (jack_client_t *client, jack_nframes_t nframes);
  66. EXPORT int jack_set_buffer_size_callback (jack_client_t *client,
  67. JackBufferSizeCallback bufsize_callback,
  68. void *arg);
  69. EXPORT int jack_set_sample_rate_callback (jack_client_t *client,
  70. JackSampleRateCallback srate_callback,
  71. void *arg);
  72. EXPORT int jack_set_client_registration_callback (jack_client_t *,
  73. JackClientRegistrationCallback
  74. registration_callback, void *arg);
  75. EXPORT int jack_set_port_registration_callback (jack_client_t *,
  76. JackPortRegistrationCallback
  77. registration_callback, void *arg);
  78. EXPORT int jack_set_port_connect_callback (jack_client_t *,
  79. JackPortConnectCallback
  80. connect_callback, void *arg);
  81. EXPORT int jack_set_graph_order_callback (jack_client_t *,
  82. JackGraphOrderCallback graph_callback,
  83. void *);
  84. EXPORT int jack_set_xrun_callback (jack_client_t *,
  85. JackXRunCallback xrun_callback, void *arg);
  86. EXPORT int jack_activate (jack_client_t *client);
  87. EXPORT int jack_deactivate (jack_client_t *client);
  88. EXPORT jack_port_t * jack_port_register (jack_client_t *client,
  89. const char *port_name,
  90. const char *port_type,
  91. unsigned long flags,
  92. unsigned long buffer_size);
  93. EXPORT int jack_port_unregister (jack_client_t *, jack_port_t *);
  94. EXPORT void * jack_port_get_buffer (jack_port_t *, jack_nframes_t);
  95. EXPORT const char * jack_port_name (const jack_port_t *port);
  96. EXPORT const char * jack_port_short_name (const jack_port_t *port);
  97. EXPORT int jack_port_flags (const jack_port_t *port);
  98. EXPORT const char * jack_port_type (const jack_port_t *port);
  99. EXPORT int jack_port_is_mine (const jack_client_t *, const jack_port_t *port);
  100. EXPORT int jack_port_connected (const jack_port_t *port);
  101. EXPORT int jack_port_connected_to (const jack_port_t *port,
  102. const char *port_name);
  103. EXPORT const char ** jack_port_get_connections (const jack_port_t *port);
  104. EXPORT const char ** jack_port_get_all_connections (const jack_client_t *client,
  105. const jack_port_t *port);
  106. EXPORT int jack_port_tie (jack_port_t *src, jack_port_t *dst);
  107. EXPORT int jack_port_untie (jack_port_t *port);
  108. EXPORT jack_nframes_t jack_port_get_latency (jack_port_t *port);
  109. EXPORT jack_nframes_t jack_port_get_total_latency (jack_client_t *,
  110. jack_port_t *port);
  111. EXPORT void jack_port_set_latency (jack_port_t *, jack_nframes_t);
  112. EXPORT int jack_recompute_total_latency (jack_client_t*, jack_port_t* port);
  113. EXPORT int jack_recompute_total_latencies (jack_client_t*);
  114. EXPORT int jack_port_set_name (jack_port_t *port, const char *port_name);
  115. EXPORT int jack_port_set_alias (jack_port_t *port, const char *alias);
  116. EXPORT int jack_port_unset_alias (jack_port_t *port, const char *alias);
  117. EXPORT int jack_port_get_aliases (const jack_port_t *port, char* const aliases[2]);
  118. EXPORT int jack_port_request_monitor (jack_port_t *port, int onoff);
  119. EXPORT int jack_port_request_monitor_by_name (jack_client_t *client,
  120. const char *port_name, int onoff);
  121. EXPORT int jack_port_ensure_monitor (jack_port_t *port, int onoff);
  122. EXPORT int jack_port_monitoring_input (jack_port_t *port);
  123. EXPORT int jack_connect (jack_client_t *,
  124. const char *source_port,
  125. const char *destination_port);
  126. EXPORT int jack_disconnect (jack_client_t *,
  127. const char *source_port,
  128. const char *destination_port);
  129. EXPORT int jack_port_disconnect (jack_client_t *, jack_port_t *);
  130. EXPORT int jack_port_name_size(void);
  131. EXPORT int jack_port_type_size(void);
  132. EXPORT jack_nframes_t jack_get_sample_rate (jack_client_t *);
  133. EXPORT jack_nframes_t jack_get_buffer_size (jack_client_t *);
  134. EXPORT const char ** jack_get_ports (jack_client_t *,
  135. const char *port_name_pattern,
  136. const char *type_name_pattern,
  137. unsigned long flags);
  138. EXPORT jack_port_t * jack_port_by_name (jack_client_t *, const char *port_name);
  139. EXPORT jack_port_t * jack_port_by_id (jack_client_t *client,
  140. jack_port_id_t port_id);
  141. EXPORT int jack_engine_takeover_timebase (jack_client_t *);
  142. EXPORT jack_nframes_t jack_frames_since_cycle_start (const jack_client_t *);
  143. EXPORT jack_time_t jack_get_time(const jack_client_t *client);
  144. EXPORT jack_nframes_t jack_time_to_frames(const jack_client_t *client, jack_time_t time);
  145. EXPORT jack_time_t jack_frames_to_time(const jack_client_t *client, jack_nframes_t frames);
  146. EXPORT jack_nframes_t jack_frame_time (const jack_client_t *);
  147. EXPORT jack_nframes_t jack_last_frame_time (const jack_client_t *client);
  148. EXPORT float jack_cpu_load (jack_client_t *client);
  149. EXPORT pthread_t jack_client_thread_id (jack_client_t *);
  150. EXPORT void jack_set_error_function (void (*func)(const char *));
  151. EXPORT float jack_get_max_delayed_usecs (jack_client_t *client);
  152. EXPORT float jack_get_xrun_delayed_usecs (jack_client_t *client);
  153. EXPORT void jack_reset_max_delayed_usecs (jack_client_t *client);
  154. EXPORT int jack_release_timebase (jack_client_t *client);
  155. EXPORT int jack_set_sync_callback (jack_client_t *client,
  156. JackSyncCallback sync_callback,
  157. void *arg);
  158. EXPORT int jack_set_sync_timeout (jack_client_t *client,
  159. jack_time_t timeout);
  160. EXPORT int jack_set_timebase_callback (jack_client_t *client,
  161. int conditional,
  162. JackTimebaseCallback timebase_callback,
  163. void *arg);
  164. EXPORT int jack_transport_locate (jack_client_t *client,
  165. jack_nframes_t frame);
  166. EXPORT jack_transport_state_t jack_transport_query (const jack_client_t *client,
  167. jack_position_t *pos);
  168. EXPORT jack_nframes_t jack_get_current_transport_frame (const jack_client_t *client);
  169. EXPORT int jack_transport_reposition (jack_client_t *client,
  170. jack_position_t *pos);
  171. EXPORT void jack_transport_start (jack_client_t *client);
  172. EXPORT void jack_transport_stop (jack_client_t *client);
  173. EXPORT void jack_get_transport_info (jack_client_t *client,
  174. jack_transport_info_t *tinfo);
  175. EXPORT void jack_set_transport_info (jack_client_t *client,
  176. jack_transport_info_t *tinfo);
  177. EXPORT int jack_acquire_real_time_scheduling (pthread_t thread, int priority);
  178. EXPORT int jack_client_create_thread (jack_client_t* client,
  179. pthread_t *thread,
  180. int priority,
  181. int realtime, // boolean
  182. void *(*start_routine)(void*),
  183. void *arg);
  184. EXPORT int jack_drop_real_time_scheduling (pthread_t thread);
  185. EXPORT char * jack_get_internal_client_name (jack_client_t *client,
  186. jack_intclient_t intclient);
  187. EXPORT jack_intclient_t jack_internal_client_handle (jack_client_t *client,
  188. const char *client_name,
  189. jack_status_t *status);
  190. EXPORT jack_intclient_t jack_internal_client_load (jack_client_t *client,
  191. const char *client_name,
  192. jack_options_t options,
  193. jack_status_t *status, ...);
  194. EXPORT jack_status_t jack_internal_client_unload (jack_client_t *client,
  195. jack_intclient_t intclient);
  196. #ifdef __cplusplus
  197. }
  198. #endif
  199. #ifdef WIN32
  200. /* missing on Windows : see http://bugs.mysql.com/bug.php?id=15936 */
  201. inline double rint(double nr)
  202. {
  203. double f = floor(nr);
  204. double c = ceil(nr);
  205. return (((c -nr) >= (nr - f)) ? f : c);
  206. }
  207. #endif
  208. static inline bool CheckPort(jack_port_id_t port_index)
  209. {
  210. return (port_index > 0 && port_index < PORT_NUM);
  211. }
  212. static inline bool CheckBufferSize(jack_nframes_t buffer_size)
  213. {
  214. return (buffer_size <= BUFFER_SIZE_MAX);
  215. }
  216. static inline void WaitGraphChange()
  217. {
  218. JackGraphManager* manager = GetGraphManager();
  219. JackEngineControl* control = GetEngineControl();
  220. if (manager && control && manager->IsPendingChange()) {
  221. JackLog("WaitGraphChange...\n");
  222. JackSleep(int(control->fPeriodUsecs * 1.1f));
  223. }
  224. }
  225. EXPORT void jack_set_error_function (void (*func)(const char *))
  226. {
  227. jack_error_callback = func;
  228. }
  229. EXPORT jack_client_t* jack_client_new(const char* client_name)
  230. {
  231. jack_error("jack_client_new: deprecated");
  232. int options = JackUseExactName;
  233. if (getenv("JACK_START_SERVER") == NULL)
  234. options |= JackNoStartServer;
  235. return jack_client_open(client_name, (jack_options_t)options, NULL);
  236. }
  237. EXPORT void* jack_port_get_buffer(jack_port_t* port, jack_nframes_t frames)
  238. {
  239. #ifdef __CLIENTDEBUG__
  240. JackLibGlobals::CheckContext();
  241. #endif
  242. jack_port_id_t myport = (jack_port_id_t)port;
  243. if (!CheckPort(myport)) {
  244. jack_error("jack_port_get_buffer called with an incorrect port %ld", myport);
  245. return NULL;
  246. } else {
  247. JackGraphManager* manager = GetGraphManager();
  248. return (manager ? manager->GetBuffer(myport, frames) : NULL);
  249. }
  250. }
  251. EXPORT const char* jack_port_name(const jack_port_t* port)
  252. {
  253. #ifdef __CLIENTDEBUG__
  254. JackLibGlobals::CheckContext();
  255. #endif
  256. jack_port_id_t myport = (jack_port_id_t)port;
  257. if (!CheckPort(myport)) {
  258. jack_error("jack_port_name called with an incorrect port %ld", myport);
  259. return NULL;
  260. } else {
  261. JackGraphManager* manager = GetGraphManager();
  262. return (manager ? manager->GetPort(myport)->GetName() : NULL);
  263. }
  264. }
  265. EXPORT const char* jack_port_short_name(const jack_port_t* port)
  266. {
  267. #ifdef __CLIENTDEBUG__
  268. JackLibGlobals::CheckContext();
  269. #endif
  270. jack_port_id_t myport = (jack_port_id_t)port;
  271. if (!CheckPort(myport)) {
  272. jack_error("jack_port_short_name called with an incorrect port %ld", myport);
  273. return NULL;
  274. } else {
  275. JackGraphManager* manager = GetGraphManager();
  276. return (manager ? manager->GetPort(myport)->GetShortName() : NULL);
  277. }
  278. }
  279. EXPORT int jack_port_flags(const jack_port_t* port)
  280. {
  281. #ifdef __CLIENTDEBUG__
  282. JackLibGlobals::CheckContext();
  283. #endif
  284. jack_port_id_t myport = (jack_port_id_t)port;
  285. if (!CheckPort(myport)) {
  286. jack_error("jack_port_flags called with an incorrect port %ld", myport);
  287. return -1;
  288. } else {
  289. JackGraphManager* manager = GetGraphManager();
  290. return (manager ? manager->GetPort(myport)->GetFlags() : -1);
  291. }
  292. }
  293. EXPORT const char* jack_port_type(const jack_port_t* port)
  294. {
  295. #ifdef __CLIENTDEBUG__
  296. JackLibGlobals::CheckContext();
  297. #endif
  298. jack_port_id_t myport = (jack_port_id_t)port;
  299. if (!CheckPort(myport)) {
  300. jack_error("jack_port_flags called an incorrect port %ld", myport);
  301. return NULL;
  302. } else {
  303. JackGraphManager* manager = GetGraphManager();
  304. return (manager ? manager->GetPort(myport)->GetType() : NULL);
  305. }
  306. }
  307. EXPORT int jack_port_connected(const jack_port_t* port)
  308. {
  309. #ifdef __CLIENTDEBUG__
  310. JackLibGlobals::CheckContext();
  311. #endif
  312. jack_port_id_t myport = (jack_port_id_t)port;
  313. if (!CheckPort(myport)) {
  314. jack_error("jack_port_connected called with an incorrect port %ld", myport);
  315. return -1;
  316. } else {
  317. WaitGraphChange();
  318. JackGraphManager* manager = GetGraphManager();
  319. return (manager ? manager->GetConnectionsNum(myport) : -1);
  320. }
  321. }
  322. EXPORT int jack_port_connected_to(const jack_port_t* port, const char* port_name)
  323. {
  324. #ifdef __CLIENTDEBUG__
  325. JackLibGlobals::CheckContext();
  326. #endif
  327. jack_port_id_t src = (jack_port_id_t)port;
  328. if (!CheckPort(src)) {
  329. jack_error("jack_port_connected_to called with an incorrect port %ld", src);
  330. return -1;
  331. } else if (port_name == NULL) {
  332. jack_error("jack_port_connected_to called with a NULL port name");
  333. return -1;
  334. } else {
  335. WaitGraphChange();
  336. JackGraphManager* manager = GetGraphManager();
  337. jack_port_id_t dst = (manager ? manager->GetPort(port_name) : NO_PORT);
  338. if (dst == NO_PORT) {
  339. jack_error("Unknown destination port port_name = %s", port_name);
  340. return 0;
  341. } else {
  342. return manager->IsConnected(src, dst);
  343. }
  344. }
  345. }
  346. EXPORT int jack_port_tie(jack_port_t* src, jack_port_t* dst)
  347. {
  348. #ifdef __CLIENTDEBUG__
  349. JackLibGlobals::CheckContext();
  350. #endif
  351. jack_port_id_t mysrc = (jack_port_id_t)src;
  352. if (!CheckPort(mysrc)) {
  353. jack_error("jack_port_tie called with a NULL src port");
  354. return -1;
  355. }
  356. jack_port_id_t mydst = (jack_port_id_t)dst;
  357. if (!CheckPort(mydst)) {
  358. jack_error("jack_port_tie called with a NULL dst port");
  359. return -1;
  360. }
  361. JackGraphManager* manager = GetGraphManager();
  362. if (manager && manager->GetPort(mysrc)->GetRefNum() != manager->GetPort(mydst)->GetRefNum()) {
  363. jack_error("jack_port_tie called with ports not belonging to the same client");
  364. return -1;
  365. } else {
  366. return manager->GetPort(mydst)->Tie(mysrc);
  367. }
  368. }
  369. EXPORT int jack_port_untie(jack_port_t* port)
  370. {
  371. #ifdef __CLIENTDEBUG__
  372. JackLibGlobals::CheckContext();
  373. #endif
  374. jack_port_id_t myport = (jack_port_id_t)port;
  375. if (!CheckPort(myport)) {
  376. jack_error("jack_port_untie called with an incorrect port %ld", myport);
  377. return -1;
  378. } else {
  379. JackGraphManager* manager = GetGraphManager();
  380. return (manager ? manager->GetPort(myport)->UnTie() : -1);
  381. }
  382. }
  383. EXPORT jack_nframes_t jack_port_get_latency(jack_port_t* port)
  384. {
  385. #ifdef __CLIENTDEBUG__
  386. JackLibGlobals::CheckContext();
  387. #endif
  388. jack_port_id_t myport = (jack_port_id_t)port;
  389. if (!CheckPort(myport)) {
  390. jack_error("jack_port_get_latency called with an incorrect port %ld", myport);
  391. return 0;
  392. } else {
  393. WaitGraphChange();
  394. JackGraphManager* manager = GetGraphManager();
  395. return (manager ? manager->GetPort(myport)->GetLatency() : 0);
  396. }
  397. }
  398. EXPORT void jack_port_set_latency(jack_port_t* port, jack_nframes_t frames)
  399. {
  400. #ifdef __CLIENTDEBUG__
  401. JackLibGlobals::CheckContext();
  402. #endif
  403. jack_port_id_t myport = (jack_port_id_t)port;
  404. if (!CheckPort(myport)) {
  405. jack_error("jack_port_set_latency called with an incorrect port %ld", myport);
  406. } else {
  407. JackGraphManager* manager = GetGraphManager();
  408. if (manager)
  409. manager->GetPort(myport)->SetLatency(frames);
  410. }
  411. }
  412. EXPORT int jack_recompute_total_latency(jack_client_t* ext_client, jack_port_t* port)
  413. {
  414. #ifdef __CLIENTDEBUG__
  415. JackLibGlobals::CheckContext();
  416. #endif
  417. JackClient* client = (JackClient*)ext_client;
  418. jack_port_id_t myport = (jack_port_id_t)port;
  419. if (client == NULL) {
  420. jack_error("jack_recompute_total_latencies called with a NULL client");
  421. return -1;
  422. } else if (!CheckPort(myport)) {
  423. jack_error("jack_recompute_total_latencies called with a NULL port");
  424. return -1;
  425. } else {
  426. WaitGraphChange();
  427. JackGraphManager* manager = GetGraphManager();
  428. return (manager ? manager->ComputeTotalLatency(myport) : -1);
  429. }
  430. }
  431. EXPORT int jack_recompute_total_latencies(jack_client_t* ext_client)
  432. {
  433. #ifdef __CLIENTDEBUG__
  434. JackLibGlobals::CheckContext();
  435. #endif
  436. JackClient* client = (JackClient*)ext_client;
  437. if (client == NULL) {
  438. jack_error("jack_recompute_total_latencies called with a NULL client");
  439. return -1;
  440. } else {
  441. WaitGraphChange();
  442. JackGraphManager* manager = GetGraphManager();
  443. return (manager ? manager->ComputeTotalLatencies() : -1);
  444. }
  445. }
  446. EXPORT int jack_port_set_name(jack_port_t* port, const char* name)
  447. {
  448. #ifdef __CLIENTDEBUG__
  449. JackLibGlobals::CheckContext();
  450. #endif
  451. jack_port_id_t myport = (jack_port_id_t)port;
  452. if (!CheckPort(myport)) {
  453. jack_error("jack_port_set_name called with an incorrect port %ld", myport);
  454. return -1;
  455. } else if (name == NULL) {
  456. jack_error("jack_port_set_name called with a NULL port name");
  457. return -1;
  458. } else {
  459. JackGraphManager* manager = GetGraphManager();
  460. return (manager ? manager->GetPort(myport)->SetName(name) : -1);
  461. }
  462. }
  463. EXPORT int jack_port_set_alias(jack_port_t* port, const char* name)
  464. {
  465. #ifdef __CLIENTDEBUG__
  466. JackLibGlobals::CheckContext();
  467. #endif
  468. jack_port_id_t myport = (jack_port_id_t)port;
  469. if (!CheckPort(myport)) {
  470. jack_error("jack_port_set_alias called with an incorrect port %ld", myport);
  471. return -1;
  472. } else if (name == NULL) {
  473. jack_error("jack_port_set_alias called with a NULL port name");
  474. return -1;
  475. } else {
  476. JackGraphManager* manager = GetGraphManager();
  477. return (manager ? manager->GetPort(myport)->SetAlias(name) : -1);
  478. }
  479. }
  480. EXPORT int jack_port_unset_alias(jack_port_t* port, const char* name)
  481. {
  482. #ifdef __CLIENTDEBUG__
  483. JackLibGlobals::CheckContext();
  484. #endif
  485. jack_port_id_t myport = (jack_port_id_t)port;
  486. if (!CheckPort(myport)) {
  487. jack_error("jack_port_unset_alias called with an incorrect port %ld", myport);
  488. return -1;
  489. } else if (name == NULL) {
  490. jack_error("jack_port_unset_alias called with a NULL port name");
  491. return -1;
  492. } else {
  493. JackGraphManager* manager = GetGraphManager();
  494. return (manager ? manager->GetPort(myport)->UnsetAlias(name) : -1);
  495. }
  496. }
  497. EXPORT int jack_port_get_aliases(const jack_port_t* port, char* const aliases[2])
  498. {
  499. #ifdef __CLIENTDEBUG__
  500. JackLibGlobals::CheckContext();
  501. #endif
  502. jack_port_id_t myport = (jack_port_id_t)port;
  503. if (!CheckPort(myport)) {
  504. jack_error("jack_port_get_aliases called with an incorrect port %ld", myport);
  505. return -1;
  506. } else {
  507. JackGraphManager* manager = GetGraphManager();
  508. return (manager ? manager->GetPort(myport)->GetAliases(aliases) : -1);
  509. }
  510. }
  511. EXPORT int jack_port_request_monitor(jack_port_t* port, int onoff)
  512. {
  513. #ifdef __CLIENTDEBUG__
  514. JackLibGlobals::CheckContext();
  515. #endif
  516. jack_port_id_t myport = (jack_port_id_t)port;
  517. if (!CheckPort(myport)) {
  518. jack_error("jack_port_request_monitor called with an incorrect port %ld", myport);
  519. return -1;
  520. } else {
  521. JackGraphManager* manager = GetGraphManager();
  522. return (manager ? manager->RequestMonitor(myport, onoff) : -1);
  523. }
  524. }
  525. EXPORT int jack_port_request_monitor_by_name(jack_client_t* ext_client, const char* port_name, int onoff)
  526. {
  527. #ifdef __CLIENTDEBUG__
  528. JackLibGlobals::CheckContext();
  529. #endif
  530. JackClient* client = (JackClient*)ext_client;
  531. if (client == NULL) {
  532. jack_error("jack_port_request_monitor_by_name called with a NULL client");
  533. return -1;
  534. } else {
  535. JackGraphManager* manager = GetGraphManager();
  536. if (!manager)
  537. return -1;
  538. jack_port_id_t myport = manager->GetPort(port_name);
  539. if (!CheckPort(myport)) {
  540. jack_error("jack_port_request_monitor_by_name called with an incorrect port %s", port_name);
  541. return -1;
  542. } else {
  543. return manager->RequestMonitor(myport, onoff);
  544. }
  545. }
  546. }
  547. EXPORT int jack_port_ensure_monitor(jack_port_t* port, int onoff)
  548. {
  549. #ifdef __CLIENTDEBUG__
  550. JackLibGlobals::CheckContext();
  551. #endif
  552. jack_port_id_t myport = (jack_port_id_t)port;
  553. if (!CheckPort(myport)) {
  554. jack_error("jack_port_ensure_monitor called with an incorrect port %ld", myport);
  555. return -1;
  556. } else {
  557. JackGraphManager* manager = GetGraphManager();
  558. return (manager ? manager->GetPort(myport)->EnsureMonitor(onoff) : -1);
  559. }
  560. }
  561. EXPORT int jack_port_monitoring_input(jack_port_t* port)
  562. {
  563. #ifdef __CLIENTDEBUG__
  564. JackLibGlobals::CheckContext();
  565. #endif
  566. jack_port_id_t myport = (jack_port_id_t)port;
  567. if (!CheckPort(myport)) {
  568. jack_error("jack_port_monitoring_input called with an incorrect port %ld", myport);
  569. return -1;
  570. } else {
  571. JackGraphManager* manager = GetGraphManager();
  572. return (manager ? manager->GetPort(myport)->MonitoringInput() : -1);
  573. }
  574. }
  575. EXPORT int jack_is_realtime(jack_client_t* ext_client)
  576. {
  577. #ifdef __CLIENTDEBUG__
  578. JackLibGlobals::CheckContext();
  579. #endif
  580. JackClient* client = (JackClient*)ext_client;
  581. if (client == NULL) {
  582. jack_error("jack_is_realtime called with a NULL client");
  583. return -1;
  584. } else {
  585. JackEngineControl* control = GetEngineControl();
  586. return (control ? control->fRealTime : -1);
  587. }
  588. }
  589. EXPORT void jack_on_shutdown(jack_client_t* ext_client, void (*function)(void* arg), void* arg)
  590. {
  591. #ifdef __CLIENTDEBUG__
  592. JackLibGlobals::CheckContext();
  593. #endif
  594. JackClient* client = (JackClient*)ext_client;
  595. if (client == NULL) {
  596. jack_error("jack_on_shutdown called with a NULL client");
  597. } else {
  598. client->OnShutdown(function, arg);
  599. }
  600. }
  601. EXPORT int jack_set_process_callback(jack_client_t* ext_client, JackProcessCallback callback, void* arg)
  602. {
  603. #ifdef __CLIENTDEBUG__
  604. JackLibGlobals::CheckContext();
  605. #endif
  606. JackClient* client = (JackClient*)ext_client;
  607. if (client == NULL) {
  608. jack_error("jack_set_process_callback called with a NULL client");
  609. return -1;
  610. } else {
  611. return client->SetProcessCallback(callback, arg);
  612. }
  613. }
  614. EXPORT jack_nframes_t jack_thread_wait(jack_client_t* ext_client, int status)
  615. {
  616. #ifdef __CLIENTDEBUG__
  617. JackLibGlobals::CheckContext();
  618. #endif
  619. JackClient* client = (JackClient*)ext_client;
  620. if (client == NULL) {
  621. jack_error("jack_thread_wait called with a NULL client");
  622. return 0;
  623. } else {
  624. return client->Wait(status);
  625. }
  626. }
  627. EXPORT int jack_set_freewheel_callback(jack_client_t* ext_client, JackFreewheelCallback freewheel_callback, void* arg)
  628. {
  629. #ifdef __CLIENTDEBUG__
  630. JackLibGlobals::CheckContext();
  631. #endif
  632. JackClient* client = (JackClient*)ext_client;
  633. if (client == NULL) {
  634. jack_error("jack_set_freewheel_callback called with a NULL client");
  635. return -1;
  636. } else {
  637. return client->SetFreewheelCallback(freewheel_callback, arg);
  638. }
  639. }
  640. EXPORT int jack_set_freewheel(jack_client_t* ext_client, int onoff)
  641. {
  642. #ifdef __CLIENTDEBUG__
  643. JackLibGlobals::CheckContext();
  644. #endif
  645. JackClient* client = (JackClient*)ext_client;
  646. if (client == NULL) {
  647. jack_error("jack_set_freewheel called with a NULL client");
  648. return -1;
  649. } else {
  650. return client->SetFreeWheel(onoff);
  651. }
  652. }
  653. EXPORT int jack_set_buffer_size(jack_client_t* ext_client, jack_nframes_t buffer_size)
  654. {
  655. #ifdef __CLIENTDEBUG__
  656. JackLibGlobals::CheckContext();
  657. #endif
  658. JackClient* client = (JackClient*)ext_client;
  659. if (client == NULL) {
  660. jack_error("jack_set_buffer_size called with a NULL client");
  661. return -1;
  662. } else if (!CheckBufferSize(buffer_size)) {
  663. return -1;
  664. } else {
  665. return client->SetBufferSize(buffer_size);
  666. }
  667. }
  668. EXPORT int jack_set_buffer_size_callback(jack_client_t* ext_client, JackBufferSizeCallback bufsize_callback, void* arg)
  669. {
  670. #ifdef __CLIENTDEBUG__
  671. JackLibGlobals::CheckContext();
  672. #endif
  673. JackClient* client = (JackClient*)ext_client;
  674. if (client == NULL) {
  675. jack_error("jack_set_buffer_size_callback called with a NULL client");
  676. return -1;
  677. } else {
  678. return client->SetBufferSizeCallback(bufsize_callback, arg);
  679. }
  680. }
  681. EXPORT int jack_set_sample_rate_callback(jack_client_t* ext_client, JackSampleRateCallback srate_callback, void* arg)
  682. {
  683. #ifdef __CLIENTDEBUG__
  684. JackLibGlobals::CheckContext();
  685. #endif
  686. JackClient* client = (JackClient*)ext_client;
  687. if (client == NULL) {
  688. jack_error("jack_set_sample_rate_callback called with a NULL client");
  689. return -1;
  690. } else {
  691. jack_error("jack_set_sample_rate_callback: deprecated");
  692. return 0;
  693. }
  694. }
  695. EXPORT int jack_set_client_registration_callback(jack_client_t* ext_client, JackClientRegistrationCallback registration_callback, void* arg)
  696. {
  697. #ifdef __CLIENTDEBUG__
  698. JackLibGlobals::CheckContext();
  699. #endif
  700. JackClient* client = (JackClient*)ext_client;
  701. if (client == NULL) {
  702. jack_error("jack_set_client_registration_callback called with a NULL client");
  703. return -1;
  704. } else {
  705. return client->SetClientRegistrationCallback(registration_callback, arg);
  706. }
  707. }
  708. EXPORT int jack_set_port_registration_callback(jack_client_t* ext_client, JackPortRegistrationCallback registration_callback, void* arg)
  709. {
  710. #ifdef __CLIENTDEBUG__
  711. JackLibGlobals::CheckContext();
  712. #endif
  713. JackClient* client = (JackClient*)ext_client;
  714. if (client == NULL) {
  715. jack_error("jack_set_port_registration_callback called with a NULL client");
  716. return -1;
  717. } else {
  718. return client->SetPortRegistrationCallback(registration_callback, arg);
  719. }
  720. }
  721. EXPORT int jack_set_port_connect_callback(jack_client_t* ext_client, JackPortConnectCallback portconnect_callback, void* arg)
  722. {
  723. #ifdef __CLIENTDEBUG__
  724. JackLibGlobals::CheckContext();
  725. #endif
  726. JackClient* client = (JackClient*)ext_client;
  727. if (client == NULL) {
  728. jack_error("jack_set_port_connect_callback called with a NULL client");
  729. return -1;
  730. } else {
  731. return client->SetPortConnectCallback(portconnect_callback, arg);
  732. }
  733. }
  734. EXPORT int jack_set_graph_order_callback(jack_client_t* ext_client, JackGraphOrderCallback graph_callback, void* arg)
  735. {
  736. #ifdef __CLIENTDEBUG__
  737. JackLibGlobals::CheckContext();
  738. #endif
  739. JackClient* client = (JackClient*)ext_client;
  740. JackLog("jack_set_graph_order_callback ext_client %x client %x \n", ext_client, client);
  741. if (client == NULL) {
  742. jack_error("jack_set_graph_order_callback called with a NULL client");
  743. return -1;
  744. } else {
  745. return client->SetGraphOrderCallback(graph_callback, arg);
  746. }
  747. }
  748. EXPORT int jack_set_xrun_callback(jack_client_t* ext_client, JackXRunCallback xrun_callback, void* arg)
  749. {
  750. #ifdef __CLIENTDEBUG__
  751. JackLibGlobals::CheckContext();
  752. #endif
  753. JackClient* client = (JackClient*)ext_client;
  754. if (client == NULL) {
  755. jack_error("jack_set_xrun_callback called with a NULL client");
  756. return -1;
  757. } else {
  758. return client->SetXRunCallback(xrun_callback, arg);
  759. }
  760. }
  761. EXPORT int jack_set_thread_init_callback(jack_client_t* ext_client, JackThreadInitCallback init_callback, void *arg)
  762. {
  763. #ifdef __CLIENTDEBUG__
  764. JackLibGlobals::CheckContext();
  765. #endif
  766. JackClient* client = (JackClient*)ext_client;
  767. JackLog("jack_set_thread_init_callback ext_client %x client %x \n", ext_client, client);
  768. if (client == NULL) {
  769. jack_error("jack_set_thread_init_callback called with a NULL client");
  770. return -1;
  771. } else {
  772. return client->SetInitCallback(init_callback, arg);
  773. }
  774. }
  775. EXPORT int jack_activate(jack_client_t* ext_client)
  776. {
  777. #ifdef __CLIENTDEBUG__
  778. JackLibGlobals::CheckContext();
  779. #endif
  780. JackClient* client = (JackClient*)ext_client;
  781. if (client == NULL) {
  782. jack_error("jack_activate called with a NULL client");
  783. return -1;
  784. } else {
  785. return client->Activate();
  786. }
  787. }
  788. EXPORT int jack_deactivate(jack_client_t* ext_client)
  789. {
  790. #ifdef __CLIENTDEBUG__
  791. JackLibGlobals::CheckContext();
  792. #endif
  793. JackClient* client = (JackClient*)ext_client;
  794. if (client == NULL) {
  795. jack_error("jack_deactivate called with a NULL client");
  796. return -1;
  797. } else {
  798. return client->Deactivate();
  799. }
  800. }
  801. 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)
  802. {
  803. #ifdef __CLIENTDEBUG__
  804. JackLibGlobals::CheckContext();
  805. #endif
  806. JackClient* client = (JackClient*)ext_client;
  807. if (client == NULL) {
  808. jack_error("jack_port_register called with a NULL client");
  809. return NULL;
  810. } else if ((port_name == NULL) || (port_type == NULL)) {
  811. jack_error("jack_port_register called with a NULL port name or a NULL port_type");
  812. return NULL;
  813. } else {
  814. return (jack_port_t *)client->PortRegister(port_name, port_type, flags, buffer_size);
  815. }
  816. }
  817. EXPORT int jack_port_unregister(jack_client_t* ext_client, jack_port_t* port)
  818. {
  819. #ifdef __CLIENTDEBUG__
  820. JackLibGlobals::CheckContext();
  821. #endif
  822. JackClient* client = (JackClient*)ext_client;
  823. if (client == NULL) {
  824. jack_error("jack_port_unregister called with a NULL client");
  825. return -1;
  826. }
  827. jack_port_id_t myport = (jack_port_id_t)port;
  828. if (!CheckPort(myport)) {
  829. jack_error("jack_port_unregister called with an incorrect port %ld", myport);
  830. return -1;
  831. }
  832. return client->PortUnRegister(myport);
  833. }
  834. EXPORT int jack_port_is_mine(const jack_client_t* ext_client, const jack_port_t* port)
  835. {
  836. #ifdef __CLIENTDEBUG__
  837. JackLibGlobals::CheckContext();
  838. #endif
  839. JackClient* client = (JackClient*)ext_client;
  840. if (client == NULL) {
  841. jack_error("jack_port_is_mine called with a NULL client");
  842. return -1;
  843. }
  844. jack_port_id_t myport = (jack_port_id_t)port;
  845. if (!CheckPort(myport)) {
  846. jack_error("jack_port_is_mine called with an incorrect port %ld", myport);
  847. return -1;
  848. }
  849. return client->PortIsMine(myport);
  850. }
  851. EXPORT const char** jack_port_get_connections(const jack_port_t* port)
  852. {
  853. #ifdef __CLIENTDEBUG__
  854. JackLibGlobals::CheckContext();
  855. #endif
  856. jack_port_id_t myport = (jack_port_id_t)port;
  857. if (!CheckPort(myport)) {
  858. jack_error("jack_port_get_connections called with an incorrect port %ld", myport);
  859. return NULL;
  860. } else {
  861. WaitGraphChange();
  862. JackGraphManager* manager = GetGraphManager();
  863. return (manager ? manager->GetConnections(myport) : NULL);
  864. }
  865. }
  866. // Calling client does not need to "own" the port
  867. EXPORT const char** jack_port_get_all_connections(const jack_client_t* ext_client, const jack_port_t* port)
  868. {
  869. #ifdef __CLIENTDEBUG__
  870. JackLibGlobals::CheckContext();
  871. #endif
  872. JackClient* client = (JackClient*)ext_client;
  873. if (client == NULL) {
  874. jack_error("jack_port_get_all_connections called with a NULL client");
  875. return NULL;
  876. }
  877. jack_port_id_t myport = (jack_port_id_t)port;
  878. if (!CheckPort(myport)) {
  879. jack_error("jack_port_get_all_connections called with an incorrect port %ld", myport);
  880. return NULL;
  881. } else {
  882. WaitGraphChange();
  883. JackGraphManager* manager = GetGraphManager();
  884. return (manager ? manager->GetConnections(myport) : NULL);
  885. }
  886. }
  887. EXPORT jack_nframes_t jack_port_get_total_latency(jack_client_t* ext_client, jack_port_t* port)
  888. {
  889. #ifdef __CLIENTDEBUG__
  890. JackLibGlobals::CheckContext();
  891. #endif
  892. JackClient* client = (JackClient*)ext_client;
  893. if (client == NULL) {
  894. jack_error("jack_port_get_total_latency called with a NULL client");
  895. return 0;
  896. }
  897. jack_port_id_t myport = (jack_port_id_t)port;
  898. if (!CheckPort(myport)) {
  899. jack_error("jack_port_get_total_latency called with an incorrect port %ld", myport);
  900. return 0;
  901. } else {
  902. WaitGraphChange();
  903. JackGraphManager* manager = GetGraphManager();
  904. return (manager ? manager->GetPort(myport)->GetTotalLatency() : 0);
  905. }
  906. }
  907. EXPORT int jack_connect(jack_client_t* ext_client, const char* src, const char* dst)
  908. {
  909. #ifdef __CLIENTDEBUG__
  910. JackLibGlobals::CheckContext();
  911. #endif
  912. JackClient* client = (JackClient*)ext_client;
  913. if (client == NULL) {
  914. jack_error("jack_connect called with a NULL client");
  915. return -1;
  916. } else if ((src == NULL) || (dst == NULL)) {
  917. jack_error("jack_connect called with a NULL port name");
  918. return -1;
  919. } else {
  920. return client->PortConnect(src, dst);
  921. }
  922. }
  923. EXPORT int jack_disconnect(jack_client_t* ext_client, const char* src, const char* dst)
  924. {
  925. #ifdef __CLIENTDEBUG__
  926. JackLibGlobals::CheckContext();
  927. #endif
  928. JackClient* client = (JackClient*)ext_client;
  929. if (client == NULL) {
  930. jack_error("jack_disconnect called with a NULL client");
  931. return -1;
  932. } else if ((src == NULL) || (dst == NULL)) {
  933. jack_error("jack_connect called with a NULL port name");
  934. return -1;
  935. } else {
  936. return client->PortDisconnect(src, dst);
  937. }
  938. }
  939. EXPORT int jack_port_connect(jack_client_t* ext_client, jack_port_t* src, jack_port_t* dst)
  940. {
  941. #ifdef __CLIENTDEBUG__
  942. JackLibGlobals::CheckContext();
  943. #endif
  944. JackClient* client = (JackClient*)ext_client;
  945. if (client == NULL) {
  946. jack_error("jack_port_connect called with a NULL client");
  947. return -1;
  948. }
  949. jack_port_id_t mysrc = (jack_port_id_t)src;
  950. if (!CheckPort(mysrc)) {
  951. jack_error("jack_port_connect called with a NULL src port");
  952. return -1;
  953. }
  954. jack_port_id_t mydst = (jack_port_id_t)dst;
  955. if (!CheckPort(mydst)) {
  956. jack_error("jack_port_connect called with a NULL dst port");
  957. return -1;
  958. }
  959. return client->PortConnect(mysrc, mydst);
  960. }
  961. EXPORT int jack_port_disconnect(jack_client_t* ext_client, jack_port_t* src)
  962. {
  963. #ifdef __CLIENTDEBUG__
  964. JackLibGlobals::CheckContext();
  965. #endif
  966. JackClient* client = (JackClient*)ext_client;
  967. if (client == NULL) {
  968. jack_error("jack_port_disconnect called with a NULL client");
  969. return -1;
  970. }
  971. jack_port_id_t myport = (jack_port_id_t)src;
  972. if (!CheckPort(myport)) {
  973. jack_error("jack_port_disconnect called with an incorrect port %ld", myport);
  974. return -1;
  975. }
  976. return client->PortDisconnect(myport);
  977. }
  978. EXPORT jack_nframes_t jack_get_sample_rate(jack_client_t* ext_client)
  979. {
  980. #ifdef __CLIENTDEBUG__
  981. JackLibGlobals::CheckContext();
  982. #endif
  983. JackClient* client = (JackClient*)ext_client;
  984. if (client == NULL) {
  985. jack_error("jack_get_sample_rate called with a NULL client");
  986. return 0;
  987. } else {
  988. JackEngineControl* control = GetEngineControl();
  989. return (control ? control->fSampleRate : 0);
  990. }
  991. }
  992. EXPORT jack_nframes_t jack_get_buffer_size(jack_client_t* ext_client)
  993. {
  994. #ifdef __CLIENTDEBUG__
  995. JackLibGlobals::CheckContext();
  996. #endif
  997. JackClient* client = (JackClient*)ext_client;
  998. if (client == NULL) {
  999. jack_error("jack_get_buffer_size called with a NULL client");
  1000. return 0;
  1001. } else {
  1002. JackEngineControl* control = GetEngineControl();
  1003. return (control ? control->fBufferSize : 0);
  1004. }
  1005. }
  1006. EXPORT const char** jack_get_ports(jack_client_t* ext_client, const char* port_name_pattern, const char* type_name_pattern, unsigned long flags)
  1007. {
  1008. #ifdef __CLIENTDEBUG__
  1009. JackLibGlobals::CheckContext();
  1010. #endif
  1011. JackClient* client = (JackClient*)ext_client;
  1012. if (client == NULL) {
  1013. jack_error("jack_get_ports called with a NULL client");
  1014. return NULL;
  1015. }
  1016. JackGraphManager* manager = GetGraphManager();
  1017. return (manager ? manager->GetPorts(port_name_pattern, type_name_pattern, flags) : NULL);
  1018. }
  1019. EXPORT jack_port_t* jack_port_by_name(jack_client_t* ext_client, const char* portname)
  1020. {
  1021. #ifdef __CLIENTDEBUG__
  1022. JackLibGlobals::CheckContext();
  1023. #endif
  1024. JackClient* client = (JackClient*)ext_client;
  1025. if (client == NULL) {
  1026. jack_error("jack_get_ports called with a NULL client");
  1027. return 0;
  1028. }
  1029. if (portname == NULL) {
  1030. jack_error("jack_port_by_name called with a NULL port name");
  1031. return NULL;
  1032. } else {
  1033. JackGraphManager* manager = GetGraphManager();
  1034. if (!manager)
  1035. return NULL;
  1036. int res = manager->GetPort(portname); // returns a port index at least > 1
  1037. return (res == NO_PORT) ? NULL : (jack_port_t*)res;
  1038. }
  1039. }
  1040. EXPORT jack_port_t* jack_port_by_id(jack_client_t* ext_client, jack_port_id_t id)
  1041. {
  1042. #ifdef __CLIENTDEBUG__
  1043. JackLibGlobals::CheckContext();
  1044. #endif
  1045. /* jack_port_t* type is actually the port index */
  1046. return (jack_port_t*)id;
  1047. }
  1048. EXPORT int jack_engine_takeover_timebase(jack_client_t* ext_client)
  1049. {
  1050. #ifdef __CLIENTDEBUG__
  1051. JackLibGlobals::CheckContext();
  1052. #endif
  1053. JackClient* client = (JackClient*)ext_client;
  1054. if (client == NULL) {
  1055. jack_error("jack_engine_takeover_timebase called with a NULL client");
  1056. return -1;
  1057. } else {
  1058. jack_error("jack_engine_takeover_timebase: deprecated\n");
  1059. return 0;
  1060. }
  1061. }
  1062. EXPORT jack_nframes_t jack_frames_since_cycle_start(const jack_client_t* ext_client)
  1063. {
  1064. #ifdef __CLIENTDEBUG__
  1065. JackLibGlobals::CheckContext();
  1066. #endif
  1067. JackTimer timer;
  1068. JackEngineControl* control = GetEngineControl();
  1069. if (!control)
  1070. return 0;
  1071. control->ReadFrameTime(&timer);
  1072. return (jack_nframes_t) floor((((float)control->fSampleRate) / 1000000.0f) * (GetMicroSeconds() - timer.fCurrentCallback));
  1073. }
  1074. EXPORT jack_time_t jack_get_time(jack_client_t *client)
  1075. {
  1076. return GetMicroSeconds();
  1077. }
  1078. EXPORT jack_time_t jack_frames_to_time(const jack_client_t* ext_client, jack_nframes_t frames)
  1079. {
  1080. #ifdef __CLIENTDEBUG__
  1081. JackLibGlobals::CheckContext();
  1082. #endif
  1083. JackClient* client = (JackClient*)ext_client;
  1084. if (client == NULL) {
  1085. jack_error("jack_frames_to_time called with a NULL client");
  1086. return 0;
  1087. } else {
  1088. JackTimer timer;
  1089. JackEngineControl* control = GetEngineControl();
  1090. if (!control)
  1091. return 0;
  1092. control->ReadFrameTime(&timer);
  1093. if (timer.fInitialized) {
  1094. return timer.fCurrentWakeup +
  1095. (long) rint(((double) ((frames - timer.fFrames)) *
  1096. ((jack_time_t)(timer.fNextWakeUp - timer.fCurrentWakeup))) / control->fBufferSize);
  1097. } else {
  1098. return 0;
  1099. }
  1100. }
  1101. }
  1102. EXPORT jack_nframes_t jack_time_to_frames(const jack_client_t* ext_client, jack_time_t time)
  1103. {
  1104. #ifdef __CLIENTDEBUG__
  1105. JackLibGlobals::CheckContext();
  1106. #endif
  1107. JackClient* client = (JackClient*)ext_client;
  1108. if (client == NULL) {
  1109. jack_error("jack_time_to_frames called with a NULL client");
  1110. return 0;
  1111. } else {
  1112. JackTimer timer;
  1113. JackEngineControl* control = GetEngineControl();
  1114. if (!control)
  1115. return 0;
  1116. control->ReadFrameTime(&timer);
  1117. if (timer.fInitialized) {
  1118. return timer.fFrames +
  1119. (long) rint(((double) ((time - timer.fCurrentWakeup)) /
  1120. ((jack_time_t)(timer.fNextWakeUp - timer.fCurrentWakeup))) * control->fBufferSize);
  1121. } else {
  1122. return 0;
  1123. }
  1124. }
  1125. }
  1126. EXPORT jack_nframes_t jack_frame_time(const jack_client_t* ext_client)
  1127. {
  1128. return jack_time_to_frames(ext_client, GetMicroSeconds());
  1129. }
  1130. EXPORT jack_nframes_t jack_last_frame_time(const jack_client_t* ext_client)
  1131. {
  1132. #ifdef __CLIENTDEBUG__
  1133. JackLibGlobals::CheckContext();
  1134. #endif
  1135. JackTimer timer;
  1136. JackEngineControl* control = GetEngineControl();
  1137. if (control) {
  1138. control->ReadFrameTime(&timer);
  1139. return timer.fFrames;
  1140. } else {
  1141. return 0;
  1142. }
  1143. }
  1144. EXPORT float jack_cpu_load(jack_client_t* ext_client)
  1145. {
  1146. #ifdef __CLIENTDEBUG__
  1147. JackLibGlobals::CheckContext();
  1148. #endif
  1149. JackClient* client = (JackClient*)ext_client;
  1150. if (client == NULL) {
  1151. jack_error("jack_cpu_load called with a NULL client");
  1152. return 0.0f;
  1153. } else {
  1154. JackEngineControl* control = GetEngineControl();
  1155. return (control ? control->fCPULoad : 0.0f);
  1156. }
  1157. }
  1158. EXPORT pthread_t jack_client_thread_id(jack_client_t* ext_client)
  1159. {
  1160. #ifdef __CLIENTDEBUG__
  1161. JackLibGlobals::CheckContext();
  1162. #endif
  1163. JackClient* client = (JackClient*)ext_client;
  1164. if (client == NULL) {
  1165. jack_error("jack_client_thread_id called with a NULL client");
  1166. return (pthread_t)NULL;
  1167. } else {
  1168. return client->GetThreadID();
  1169. }
  1170. }
  1171. EXPORT char* jack_get_client_name (jack_client_t* ext_client)
  1172. {
  1173. #ifdef __CLIENTDEBUG__
  1174. JackLibGlobals::CheckContext();
  1175. #endif
  1176. JackClient* client = (JackClient*)ext_client;
  1177. if (client == NULL) {
  1178. jack_error("jack_get_client_name called with a NULL client");
  1179. return NULL;
  1180. } else {
  1181. return client->GetClientControl()->fName;
  1182. }
  1183. }
  1184. EXPORT int jack_client_name_size(void)
  1185. {
  1186. return JACK_CLIENT_NAME_SIZE;
  1187. }
  1188. EXPORT int jack_port_name_size(void)
  1189. {
  1190. return JACK_PORT_NAME_SIZE;
  1191. }
  1192. EXPORT int jack_port_type_size(void)
  1193. {
  1194. return JACK_PORT_TYPE_SIZE;
  1195. }
  1196. // transport.h
  1197. EXPORT int jack_release_timebase(jack_client_t* ext_client)
  1198. {
  1199. #ifdef __CLIENTDEBUG__
  1200. JackLibGlobals::CheckContext();
  1201. #endif
  1202. JackClient* client = (JackClient*)ext_client;
  1203. if (client == NULL) {
  1204. jack_error("jack_release_timebase called with a NULL client");
  1205. return -1;
  1206. } else {
  1207. return client->ReleaseTimebase();
  1208. }
  1209. }
  1210. EXPORT int jack_set_sync_callback(jack_client_t* ext_client, JackSyncCallback sync_callback, void *arg)
  1211. {
  1212. #ifdef __CLIENTDEBUG__
  1213. JackLibGlobals::CheckContext();
  1214. #endif
  1215. JackClient* client = (JackClient*)ext_client;
  1216. if (client == NULL) {
  1217. jack_error("jack_set_sync_callback called with a NULL client");
  1218. return -1;
  1219. } else {
  1220. return client->SetSyncCallback(sync_callback, arg);
  1221. }
  1222. }
  1223. EXPORT int jack_set_sync_timeout(jack_client_t* ext_client, jack_time_t timeout)
  1224. {
  1225. #ifdef __CLIENTDEBUG__
  1226. JackLibGlobals::CheckContext();
  1227. #endif
  1228. JackClient* client = (JackClient*)ext_client;
  1229. if (client == NULL) {
  1230. jack_error("jack_set_sync_timeout called with a NULL client");
  1231. return -1;
  1232. } else {
  1233. return client->SetSyncTimeout(timeout);
  1234. }
  1235. }
  1236. EXPORT int jack_set_timebase_callback(jack_client_t* ext_client, int conditional, JackTimebaseCallback timebase_callback, void* arg)
  1237. {
  1238. #ifdef __CLIENTDEBUG__
  1239. JackLibGlobals::CheckContext();
  1240. #endif
  1241. JackClient* client = (JackClient*)ext_client;
  1242. if (client == NULL) {
  1243. jack_error("jack_set_timebase_callback called with a NULL client");
  1244. return -1;
  1245. } else {
  1246. return client->SetTimebaseCallback(conditional, timebase_callback, arg);
  1247. }
  1248. }
  1249. EXPORT int jack_transport_locate(jack_client_t* ext_client, jack_nframes_t frame)
  1250. {
  1251. #ifdef __CLIENTDEBUG__
  1252. JackLibGlobals::CheckContext();
  1253. #endif
  1254. JackClient* client = (JackClient*)ext_client;
  1255. if (client == NULL) {
  1256. jack_error("jack_transport_locate called with a NULL client");
  1257. return -1;
  1258. } else {
  1259. return client->TransportLocate(frame);
  1260. }
  1261. }
  1262. EXPORT jack_transport_state_t jack_transport_query(const jack_client_t* ext_client, jack_position_t* pos)
  1263. {
  1264. #ifdef __CLIENTDEBUG__
  1265. JackLibGlobals::CheckContext();
  1266. #endif
  1267. JackClient* client = (JackClient*)ext_client;
  1268. if (client == NULL) {
  1269. jack_error("jack_transport_query called with a NULL client");
  1270. return JackTransportStopped;
  1271. } else {
  1272. return client->TransportQuery(pos);
  1273. }
  1274. }
  1275. EXPORT jack_nframes_t jack_get_current_transport_frame(const jack_client_t* ext_client)
  1276. {
  1277. #ifdef __CLIENTDEBUG__
  1278. JackLibGlobals::CheckContext();
  1279. #endif
  1280. JackClient* client = (JackClient*)ext_client;
  1281. if (client == NULL) {
  1282. jack_error("jack_get_current_transport_frame called with a NULL client");
  1283. return 0;
  1284. } else {
  1285. return client->GetCurrentTransportFrame();
  1286. }
  1287. }
  1288. EXPORT int jack_transport_reposition(jack_client_t* ext_client, jack_position_t* pos)
  1289. {
  1290. #ifdef __CLIENTDEBUG__
  1291. JackLibGlobals::CheckContext();
  1292. #endif
  1293. JackClient* client = (JackClient*)ext_client;
  1294. if (client == NULL) {
  1295. jack_error("jack_transport_reposition called with a NULL client");
  1296. return -1;
  1297. } else {
  1298. return client->TransportReposition(pos);
  1299. }
  1300. }
  1301. EXPORT void jack_transport_start(jack_client_t* ext_client)
  1302. {
  1303. #ifdef __CLIENTDEBUG__
  1304. JackLibGlobals::CheckContext();
  1305. #endif
  1306. JackClient* client = (JackClient*)ext_client;
  1307. if (client == NULL) {
  1308. jack_error("jack_transport_start called with a NULL client");
  1309. } else {
  1310. client->TransportStart();
  1311. }
  1312. }
  1313. EXPORT void jack_transport_stop(jack_client_t* ext_client)
  1314. {
  1315. #ifdef __CLIENTDEBUG__
  1316. JackLibGlobals::CheckContext();
  1317. #endif
  1318. JackClient* client = (JackClient*)ext_client;
  1319. if (client == NULL) {
  1320. jack_error("jack_transport_stop called with a NULL client");
  1321. } else {
  1322. client->TransportStop();
  1323. }
  1324. }
  1325. // deprecated
  1326. EXPORT void jack_get_transport_info(jack_client_t* ext_client, jack_transport_info_t* tinfo)
  1327. {
  1328. jack_error("jack_get_transport_info: deprecated");
  1329. if (tinfo)
  1330. memset(tinfo, 0, sizeof(jack_transport_info_t));
  1331. }
  1332. EXPORT void jack_set_transport_info(jack_client_t* ext_client, jack_transport_info_t* tinfo)
  1333. {
  1334. jack_error("jack_set_transport_info: deprecated");
  1335. if (tinfo)
  1336. memset(tinfo, 0, sizeof(jack_transport_info_t));
  1337. }
  1338. // statistics.h
  1339. EXPORT float jack_get_max_delayed_usecs(jack_client_t* ext_client)
  1340. {
  1341. #ifdef __CLIENTDEBUG__
  1342. JackLibGlobals::CheckContext();
  1343. #endif
  1344. JackLog("jack_get_max_delayed_usecs: not yet implemented\n");
  1345. return 0.f;
  1346. }
  1347. EXPORT float jack_get_xrun_delayed_usecs(jack_client_t* ext_client)
  1348. {
  1349. #ifdef __CLIENTDEBUG__
  1350. JackLibGlobals::CheckContext();
  1351. #endif
  1352. JackLog("jack_get_xrun_delayed_usecs: not yet implemented\n");
  1353. return 0.f;
  1354. }
  1355. EXPORT void jack_reset_max_delayed_usecs(jack_client_t* ext_client)
  1356. {
  1357. #ifdef __CLIENTDEBUG__
  1358. JackLibGlobals::CheckContext();
  1359. #endif
  1360. JackLog("jack_reset_max_delayed_usecs: not yet implemented\n");
  1361. }
  1362. // thread.h
  1363. EXPORT int jack_acquire_real_time_scheduling(pthread_t thread, int priority)
  1364. {
  1365. #ifdef __APPLE__
  1366. JackEngineControl* control = GetEngineControl();
  1367. return (control ? JackMachThread::AcquireRealTimeImp(thread, GetEngineControl()->fPeriod, GetEngineControl()->fComputation, GetEngineControl()->fConstraint) : -1);
  1368. #elif WIN32
  1369. return JackWinThread::AcquireRealTimeImp(thread, priority);
  1370. #else
  1371. return JackPosixThread::AcquireRealTimeImp(thread, priority);
  1372. #endif
  1373. }
  1374. EXPORT int jack_client_create_thread(jack_client_t* client,
  1375. pthread_t *thread,
  1376. int priority,
  1377. int realtime, /* boolean */
  1378. void *(*start_routine)(void*),
  1379. void *arg)
  1380. {
  1381. #ifdef __APPLE__
  1382. return JackPosixThread::StartImp(thread, priority, realtime, start_routine, arg);
  1383. #elif WIN32
  1384. return JackWinThread::StartImp(thread, priority, realtime, (ThreadCallback)start_routine, arg);
  1385. #else
  1386. return JackPosixThread::StartImp(thread, priority, realtime, start_routine, arg);
  1387. #endif
  1388. }
  1389. EXPORT int jack_drop_real_time_scheduling(pthread_t thread)
  1390. {
  1391. #ifdef __APPLE__
  1392. return JackMachThread::DropRealTimeImp(thread);
  1393. #elif WIN32
  1394. return JackWinThread::DropRealTimeImp(thread);
  1395. #else
  1396. return JackPosixThread::DropRealTimeImp(thread);
  1397. #endif
  1398. }
  1399. // intclient.h
  1400. EXPORT int jack_internal_client_new (const char *client_name,
  1401. const char *load_name,
  1402. const char *load_init)
  1403. {
  1404. jack_error("jack_internal_client_new: deprecated");
  1405. return -1;
  1406. }
  1407. EXPORT void jack_internal_client_close (const char *client_name)
  1408. {
  1409. jack_error("jack_internal_client_close: deprecated");
  1410. }
  1411. EXPORT char* jack_get_internal_client_name(jack_client_t* ext_client, jack_intclient_t intclient)
  1412. {
  1413. #ifdef __CLIENTDEBUG__
  1414. JackLibGlobals::CheckContext();
  1415. #endif
  1416. JackClient* client = (JackClient*)ext_client;
  1417. if (client == NULL) {
  1418. jack_error("jack_get_internal_client_name called with a NULL client");
  1419. return NULL;
  1420. } else if (intclient < 0 || intclient >= CLIENT_NUM) {
  1421. jack_error("jack_get_internal_client_name: incorrect client");
  1422. return NULL;
  1423. } else {
  1424. return client->GetInternalClientName(intclient);
  1425. }
  1426. }
  1427. EXPORT jack_intclient_t jack_internal_client_handle(jack_client_t* ext_client, const char* client_name, jack_status_t* status)
  1428. {
  1429. #ifdef __CLIENTDEBUG__
  1430. JackLibGlobals::CheckContext();
  1431. #endif
  1432. JackClient* client = (JackClient*)ext_client;
  1433. if (client == NULL) {
  1434. jack_error("jack_internal_client_handle called with a NULL client");
  1435. return 0;
  1436. } else {
  1437. jack_status_t my_status;
  1438. if (status == NULL) /* no status from caller? */
  1439. status = &my_status; /* use local status word */
  1440. *status = (jack_status_t)0;
  1441. return client->InternalClientHandle(client_name, status);
  1442. }
  1443. }
  1444. EXPORT jack_intclient_t jack_internal_client_load(jack_client_t* ext_client, const char* client_name, jack_options_t options, jack_status_t* status, ...)
  1445. {
  1446. #ifdef __CLIENTDEBUG__
  1447. JackLibGlobals::CheckContext();
  1448. #endif
  1449. JackClient* client = (JackClient*)ext_client;
  1450. if (client == NULL) {
  1451. jack_error("jack_internal_client_load called with a NULL client");
  1452. return 0;
  1453. } else {
  1454. va_list ap;
  1455. jack_varargs_t va;
  1456. jack_status_t my_status;
  1457. if (status == NULL) /* no status from caller? */
  1458. status = &my_status; /* use local status word */
  1459. *status = (jack_status_t)0;
  1460. /* validate parameters */
  1461. if ((options & ~JackLoadOptions)) {
  1462. int my_status1 = *status | (JackFailure | JackInvalidOption);
  1463. *status = (jack_status_t)my_status1;
  1464. return 0;
  1465. }
  1466. /* parse variable arguments */
  1467. va_start(ap, status);
  1468. jack_varargs_parse(options, ap, &va);
  1469. va_end(ap);
  1470. return client->InternalClientLoad(client_name, options, status, &va);
  1471. }
  1472. }
  1473. EXPORT jack_status_t jack_internal_client_unload(jack_client_t* ext_client, jack_intclient_t intclient)
  1474. {
  1475. #ifdef __CLIENTDEBUG__
  1476. JackLibGlobals::CheckContext();
  1477. #endif
  1478. JackClient* client = (JackClient*)ext_client;
  1479. if (client == NULL) {
  1480. jack_error("jack_internal_client_unload called with a NULL client");
  1481. return (jack_status_t)(JackNoSuchClient | JackFailure);
  1482. } else if (intclient < 0 || intclient >= CLIENT_NUM) {
  1483. jack_error("jack_internal_client_unload: incorrect client");
  1484. return (jack_status_t)(JackNoSuchClient | JackFailure);
  1485. } else {
  1486. jack_status_t my_status;
  1487. client->InternalClientUnload(intclient, &my_status);
  1488. return my_status;
  1489. }
  1490. }