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.

1659 lines
54KB

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