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.

1664 lines
54KB

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