jack1 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.

516 lines
12KB

  1. /*
  2. JACK transport client interface -- runs in the client process.
  3. Copyright (C) 2001-2003 Paul Davis
  4. Copyright (C) 2003 Jack O'Quin
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License
  7. as published by the Free Software Foundation; either version 2.1
  8. of the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with this program; if not, write to the Free
  15. Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  16. 02111-1307, USA.
  17. */
  18. #include <config.h>
  19. #include <errno.h>
  20. #include <math.h>
  21. #include <stdio.h>
  22. #include <jack/atomicity.h>
  23. #include <jack/internal.h>
  24. #include "local.h"
  25. /********************* Internal functions *********************/
  26. /* generate a unique non-zero ID, different for each call */
  27. jack_unique_t
  28. jack_generate_unique_id (jack_control_t *ectl)
  29. {
  30. /* The jack_unique_t is an opaque type. */
  31. return exchange_and_add(&ectl->seq_number, 1);
  32. }
  33. static inline void
  34. jack_read_frame_time (const jack_client_t *client, jack_frame_timer_t *copy)
  35. {
  36. int tries = 0;
  37. long timeout = 1000;
  38. do {
  39. /* throttle the busy wait if we don't get
  40. the answer very quickly.
  41. XXX This is disgusting. on a UP
  42. system, it needs to sleep
  43. if the first try didn't work. on an SMP
  44. system, it should wait for half of
  45. the context switch time before
  46. sleeping.
  47. */
  48. if (tries > 10) {
  49. usleep (20);
  50. tries = 0;
  51. /* debug code to avoid system hangs... */
  52. if (--timeout == 0) {
  53. jack_error ("hung in loop copying position A");
  54. abort();
  55. }
  56. }
  57. *copy = client->engine->frame_timer;
  58. tries++;
  59. } while (copy->guard1 != copy->guard2);
  60. }
  61. /* copy a JACK transport position structure (thread-safe) */
  62. void
  63. jack_transport_copy_position (jack_position_t *from, jack_position_t *to)
  64. {
  65. int tries = 0;
  66. long timeout = 1000;
  67. do {
  68. /* throttle the busy wait if we don't get the answer
  69. * very quickly. See comment above about this
  70. * design.
  71. */
  72. if (tries > 10) {
  73. usleep (20);
  74. tries = 0;
  75. /* debug code to avoid system hangs... */
  76. if (--timeout == 0) {
  77. jack_error ("hung in loop copying position B");
  78. abort();
  79. }
  80. }
  81. *to = *from;
  82. tries++;
  83. } while (to->unique_1 != to->unique_2);
  84. }
  85. static inline int
  86. jack_transport_request_new_pos (jack_client_t *client, jack_position_t *pos)
  87. {
  88. jack_control_t *ectl = client->engine;
  89. /* distinguish this request from all others */
  90. pos->unique_1 = pos->unique_2 = jack_generate_unique_id(ectl);
  91. /* clients may not set these fields */
  92. pos->usecs = ectl->current_time.usecs;
  93. pos->frame_rate = ectl->current_time.frame_rate;
  94. /* carefully copy requested postion into shared memory */
  95. jack_transport_copy_position (pos, &ectl->request_time);
  96. return 0;
  97. }
  98. /******************** Callback invocations ********************/
  99. void
  100. jack_call_sync_client (jack_client_t *client)
  101. {
  102. jack_client_control_t *control = client->control;
  103. jack_control_t *ectl = client->engine;
  104. /* Make sure still active and slow-sync; active_slowsync is
  105. * set in a critical section; sync_cb is not. */
  106. if ((ectl->new_pos || control->sync_poll || control->sync_new) &&
  107. control->active_slowsync) {
  108. if (client->sync_cb (ectl->transport_state,
  109. &ectl->current_time,
  110. client->sync_arg)) {
  111. if (control->sync_poll) {
  112. control->sync_poll = 0;
  113. ectl->sync_remain--;
  114. }
  115. }
  116. control->sync_new = 0;
  117. }
  118. }
  119. void
  120. jack_call_timebase_master (jack_client_t *client)
  121. {
  122. jack_client_control_t *control = client->control;
  123. jack_control_t *ectl = client->engine;
  124. int new_pos = (int) ectl->pending_pos;
  125. /* Make sure this is still the master; is_timebase is set in a
  126. * critical section; timebase_cb is not. */
  127. if (control->is_timebase) {
  128. if (control->timebase_new) { /* first callback? */
  129. control->timebase_new = 0;
  130. new_pos = 1;
  131. }
  132. if ((ectl->transport_state == JackTransportRolling) ||
  133. new_pos) {
  134. client->timebase_cb (ectl->transport_state,
  135. control->nframes,
  136. &ectl->pending_time,
  137. new_pos,
  138. client->timebase_arg);
  139. }
  140. } else {
  141. /* another master took over, so resign */
  142. client->timebase_cb = NULL;
  143. client->timebase_arg = NULL;
  144. control->timebase_cb_cbset = FALSE;
  145. }
  146. }
  147. /************************* API functions *************************/
  148. jack_nframes_t
  149. jack_get_current_transport_frame (const jack_client_t *client)
  150. {
  151. jack_position_t position;
  152. float usecs;
  153. jack_nframes_t elapsed;
  154. jack_transport_state_t tstate;
  155. /* get the current transport position information.
  156. this is thread-safe and atomic with respect
  157. to the structure contents.
  158. */
  159. tstate = jack_transport_query (client, &position);
  160. if (tstate != JackTransportRolling) {
  161. return position.frame;
  162. }
  163. /* compute the elapsed usecs then audio frames since
  164. the transport info was last updated
  165. */
  166. usecs = jack_get_microseconds() - position.usecs;
  167. elapsed = (jack_nframes_t) floor ((((float) position.frame_rate)
  168. / 1000000.0f) * usecs);
  169. /* return the estimated transport frame position
  170. */
  171. return position.frame + elapsed;
  172. }
  173. jack_nframes_t
  174. jack_frames_since_cycle_start (const jack_client_t *client)
  175. {
  176. float usecs;
  177. jack_control_t *ectl = client->engine;
  178. usecs = jack_get_microseconds() - ectl->current_time.usecs;
  179. return (jack_nframes_t) floor ((((float) ectl->current_time.frame_rate)
  180. / 1000000.0f) * usecs);
  181. }
  182. jack_time_t
  183. jack_get_time()
  184. {
  185. return jack_get_microseconds();
  186. }
  187. jack_nframes_t
  188. jack_time_to_frames(const jack_client_t *client, jack_time_t now)
  189. {
  190. jack_frame_timer_t time;
  191. jack_control_t *ectl = client->engine;
  192. jack_read_frame_time (client, &time);
  193. if (time.initialized) {
  194. #if 0
  195. jack_info ("now = %Lu current wakeup = %Lu next = %Lu frames = %lu + %f => %lu FC = %f SOI = %f",
  196. now, time.current_wakeup, time.next_wakeup, time.frames,
  197. (double) (now - time.current_wakeup)/ (time.next_wakeup - time.current_wakeup),
  198. time.frames +
  199. (long) rint (((double) ((long long) now - time.current_wakeup)/
  200. (long long) (time.next_wakeup - time.current_wakeup)) * ectl->buffer_size),
  201. time.filter_coefficient,
  202. time.second_order_integrator);
  203. #endif
  204. return time.frames +
  205. (long) rint (((double) ((long long) (now - time.current_wakeup))/
  206. ((long long) (time.next_wakeup - time.current_wakeup))) * ectl->buffer_size);
  207. }
  208. return 0;
  209. }
  210. jack_nframes_t
  211. jack_frame_time (const jack_client_t *client)
  212. {
  213. jack_time_t now = jack_get_microseconds();
  214. return jack_time_to_frames(client, now);
  215. }
  216. jack_nframes_t
  217. jack_last_frame_time (const jack_client_t *client)
  218. {
  219. return client->engine->frame_timer.frames;
  220. }
  221. jack_time_t
  222. jack_frames_to_time(const jack_client_t *client, jack_nframes_t frames)
  223. {
  224. jack_frame_timer_t time;
  225. jack_control_t *ectl = client->engine;
  226. jack_read_frame_time (client, &time);
  227. if (time.initialized) {
  228. return time.current_wakeup +
  229. (long) rint (((double) ((long long) (frames - time.frames)) *
  230. ((long long) (time.next_wakeup - time.current_wakeup)) / ectl->buffer_size) );
  231. }
  232. return 0;
  233. }
  234. jack_nframes_t
  235. jack_get_sample_rate (jack_client_t *client)
  236. {
  237. return client->engine->current_time.frame_rate;
  238. }
  239. int
  240. jack_set_sample_rate_callback (jack_client_t *client,
  241. JackSampleRateCallback callback, void *arg)
  242. {
  243. if (client->control->active) {
  244. jack_error ("You cannot set callbacks on an active client.");
  245. return -1;
  246. }
  247. client->srate_arg = arg;
  248. client->srate = callback;
  249. client->control->srate_cbset = (callback != NULL);
  250. /* Now invoke it */
  251. callback (client->engine->current_time.frame_rate, arg);
  252. return 0;
  253. }
  254. int
  255. jack_release_timebase (jack_client_t *client)
  256. {
  257. int rc;
  258. jack_request_t req;
  259. jack_client_control_t *ctl = client->control;
  260. VALGRIND_MEMSET (&req, 0, sizeof (req));
  261. req.type = ResetTimeBaseClient;
  262. req.x.client_id = ctl->id;
  263. rc = jack_client_deliver_request (client, &req);
  264. if (rc == 0) {
  265. client->timebase_cb = NULL;
  266. client->timebase_arg = NULL;
  267. ctl->timebase_cb_cbset = 0;
  268. }
  269. return rc;
  270. }
  271. int
  272. jack_set_sync_callback (jack_client_t *client,
  273. JackSyncCallback sync_callback, void *arg)
  274. {
  275. jack_client_control_t *ctl = client->control;
  276. jack_request_t req;
  277. int rc;
  278. VALGRIND_MEMSET (&req, 0, sizeof (req));
  279. if (sync_callback)
  280. req.type = SetSyncClient;
  281. else
  282. req.type = ResetSyncClient;
  283. req.x.client_id = ctl->id;
  284. rc = jack_client_deliver_request (client, &req);
  285. if (rc == 0) {
  286. client->sync_cb = sync_callback;
  287. client->sync_arg = arg;
  288. ctl->sync_cb_cbset = TRUE;
  289. }
  290. return rc;
  291. }
  292. int
  293. jack_set_sync_timeout (jack_client_t *client, jack_time_t usecs)
  294. {
  295. jack_request_t req;
  296. VALGRIND_MEMSET (&req, 0, sizeof (req));
  297. req.type = SetSyncTimeout;
  298. req.x.timeout = usecs;
  299. return jack_client_deliver_request (client, &req);
  300. }
  301. int
  302. jack_set_timebase_callback (jack_client_t *client, int conditional,
  303. JackTimebaseCallback timebase_cb, void *arg)
  304. {
  305. int rc;
  306. jack_request_t req;
  307. jack_client_control_t *ctl = client->control;
  308. VALGRIND_MEMSET (&req, 0, sizeof (req));
  309. req.type = SetTimeBaseClient;
  310. req.x.timebase.client_id = ctl->id;
  311. req.x.timebase.conditional = conditional;
  312. rc = jack_client_deliver_request (client, &req);
  313. if (rc == 0) {
  314. client->timebase_arg = arg;
  315. client->timebase_cb = timebase_cb;
  316. ctl->timebase_cb_cbset = TRUE;
  317. }
  318. return rc;
  319. }
  320. int
  321. jack_transport_locate (jack_client_t *client, jack_nframes_t frame)
  322. {
  323. jack_position_t pos;
  324. pos.frame = frame;
  325. pos.valid = 0;
  326. return jack_transport_request_new_pos (client, &pos);
  327. }
  328. jack_transport_state_t
  329. jack_transport_query (const jack_client_t *client, jack_position_t *pos)
  330. {
  331. jack_control_t *ectl = client->engine;
  332. if (pos) {
  333. /* the guarded copy makes this function work in any
  334. * thread
  335. */
  336. jack_transport_copy_position (&ectl->current_time, pos);
  337. }
  338. return ectl->transport_state;
  339. }
  340. int
  341. jack_transport_reposition (jack_client_t *client, jack_position_t *pos)
  342. {
  343. /* copy the input, to avoid modifying its contents */
  344. jack_position_t tmp = *pos;
  345. /* validate input */
  346. if (tmp.valid & ~JACK_POSITION_MASK) /* unknown field present? */
  347. return EINVAL;
  348. return jack_transport_request_new_pos (client, &tmp);
  349. }
  350. void
  351. jack_transport_start (jack_client_t *client)
  352. {
  353. client->engine->transport_cmd = TransportCommandStart;
  354. }
  355. void
  356. jack_transport_stop (jack_client_t *client)
  357. {
  358. client->engine->transport_cmd = TransportCommandStop;
  359. }
  360. #ifdef OLD_TRANSPORT
  361. /************* Compatibility with old transport API. *************/
  362. int
  363. jack_engine_takeover_timebase (jack_client_t *client)
  364. {
  365. jack_error ("jack_engine_takeover_timebase() is no longer supported.");
  366. return ENOSYS;
  367. }
  368. void
  369. jack_get_transport_info (jack_client_t *client,
  370. jack_transport_info_t *info)
  371. {
  372. jack_control_t *ectl = client->engine;
  373. static int first_time = 1;
  374. if (first_time)
  375. jack_error ("jack_get_transport_info() is deprecated.");
  376. first_time = 0;
  377. /* check that this is the process thread */
  378. if (!pthread_equal(client->thread_id, pthread_self())) {
  379. jack_error("Invalid thread for jack_get_transport_info().");
  380. abort(); /* kill this client */
  381. }
  382. info->usecs = ectl->current_time.usecs;
  383. info->frame_rate = ectl->current_time.frame_rate;
  384. info->transport_state = ectl->transport_state;
  385. info->frame = ectl->current_time.frame;
  386. info->valid = (ectl->current_time.valid |
  387. JackTransportState | JackTransportPosition);
  388. if (info->valid & JackTransportBBT) {
  389. info->bar = ectl->current_time.bar;
  390. info->beat = ectl->current_time.beat;
  391. info->tick = ectl->current_time.tick;
  392. info->bar_start_tick = ectl->current_time.bar_start_tick;
  393. info->beats_per_bar = ectl->current_time.beats_per_bar;
  394. info->beat_type = ectl->current_time.beat_type;
  395. info->ticks_per_beat = ectl->current_time.ticks_per_beat;
  396. info->beats_per_minute = ectl->current_time.beats_per_minute;
  397. }
  398. }
  399. void
  400. jack_set_transport_info (jack_client_t *client,
  401. jack_transport_info_t *info)
  402. {
  403. static int first_time = 1;
  404. if (first_time)
  405. jack_error ("jack_set_transport_info() no longer supported.");
  406. first_time = 0;
  407. }
  408. #endif /* OLD_TRANSPORT */