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.

1639 lines
53KB

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