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.

1660 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. EXPORT int jack_port_set_name(jack_port_t* port, const char* name)
  463. {
  464. #ifdef __CLIENTDEBUG__
  465. JackLibGlobals::CheckContext();
  466. #endif
  467. jack_port_id_t myport = (jack_port_id_t)port;
  468. if (!CheckPort(myport)) {
  469. jack_error("jack_port_set_name called with an incorrect port %ld", myport);
  470. return -1;
  471. } else if (name == NULL) {
  472. jack_error("jack_port_set_name called with a NULL port name");
  473. return -1;
  474. } else {
  475. JackGraphManager* manager = GetGraphManager();
  476. return (manager ? manager->GetPort(myport)->SetName(name) : -1);
  477. }
  478. }
  479. EXPORT int jack_port_set_alias(jack_port_t* port, const char* name)
  480. {
  481. #ifdef __CLIENTDEBUG__
  482. JackLibGlobals::CheckContext();
  483. #endif
  484. jack_port_id_t myport = (jack_port_id_t)port;
  485. if (!CheckPort(myport)) {
  486. jack_error("jack_port_set_alias called with an incorrect port %ld", myport);
  487. return -1;
  488. } else if (name == NULL) {
  489. jack_error("jack_port_set_alias called with a NULL port name");
  490. return -1;
  491. } else {
  492. JackGraphManager* manager = GetGraphManager();
  493. return (manager ? manager->GetPort(myport)->SetAlias(name) : -1);
  494. }
  495. }
  496. EXPORT int jack_port_unset_alias(jack_port_t* port, const char* name)
  497. {
  498. #ifdef __CLIENTDEBUG__
  499. JackLibGlobals::CheckContext();
  500. #endif
  501. jack_port_id_t myport = (jack_port_id_t)port;
  502. if (!CheckPort(myport)) {
  503. jack_error("jack_port_unset_alias called with an incorrect port %ld", myport);
  504. return -1;
  505. } else if (name == NULL) {
  506. jack_error("jack_port_unset_alias called with a NULL port name");
  507. return -1;
  508. } else {
  509. JackGraphManager* manager = GetGraphManager();
  510. return (manager ? manager->GetPort(myport)->UnsetAlias(name) : -1);
  511. }
  512. }
  513. EXPORT int jack_port_get_aliases(const jack_port_t* port, char* const aliases[2])
  514. {
  515. #ifdef __CLIENTDEBUG__
  516. JackLibGlobals::CheckContext();
  517. #endif
  518. jack_port_id_t myport = (jack_port_id_t)port;
  519. if (!CheckPort(myport)) {
  520. jack_error("jack_port_get_aliases called with an incorrect port %ld", myport);
  521. return -1;
  522. } else {
  523. JackGraphManager* manager = GetGraphManager();
  524. return (manager ? manager->GetPort(myport)->GetAliases(aliases) : -1);
  525. }
  526. }
  527. EXPORT int jack_port_request_monitor(jack_port_t* port, int onoff)
  528. {
  529. #ifdef __CLIENTDEBUG__
  530. JackLibGlobals::CheckContext();
  531. #endif
  532. jack_port_id_t myport = (jack_port_id_t)port;
  533. if (!CheckPort(myport)) {
  534. jack_error("jack_port_request_monitor called with an incorrect port %ld", myport);
  535. return -1;
  536. } else {
  537. JackGraphManager* manager = GetGraphManager();
  538. return (manager ? manager->RequestMonitor(myport, onoff) : -1);
  539. }
  540. }
  541. EXPORT int jack_port_request_monitor_by_name(jack_client_t* ext_client, const char* port_name, int onoff)
  542. {
  543. #ifdef __CLIENTDEBUG__
  544. JackLibGlobals::CheckContext();
  545. #endif
  546. JackClient* client = (JackClient*)ext_client;
  547. if (client == NULL) {
  548. jack_error("jack_port_request_monitor_by_name called with a NULL client");
  549. return -1;
  550. } else {
  551. JackGraphManager* manager = GetGraphManager();
  552. if (!manager)
  553. return -1;
  554. jack_port_id_t myport = manager->GetPort(port_name);
  555. if (!CheckPort(myport)) {
  556. jack_error("jack_port_request_monitor_by_name called with an incorrect port %s", port_name);
  557. return -1;
  558. } else {
  559. return manager->RequestMonitor(myport, onoff);
  560. }
  561. }
  562. }
  563. EXPORT int jack_port_ensure_monitor(jack_port_t* port, int onoff)
  564. {
  565. #ifdef __CLIENTDEBUG__
  566. JackLibGlobals::CheckContext();
  567. #endif
  568. jack_port_id_t myport = (jack_port_id_t)port;
  569. if (!CheckPort(myport)) {
  570. jack_error("jack_port_ensure_monitor called with an incorrect port %ld", myport);
  571. return -1;
  572. } else {
  573. JackGraphManager* manager = GetGraphManager();
  574. return (manager ? manager->GetPort(myport)->EnsureMonitor(onoff) : -1);
  575. }
  576. }
  577. EXPORT int jack_port_monitoring_input(jack_port_t* port)
  578. {
  579. #ifdef __CLIENTDEBUG__
  580. JackLibGlobals::CheckContext();
  581. #endif
  582. jack_port_id_t myport = (jack_port_id_t)port;
  583. if (!CheckPort(myport)) {
  584. jack_error("jack_port_monitoring_input called with an incorrect port %ld", myport);
  585. return -1;
  586. } else {
  587. JackGraphManager* manager = GetGraphManager();
  588. return (manager ? manager->GetPort(myport)->MonitoringInput() : -1);
  589. }
  590. }
  591. EXPORT int jack_is_realtime(jack_client_t* ext_client)
  592. {
  593. #ifdef __CLIENTDEBUG__
  594. JackLibGlobals::CheckContext();
  595. #endif
  596. JackClient* client = (JackClient*)ext_client;
  597. if (client == NULL) {
  598. jack_error("jack_is_realtime called with a NULL client");
  599. return -1;
  600. } else {
  601. JackEngineControl* control = GetEngineControl();
  602. return (control ? control->fRealTime : -1);
  603. }
  604. }
  605. EXPORT void jack_on_shutdown(jack_client_t* ext_client, void (*function)(void* arg), void* arg)
  606. {
  607. #ifdef __CLIENTDEBUG__
  608. JackLibGlobals::CheckContext();
  609. #endif
  610. JackClient* client = (JackClient*)ext_client;
  611. if (client == NULL) {
  612. jack_error("jack_on_shutdown called with a NULL client");
  613. } else {
  614. client->OnShutdown(function, arg);
  615. }
  616. }
  617. EXPORT int jack_set_process_callback(jack_client_t* ext_client, JackProcessCallback callback, void* arg)
  618. {
  619. #ifdef __CLIENTDEBUG__
  620. JackLibGlobals::CheckContext();
  621. #endif
  622. JackClient* client = (JackClient*)ext_client;
  623. if (client == NULL) {
  624. jack_error("jack_set_process_callback called with a NULL client");
  625. return -1;
  626. } else {
  627. return client->SetProcessCallback(callback, arg);
  628. }
  629. }
  630. EXPORT jack_nframes_t jack_thread_wait(jack_client_t* ext_client, int status)
  631. {
  632. #ifdef __CLIENTDEBUG__
  633. JackLibGlobals::CheckContext();
  634. #endif
  635. JackClient* client = (JackClient*)ext_client;
  636. if (client == NULL) {
  637. jack_error("jack_thread_wait called with a NULL client");
  638. return 0;
  639. } else {
  640. return client->Wait(status);
  641. }
  642. }
  643. EXPORT jack_nframes_t jack_cycle_wait(jack_client_t* ext_client)
  644. {
  645. #ifdef __CLIENTDEBUG__
  646. JackLibGlobals::CheckContext();
  647. #endif
  648. JackClient* client = (JackClient*)ext_client;
  649. if (client == NULL) {
  650. jack_error("jack_cycle_wait called with a NULL client");
  651. return 0;
  652. } else {
  653. return client->CycleWait();
  654. }
  655. }
  656. EXPORT void jack_cycle_signal(jack_client_t* ext_client, int status)
  657. {
  658. #ifdef __CLIENTDEBUG__
  659. JackLibGlobals::CheckContext();
  660. #endif
  661. JackClient* client = (JackClient*)ext_client;
  662. if (client == NULL) {
  663. jack_error("jack_cycle_signal called with a NULL client");
  664. } else {
  665. client->CycleSignal(status);
  666. }
  667. }
  668. EXPORT int jack_set_process_thread(jack_client_t* ext_client, JackThreadCallback fun, void *arg)
  669. {
  670. #ifdef __CLIENTDEBUG__
  671. JackLibGlobals::CheckContext();
  672. #endif
  673. JackClient* client = (JackClient*)ext_client;
  674. if (client == NULL) {
  675. jack_error("jack_set_process_thread called with a NULL client");
  676. return -1;
  677. } else {
  678. return client->SetProcessThread(fun, arg);
  679. }
  680. }
  681. EXPORT int jack_set_freewheel_callback(jack_client_t* ext_client, JackFreewheelCallback freewheel_callback, void* arg)
  682. {
  683. #ifdef __CLIENTDEBUG__
  684. JackLibGlobals::CheckContext();
  685. #endif
  686. JackClient* client = (JackClient*)ext_client;
  687. if (client == NULL) {
  688. jack_error("jack_set_freewheel_callback called with a NULL client");
  689. return -1;
  690. } else {
  691. return client->SetFreewheelCallback(freewheel_callback, arg);
  692. }
  693. }
  694. EXPORT int jack_set_freewheel(jack_client_t* ext_client, int onoff)
  695. {
  696. #ifdef __CLIENTDEBUG__
  697. JackLibGlobals::CheckContext();
  698. #endif
  699. JackClient* client = (JackClient*)ext_client;
  700. if (client == NULL) {
  701. jack_error("jack_set_freewheel called with a NULL client");
  702. return -1;
  703. } else {
  704. return client->SetFreeWheel(onoff);
  705. }
  706. }
  707. EXPORT int jack_set_buffer_size(jack_client_t* ext_client, jack_nframes_t buffer_size)
  708. {
  709. #ifdef __CLIENTDEBUG__
  710. JackLibGlobals::CheckContext();
  711. #endif
  712. JackClient* client = (JackClient*)ext_client;
  713. if (client == NULL) {
  714. jack_error("jack_set_buffer_size called with a NULL client");
  715. return -1;
  716. } else if (!CheckBufferSize(buffer_size)) {
  717. return -1;
  718. } else {
  719. return client->SetBufferSize(buffer_size);
  720. }
  721. }
  722. EXPORT int jack_set_buffer_size_callback(jack_client_t* ext_client, JackBufferSizeCallback bufsize_callback, void* arg)
  723. {
  724. #ifdef __CLIENTDEBUG__
  725. JackLibGlobals::CheckContext();
  726. #endif
  727. JackClient* client = (JackClient*)ext_client;
  728. if (client == NULL) {
  729. jack_error("jack_set_buffer_size_callback called with a NULL client");
  730. return -1;
  731. } else {
  732. return client->SetBufferSizeCallback(bufsize_callback, arg);
  733. }
  734. }
  735. EXPORT int jack_set_sample_rate_callback(jack_client_t* ext_client, JackSampleRateCallback srate_callback, void* arg)
  736. {
  737. #ifdef __CLIENTDEBUG__
  738. JackLibGlobals::CheckContext();
  739. #endif
  740. JackClient* client = (JackClient*)ext_client;
  741. if (client == NULL) {
  742. jack_error("jack_set_sample_rate_callback called with a NULL client");
  743. return -1;
  744. } else {
  745. jack_error("jack_set_sample_rate_callback: deprecated");
  746. return 0;
  747. }
  748. }
  749. EXPORT int jack_set_client_registration_callback(jack_client_t* ext_client, JackClientRegistrationCallback registration_callback, void* arg)
  750. {
  751. #ifdef __CLIENTDEBUG__
  752. JackLibGlobals::CheckContext();
  753. #endif
  754. JackClient* client = (JackClient*)ext_client;
  755. if (client == NULL) {
  756. jack_error("jack_set_client_registration_callback called with a NULL client");
  757. return -1;
  758. } else {
  759. return client->SetClientRegistrationCallback(registration_callback, arg);
  760. }
  761. }
  762. EXPORT int jack_set_port_registration_callback(jack_client_t* ext_client, JackPortRegistrationCallback registration_callback, void* arg)
  763. {
  764. #ifdef __CLIENTDEBUG__
  765. JackLibGlobals::CheckContext();
  766. #endif
  767. JackClient* client = (JackClient*)ext_client;
  768. if (client == NULL) {
  769. jack_error("jack_set_port_registration_callback called with a NULL client");
  770. return -1;
  771. } else {
  772. return client->SetPortRegistrationCallback(registration_callback, arg);
  773. }
  774. }
  775. EXPORT int jack_set_port_connect_callback(jack_client_t* ext_client, JackPortConnectCallback portconnect_callback, void* arg)
  776. {
  777. #ifdef __CLIENTDEBUG__
  778. JackLibGlobals::CheckContext();
  779. #endif
  780. JackClient* client = (JackClient*)ext_client;
  781. if (client == NULL) {
  782. jack_error("jack_set_port_connect_callback called with a NULL client");
  783. return -1;
  784. } else {
  785. return client->SetPortConnectCallback(portconnect_callback, arg);
  786. }
  787. }
  788. EXPORT int jack_set_graph_order_callback(jack_client_t* ext_client, JackGraphOrderCallback graph_callback, void* arg)
  789. {
  790. #ifdef __CLIENTDEBUG__
  791. JackLibGlobals::CheckContext();
  792. #endif
  793. JackClient* client = (JackClient*)ext_client;
  794. jack_log("jack_set_graph_order_callback ext_client %x client %x ", ext_client, client);
  795. if (client == NULL) {
  796. jack_error("jack_set_graph_order_callback called with a NULL client");
  797. return -1;
  798. } else {
  799. return client->SetGraphOrderCallback(graph_callback, arg);
  800. }
  801. }
  802. EXPORT int jack_set_xrun_callback(jack_client_t* ext_client, JackXRunCallback xrun_callback, void* arg)
  803. {
  804. #ifdef __CLIENTDEBUG__
  805. JackLibGlobals::CheckContext();
  806. #endif
  807. JackClient* client = (JackClient*)ext_client;
  808. if (client == NULL) {
  809. jack_error("jack_set_xrun_callback called with a NULL client");
  810. return -1;
  811. } else {
  812. return client->SetXRunCallback(xrun_callback, arg);
  813. }
  814. }
  815. EXPORT int jack_set_thread_init_callback(jack_client_t* ext_client, JackThreadInitCallback init_callback, void *arg)
  816. {
  817. #ifdef __CLIENTDEBUG__
  818. JackLibGlobals::CheckContext();
  819. #endif
  820. JackClient* client = (JackClient*)ext_client;
  821. jack_log("jack_set_thread_init_callback ext_client %x client %x ", ext_client, client);
  822. if (client == NULL) {
  823. jack_error("jack_set_thread_init_callback called with a NULL client");
  824. return -1;
  825. } else {
  826. return client->SetInitCallback(init_callback, arg);
  827. }
  828. }
  829. EXPORT int jack_activate(jack_client_t* ext_client)
  830. {
  831. #ifdef __CLIENTDEBUG__
  832. JackLibGlobals::CheckContext();
  833. #endif
  834. JackClient* client = (JackClient*)ext_client;
  835. if (client == NULL) {
  836. jack_error("jack_activate called with a NULL client");
  837. return -1;
  838. } else {
  839. return client->Activate();
  840. }
  841. }
  842. EXPORT int jack_deactivate(jack_client_t* ext_client)
  843. {
  844. #ifdef __CLIENTDEBUG__
  845. JackLibGlobals::CheckContext();
  846. #endif
  847. JackClient* client = (JackClient*)ext_client;
  848. if (client == NULL) {
  849. jack_error("jack_deactivate called with a NULL client");
  850. return -1;
  851. } else {
  852. return client->Deactivate();
  853. }
  854. }
  855. 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)
  856. {
  857. #ifdef __CLIENTDEBUG__
  858. JackLibGlobals::CheckContext();
  859. #endif
  860. JackClient* client = (JackClient*)ext_client;
  861. if (client == NULL) {
  862. jack_error("jack_port_register called with a NULL client");
  863. return NULL;
  864. } else if ((port_name == NULL) || (port_type == NULL)) {
  865. jack_error("jack_port_register called with a NULL port name or a NULL port_type");
  866. return NULL;
  867. } else {
  868. return (jack_port_t *)client->PortRegister(port_name, port_type, flags, buffer_size);
  869. }
  870. }
  871. EXPORT int jack_port_unregister(jack_client_t* ext_client, jack_port_t* port)
  872. {
  873. #ifdef __CLIENTDEBUG__
  874. JackLibGlobals::CheckContext();
  875. #endif
  876. JackClient* client = (JackClient*)ext_client;
  877. if (client == NULL) {
  878. jack_error("jack_port_unregister called with a NULL client");
  879. return -1;
  880. }
  881. jack_port_id_t myport = (jack_port_id_t)port;
  882. if (!CheckPort(myport)) {
  883. jack_error("jack_port_unregister called with an incorrect port %ld", myport);
  884. return -1;
  885. }
  886. return client->PortUnRegister(myport);
  887. }
  888. EXPORT int jack_port_is_mine(const jack_client_t* ext_client, const jack_port_t* port)
  889. {
  890. #ifdef __CLIENTDEBUG__
  891. JackLibGlobals::CheckContext();
  892. #endif
  893. JackClient* client = (JackClient*)ext_client;
  894. if (client == NULL) {
  895. jack_error("jack_port_is_mine called with a NULL client");
  896. return -1;
  897. }
  898. jack_port_id_t myport = (jack_port_id_t)port;
  899. if (!CheckPort(myport)) {
  900. jack_error("jack_port_is_mine called with an incorrect port %ld", myport);
  901. return -1;
  902. }
  903. return client->PortIsMine(myport);
  904. }
  905. EXPORT const char** jack_port_get_connections(const jack_port_t* port)
  906. {
  907. #ifdef __CLIENTDEBUG__
  908. JackLibGlobals::CheckContext();
  909. #endif
  910. jack_port_id_t myport = (jack_port_id_t)port;
  911. if (!CheckPort(myport)) {
  912. jack_error("jack_port_get_connections called with an incorrect port %ld", myport);
  913. return NULL;
  914. } else {
  915. WaitGraphChange();
  916. JackGraphManager* manager = GetGraphManager();
  917. return (manager ? manager->GetConnections(myport) : NULL);
  918. }
  919. }
  920. // Calling client does not need to "own" the port
  921. EXPORT const char** jack_port_get_all_connections(const jack_client_t* ext_client, const jack_port_t* port)
  922. {
  923. #ifdef __CLIENTDEBUG__
  924. JackLibGlobals::CheckContext();
  925. #endif
  926. JackClient* client = (JackClient*)ext_client;
  927. if (client == NULL) {
  928. jack_error("jack_port_get_all_connections called with a NULL client");
  929. return NULL;
  930. }
  931. jack_port_id_t myport = (jack_port_id_t)port;
  932. if (!CheckPort(myport)) {
  933. jack_error("jack_port_get_all_connections called with an incorrect port %ld", myport);
  934. return NULL;
  935. } else {
  936. WaitGraphChange();
  937. JackGraphManager* manager = GetGraphManager();
  938. return (manager ? manager->GetConnections(myport) : NULL);
  939. }
  940. }
  941. EXPORT jack_nframes_t jack_port_get_total_latency(jack_client_t* ext_client, jack_port_t* port)
  942. {
  943. #ifdef __CLIENTDEBUG__
  944. JackLibGlobals::CheckContext();
  945. #endif
  946. JackClient* client = (JackClient*)ext_client;
  947. if (client == NULL) {
  948. jack_error("jack_port_get_total_latency called with a NULL client");
  949. return 0;
  950. }
  951. jack_port_id_t myport = (jack_port_id_t)port;
  952. if (!CheckPort(myport)) {
  953. jack_error("jack_port_get_total_latency called with an incorrect port %ld", myport);
  954. return 0;
  955. } else {
  956. WaitGraphChange();
  957. JackGraphManager* manager = GetGraphManager();
  958. if (manager) {
  959. manager->ComputeTotalLatency(myport);
  960. return manager->GetPort(myport)->GetTotalLatency();
  961. } else {
  962. return 0;
  963. }
  964. }
  965. }
  966. EXPORT int jack_connect(jack_client_t* ext_client, const char* src, const char* dst)
  967. {
  968. #ifdef __CLIENTDEBUG__
  969. JackLibGlobals::CheckContext();
  970. #endif
  971. JackClient* client = (JackClient*)ext_client;
  972. if (client == NULL) {
  973. jack_error("jack_connect called with a NULL client");
  974. return -1;
  975. } else if ((src == NULL) || (dst == NULL)) {
  976. jack_error("jack_connect called with a NULL port name");
  977. return -1;
  978. } else {
  979. return client->PortConnect(src, dst);
  980. }
  981. }
  982. EXPORT int jack_disconnect(jack_client_t* ext_client, const char* src, const char* dst)
  983. {
  984. #ifdef __CLIENTDEBUG__
  985. JackLibGlobals::CheckContext();
  986. #endif
  987. JackClient* client = (JackClient*)ext_client;
  988. if (client == NULL) {
  989. jack_error("jack_disconnect called with a NULL client");
  990. return -1;
  991. } else if ((src == NULL) || (dst == NULL)) {
  992. jack_error("jack_connect called with a NULL port name");
  993. return -1;
  994. } else {
  995. return client->PortDisconnect(src, dst);
  996. }
  997. }
  998. EXPORT int jack_port_disconnect(jack_client_t* ext_client, jack_port_t* src)
  999. {
  1000. #ifdef __CLIENTDEBUG__
  1001. JackLibGlobals::CheckContext();
  1002. #endif
  1003. JackClient* client = (JackClient*)ext_client;
  1004. if (client == NULL) {
  1005. jack_error("jack_port_disconnect called with a NULL client");
  1006. return -1;
  1007. }
  1008. jack_port_id_t myport = (jack_port_id_t)src;
  1009. if (!CheckPort(myport)) {
  1010. jack_error("jack_port_disconnect called with an incorrect port %ld", myport);
  1011. return -1;
  1012. }
  1013. return client->PortDisconnect(myport);
  1014. }
  1015. EXPORT jack_nframes_t jack_get_sample_rate(jack_client_t* ext_client)
  1016. {
  1017. #ifdef __CLIENTDEBUG__
  1018. JackLibGlobals::CheckContext();
  1019. #endif
  1020. JackClient* client = (JackClient*)ext_client;
  1021. if (client == NULL) {
  1022. jack_error("jack_get_sample_rate called with a NULL client");
  1023. return 0;
  1024. } else {
  1025. JackEngineControl* control = GetEngineControl();
  1026. return (control ? control->fSampleRate : 0);
  1027. }
  1028. }
  1029. EXPORT jack_nframes_t jack_get_buffer_size(jack_client_t* ext_client)
  1030. {
  1031. #ifdef __CLIENTDEBUG__
  1032. JackLibGlobals::CheckContext();
  1033. #endif
  1034. JackClient* client = (JackClient*)ext_client;
  1035. if (client == NULL) {
  1036. jack_error("jack_get_buffer_size called with a NULL client");
  1037. return 0;
  1038. } else {
  1039. JackEngineControl* control = GetEngineControl();
  1040. return (control ? control->fBufferSize : 0);
  1041. }
  1042. }
  1043. EXPORT const char** jack_get_ports(jack_client_t* ext_client, const char* port_name_pattern, const char* type_name_pattern, unsigned long flags)
  1044. {
  1045. #ifdef __CLIENTDEBUG__
  1046. JackLibGlobals::CheckContext();
  1047. #endif
  1048. JackClient* client = (JackClient*)ext_client;
  1049. if (client == NULL) {
  1050. jack_error("jack_get_ports called with a NULL client");
  1051. return NULL;
  1052. }
  1053. JackGraphManager* manager = GetGraphManager();
  1054. return (manager ? manager->GetPorts(port_name_pattern, type_name_pattern, flags) : NULL);
  1055. }
  1056. EXPORT jack_port_t* jack_port_by_name(jack_client_t* ext_client, const char* portname)
  1057. {
  1058. #ifdef __CLIENTDEBUG__
  1059. JackLibGlobals::CheckContext();
  1060. #endif
  1061. JackClient* client = (JackClient*)ext_client;
  1062. if (client == NULL) {
  1063. jack_error("jack_get_ports called with a NULL client");
  1064. return 0;
  1065. }
  1066. if (portname == NULL) {
  1067. jack_error("jack_port_by_name called with a NULL port name");
  1068. return NULL;
  1069. } else {
  1070. JackGraphManager* manager = GetGraphManager();
  1071. if (!manager)
  1072. return NULL;
  1073. int res = manager->GetPort(portname); // returns a port index at least > 1
  1074. return (res == NO_PORT) ? NULL : (jack_port_t*)res;
  1075. }
  1076. }
  1077. EXPORT jack_port_t* jack_port_by_id(jack_client_t* ext_client, jack_port_id_t id)
  1078. {
  1079. #ifdef __CLIENTDEBUG__
  1080. JackLibGlobals::CheckContext();
  1081. #endif
  1082. /* jack_port_t* type is actually the port index */
  1083. return (jack_port_t*)id;
  1084. }
  1085. EXPORT int jack_engine_takeover_timebase(jack_client_t* ext_client)
  1086. {
  1087. #ifdef __CLIENTDEBUG__
  1088. JackLibGlobals::CheckContext();
  1089. #endif
  1090. JackClient* client = (JackClient*)ext_client;
  1091. if (client == NULL) {
  1092. jack_error("jack_engine_takeover_timebase called with a NULL client");
  1093. return -1;
  1094. } else {
  1095. jack_error("jack_engine_takeover_timebase: deprecated\n");
  1096. return 0;
  1097. }
  1098. }
  1099. EXPORT jack_nframes_t jack_frames_since_cycle_start(const jack_client_t* ext_client)
  1100. {
  1101. #ifdef __CLIENTDEBUG__
  1102. JackLibGlobals::CheckContext();
  1103. #endif
  1104. JackTimer timer;
  1105. JackEngineControl* control = GetEngineControl();
  1106. if (!control)
  1107. return 0;
  1108. control->ReadFrameTime(&timer);
  1109. return (jack_nframes_t) floor((((float)control->fSampleRate) / 1000000.0f) * (GetMicroSeconds() - timer.fCurrentCallback));
  1110. }
  1111. EXPORT jack_time_t jack_get_time()
  1112. {
  1113. return GetMicroSeconds();
  1114. }
  1115. EXPORT jack_time_t jack_frames_to_time(const jack_client_t* ext_client, jack_nframes_t frames)
  1116. {
  1117. #ifdef __CLIENTDEBUG__
  1118. JackLibGlobals::CheckContext();
  1119. #endif
  1120. JackClient* client = (JackClient*)ext_client;
  1121. if (client == NULL) {
  1122. jack_error("jack_frames_to_time called with a NULL client");
  1123. return 0;
  1124. } else {
  1125. JackTimer timer;
  1126. JackEngineControl* control = GetEngineControl();
  1127. if (!control)
  1128. return 0;
  1129. control->ReadFrameTime(&timer);
  1130. if (timer.fInitialized) {
  1131. return timer.fCurrentWakeup +
  1132. (long) rint(((double) ((frames - timer.fFrames)) *
  1133. ((jack_time_t)(timer.fNextWakeUp - timer.fCurrentWakeup))) / control->fBufferSize);
  1134. } else {
  1135. return 0;
  1136. }
  1137. }
  1138. }
  1139. EXPORT jack_nframes_t jack_time_to_frames(const jack_client_t* ext_client, jack_time_t time)
  1140. {
  1141. #ifdef __CLIENTDEBUG__
  1142. JackLibGlobals::CheckContext();
  1143. #endif
  1144. JackClient* client = (JackClient*)ext_client;
  1145. if (client == NULL) {
  1146. jack_error("jack_time_to_frames called with a NULL client");
  1147. return 0;
  1148. } else {
  1149. JackTimer timer;
  1150. JackEngineControl* control = GetEngineControl();
  1151. if (!control)
  1152. return 0;
  1153. control->ReadFrameTime(&timer);
  1154. if (timer.fInitialized) {
  1155. return timer.fFrames +
  1156. (long) rint(((double) ((time - timer.fCurrentWakeup)) /
  1157. ((jack_time_t)(timer.fNextWakeUp - timer.fCurrentWakeup))) * control->fBufferSize);
  1158. } else {
  1159. return 0;
  1160. }
  1161. }
  1162. }
  1163. EXPORT jack_nframes_t jack_frame_time(const jack_client_t* ext_client)
  1164. {
  1165. return jack_time_to_frames(ext_client, GetMicroSeconds());
  1166. }
  1167. EXPORT jack_nframes_t jack_last_frame_time(const jack_client_t* ext_client)
  1168. {
  1169. #ifdef __CLIENTDEBUG__
  1170. JackLibGlobals::CheckContext();
  1171. #endif
  1172. JackTimer timer;
  1173. JackEngineControl* control = GetEngineControl();
  1174. if (control) {
  1175. control->ReadFrameTime(&timer);
  1176. return timer.fFrames;
  1177. } else {
  1178. return 0;
  1179. }
  1180. }
  1181. EXPORT float jack_cpu_load(jack_client_t* ext_client)
  1182. {
  1183. #ifdef __CLIENTDEBUG__
  1184. JackLibGlobals::CheckContext();
  1185. #endif
  1186. JackClient* client = (JackClient*)ext_client;
  1187. if (client == NULL) {
  1188. jack_error("jack_cpu_load called with a NULL client");
  1189. return 0.0f;
  1190. } else {
  1191. JackEngineControl* control = GetEngineControl();
  1192. return (control ? control->fCPULoad : 0.0f);
  1193. }
  1194. }
  1195. EXPORT pthread_t jack_client_thread_id(jack_client_t* ext_client)
  1196. {
  1197. #ifdef __CLIENTDEBUG__
  1198. JackLibGlobals::CheckContext();
  1199. #endif
  1200. JackClient* client = (JackClient*)ext_client;
  1201. if (client == NULL) {
  1202. jack_error("jack_client_thread_id called with a NULL client");
  1203. return (pthread_t)NULL;
  1204. } else {
  1205. return client->GetThreadID();
  1206. }
  1207. }
  1208. EXPORT char* jack_get_client_name (jack_client_t* ext_client)
  1209. {
  1210. #ifdef __CLIENTDEBUG__
  1211. JackLibGlobals::CheckContext();
  1212. #endif
  1213. JackClient* client = (JackClient*)ext_client;
  1214. if (client == NULL) {
  1215. jack_error("jack_get_client_name called with a NULL client");
  1216. return NULL;
  1217. } else {
  1218. return client->GetClientControl()->fName;
  1219. }
  1220. }
  1221. EXPORT int jack_client_name_size(void)
  1222. {
  1223. return JACK_CLIENT_NAME_SIZE;
  1224. }
  1225. EXPORT int jack_port_name_size(void)
  1226. {
  1227. return JACK_PORT_NAME_SIZE;
  1228. }
  1229. EXPORT int jack_port_type_size(void)
  1230. {
  1231. return JACK_PORT_TYPE_SIZE;
  1232. }
  1233. // transport.h
  1234. EXPORT int jack_release_timebase(jack_client_t* ext_client)
  1235. {
  1236. #ifdef __CLIENTDEBUG__
  1237. JackLibGlobals::CheckContext();
  1238. #endif
  1239. JackClient* client = (JackClient*)ext_client;
  1240. if (client == NULL) {
  1241. jack_error("jack_release_timebase called with a NULL client");
  1242. return -1;
  1243. } else {
  1244. return client->ReleaseTimebase();
  1245. }
  1246. }
  1247. EXPORT int jack_set_sync_callback(jack_client_t* ext_client, JackSyncCallback sync_callback, void *arg)
  1248. {
  1249. #ifdef __CLIENTDEBUG__
  1250. JackLibGlobals::CheckContext();
  1251. #endif
  1252. JackClient* client = (JackClient*)ext_client;
  1253. if (client == NULL) {
  1254. jack_error("jack_set_sync_callback called with a NULL client");
  1255. return -1;
  1256. } else {
  1257. return client->SetSyncCallback(sync_callback, arg);
  1258. }
  1259. }
  1260. EXPORT int jack_set_sync_timeout(jack_client_t* ext_client, jack_time_t timeout)
  1261. {
  1262. #ifdef __CLIENTDEBUG__
  1263. JackLibGlobals::CheckContext();
  1264. #endif
  1265. JackClient* client = (JackClient*)ext_client;
  1266. if (client == NULL) {
  1267. jack_error("jack_set_sync_timeout called with a NULL client");
  1268. return -1;
  1269. } else {
  1270. return client->SetSyncTimeout(timeout);
  1271. }
  1272. }
  1273. EXPORT int jack_set_timebase_callback(jack_client_t* ext_client, int conditional, JackTimebaseCallback timebase_callback, void* arg)
  1274. {
  1275. #ifdef __CLIENTDEBUG__
  1276. JackLibGlobals::CheckContext();
  1277. #endif
  1278. JackClient* client = (JackClient*)ext_client;
  1279. if (client == NULL) {
  1280. jack_error("jack_set_timebase_callback called with a NULL client");
  1281. return -1;
  1282. } else {
  1283. return client->SetTimebaseCallback(conditional, timebase_callback, arg);
  1284. }
  1285. }
  1286. EXPORT int jack_transport_locate(jack_client_t* ext_client, jack_nframes_t frame)
  1287. {
  1288. #ifdef __CLIENTDEBUG__
  1289. JackLibGlobals::CheckContext();
  1290. #endif
  1291. JackClient* client = (JackClient*)ext_client;
  1292. if (client == NULL) {
  1293. jack_error("jack_transport_locate called with a NULL client");
  1294. return -1;
  1295. } else {
  1296. return client->TransportLocate(frame);
  1297. }
  1298. }
  1299. EXPORT jack_transport_state_t jack_transport_query(const jack_client_t* ext_client, jack_position_t* pos)
  1300. {
  1301. #ifdef __CLIENTDEBUG__
  1302. JackLibGlobals::CheckContext();
  1303. #endif
  1304. JackClient* client = (JackClient*)ext_client;
  1305. if (client == NULL) {
  1306. jack_error("jack_transport_query called with a NULL client");
  1307. return JackTransportStopped;
  1308. } else {
  1309. return client->TransportQuery(pos);
  1310. }
  1311. }
  1312. EXPORT jack_nframes_t jack_get_current_transport_frame(const jack_client_t* ext_client)
  1313. {
  1314. #ifdef __CLIENTDEBUG__
  1315. JackLibGlobals::CheckContext();
  1316. #endif
  1317. JackClient* client = (JackClient*)ext_client;
  1318. if (client == NULL) {
  1319. jack_error("jack_get_current_transport_frame called with a NULL client");
  1320. return 0;
  1321. } else {
  1322. return client->GetCurrentTransportFrame();
  1323. }
  1324. }
  1325. EXPORT int jack_transport_reposition(jack_client_t* ext_client, jack_position_t* pos)
  1326. {
  1327. #ifdef __CLIENTDEBUG__
  1328. JackLibGlobals::CheckContext();
  1329. #endif
  1330. JackClient* client = (JackClient*)ext_client;
  1331. if (client == NULL) {
  1332. jack_error("jack_transport_reposition called with a NULL client");
  1333. return -1;
  1334. } else {
  1335. return client->TransportReposition(pos);
  1336. }
  1337. }
  1338. EXPORT void jack_transport_start(jack_client_t* ext_client)
  1339. {
  1340. #ifdef __CLIENTDEBUG__
  1341. JackLibGlobals::CheckContext();
  1342. #endif
  1343. JackClient* client = (JackClient*)ext_client;
  1344. if (client == NULL) {
  1345. jack_error("jack_transport_start called with a NULL client");
  1346. } else {
  1347. client->TransportStart();
  1348. }
  1349. }
  1350. EXPORT void jack_transport_stop(jack_client_t* ext_client)
  1351. {
  1352. #ifdef __CLIENTDEBUG__
  1353. JackLibGlobals::CheckContext();
  1354. #endif
  1355. JackClient* client = (JackClient*)ext_client;
  1356. if (client == NULL) {
  1357. jack_error("jack_transport_stop called with a NULL client");
  1358. } else {
  1359. client->TransportStop();
  1360. }
  1361. }
  1362. // deprecated
  1363. EXPORT void jack_get_transport_info(jack_client_t* ext_client, jack_transport_info_t* tinfo)
  1364. {
  1365. jack_error("jack_get_transport_info: deprecated");
  1366. if (tinfo)
  1367. memset(tinfo, 0, sizeof(jack_transport_info_t));
  1368. }
  1369. EXPORT void jack_set_transport_info(jack_client_t* ext_client, jack_transport_info_t* tinfo)
  1370. {
  1371. jack_error("jack_set_transport_info: deprecated");
  1372. if (tinfo)
  1373. memset(tinfo, 0, sizeof(jack_transport_info_t));
  1374. }
  1375. // statistics.h
  1376. EXPORT float jack_get_max_delayed_usecs(jack_client_t* ext_client)
  1377. {
  1378. #ifdef __CLIENTDEBUG__
  1379. JackLibGlobals::CheckContext();
  1380. #endif
  1381. jack_log("jack_get_max_delayed_usecs: not yet implemented");
  1382. return 0.f;
  1383. }
  1384. EXPORT float jack_get_xrun_delayed_usecs(jack_client_t* ext_client)
  1385. {
  1386. #ifdef __CLIENTDEBUG__
  1387. JackLibGlobals::CheckContext();
  1388. #endif
  1389. jack_log("jack_get_xrun_delayed_usecs: not yet implemented");
  1390. return 0.f;
  1391. }
  1392. EXPORT void jack_reset_max_delayed_usecs(jack_client_t* ext_client)
  1393. {
  1394. #ifdef __CLIENTDEBUG__
  1395. JackLibGlobals::CheckContext();
  1396. #endif
  1397. jack_log("jack_reset_max_delayed_usecs: not yet implemented");
  1398. }
  1399. // thread.h
  1400. EXPORT int jack_acquire_real_time_scheduling(pthread_t thread, int priority)
  1401. {
  1402. #ifdef __APPLE__
  1403. JackEngineControl* control = GetEngineControl();
  1404. return (control ? JackMachThread::AcquireRealTimeImp(thread, GetEngineControl()->fPeriod, GetEngineControl()->fComputation, GetEngineControl()->fConstraint) : -1);
  1405. #elif WIN32
  1406. return JackWinThread::AcquireRealTimeImp(thread, priority);
  1407. #else
  1408. return JackPosixThread::AcquireRealTimeImp(thread, priority);
  1409. #endif
  1410. }
  1411. EXPORT int jack_client_create_thread(jack_client_t* client,
  1412. pthread_t *thread,
  1413. int priority,
  1414. int realtime, /* boolean */
  1415. void *(*start_routine)(void*),
  1416. void *arg)
  1417. {
  1418. #ifdef __APPLE__
  1419. return JackPosixThread::StartImp(thread, priority, realtime, start_routine, arg);
  1420. #elif WIN32
  1421. return JackWinThread::StartImp(thread, priority, realtime, (ThreadCallback)start_routine, arg);
  1422. #else
  1423. return JackPosixThread::StartImp(thread, priority, realtime, start_routine, arg);
  1424. #endif
  1425. }
  1426. EXPORT int jack_drop_real_time_scheduling(pthread_t thread)
  1427. {
  1428. #ifdef __APPLE__
  1429. return JackMachThread::DropRealTimeImp(thread);
  1430. #elif WIN32
  1431. return JackWinThread::DropRealTimeImp(thread);
  1432. #else
  1433. return JackPosixThread::DropRealTimeImp(thread);
  1434. #endif
  1435. }
  1436. // intclient.h
  1437. EXPORT int jack_internal_client_new (const char *client_name,
  1438. const char *load_name,
  1439. const char *load_init)
  1440. {
  1441. jack_error("jack_internal_client_new: deprecated");
  1442. return -1;
  1443. }
  1444. EXPORT void jack_internal_client_close (const char *client_name)
  1445. {
  1446. jack_error("jack_internal_client_close: deprecated");
  1447. }
  1448. EXPORT char* jack_get_internal_client_name(jack_client_t* ext_client, jack_intclient_t intclient)
  1449. {
  1450. #ifdef __CLIENTDEBUG__
  1451. JackLibGlobals::CheckContext();
  1452. #endif
  1453. JackClient* client = (JackClient*)ext_client;
  1454. if (client == NULL) {
  1455. jack_error("jack_get_internal_client_name called with a NULL client");
  1456. return NULL;
  1457. } else if (intclient < 0 || intclient >= CLIENT_NUM) {
  1458. jack_error("jack_get_internal_client_name: incorrect client");
  1459. return NULL;
  1460. } else {
  1461. return client->GetInternalClientName(intclient);
  1462. }
  1463. }
  1464. EXPORT jack_intclient_t jack_internal_client_handle(jack_client_t* ext_client, const char* client_name, jack_status_t* status)
  1465. {
  1466. #ifdef __CLIENTDEBUG__
  1467. JackLibGlobals::CheckContext();
  1468. #endif
  1469. JackClient* client = (JackClient*)ext_client;
  1470. if (client == NULL) {
  1471. jack_error("jack_internal_client_handle called with a NULL client");
  1472. return 0;
  1473. } else {
  1474. jack_status_t my_status;
  1475. if (status == NULL) /* no status from caller? */
  1476. status = &my_status; /* use local status word */
  1477. *status = (jack_status_t)0;
  1478. return client->InternalClientHandle(client_name, status);
  1479. }
  1480. }
  1481. 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)
  1482. {
  1483. #ifdef __CLIENTDEBUG__
  1484. JackLibGlobals::CheckContext();
  1485. #endif
  1486. JackClient* client = (JackClient*)ext_client;
  1487. if (client == NULL) {
  1488. jack_error("jack_internal_client_load called with a NULL client");
  1489. return 0;
  1490. } else {
  1491. jack_varargs_t va;
  1492. jack_status_t my_status;
  1493. if (status == NULL) /* no status from caller? */
  1494. status = &my_status; /* use local status word */
  1495. *status = (jack_status_t)0;
  1496. /* validate parameters */
  1497. if ((options & ~JackLoadOptions)) {
  1498. int my_status1 = *status | (JackFailure | JackInvalidOption);
  1499. *status = (jack_status_t)my_status1;
  1500. return 0;
  1501. }
  1502. /* parse variable arguments */
  1503. jack_varargs_parse(options, ap, &va);
  1504. return client->InternalClientLoad(client_name, options, status, &va);
  1505. }
  1506. }
  1507. EXPORT jack_intclient_t jack_internal_client_load(jack_client_t *client, const char *client_name, jack_options_t options, jack_status_t *status, ...)
  1508. {
  1509. va_list ap;
  1510. va_start(ap, status);
  1511. jack_intclient_t res = jack_internal_client_load_aux(client, client_name, options, status, ap);
  1512. va_end(ap);
  1513. return res;
  1514. }
  1515. EXPORT jack_status_t jack_internal_client_unload(jack_client_t* ext_client, jack_intclient_t intclient)
  1516. {
  1517. #ifdef __CLIENTDEBUG__
  1518. JackLibGlobals::CheckContext();
  1519. #endif
  1520. JackClient* client = (JackClient*)ext_client;
  1521. if (client == NULL) {
  1522. jack_error("jack_internal_client_unload called with a NULL client");
  1523. return (jack_status_t)(JackNoSuchClient | JackFailure);
  1524. } else if (intclient < 0 || intclient >= CLIENT_NUM) {
  1525. jack_error("jack_internal_client_unload: incorrect client");
  1526. return (jack_status_t)(JackNoSuchClient | JackFailure);
  1527. } else {
  1528. jack_status_t my_status;
  1529. client->InternalClientUnload(intclient, &my_status);
  1530. return my_status;
  1531. }
  1532. }