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.

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