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.

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