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.

1740 lines
56KB

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