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.

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