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

429 lines
16KB

  1. /*
  2. Copyright (C) 2002 Paul Davis
  3. Copyright (C) 2003 Jack O'Quin
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. #ifndef __jack_transport_h__
  17. #define __jack_transport_h__
  18. #ifdef __cplusplus
  19. extern "C"
  20. {
  21. #endif
  22. #include <jack/types.h>
  23. /**
  24. * Transport states.
  25. */
  26. typedef enum {
  27. /* the order matters for binary compatibility */
  28. JackTransportStopped = 0, /**< Transport halted */
  29. JackTransportRolling = 1, /**< Transport playing */
  30. JackTransportLooping = 2, /**< For OLD_TRANSPORT, now ignored */
  31. JackTransportStarting = 3, /**< Waiting for sync ready */
  32. JackTransportSynching = 4 /**< temporary*/
  33. } jack_transport_state_t;
  34. typedef uint64_t jack_unique_t; /**< Unique ID (opaque) */
  35. /**
  36. * Optional struct jack_position_t fields.
  37. */
  38. typedef enum {
  39. JackPositionBBT = 0x10, /**< Bar, Beat, Tick */
  40. JackPositionTimecode = 0x20 /**< External timecode */
  41. } jack_position_bits_t;
  42. /** all valid position bits */
  43. #define JACK_POSITION_MASK (JackPositionBBT|JackPositionTimecode)
  44. #define EXTENDED_TIME_INFO
  45. /**
  46. * Struct for transport position information.
  47. */
  48. typedef struct {
  49. /* these four cannot be set from clients: the server sets them */
  50. jack_unique_t unique_1; /**< unique ID */
  51. jack_time_t usecs; /**< monotonic, free-rolling */
  52. jack_nframes_t frame_rate; /**< current frame rate (per second) */
  53. jack_nframes_t frame; /**< frame number, always present */
  54. jack_position_bits_t valid; /**< which other fields are valid */
  55. /* JackPositionBBT fields: */
  56. int32_t bar; /**< current bar */
  57. int32_t beat; /**< current beat-within-bar */
  58. int32_t tick; /**< current tick-within-beat */
  59. double bar_start_tick;
  60. float beats_per_bar; /**< time signature "numerator" */
  61. float beat_type; /**< time signature "denominator" */
  62. double ticks_per_beat;
  63. double beats_per_minute;
  64. /* JackPositionTimecode fields: (EXPERIMENTAL: could change) */
  65. double frame_time; /**< current time in seconds */
  66. double next_time; /**< next sequential frame_time
  67. (unless repositioned) */
  68. /* For binary compatibility, new fields should be allocated from
  69. * this padding area with new valid bits controlling access, so
  70. * the existing structure size and offsets are preserved. */
  71. int32_t padding[10];
  72. /* When (unique_1 == unique_2) the contents are consistent. */
  73. jack_unique_t unique_2; /**< unique ID */
  74. }
  75. jack_position_t;
  76. /**
  77. * Called by the timebase master to release itself from that
  78. * responsibility.
  79. *
  80. * If the timebase master releases the timebase or leaves the JACK
  81. * graph for any reason, the JACK engine takes over at the start of
  82. * the next process cycle. The transport state does not change. If
  83. * rolling, it continues to play, with frame numbers as the only
  84. * available position information.
  85. *
  86. * @see jack_set_timebase_callback
  87. *
  88. * @param client the JACK client structure.
  89. *
  90. * @return 0 on success, otherwise a non-zero error code.
  91. */
  92. int jack_release_timebase (jack_client_t *client);
  93. /**
  94. * Prototype for the @a sync_callback defined by slow-sync clients.
  95. * When the client is active, this callback is invoked just before
  96. * process() in the same thread. This occurs once after registration,
  97. * then subsequently whenever some client requests a new position, or
  98. * the transport enters the ::JackTransportStarting state. This
  99. * realtime function must not wait.
  100. *
  101. * The transport @a state will be:
  102. *
  103. * - ::JackTransportStopped when a new position is requested;
  104. * - ::JackTransportStarting when the transport is waiting to start;
  105. * - ::JackTransportRolling when the timeout has expired, and the
  106. * position is now a moving target.
  107. *
  108. * @param state current transport state.
  109. * @param pos new transport position.
  110. * @param arg the argument supplied by jack_set_sync_callback().
  111. *
  112. * @return TRUE (non-zero) when ready to roll.
  113. */
  114. typedef int (*JackSyncCallback)(jack_transport_state_t state,
  115. jack_position_t *pos,
  116. void *arg);
  117. /**
  118. * Register (or unregister) as a slow-sync client, one that cannot
  119. * respond immediately to transport position changes.
  120. *
  121. * The @a sync_callback will be invoked at the first available
  122. * opportunity after its registration is complete. If the client is
  123. * currently active this will be the following process cycle,
  124. * otherwise it will be the first cycle after calling jack_activate().
  125. * After that, it runs according to the ::JackSyncCallback rules.
  126. * Clients that don't set a @a sync_callback are assumed to be ready
  127. * immediately any time the transport wants to start.
  128. *
  129. * @param client the JACK client structure.
  130. * @param sync_callback is a realtime function that returns TRUE when
  131. * the client is ready. Setting @a sync_callback to NULL declares that
  132. * this client no longer requires slow-sync processing.
  133. * @param arg an argument for the @a sync_callback function.
  134. *
  135. * @return 0 on success, otherwise a non-zero error code.
  136. */
  137. int jack_set_sync_callback (jack_client_t *client,
  138. JackSyncCallback sync_callback,
  139. void *arg);
  140. /**
  141. * Set the timeout value for slow-sync clients.
  142. *
  143. * This timeout prevents unresponsive slow-sync clients from
  144. * completely halting the transport mechanism. The default is two
  145. * seconds. When the timeout expires, the transport starts rolling,
  146. * even if some slow-sync clients are still unready. The @a
  147. * sync_callbacks of these clients continue being invoked, giving them
  148. * a chance to catch up.
  149. *
  150. * @see jack_set_sync_callback
  151. *
  152. * @param client the JACK client structure.
  153. * @param timeout is delay (in microseconds) before the timeout expires.
  154. *
  155. * @return 0 on success, otherwise a non-zero error code.
  156. */
  157. int jack_set_sync_timeout (jack_client_t *client,
  158. jack_time_t timeout);
  159. /**
  160. * Prototype for the @a timebase_callback used to provide extended
  161. * position information. Its output affects all of the following
  162. * process cycle. This realtime function must not wait.
  163. *
  164. * This function is called immediately after process() in the same
  165. * thread whenever the transport is rolling, or when any client has
  166. * requested a new position in the previous cycle. The first cycle
  167. * after jack_set_timebase_callback() is also treated as a new
  168. * position, or the first cycle after jack_activate() if the client
  169. * had been inactive.
  170. *
  171. * The timebase master may not use its @a pos argument to set @a
  172. * pos->frame. To change position, use jack_transport_reposition() or
  173. * jack_transport_locate(). These functions are realtime-safe, the @a
  174. * timebase_callback can call them directly.
  175. *
  176. * @param state current transport state.
  177. * @param nframes number of frames in current period.
  178. * @param pos address of the position structure for the next cycle; @a
  179. * pos->frame will be its frame number. If @a new_pos is FALSE, this
  180. * structure contains extended position information from the current
  181. * cycle. If TRUE, it contains whatever was set by the requester.
  182. * The @a timebase_callback's task is to update the extended
  183. * information here.
  184. * @param new_pos TRUE (non-zero) for a newly requested @a pos, or for
  185. * the first cycle after the @a timebase_callback is defined.
  186. * @param arg the argument supplied by jack_set_timebase_callback().
  187. */
  188. typedef void (*JackTimebaseCallback)(jack_transport_state_t state,
  189. jack_nframes_t nframes,
  190. jack_position_t *pos,
  191. int new_pos,
  192. void *arg);
  193. /**
  194. * Register as timebase master for the JACK subsystem.
  195. *
  196. * The timebase master registers a callback that updates extended
  197. * position information such as beats or timecode whenever necessary.
  198. * Without this extended information, there is no need for this
  199. * function.
  200. *
  201. * There is never more than one master at a time. When a new client
  202. * takes over, the former @a timebase_callback is no longer called.
  203. * Taking over the timebase may be done conditionally, so it fails if
  204. * there was a master already.
  205. *
  206. * @param client the JACK client structure.
  207. * @param conditional non-zero for a conditional request.
  208. * @param timebase_callback is a realtime function that returns
  209. * position information.
  210. * @param arg an argument for the @a timebase_callback function.
  211. *
  212. * @return
  213. * - 0 on success;
  214. * - EBUSY if a conditional request fails because there was already a
  215. * timebase master;
  216. * - other non-zero error code.
  217. */
  218. int jack_set_timebase_callback (jack_client_t *client,
  219. int conditional,
  220. JackTimebaseCallback timebase_callback,
  221. void *arg);
  222. /**
  223. * Reposition the transport to a new frame number.
  224. *
  225. * May be called at any time by any client. The new position takes
  226. * effect in two process cycles. If there are slow-sync clients and
  227. * the transport is already rolling, it will enter the
  228. * ::JackTransportStarting state and begin invoking their @a
  229. * sync_callbacks until ready. This function is realtime-safe.
  230. *
  231. * @see jack_transport_reposition, jack_set_sync_callback
  232. *
  233. * @param client the JACK client structure.
  234. * @param frame frame number of new transport position.
  235. *
  236. * @return 0 if valid request, non-zero otherwise.
  237. */
  238. int jack_transport_locate (jack_client_t *client,
  239. jack_nframes_t frame);
  240. /**
  241. * Query the current transport state and position.
  242. *
  243. * This function is realtime-safe, and can be called from any thread.
  244. * If called from the process thread, @a pos corresponds to the first
  245. * frame of the current cycle and the state returned is valid for the
  246. * entire cycle.
  247. *
  248. * @param client the JACK client structure.
  249. * @param pos pointer to structure for returning current transport
  250. * position; @a pos->valid will show which fields contain valid data.
  251. * If @a pos is NULL, do not return position information.
  252. *
  253. * @return Current transport state.
  254. */
  255. jack_transport_state_t jack_transport_query (const jack_client_t *client,
  256. jack_position_t *pos);
  257. /**
  258. * Return an estimate of the current transport frame,
  259. * including any time elapsed since the last transport
  260. * positional update.
  261. *
  262. * @param client the JACK client structure
  263. */
  264. jack_nframes_t jack_get_current_transport_frame (const jack_client_t *client);
  265. /**
  266. * Request a new transport position.
  267. *
  268. * May be called at any time by any client. The new position takes
  269. * effect in two process cycles. If there are slow-sync clients and
  270. * the transport is already rolling, it will enter the
  271. * ::JackTransportStarting state and begin invoking their @a
  272. * sync_callbacks until ready. This function is realtime-safe.
  273. *
  274. * @see jack_transport_locate, jack_set_sync_callback
  275. *
  276. * @param client the JACK client structure.
  277. * @param pos requested new transport position.
  278. *
  279. * @return 0 if valid request, EINVAL if position structure rejected.
  280. */
  281. int jack_transport_reposition (jack_client_t *client,
  282. jack_position_t *pos);
  283. /**
  284. * Start the JACK transport rolling.
  285. *
  286. * Any client can make this request at any time. It takes effect no
  287. * sooner than the next process cycle, perhaps later if there are
  288. * slow-sync clients. This function is realtime-safe.
  289. *
  290. * @see jack_set_sync_callback
  291. *
  292. * @param client the JACK client structure.
  293. */
  294. void jack_transport_start (jack_client_t *client);
  295. /**
  296. * Stop the JACK transport.
  297. *
  298. * Any client can make this request at any time. It takes effect on
  299. * the next process cycle. This function is realtime-safe.
  300. *
  301. * @param client the JACK client structure.
  302. */
  303. void jack_transport_stop (jack_client_t *client);
  304. /*********************************************************************
  305. * The following interfaces are DEPRECATED. They are only provided
  306. * for compatibility with the earlier JACK transport implementation.
  307. *********************************************************************/
  308. /**
  309. * Optional struct jack_transport_info_t fields.
  310. *
  311. * @see jack_position_bits_t.
  312. */
  313. typedef enum {
  314. JackTransportState = 0x1, /**< Transport state */
  315. JackTransportPosition = 0x2, /**< Frame number */
  316. JackTransportLoop = 0x4, /**< Loop boundaries (ignored) */
  317. JackTransportSMPTE = 0x8, /**< SMPTE (ignored) */
  318. JackTransportBBT = 0x10 /**< Bar, Beat, Tick */
  319. } jack_transport_bits_t;
  320. /**
  321. * Deprecated struct for transport position information.
  322. *
  323. * @deprecated This is for compatibility with the earlier transport
  324. * interface. Use the jack_position_t struct, instead.
  325. */
  326. typedef struct {
  327. /* these two cannot be set from clients: the server sets them */
  328. jack_nframes_t frame_rate; /**< current frame rate (per second) */
  329. jack_time_t usecs; /**< monotonic, free-rolling */
  330. jack_transport_bits_t valid; /**< which fields are legal to read */
  331. jack_transport_state_t transport_state;
  332. jack_nframes_t frame;
  333. jack_nframes_t loop_start;
  334. jack_nframes_t loop_end;
  335. long smpte_offset; /**< SMPTE offset (from frame 0) */
  336. float smpte_frame_rate; /**< 29.97, 30, 24 etc. */
  337. int bar;
  338. int beat;
  339. int tick;
  340. double bar_start_tick;
  341. float beats_per_bar;
  342. float beat_type;
  343. double ticks_per_beat;
  344. double beats_per_minute;
  345. }
  346. jack_transport_info_t;
  347. /**
  348. * Gets the current transport info structure (deprecated).
  349. *
  350. * @param client the JACK client structure.
  351. * @param tinfo current transport info structure. The "valid" field
  352. * describes which fields contain valid data.
  353. *
  354. * @deprecated This is for compatibility with the earlier transport
  355. * interface. Use jack_transport_query(), instead.
  356. *
  357. * @pre Must be called from the process thread.
  358. */
  359. void jack_get_transport_info (jack_client_t *client,
  360. jack_transport_info_t *tinfo);
  361. /**
  362. * Set the transport info structure (deprecated).
  363. *
  364. * @deprecated This function still exists for compatibility with the
  365. * earlier transport interface, but it does nothing. Instead, define
  366. * a ::JackTimebaseCallback.
  367. */
  368. void jack_set_transport_info (jack_client_t *client,
  369. jack_transport_info_t *tinfo);
  370. #ifdef __cplusplus
  371. }
  372. #endif
  373. #endif /* __jack_transport_h__ */