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.

1644 lines
52KB

  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_thread_init_callback (jack_client_t *client,
  62. JackThreadInitCallback thread_init_callback,
  63. void *arg);
  64. EXPORT int jack_set_freewheel_callback (jack_client_t *client,
  65. JackFreewheelCallback freewheel_callback,
  66. void *arg);
  67. EXPORT int jack_set_freewheel(jack_client_t* client, int onoff);
  68. EXPORT int jack_set_buffer_size (jack_client_t *client, jack_nframes_t nframes);
  69. EXPORT int jack_set_buffer_size_callback (jack_client_t *client,
  70. JackBufferSizeCallback bufsize_callback,
  71. void *arg);
  72. EXPORT int jack_set_sample_rate_callback (jack_client_t *client,
  73. JackSampleRateCallback srate_callback,
  74. void *arg);
  75. EXPORT int jack_set_client_registration_callback (jack_client_t *,
  76. JackClientRegistrationCallback
  77. registration_callback, void *arg);
  78. EXPORT int jack_set_port_registration_callback (jack_client_t *,
  79. JackPortRegistrationCallback
  80. registration_callback, void *arg);
  81. EXPORT int jack_set_port_connect_callback (jack_client_t *,
  82. JackPortConnectCallback
  83. connect_callback, void *arg);
  84. EXPORT int jack_set_graph_order_callback (jack_client_t *,
  85. JackGraphOrderCallback graph_callback,
  86. void *);
  87. EXPORT int jack_set_xrun_callback (jack_client_t *,
  88. JackXRunCallback xrun_callback, void *arg);
  89. EXPORT int jack_activate (jack_client_t *client);
  90. EXPORT int jack_deactivate (jack_client_t *client);
  91. EXPORT jack_port_t * jack_port_register (jack_client_t *client,
  92. const char *port_name,
  93. const char *port_type,
  94. unsigned long flags,
  95. unsigned long buffer_size);
  96. EXPORT int jack_port_unregister (jack_client_t *, jack_port_t *);
  97. EXPORT void * jack_port_get_buffer (jack_port_t *, jack_nframes_t);
  98. EXPORT const char * jack_port_name (const jack_port_t *port);
  99. EXPORT const char * jack_port_short_name (const jack_port_t *port);
  100. EXPORT int jack_port_flags (const jack_port_t *port);
  101. EXPORT const char * jack_port_type (const jack_port_t *port);
  102. EXPORT int jack_port_is_mine (const jack_client_t *, const jack_port_t *port);
  103. EXPORT int jack_port_connected (const jack_port_t *port);
  104. EXPORT int jack_port_connected_to (const jack_port_t *port,
  105. const char *port_name);
  106. EXPORT const char ** jack_port_get_connections (const jack_port_t *port);
  107. EXPORT const char ** jack_port_get_all_connections (const jack_client_t *client,
  108. const jack_port_t *port);
  109. EXPORT int jack_port_tie (jack_port_t *src, jack_port_t *dst);
  110. EXPORT int jack_port_untie (jack_port_t *port);
  111. EXPORT jack_nframes_t jack_port_get_latency (jack_port_t *port);
  112. EXPORT jack_nframes_t jack_port_get_total_latency (jack_client_t *,
  113. jack_port_t *port);
  114. EXPORT void jack_port_set_latency (jack_port_t *, jack_nframes_t);
  115. EXPORT int jack_recompute_total_latency (jack_client_t*, jack_port_t* port);
  116. EXPORT int jack_recompute_total_latencies (jack_client_t*);
  117. EXPORT int jack_port_set_name (jack_port_t *port, const char *port_name);
  118. EXPORT int jack_port_set_alias (jack_port_t *port, const char *alias);
  119. EXPORT int jack_port_unset_alias (jack_port_t *port, const char *alias);
  120. EXPORT int jack_port_get_aliases (const jack_port_t *port, char* const aliases[2]);
  121. EXPORT int jack_port_request_monitor (jack_port_t *port, int onoff);
  122. EXPORT int jack_port_request_monitor_by_name (jack_client_t *client,
  123. const char *port_name, int onoff);
  124. EXPORT int jack_port_ensure_monitor (jack_port_t *port, int onoff);
  125. EXPORT int jack_port_monitoring_input (jack_port_t *port);
  126. EXPORT int jack_connect (jack_client_t *,
  127. const char *source_port,
  128. const char *destination_port);
  129. EXPORT int jack_disconnect (jack_client_t *,
  130. const char *source_port,
  131. const char *destination_port);
  132. EXPORT int jack_port_disconnect (jack_client_t *, jack_port_t *);
  133. EXPORT int jack_port_name_size(void);
  134. EXPORT int jack_port_type_size(void);
  135. EXPORT jack_nframes_t jack_get_sample_rate (jack_client_t *);
  136. EXPORT jack_nframes_t jack_get_buffer_size (jack_client_t *);
  137. EXPORT const char ** jack_get_ports (jack_client_t *,
  138. const char *port_name_pattern,
  139. const char *type_name_pattern,
  140. unsigned long flags);
  141. EXPORT jack_port_t * jack_port_by_name (jack_client_t *, const char *port_name);
  142. EXPORT jack_port_t * jack_port_by_id (jack_client_t *client,
  143. jack_port_id_t port_id);
  144. EXPORT int jack_engine_takeover_timebase (jack_client_t *);
  145. EXPORT jack_nframes_t jack_frames_since_cycle_start (const jack_client_t *);
  146. EXPORT jack_time_t jack_get_time(const jack_client_t *client);
  147. EXPORT jack_nframes_t jack_time_to_frames(const jack_client_t *client, jack_time_t time);
  148. EXPORT jack_time_t jack_frames_to_time(const jack_client_t *client, jack_nframes_t frames);
  149. EXPORT jack_nframes_t jack_frame_time (const jack_client_t *);
  150. EXPORT jack_nframes_t jack_last_frame_time (const jack_client_t *client);
  151. EXPORT float jack_cpu_load (jack_client_t *client);
  152. EXPORT pthread_t jack_client_thread_id (jack_client_t *);
  153. EXPORT void jack_set_error_function (void (*func)(const char *));
  154. EXPORT float jack_get_max_delayed_usecs (jack_client_t *client);
  155. EXPORT float jack_get_xrun_delayed_usecs (jack_client_t *client);
  156. EXPORT void jack_reset_max_delayed_usecs (jack_client_t *client);
  157. EXPORT int jack_release_timebase (jack_client_t *client);
  158. EXPORT int jack_set_sync_callback (jack_client_t *client,
  159. JackSyncCallback sync_callback,
  160. void *arg);
  161. EXPORT int jack_set_sync_timeout (jack_client_t *client,
  162. jack_time_t timeout);
  163. EXPORT int jack_set_timebase_callback (jack_client_t *client,
  164. int conditional,
  165. JackTimebaseCallback timebase_callback,
  166. void *arg);
  167. EXPORT int jack_transport_locate (jack_client_t *client,
  168. jack_nframes_t frame);
  169. EXPORT jack_transport_state_t jack_transport_query (const jack_client_t *client,
  170. jack_position_t *pos);
  171. EXPORT jack_nframes_t jack_get_current_transport_frame (const jack_client_t *client);
  172. EXPORT int jack_transport_reposition (jack_client_t *client,
  173. jack_position_t *pos);
  174. EXPORT void jack_transport_start (jack_client_t *client);
  175. EXPORT void jack_transport_stop (jack_client_t *client);
  176. EXPORT void jack_get_transport_info (jack_client_t *client,
  177. jack_transport_info_t *tinfo);
  178. EXPORT void jack_set_transport_info (jack_client_t *client,
  179. jack_transport_info_t *tinfo);
  180. EXPORT int jack_acquire_real_time_scheduling (pthread_t thread, int priority);
  181. EXPORT int jack_client_create_thread (jack_client_t* client,
  182. pthread_t *thread,
  183. int priority,
  184. int realtime, // boolean
  185. void *(*start_routine)(void*),
  186. void *arg);
  187. EXPORT int jack_drop_real_time_scheduling (pthread_t thread);
  188. EXPORT char * jack_get_internal_client_name (jack_client_t *client,
  189. jack_intclient_t intclient);
  190. EXPORT jack_intclient_t jack_internal_client_handle (jack_client_t *client,
  191. const char *client_name,
  192. jack_status_t *status);
  193. EXPORT jack_intclient_t jack_internal_client_load (jack_client_t *client,
  194. const char *client_name,
  195. jack_options_t options,
  196. jack_status_t *status, ...);
  197. EXPORT jack_status_t jack_internal_client_unload (jack_client_t *client,
  198. jack_intclient_t intclient);
  199. #ifdef __cplusplus
  200. }
  201. #endif
  202. #ifdef WIN32
  203. /* missing on Windows : see http://bugs.mysql.com/bug.php?id=15936 */
  204. inline double rint(double nr)
  205. {
  206. double f = floor(nr);
  207. double c = ceil(nr);
  208. return (((c -nr) >= (nr - f)) ? f : c);
  209. }
  210. #endif
  211. static inline bool CheckPort(jack_port_id_t port_index)
  212. {
  213. return (port_index > 0 && port_index < PORT_NUM);
  214. }
  215. static inline bool CheckBufferSize(jack_nframes_t buffer_size)
  216. {
  217. return (buffer_size <= BUFFER_SIZE_MAX);
  218. }
  219. static inline void WaitGraphChange()
  220. {
  221. JackGraphManager* manager = GetGraphManager();
  222. JackEngineControl* control = GetEngineControl();
  223. if (manager && control && manager->IsPendingChange()) {
  224. JackLog("WaitGraphChange...\n");
  225. JackSleep(int(control->fPeriodUsecs * 1.1f));
  226. }
  227. }
  228. EXPORT void jack_set_error_function (void (*func)(const char *))
  229. {
  230. jack_error_callback = func;
  231. }
  232. EXPORT jack_client_t* jack_client_new(const char* client_name)
  233. {
  234. jack_error("jack_client_new: deprecated");
  235. int options = JackUseExactName;
  236. if (getenv("JACK_START_SERVER") == NULL)
  237. options |= JackNoStartServer;
  238. return jack_client_open(client_name, (jack_options_t)options, NULL);
  239. }
  240. EXPORT void* jack_port_get_buffer(jack_port_t* port, jack_nframes_t frames)
  241. {
  242. #ifdef __CLIENTDEBUG__
  243. JackLibGlobals::CheckContext();
  244. #endif
  245. jack_port_id_t myport = (jack_port_id_t)port;
  246. if (!CheckPort(myport)) {
  247. jack_error("jack_port_get_buffer called with an incorrect port %ld", myport);
  248. return NULL;
  249. } else {
  250. JackGraphManager* manager = GetGraphManager();
  251. return (manager ? manager->GetBuffer(myport, frames) : NULL);
  252. }
  253. }
  254. EXPORT const char* jack_port_name(const jack_port_t* port)
  255. {
  256. #ifdef __CLIENTDEBUG__
  257. JackLibGlobals::CheckContext();
  258. #endif
  259. jack_port_id_t myport = (jack_port_id_t)port;
  260. if (!CheckPort(myport)) {
  261. jack_error("jack_port_name called with an incorrect port %ld", myport);
  262. return NULL;
  263. } else {
  264. JackGraphManager* manager = GetGraphManager();
  265. return (manager ? manager->GetPort(myport)->GetName() : NULL);
  266. }
  267. }
  268. EXPORT const char* jack_port_short_name(const jack_port_t* port)
  269. {
  270. #ifdef __CLIENTDEBUG__
  271. JackLibGlobals::CheckContext();
  272. #endif
  273. jack_port_id_t myport = (jack_port_id_t)port;
  274. if (!CheckPort(myport)) {
  275. jack_error("jack_port_short_name called with an incorrect port %ld", myport);
  276. return NULL;
  277. } else {
  278. JackGraphManager* manager = GetGraphManager();
  279. return (manager ? manager->GetPort(myport)->GetShortName() : NULL);
  280. }
  281. }
  282. EXPORT int jack_port_flags(const jack_port_t* port)
  283. {
  284. #ifdef __CLIENTDEBUG__
  285. JackLibGlobals::CheckContext();
  286. #endif
  287. jack_port_id_t myport = (jack_port_id_t)port;
  288. if (!CheckPort(myport)) {
  289. jack_error("jack_port_flags called with an incorrect port %ld", myport);
  290. return -1;
  291. } else {
  292. JackGraphManager* manager = GetGraphManager();
  293. return (manager ? manager->GetPort(myport)->GetFlags() : -1);
  294. }
  295. }
  296. EXPORT const char* jack_port_type(const jack_port_t* port)
  297. {
  298. #ifdef __CLIENTDEBUG__
  299. JackLibGlobals::CheckContext();
  300. #endif
  301. jack_port_id_t myport = (jack_port_id_t)port;
  302. if (!CheckPort(myport)) {
  303. jack_error("jack_port_flags called an incorrect port %ld", myport);
  304. return NULL;
  305. } else {
  306. JackGraphManager* manager = GetGraphManager();
  307. return (manager ? manager->GetPort(myport)->GetType() : NULL);
  308. }
  309. }
  310. EXPORT int jack_port_connected(const jack_port_t* port)
  311. {
  312. #ifdef __CLIENTDEBUG__
  313. JackLibGlobals::CheckContext();
  314. #endif
  315. jack_port_id_t myport = (jack_port_id_t)port;
  316. if (!CheckPort(myport)) {
  317. jack_error("jack_port_connected called with an incorrect port %ld", myport);
  318. return -1;
  319. } else {
  320. WaitGraphChange();
  321. JackGraphManager* manager = GetGraphManager();
  322. return (manager ? manager->GetConnectionsNum(myport) : -1);
  323. }
  324. }
  325. EXPORT int jack_port_connected_to(const jack_port_t* port, const char* port_name)
  326. {
  327. #ifdef __CLIENTDEBUG__
  328. JackLibGlobals::CheckContext();
  329. #endif
  330. jack_port_id_t src = (jack_port_id_t)port;
  331. if (!CheckPort(src)) {
  332. jack_error("jack_port_connected_to called with an incorrect port %ld", src);
  333. return -1;
  334. } else if (port_name == NULL) {
  335. jack_error("jack_port_connected_to called with a NULL port name");
  336. return -1;
  337. } else {
  338. WaitGraphChange();
  339. JackGraphManager* manager = GetGraphManager();
  340. jack_port_id_t dst = (manager ? manager->GetPort(port_name) : NO_PORT);
  341. if (dst == NO_PORT) {
  342. jack_error("Unknown destination port port_name = %s", port_name);
  343. return 0;
  344. } else {
  345. return manager->IsConnected(src, dst);
  346. }
  347. }
  348. }
  349. EXPORT int jack_port_tie(jack_port_t* src, jack_port_t* dst)
  350. {
  351. #ifdef __CLIENTDEBUG__
  352. JackLibGlobals::CheckContext();
  353. #endif
  354. jack_port_id_t mysrc = (jack_port_id_t)src;
  355. if (!CheckPort(mysrc)) {
  356. jack_error("jack_port_tie called with a NULL src port");
  357. return -1;
  358. }
  359. jack_port_id_t mydst = (jack_port_id_t)dst;
  360. if (!CheckPort(mydst)) {
  361. jack_error("jack_port_tie called with a NULL dst port");
  362. return -1;
  363. }
  364. JackGraphManager* manager = GetGraphManager();
  365. if (manager && manager->GetPort(mysrc)->GetRefNum() != manager->GetPort(mydst)->GetRefNum()) {
  366. jack_error("jack_port_tie called with ports not belonging to the same client");
  367. return -1;
  368. } else {
  369. return manager->GetPort(mydst)->Tie(mysrc);
  370. }
  371. }
  372. EXPORT int jack_port_untie(jack_port_t* port)
  373. {
  374. #ifdef __CLIENTDEBUG__
  375. JackLibGlobals::CheckContext();
  376. #endif
  377. jack_port_id_t myport = (jack_port_id_t)port;
  378. if (!CheckPort(myport)) {
  379. jack_error("jack_port_untie called with an incorrect port %ld", myport);
  380. return -1;
  381. } else {
  382. JackGraphManager* manager = GetGraphManager();
  383. return (manager ? manager->GetPort(myport)->UnTie() : -1);
  384. }
  385. }
  386. EXPORT jack_nframes_t jack_port_get_latency(jack_port_t* port)
  387. {
  388. #ifdef __CLIENTDEBUG__
  389. JackLibGlobals::CheckContext();
  390. #endif
  391. jack_port_id_t myport = (jack_port_id_t)port;
  392. if (!CheckPort(myport)) {
  393. jack_error("jack_port_get_latency called with an incorrect port %ld", myport);
  394. return 0;
  395. } else {
  396. WaitGraphChange();
  397. JackGraphManager* manager = GetGraphManager();
  398. return (manager ? manager->GetPort(myport)->GetLatency() : 0);
  399. }
  400. }
  401. EXPORT void jack_port_set_latency(jack_port_t* port, jack_nframes_t frames)
  402. {
  403. #ifdef __CLIENTDEBUG__
  404. JackLibGlobals::CheckContext();
  405. #endif
  406. jack_port_id_t myport = (jack_port_id_t)port;
  407. if (!CheckPort(myport)) {
  408. jack_error("jack_port_set_latency called with an incorrect port %ld", myport);
  409. } else {
  410. JackGraphManager* manager = GetGraphManager();
  411. if (manager)
  412. manager->GetPort(myport)->SetLatency(frames);
  413. }
  414. }
  415. EXPORT int jack_recompute_total_latency(jack_client_t* ext_client, jack_port_t* port)
  416. {
  417. #ifdef __CLIENTDEBUG__
  418. JackLibGlobals::CheckContext();
  419. #endif
  420. JackClient* client = (JackClient*)ext_client;
  421. jack_port_id_t myport = (jack_port_id_t)port;
  422. if (client == NULL) {
  423. jack_error("jack_recompute_total_latencies called with a NULL client");
  424. return -1;
  425. } else if (!CheckPort(myport)) {
  426. jack_error("jack_recompute_total_latencies called with a NULL port");
  427. return -1;
  428. } else {
  429. WaitGraphChange();
  430. JackGraphManager* manager = GetGraphManager();
  431. return (manager ? manager->ComputeTotalLatency(myport) : -1);
  432. }
  433. }
  434. EXPORT int jack_recompute_total_latencies(jack_client_t* ext_client)
  435. {
  436. #ifdef __CLIENTDEBUG__
  437. JackLibGlobals::CheckContext();
  438. #endif
  439. JackClient* client = (JackClient*)ext_client;
  440. if (client == NULL) {
  441. jack_error("jack_recompute_total_latencies called with a NULL client");
  442. return -1;
  443. } else {
  444. WaitGraphChange();
  445. JackGraphManager* manager = GetGraphManager();
  446. return (manager ? manager->ComputeTotalLatencies() : -1);
  447. }
  448. }
  449. EXPORT int jack_port_set_name(jack_port_t* port, const char* name)
  450. {
  451. #ifdef __CLIENTDEBUG__
  452. JackLibGlobals::CheckContext();
  453. #endif
  454. jack_port_id_t myport = (jack_port_id_t)port;
  455. if (!CheckPort(myport)) {
  456. jack_error("jack_port_set_name called with an incorrect port %ld", myport);
  457. return -1;
  458. } else if (name == NULL) {
  459. jack_error("jack_port_set_name called with a NULL port name");
  460. return -1;
  461. } else {
  462. JackGraphManager* manager = GetGraphManager();
  463. return (manager ? manager->GetPort(myport)->SetName(name) : -1);
  464. }
  465. }
  466. EXPORT int jack_port_set_alias(jack_port_t* port, const char* name)
  467. {
  468. #ifdef __CLIENTDEBUG__
  469. JackLibGlobals::CheckContext();
  470. #endif
  471. jack_port_id_t myport = (jack_port_id_t)port;
  472. if (!CheckPort(myport)) {
  473. jack_error("jack_port_set_alias called with an incorrect port %ld", myport);
  474. return -1;
  475. } else if (name == NULL) {
  476. jack_error("jack_port_set_alias called with a NULL port name");
  477. return -1;
  478. } else {
  479. JackGraphManager* manager = GetGraphManager();
  480. return (manager ? manager->GetPort(myport)->SetAlias(name) : -1);
  481. }
  482. }
  483. EXPORT int jack_port_unset_alias(jack_port_t* port, const char* name)
  484. {
  485. #ifdef __CLIENTDEBUG__
  486. JackLibGlobals::CheckContext();
  487. #endif
  488. jack_port_id_t myport = (jack_port_id_t)port;
  489. if (!CheckPort(myport)) {
  490. jack_error("jack_port_unset_alias called with an incorrect port %ld", myport);
  491. return -1;
  492. } else if (name == NULL) {
  493. jack_error("jack_port_unset_alias called with a NULL port name");
  494. return -1;
  495. } else {
  496. JackGraphManager* manager = GetGraphManager();
  497. return (manager ? manager->GetPort(myport)->UnsetAlias(name) : -1);
  498. }
  499. }
  500. EXPORT int jack_port_get_aliases(const jack_port_t* port, char* const aliases[2])
  501. {
  502. #ifdef __CLIENTDEBUG__
  503. JackLibGlobals::CheckContext();
  504. #endif
  505. jack_port_id_t myport = (jack_port_id_t)port;
  506. if (!CheckPort(myport)) {
  507. jack_error("jack_port_get_aliases called with an incorrect port %ld", myport);
  508. return -1;
  509. } else {
  510. JackGraphManager* manager = GetGraphManager();
  511. return (manager ? manager->GetPort(myport)->GetAliases(aliases) : -1);
  512. }
  513. }
  514. EXPORT int jack_port_request_monitor(jack_port_t* port, int onoff)
  515. {
  516. #ifdef __CLIENTDEBUG__
  517. JackLibGlobals::CheckContext();
  518. #endif
  519. jack_port_id_t myport = (jack_port_id_t)port;
  520. if (!CheckPort(myport)) {
  521. jack_error("jack_port_request_monitor called with an incorrect port %ld", myport);
  522. return -1;
  523. } else {
  524. JackGraphManager* manager = GetGraphManager();
  525. return (manager ? manager->RequestMonitor(myport, onoff) : -1);
  526. }
  527. }
  528. EXPORT int jack_port_request_monitor_by_name(jack_client_t* ext_client, const char* port_name, int onoff)
  529. {
  530. #ifdef __CLIENTDEBUG__
  531. JackLibGlobals::CheckContext();
  532. #endif
  533. JackClient* client = (JackClient*)ext_client;
  534. if (client == NULL) {
  535. jack_error("jack_port_request_monitor_by_name called with a NULL client");
  536. return -1;
  537. } else {
  538. JackGraphManager* manager = GetGraphManager();
  539. if (!manager)
  540. return -1;
  541. jack_port_id_t myport = manager->GetPort(port_name);
  542. if (!CheckPort(myport)) {
  543. jack_error("jack_port_request_monitor_by_name called with an incorrect port %s", port_name);
  544. return -1;
  545. } else {
  546. return manager->RequestMonitor(myport, onoff);
  547. }
  548. }
  549. }
  550. EXPORT int jack_port_ensure_monitor(jack_port_t* port, int onoff)
  551. {
  552. #ifdef __CLIENTDEBUG__
  553. JackLibGlobals::CheckContext();
  554. #endif
  555. jack_port_id_t myport = (jack_port_id_t)port;
  556. if (!CheckPort(myport)) {
  557. jack_error("jack_port_ensure_monitor called with an incorrect port %ld", myport);
  558. return -1;
  559. } else {
  560. JackGraphManager* manager = GetGraphManager();
  561. return (manager ? manager->GetPort(myport)->EnsureMonitor(onoff) : -1);
  562. }
  563. }
  564. EXPORT int jack_port_monitoring_input(jack_port_t* port)
  565. {
  566. #ifdef __CLIENTDEBUG__
  567. JackLibGlobals::CheckContext();
  568. #endif
  569. jack_port_id_t myport = (jack_port_id_t)port;
  570. if (!CheckPort(myport)) {
  571. jack_error("jack_port_monitoring_input called with an incorrect port %ld", myport);
  572. return -1;
  573. } else {
  574. JackGraphManager* manager = GetGraphManager();
  575. return (manager ? manager->GetPort(myport)->MonitoringInput() : -1);
  576. }
  577. }
  578. EXPORT int jack_is_realtime(jack_client_t* ext_client)
  579. {
  580. #ifdef __CLIENTDEBUG__
  581. JackLibGlobals::CheckContext();
  582. #endif
  583. JackClient* client = (JackClient*)ext_client;
  584. if (client == NULL) {
  585. jack_error("jack_is_realtime called with a NULL client");
  586. return -1;
  587. } else {
  588. JackEngineControl* control = GetEngineControl();
  589. return (control ? control->fRealTime : -1);
  590. }
  591. }
  592. EXPORT void jack_on_shutdown(jack_client_t* ext_client, void (*function)(void* arg), void* arg)
  593. {
  594. #ifdef __CLIENTDEBUG__
  595. JackLibGlobals::CheckContext();
  596. #endif
  597. JackClient* client = (JackClient*)ext_client;
  598. if (client == NULL) {
  599. jack_error("jack_on_shutdown called with a NULL client");
  600. } else {
  601. client->OnShutdown(function, arg);
  602. }
  603. }
  604. EXPORT int jack_set_process_callback(jack_client_t* ext_client, JackProcessCallback callback, void* arg)
  605. {
  606. #ifdef __CLIENTDEBUG__
  607. JackLibGlobals::CheckContext();
  608. #endif
  609. JackClient* client = (JackClient*)ext_client;
  610. if (client == NULL) {
  611. jack_error("jack_set_process_callback called with a NULL client");
  612. return -1;
  613. } else {
  614. return client->SetProcessCallback(callback, arg);
  615. }
  616. }
  617. EXPORT jack_nframes_t jack_thread_wait(jack_client_t* ext_client, int status)
  618. {
  619. #ifdef __CLIENTDEBUG__
  620. JackLibGlobals::CheckContext();
  621. #endif
  622. JackClient* client = (JackClient*)ext_client;
  623. if (client == NULL) {
  624. jack_error("jack_thread_wait called with a NULL client");
  625. return 0;
  626. } else {
  627. return client->Wait(status);
  628. }
  629. }
  630. EXPORT jack_nframes_t jack_cycle_wait(jack_client_t* ext_client)
  631. {
  632. #ifdef __CLIENTDEBUG__
  633. JackLibGlobals::CheckContext();
  634. #endif
  635. JackClient* client = (JackClient*)ext_client;
  636. if (client == NULL) {
  637. jack_error("jack_cycle_wait called with a NULL client");
  638. return 0;
  639. } else {
  640. return client->CycleWait();
  641. }
  642. }
  643. EXPORT void jack_cycle_signal(jack_client_t* ext_client, int status)
  644. {
  645. #ifdef __CLIENTDEBUG__
  646. JackLibGlobals::CheckContext();
  647. #endif
  648. JackClient* client = (JackClient*)ext_client;
  649. if (client == NULL) {
  650. jack_error("jack_cycle_signal called with a NULL client");
  651. } else {
  652. client->CycleSignal(status);
  653. }
  654. }
  655. EXPORT int jack_set_freewheel_callback(jack_client_t* ext_client, JackFreewheelCallback freewheel_callback, void* arg)
  656. {
  657. #ifdef __CLIENTDEBUG__
  658. JackLibGlobals::CheckContext();
  659. #endif
  660. JackClient* client = (JackClient*)ext_client;
  661. if (client == NULL) {
  662. jack_error("jack_set_freewheel_callback called with a NULL client");
  663. return -1;
  664. } else {
  665. return client->SetFreewheelCallback(freewheel_callback, arg);
  666. }
  667. }
  668. EXPORT int jack_set_freewheel(jack_client_t* ext_client, int onoff)
  669. {
  670. #ifdef __CLIENTDEBUG__
  671. JackLibGlobals::CheckContext();
  672. #endif
  673. JackClient* client = (JackClient*)ext_client;
  674. if (client == NULL) {
  675. jack_error("jack_set_freewheel called with a NULL client");
  676. return -1;
  677. } else {
  678. return client->SetFreeWheel(onoff);
  679. }
  680. }
  681. EXPORT int jack_set_buffer_size(jack_client_t* ext_client, jack_nframes_t buffer_size)
  682. {
  683. #ifdef __CLIENTDEBUG__
  684. JackLibGlobals::CheckContext();
  685. #endif
  686. JackClient* client = (JackClient*)ext_client;
  687. if (client == NULL) {
  688. jack_error("jack_set_buffer_size called with a NULL client");
  689. return -1;
  690. } else if (!CheckBufferSize(buffer_size)) {
  691. return -1;
  692. } else {
  693. return client->SetBufferSize(buffer_size);
  694. }
  695. }
  696. EXPORT int jack_set_buffer_size_callback(jack_client_t* ext_client, JackBufferSizeCallback bufsize_callback, void* arg)
  697. {
  698. #ifdef __CLIENTDEBUG__
  699. JackLibGlobals::CheckContext();
  700. #endif
  701. JackClient* client = (JackClient*)ext_client;
  702. if (client == NULL) {
  703. jack_error("jack_set_buffer_size_callback called with a NULL client");
  704. return -1;
  705. } else {
  706. return client->SetBufferSizeCallback(bufsize_callback, arg);
  707. }
  708. }
  709. EXPORT int jack_set_sample_rate_callback(jack_client_t* ext_client, JackSampleRateCallback srate_callback, void* arg)
  710. {
  711. #ifdef __CLIENTDEBUG__
  712. JackLibGlobals::CheckContext();
  713. #endif
  714. JackClient* client = (JackClient*)ext_client;
  715. if (client == NULL) {
  716. jack_error("jack_set_sample_rate_callback called with a NULL client");
  717. return -1;
  718. } else {
  719. jack_error("jack_set_sample_rate_callback: deprecated");
  720. return 0;
  721. }
  722. }
  723. EXPORT int jack_set_client_registration_callback(jack_client_t* ext_client, JackClientRegistrationCallback registration_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_client_registration_callback called with a NULL client");
  731. return -1;
  732. } else {
  733. return client->SetClientRegistrationCallback(registration_callback, arg);
  734. }
  735. }
  736. EXPORT int jack_set_port_registration_callback(jack_client_t* ext_client, JackPortRegistrationCallback registration_callback, void* arg)
  737. {
  738. #ifdef __CLIENTDEBUG__
  739. JackLibGlobals::CheckContext();
  740. #endif
  741. JackClient* client = (JackClient*)ext_client;
  742. if (client == NULL) {
  743. jack_error("jack_set_port_registration_callback called with a NULL client");
  744. return -1;
  745. } else {
  746. return client->SetPortRegistrationCallback(registration_callback, arg);
  747. }
  748. }
  749. EXPORT int jack_set_port_connect_callback(jack_client_t* ext_client, JackPortConnectCallback portconnect_callback, void* arg)
  750. {
  751. #ifdef __CLIENTDEBUG__
  752. JackLibGlobals::CheckContext();
  753. #endif
  754. JackClient* client = (JackClient*)ext_client;
  755. if (client == NULL) {
  756. jack_error("jack_set_port_connect_callback called with a NULL client");
  757. return -1;
  758. } else {
  759. return client->SetPortConnectCallback(portconnect_callback, arg);
  760. }
  761. }
  762. EXPORT int jack_set_graph_order_callback(jack_client_t* ext_client, JackGraphOrderCallback graph_callback, void* arg)
  763. {
  764. #ifdef __CLIENTDEBUG__
  765. JackLibGlobals::CheckContext();
  766. #endif
  767. JackClient* client = (JackClient*)ext_client;
  768. JackLog("jack_set_graph_order_callback ext_client %x client %x \n", ext_client, client);
  769. if (client == NULL) {
  770. jack_error("jack_set_graph_order_callback called with a NULL client");
  771. return -1;
  772. } else {
  773. return client->SetGraphOrderCallback(graph_callback, arg);
  774. }
  775. }
  776. EXPORT int jack_set_xrun_callback(jack_client_t* ext_client, JackXRunCallback xrun_callback, void* arg)
  777. {
  778. #ifdef __CLIENTDEBUG__
  779. JackLibGlobals::CheckContext();
  780. #endif
  781. JackClient* client = (JackClient*)ext_client;
  782. if (client == NULL) {
  783. jack_error("jack_set_xrun_callback called with a NULL client");
  784. return -1;
  785. } else {
  786. return client->SetXRunCallback(xrun_callback, arg);
  787. }
  788. }
  789. EXPORT int jack_set_thread_init_callback(jack_client_t* ext_client, JackThreadInitCallback init_callback, void *arg)
  790. {
  791. #ifdef __CLIENTDEBUG__
  792. JackLibGlobals::CheckContext();
  793. #endif
  794. JackClient* client = (JackClient*)ext_client;
  795. JackLog("jack_set_thread_init_callback ext_client %x client %x \n", ext_client, client);
  796. if (client == NULL) {
  797. jack_error("jack_set_thread_init_callback called with a NULL client");
  798. return -1;
  799. } else {
  800. return client->SetInitCallback(init_callback, arg);
  801. }
  802. }
  803. EXPORT int jack_activate(jack_client_t* ext_client)
  804. {
  805. #ifdef __CLIENTDEBUG__
  806. JackLibGlobals::CheckContext();
  807. #endif
  808. JackClient* client = (JackClient*)ext_client;
  809. if (client == NULL) {
  810. jack_error("jack_activate called with a NULL client");
  811. return -1;
  812. } else {
  813. return client->Activate();
  814. }
  815. }
  816. EXPORT int jack_deactivate(jack_client_t* ext_client)
  817. {
  818. #ifdef __CLIENTDEBUG__
  819. JackLibGlobals::CheckContext();
  820. #endif
  821. JackClient* client = (JackClient*)ext_client;
  822. if (client == NULL) {
  823. jack_error("jack_deactivate called with a NULL client");
  824. return -1;
  825. } else {
  826. return client->Deactivate();
  827. }
  828. }
  829. 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)
  830. {
  831. #ifdef __CLIENTDEBUG__
  832. JackLibGlobals::CheckContext();
  833. #endif
  834. JackClient* client = (JackClient*)ext_client;
  835. if (client == NULL) {
  836. jack_error("jack_port_register called with a NULL client");
  837. return NULL;
  838. } else if ((port_name == NULL) || (port_type == NULL)) {
  839. jack_error("jack_port_register called with a NULL port name or a NULL port_type");
  840. return NULL;
  841. } else {
  842. return (jack_port_t *)client->PortRegister(port_name, port_type, flags, buffer_size);
  843. }
  844. }
  845. EXPORT int jack_port_unregister(jack_client_t* ext_client, jack_port_t* port)
  846. {
  847. #ifdef __CLIENTDEBUG__
  848. JackLibGlobals::CheckContext();
  849. #endif
  850. JackClient* client = (JackClient*)ext_client;
  851. if (client == NULL) {
  852. jack_error("jack_port_unregister called with a NULL client");
  853. return -1;
  854. }
  855. jack_port_id_t myport = (jack_port_id_t)port;
  856. if (!CheckPort(myport)) {
  857. jack_error("jack_port_unregister called with an incorrect port %ld", myport);
  858. return -1;
  859. }
  860. return client->PortUnRegister(myport);
  861. }
  862. EXPORT int jack_port_is_mine(const jack_client_t* ext_client, const jack_port_t* port)
  863. {
  864. #ifdef __CLIENTDEBUG__
  865. JackLibGlobals::CheckContext();
  866. #endif
  867. JackClient* client = (JackClient*)ext_client;
  868. if (client == NULL) {
  869. jack_error("jack_port_is_mine called with a NULL client");
  870. return -1;
  871. }
  872. jack_port_id_t myport = (jack_port_id_t)port;
  873. if (!CheckPort(myport)) {
  874. jack_error("jack_port_is_mine called with an incorrect port %ld", myport);
  875. return -1;
  876. }
  877. return client->PortIsMine(myport);
  878. }
  879. EXPORT const char** jack_port_get_connections(const jack_port_t* port)
  880. {
  881. #ifdef __CLIENTDEBUG__
  882. JackLibGlobals::CheckContext();
  883. #endif
  884. jack_port_id_t myport = (jack_port_id_t)port;
  885. if (!CheckPort(myport)) {
  886. jack_error("jack_port_get_connections called with an incorrect port %ld", myport);
  887. return NULL;
  888. } else {
  889. WaitGraphChange();
  890. JackGraphManager* manager = GetGraphManager();
  891. return (manager ? manager->GetConnections(myport) : NULL);
  892. }
  893. }
  894. // Calling client does not need to "own" the port
  895. EXPORT const char** jack_port_get_all_connections(const jack_client_t* ext_client, const jack_port_t* port)
  896. {
  897. #ifdef __CLIENTDEBUG__
  898. JackLibGlobals::CheckContext();
  899. #endif
  900. JackClient* client = (JackClient*)ext_client;
  901. if (client == NULL) {
  902. jack_error("jack_port_get_all_connections called with a NULL client");
  903. return NULL;
  904. }
  905. jack_port_id_t myport = (jack_port_id_t)port;
  906. if (!CheckPort(myport)) {
  907. jack_error("jack_port_get_all_connections called with an incorrect port %ld", myport);
  908. return NULL;
  909. } else {
  910. WaitGraphChange();
  911. JackGraphManager* manager = GetGraphManager();
  912. return (manager ? manager->GetConnections(myport) : NULL);
  913. }
  914. }
  915. EXPORT jack_nframes_t jack_port_get_total_latency(jack_client_t* ext_client, jack_port_t* port)
  916. {
  917. #ifdef __CLIENTDEBUG__
  918. JackLibGlobals::CheckContext();
  919. #endif
  920. JackClient* client = (JackClient*)ext_client;
  921. if (client == NULL) {
  922. jack_error("jack_port_get_total_latency called with a NULL client");
  923. return 0;
  924. }
  925. jack_port_id_t myport = (jack_port_id_t)port;
  926. if (!CheckPort(myport)) {
  927. jack_error("jack_port_get_total_latency called with an incorrect port %ld", myport);
  928. return 0;
  929. } else {
  930. WaitGraphChange();
  931. JackGraphManager* manager = GetGraphManager();
  932. return (manager ? manager->GetPort(myport)->GetTotalLatency() : 0);
  933. }
  934. }
  935. EXPORT int jack_connect(jack_client_t* ext_client, const char* src, const char* dst)
  936. {
  937. #ifdef __CLIENTDEBUG__
  938. JackLibGlobals::CheckContext();
  939. #endif
  940. JackClient* client = (JackClient*)ext_client;
  941. if (client == NULL) {
  942. jack_error("jack_connect called with a NULL client");
  943. return -1;
  944. } else if ((src == NULL) || (dst == NULL)) {
  945. jack_error("jack_connect called with a NULL port name");
  946. return -1;
  947. } else {
  948. return client->PortConnect(src, dst);
  949. }
  950. }
  951. EXPORT int jack_disconnect(jack_client_t* ext_client, const char* src, const char* dst)
  952. {
  953. #ifdef __CLIENTDEBUG__
  954. JackLibGlobals::CheckContext();
  955. #endif
  956. JackClient* client = (JackClient*)ext_client;
  957. if (client == NULL) {
  958. jack_error("jack_disconnect called with a NULL client");
  959. return -1;
  960. } else if ((src == NULL) || (dst == NULL)) {
  961. jack_error("jack_connect called with a NULL port name");
  962. return -1;
  963. } else {
  964. return client->PortDisconnect(src, dst);
  965. }
  966. }
  967. EXPORT int jack_port_connect(jack_client_t* ext_client, jack_port_t* src, jack_port_t* dst)
  968. {
  969. #ifdef __CLIENTDEBUG__
  970. JackLibGlobals::CheckContext();
  971. #endif
  972. JackClient* client = (JackClient*)ext_client;
  973. if (client == NULL) {
  974. jack_error("jack_port_connect called with a NULL client");
  975. return -1;
  976. }
  977. jack_port_id_t mysrc = (jack_port_id_t)src;
  978. if (!CheckPort(mysrc)) {
  979. jack_error("jack_port_connect called with a NULL src port");
  980. return -1;
  981. }
  982. jack_port_id_t mydst = (jack_port_id_t)dst;
  983. if (!CheckPort(mydst)) {
  984. jack_error("jack_port_connect called with a NULL dst port");
  985. return -1;
  986. }
  987. return client->PortConnect(mysrc, mydst);
  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(jack_client_t *client)
  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. JackLog("jack_get_max_delayed_usecs: not yet implemented\n");
  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. JackLog("jack_get_xrun_delayed_usecs: not yet implemented\n");
  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. JackLog("jack_reset_max_delayed_usecs: not yet implemented\n");
  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. }