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.

503 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 (control->sync_cb (ectl->transport_state,
  109. &ectl->current_time,
  110. control->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. control->timebase_cb (ectl->transport_state,
  135. control->nframes,
  136. &ectl->pending_time,
  137. new_pos,
  138. control->timebase_arg);
  139. }
  140. } else {
  141. /* another master took over, so resign */
  142. control->timebase_cb = NULL;
  143. control->timebase_arg = NULL;
  144. }
  145. }
  146. /************************* API functions *************************/
  147. jack_nframes_t
  148. jack_get_current_transport_frame (const jack_client_t *client)
  149. {
  150. jack_position_t position;
  151. float usecs;
  152. jack_nframes_t elapsed;
  153. jack_transport_state_t tstate;
  154. /* get the current transport position information.
  155. this is thread-safe and atomic with respect
  156. to the structure contents.
  157. */
  158. tstate = jack_transport_query (client, &position);
  159. if (tstate != JackTransportRolling) {
  160. return position.frame;
  161. }
  162. /* compute the elapsed usecs then audio frames since
  163. the transport info was last updated
  164. */
  165. usecs = jack_get_microseconds() - position.usecs;
  166. elapsed = (jack_nframes_t) floor ((((float) position.frame_rate)
  167. / 1000000.0f) * usecs);
  168. /* return the estimated transport frame position
  169. */
  170. return position.frame + elapsed;
  171. }
  172. jack_nframes_t
  173. jack_frames_since_cycle_start (const jack_client_t *client)
  174. {
  175. float usecs;
  176. jack_control_t *ectl = client->engine;
  177. usecs = jack_get_microseconds() - ectl->current_time.usecs;
  178. return (jack_nframes_t) floor ((((float) ectl->current_time.frame_rate)
  179. / 1000000.0f) * usecs);
  180. }
  181. jack_time_t
  182. jack_get_time()
  183. {
  184. return jack_get_microseconds();
  185. }
  186. jack_nframes_t
  187. jack_time_to_frames(const jack_client_t *client, jack_time_t now)
  188. {
  189. jack_frame_timer_t time;
  190. jack_control_t *ectl = client->engine;
  191. jack_read_frame_time (client, &time);
  192. if (time.initialized) {
  193. #if 0
  194. fprintf (stderr, "now = %Lu current wakeup = %Lu next = %Lu frames = %lu + %f => %lu\n",
  195. now, time.current_wakeup, time.next_wakeup, time.frames,
  196. (double) (now - time.current_wakeup)/
  197. (time.next_wakeup - time.current_wakeup),
  198. time.frames +
  199. (long) rint (((double) (now - time.current_wakeup)/
  200. (time.next_wakeup - time.current_wakeup)) * ectl->buffer_size));
  201. #endif
  202. return time.frames +
  203. (long) rint (((double) ((long long) (now - time.current_wakeup))/
  204. ((long long) (time.next_wakeup - time.current_wakeup))) * ectl->buffer_size);
  205. }
  206. return 0;
  207. }
  208. jack_nframes_t
  209. jack_frame_time (const jack_client_t *client)
  210. {
  211. jack_time_t now = jack_get_microseconds();
  212. return jack_time_to_frames(client, now);
  213. }
  214. jack_nframes_t
  215. jack_last_frame_time (const jack_client_t *client)
  216. {
  217. jack_frame_timer_t current;
  218. jack_read_frame_time (client, &current);
  219. return current.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->control->srate_arg = arg;
  248. client->control->srate = callback;
  249. /* Now invoke it */
  250. callback (client->engine->current_time.frame_rate, arg);
  251. return 0;
  252. }
  253. int
  254. jack_release_timebase (jack_client_t *client)
  255. {
  256. int rc;
  257. jack_request_t req;
  258. jack_client_control_t *ctl = client->control;
  259. req.type = ResetTimeBaseClient;
  260. req.x.client_id = ctl->id;
  261. rc = jack_client_deliver_request (client, &req);
  262. if (rc == 0) {
  263. ctl->timebase_cb = NULL;
  264. ctl->timebase_arg = NULL;
  265. }
  266. return rc;
  267. }
  268. int
  269. jack_set_sync_callback (jack_client_t *client,
  270. JackSyncCallback sync_callback, void *arg)
  271. {
  272. jack_client_control_t *ctl = client->control;
  273. jack_request_t req;
  274. int rc;
  275. if (sync_callback)
  276. req.type = SetSyncClient;
  277. else
  278. req.type = ResetSyncClient;
  279. req.x.client_id = ctl->id;
  280. rc = jack_client_deliver_request (client, &req);
  281. if (rc == 0) {
  282. ctl->sync_cb = sync_callback;
  283. ctl->sync_arg = arg;
  284. }
  285. return rc;
  286. }
  287. int
  288. jack_set_sync_timeout (jack_client_t *client, jack_time_t usecs)
  289. {
  290. jack_request_t req;
  291. req.type = SetSyncTimeout;
  292. req.x.timeout = usecs;
  293. return jack_client_deliver_request (client, &req);
  294. }
  295. int
  296. jack_set_timebase_callback (jack_client_t *client, int conditional,
  297. JackTimebaseCallback timebase_cb, void *arg)
  298. {
  299. int rc;
  300. jack_request_t req;
  301. jack_client_control_t *ctl = client->control;
  302. req.type = SetTimeBaseClient;
  303. req.x.timebase.client_id = ctl->id;
  304. req.x.timebase.conditional = conditional;
  305. rc = jack_client_deliver_request (client, &req);
  306. if (rc == 0) {
  307. ctl->timebase_arg = arg;
  308. ctl->timebase_cb = timebase_cb;
  309. }
  310. return rc;
  311. }
  312. int
  313. jack_transport_locate (jack_client_t *client, jack_nframes_t frame)
  314. {
  315. jack_position_t pos;
  316. pos.frame = frame;
  317. pos.valid = 0;
  318. return jack_transport_request_new_pos (client, &pos);
  319. }
  320. jack_transport_state_t
  321. jack_transport_query (const jack_client_t *client, jack_position_t *pos)
  322. {
  323. jack_control_t *ectl = client->engine;
  324. if (pos) {
  325. /* the guarded copy makes this function work in any
  326. * thread
  327. */
  328. jack_transport_copy_position (&ectl->current_time, pos);
  329. }
  330. return ectl->transport_state;
  331. }
  332. int
  333. jack_transport_reposition (jack_client_t *client, jack_position_t *pos)
  334. {
  335. /* copy the input, to avoid modifying its contents */
  336. jack_position_t tmp = *pos;
  337. /* validate input */
  338. if (tmp.valid & ~JACK_POSITION_MASK) /* unknown field present? */
  339. return EINVAL;
  340. return jack_transport_request_new_pos (client, &tmp);
  341. }
  342. void
  343. jack_transport_start (jack_client_t *client)
  344. {
  345. client->engine->transport_cmd = TransportCommandStart;
  346. }
  347. void
  348. jack_transport_stop (jack_client_t *client)
  349. {
  350. client->engine->transport_cmd = TransportCommandStop;
  351. }
  352. #ifdef OLD_TRANSPORT
  353. /************* Compatibility with old transport API. *************/
  354. int
  355. jack_engine_takeover_timebase (jack_client_t *client)
  356. {
  357. jack_error ("jack_engine_takeover_timebase() is no longer supported.");
  358. return ENOSYS;
  359. }
  360. void
  361. jack_get_transport_info (jack_client_t *client,
  362. jack_transport_info_t *info)
  363. {
  364. jack_control_t *ectl = client->engine;
  365. static int first_time = 1;
  366. if (first_time)
  367. jack_error ("jack_get_transport_info() is deprecated.");
  368. first_time = 0;
  369. /* check that this is the process thread */
  370. if (!pthread_equal(client->thread_id, pthread_self())) {
  371. jack_error("Invalid thread for jack_get_transport_info().");
  372. abort(); /* kill this client */
  373. }
  374. info->usecs = ectl->current_time.usecs;
  375. info->frame_rate = ectl->current_time.frame_rate;
  376. info->transport_state = ectl->transport_state;
  377. info->frame = ectl->current_time.frame;
  378. info->valid = (ectl->current_time.valid |
  379. JackTransportState | JackTransportPosition);
  380. if (info->valid & JackTransportBBT) {
  381. info->bar = ectl->current_time.bar;
  382. info->beat = ectl->current_time.beat;
  383. info->tick = ectl->current_time.tick;
  384. info->bar_start_tick = ectl->current_time.bar_start_tick;
  385. info->beats_per_bar = ectl->current_time.beats_per_bar;
  386. info->beat_type = ectl->current_time.beat_type;
  387. info->ticks_per_beat = ectl->current_time.ticks_per_beat;
  388. info->beats_per_minute = ectl->current_time.beats_per_minute;
  389. }
  390. }
  391. void
  392. jack_set_transport_info (jack_client_t *client,
  393. jack_transport_info_t *info)
  394. {
  395. static int first_time = 1;
  396. if (first_time)
  397. jack_error ("jack_set_transport_info() no longer supported.");
  398. first_time = 0;
  399. }
  400. #endif /* OLD_TRANSPORT */