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.

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