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.

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