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.

451 lines
10KB

  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. do {
  38. /* throttle the busy wait if we don't get
  39. the answer very quickly.
  40. */
  41. if (tries > 10) {
  42. usleep (20);
  43. tries = 0;
  44. }
  45. *copy = client->engine->frame_timer;
  46. tries++;
  47. } while (copy->guard1 != copy->guard2);
  48. }
  49. jack_nframes_t
  50. jack_last_frame_time (const jack_client_t *client)
  51. {
  52. jack_frame_timer_t current;
  53. jack_read_frame_time (client, &current);
  54. return current.frames;
  55. }
  56. /* copy a JACK transport position structure (thread-safe) */
  57. void
  58. jack_transport_copy_position (jack_position_t *from, jack_position_t *to)
  59. {
  60. int tries = 0;
  61. long timeout = 1000;
  62. do {
  63. /* throttle the busy wait if we don't get the answer
  64. * very quickly. */
  65. if (tries > 10) {
  66. usleep (20);
  67. tries = 0;
  68. /* debug code to avoid system hangs... */
  69. if (--timeout == 0) {
  70. jack_error("hung in loop copying position");
  71. abort();
  72. }
  73. }
  74. *to = *from;
  75. tries++;
  76. } while (to->unique_1 != to->unique_2);
  77. }
  78. static inline int
  79. jack_transport_request_new_pos (jack_client_t *client, jack_position_t *pos)
  80. {
  81. jack_control_t *ectl = client->engine;
  82. /* distinguish this request from all others */
  83. pos->unique_1 = pos->unique_2 = jack_generate_unique_id(ectl);
  84. /* clients may not set these fields */
  85. pos->usecs = ectl->current_time.usecs;
  86. pos->frame_rate = ectl->current_time.frame_rate;
  87. /* carefully copy requested postion into shared memory */
  88. jack_transport_copy_position (pos, &ectl->request_time);
  89. return 0;
  90. }
  91. /******************** Callback invocations ********************/
  92. void
  93. jack_call_sync_client (jack_client_t *client)
  94. {
  95. jack_client_control_t *control = client->control;
  96. jack_control_t *ectl = client->engine;
  97. /* Make sure still active and slow-sync; active_slowsync is
  98. * set in a critical section; sync_cb is not. */
  99. if ((ectl->new_pos || control->sync_poll || control->sync_new) &&
  100. control->active_slowsync) {
  101. if (control->sync_cb (ectl->transport_state,
  102. &ectl->current_time,
  103. control->sync_arg)) {
  104. if (control->sync_poll) {
  105. control->sync_poll = 0;
  106. ectl->sync_remain--;
  107. }
  108. }
  109. control->sync_new = 0;
  110. }
  111. }
  112. void
  113. jack_call_timebase_master (jack_client_t *client)
  114. {
  115. jack_client_control_t *control = client->control;
  116. jack_control_t *ectl = client->engine;
  117. int new_pos = (int) ectl->pending_pos;
  118. /* Make sure this is still the master; is_timebase is set in a
  119. * critical section; timebase_cb is not. */
  120. if (control->is_timebase) {
  121. if (control->timebase_new) { /* first callback? */
  122. control->timebase_new = 0;
  123. new_pos = 1;
  124. }
  125. if ((ectl->transport_state == JackTransportRolling) ||
  126. new_pos) {
  127. control->timebase_cb (ectl->transport_state,
  128. control->nframes,
  129. &ectl->pending_time,
  130. new_pos,
  131. control->timebase_arg);
  132. }
  133. } else {
  134. /* another master took over, so resign */
  135. control->timebase_cb = NULL;
  136. control->timebase_arg = NULL;
  137. }
  138. }
  139. /************************* API functions *************************/
  140. jack_nframes_t
  141. jack_get_current_transport_frame (const jack_client_t *client)
  142. {
  143. jack_position_t position;
  144. float usecs;
  145. jack_nframes_t elapsed;
  146. jack_transport_state_t tstate;
  147. /* get the current transport position information.
  148. this is thread-safe and atomic with respect
  149. to the structure contents.
  150. */
  151. tstate = jack_transport_query (client, &position);
  152. if (tstate != JackTransportRolling) {
  153. return position.frame;
  154. }
  155. /* compute the elapsed usecs then audio frames since
  156. the transport info was last updated
  157. */
  158. usecs = jack_get_microseconds() - position.usecs;
  159. elapsed = (jack_nframes_t) floor ((((float) position.frame_rate)
  160. / 1000000.0f) * usecs);
  161. /* return the estimated transport frame position
  162. */
  163. return position.frame + elapsed;
  164. }
  165. jack_nframes_t
  166. jack_frames_since_cycle_start (const jack_client_t *client)
  167. {
  168. float usecs;
  169. jack_control_t *ectl = client->engine;
  170. usecs = jack_get_microseconds() - ectl->current_time.usecs;
  171. return (jack_nframes_t) floor ((((float) ectl->current_time.frame_rate)
  172. / 1000000.0f) * usecs);
  173. }
  174. jack_nframes_t
  175. jack_frame_time (const jack_client_t *client)
  176. {
  177. jack_frame_timer_t current;
  178. float usecs;
  179. jack_nframes_t elapsed;
  180. jack_control_t *ectl = client->engine;
  181. jack_read_frame_time (client, &current);
  182. usecs = jack_get_microseconds() - current.stamp;
  183. elapsed = (jack_nframes_t)
  184. floor ((((float) ectl->current_time.frame_rate)
  185. / 1000000.0f) * usecs);
  186. return current.frames + elapsed;
  187. }
  188. jack_nframes_t
  189. jack_get_sample_rate (jack_client_t *client)
  190. {
  191. return client->engine->current_time.frame_rate;
  192. }
  193. int
  194. jack_set_sample_rate_callback (jack_client_t *client,
  195. JackSampleRateCallback callback, void *arg)
  196. {
  197. if (client->control->active) {
  198. jack_error ("You cannot set callbacks on an active client.");
  199. return -1;
  200. }
  201. client->control->srate_arg = arg;
  202. client->control->srate = callback;
  203. /* Now invoke it */
  204. callback (client->engine->current_time.frame_rate, arg);
  205. return 0;
  206. }
  207. int
  208. jack_release_timebase (jack_client_t *client)
  209. {
  210. int rc;
  211. jack_request_t req;
  212. jack_client_control_t *ctl = client->control;
  213. req.type = ResetTimeBaseClient;
  214. req.x.client_id = ctl->id;
  215. rc = jack_client_deliver_request (client, &req);
  216. if (rc == 0) {
  217. ctl->timebase_cb = NULL;
  218. ctl->timebase_arg = NULL;
  219. }
  220. return rc;
  221. }
  222. int
  223. jack_set_sync_callback (jack_client_t *client,
  224. JackSyncCallback sync_callback, void *arg)
  225. {
  226. jack_client_control_t *ctl = client->control;
  227. jack_request_t req;
  228. int rc;
  229. if (sync_callback)
  230. req.type = SetSyncClient;
  231. else
  232. req.type = ResetSyncClient;
  233. req.x.client_id = ctl->id;
  234. rc = jack_client_deliver_request (client, &req);
  235. if (rc == 0) {
  236. ctl->sync_cb = sync_callback;
  237. ctl->sync_arg = arg;
  238. }
  239. return rc;
  240. }
  241. int
  242. jack_set_sync_timeout (jack_client_t *client, jack_time_t usecs)
  243. {
  244. jack_request_t req;
  245. req.type = SetSyncTimeout;
  246. req.x.timeout = usecs;
  247. return jack_client_deliver_request (client, &req);
  248. }
  249. int
  250. jack_set_timebase_callback (jack_client_t *client, int conditional,
  251. JackTimebaseCallback timebase_cb, void *arg)
  252. {
  253. int rc;
  254. jack_request_t req;
  255. jack_client_control_t *ctl = client->control;
  256. req.type = SetTimeBaseClient;
  257. req.x.timebase.client_id = ctl->id;
  258. req.x.timebase.conditional = conditional;
  259. rc = jack_client_deliver_request (client, &req);
  260. if (rc == 0) {
  261. ctl->timebase_arg = arg;
  262. ctl->timebase_cb = timebase_cb;
  263. }
  264. return rc;
  265. }
  266. int
  267. jack_transport_locate (jack_client_t *client, jack_nframes_t frame)
  268. {
  269. jack_position_t pos;
  270. pos.frame = frame;
  271. pos.valid = 0;
  272. return jack_transport_request_new_pos (client, &pos);
  273. }
  274. jack_transport_state_t
  275. jack_transport_query (const jack_client_t *client, jack_position_t *pos)
  276. {
  277. jack_control_t *ectl = client->engine;
  278. if (pos) {
  279. /* the guarded copy makes this function work in any
  280. * thread
  281. */
  282. jack_transport_copy_position (&ectl->current_time, pos);
  283. }
  284. return ectl->transport_state;
  285. }
  286. int
  287. jack_transport_reposition (jack_client_t *client, jack_position_t *pos)
  288. {
  289. /* copy the input, to avoid modifying its contents */
  290. jack_position_t tmp = *pos;
  291. /* validate input */
  292. if (tmp.valid & ~JACK_POSITION_MASK) /* unknown field present? */
  293. return EINVAL;
  294. return jack_transport_request_new_pos (client, &tmp);
  295. }
  296. void
  297. jack_transport_start (jack_client_t *client)
  298. {
  299. client->engine->transport_cmd = TransportCommandStart;
  300. }
  301. void
  302. jack_transport_stop (jack_client_t *client)
  303. {
  304. client->engine->transport_cmd = TransportCommandStop;
  305. }
  306. #ifdef OLD_TRANSPORT
  307. /************* Compatibility with old transport API. *************/
  308. int
  309. jack_engine_takeover_timebase (jack_client_t *client)
  310. {
  311. jack_error ("jack_engine_takeover_timebase() is no longer supported.");
  312. return ENOSYS;
  313. }
  314. void
  315. jack_get_transport_info (jack_client_t *client,
  316. jack_transport_info_t *info)
  317. {
  318. jack_control_t *ectl = client->engine;
  319. static int first_time = 1;
  320. if (first_time)
  321. jack_error ("jack_get_transport_info() is deprecated.");
  322. first_time = 0;
  323. /* check that this is the process thread */
  324. if (!pthread_equal(client->thread_id, pthread_self())) {
  325. jack_error("Invalid thread for jack_get_transport_info().");
  326. abort(); /* kill this client */
  327. }
  328. info->usecs = ectl->current_time.usecs;
  329. info->frame_rate = ectl->current_time.frame_rate;
  330. info->transport_state = ectl->transport_state;
  331. info->frame = ectl->current_time.frame;
  332. info->valid = (ectl->current_time.valid |
  333. JackTransportState | JackTransportPosition);
  334. if (info->valid & JackTransportBBT) {
  335. info->bar = ectl->current_time.bar;
  336. info->beat = ectl->current_time.beat;
  337. info->tick = ectl->current_time.tick;
  338. info->bar_start_tick = ectl->current_time.bar_start_tick;
  339. info->beats_per_bar = ectl->current_time.beats_per_bar;
  340. info->beat_type = ectl->current_time.beat_type;
  341. info->ticks_per_beat = ectl->current_time.ticks_per_beat;
  342. info->beats_per_minute = ectl->current_time.beats_per_minute;
  343. }
  344. }
  345. void
  346. jack_set_transport_info (jack_client_t *client,
  347. jack_transport_info_t *info)
  348. {
  349. static int first_time = 1;
  350. if (first_time)
  351. jack_error ("jack_set_transport_info() no longer supported.");
  352. first_time = 0;
  353. }
  354. #endif /* OLD_TRANSPORT */