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.

438 lines
11KB

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