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.

1689 lines
55KB

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