Assists music production by grouping standalone programs into sessions. Community version of "Non Session Manager".
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.

321 lines
7.9KB

  1. /*******************************************************************************/
  2. /* Copyright (C) 2008 Jonathan Moore Liles */
  3. /* */
  4. /* This program is free software; you can redistribute it and/or modify it */
  5. /* under the terms of the GNU General Public License as published by the */
  6. /* Free Software Foundation; either version 2 of the License, or (at your */
  7. /* option) any later version. */
  8. /* */
  9. /* This program is distributed in the hope that it will be useful, but WITHOUT */
  10. /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
  11. /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for */
  12. /* more details. */
  13. /* */
  14. /* You should have received a copy of the GNU General Public License along */
  15. /* with This program; see the file COPYING. If not,write to the Free Software */
  16. /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  17. /*******************************************************************************/
  18. #include "Engine.H"
  19. #include "../Transport.H"
  20. #include "../Timeline.H" // for process()
  21. #include "../Sequence_Widget.H" // for BBT and position info.
  22. #define APP_NAME "Non-DAW" // FIXME: wrong place for this!
  23. /* This is the home of the JACK process callback (does this *really*
  24. need to be a class?) */
  25. #include "const.h"
  26. #include "util/debug.h"
  27. #include "util/Thread.H"
  28. Engine::Engine ( ) : _thread( "RT" )
  29. {
  30. _freewheeling = false;
  31. _zombified = false;
  32. _client = NULL;
  33. _buffers_dropped = 0;
  34. _xruns = 0;
  35. }
  36. Engine::~Engine ( )
  37. {
  38. jack_deactivate( _client );
  39. jack_client_close( _client );
  40. }
  41. /*******************/
  42. /* Static Wrappers */
  43. /*******************/
  44. int
  45. Engine::process ( nframes_t nframes, void *arg )
  46. {
  47. return ((Engine*)arg)->process( nframes );
  48. }
  49. int
  50. Engine::sync ( jack_transport_state_t state, jack_position_t *pos, void *arg )
  51. {
  52. return ((Engine*)arg)->sync( state, pos );
  53. }
  54. int
  55. Engine::xrun ( void *arg )
  56. {
  57. return ((Engine*)arg)->xrun();
  58. }
  59. void
  60. Engine::timebase ( jack_transport_state_t state, jack_nframes_t nframes, jack_position_t *pos, int new_pos, void *arg )
  61. {
  62. ((Engine*)arg)->timebase( state, nframes, pos, new_pos );
  63. }
  64. void
  65. Engine::freewheel ( int starting, void *arg )
  66. {
  67. ((Engine*)arg)->freewheel( starting );
  68. }
  69. int
  70. Engine::buffer_size ( nframes_t nframes, void *arg )
  71. {
  72. return ((Engine*)arg)->buffer_size( nframes );
  73. }
  74. void
  75. Engine::thread_init ( void *arg )
  76. {
  77. ((Engine*)arg)->thread_init();
  78. }
  79. void
  80. Engine::shutdown ( void *arg )
  81. {
  82. ((Engine*)arg)->shutdown();
  83. }
  84. /*************/
  85. /* Callbacks */
  86. /*************/
  87. /* THREAD: RT */
  88. /** This is the jack xrun callback */
  89. int
  90. Engine::xrun ( void )
  91. {
  92. ++_xruns;
  93. return 0;
  94. }
  95. /* THREAD: RT */
  96. void
  97. Engine::freewheel ( bool starting )
  98. {
  99. _freewheeling = starting;
  100. if ( starting )
  101. DMESSAGE( "entering freewheeling mode" );
  102. else
  103. DMESSAGE( "leaving freewheeling mode" );
  104. }
  105. /* THREAD: RT (non-RT) */
  106. int
  107. Engine::buffer_size ( nframes_t nframes )
  108. {
  109. timeline->resize_buffers( nframes );
  110. return 0;
  111. }
  112. /* THREAD: RT */
  113. /** This is the jack slow-sync callback. */
  114. int
  115. Engine::sync ( jack_transport_state_t state, jack_position_t *pos )
  116. {
  117. static bool seeking = false;
  118. switch ( state )
  119. {
  120. case JackTransportStopped: /* new position requested */
  121. /* JACK docs lie. This is only called when the transport
  122. is *really* stopped, not when starting a slow-sync
  123. cycle */
  124. transport->frame = pos->frame;
  125. return 1;
  126. case JackTransportStarting: /* this means JACK is polling slow-sync clients */
  127. {
  128. if ( ! seeking )
  129. {
  130. request_locate( pos->frame );
  131. seeking = true;
  132. }
  133. int r = true;
  134. if ( timeline )
  135. r = timeline->seek_pending();
  136. if ( ! r )
  137. seeking = false;
  138. return ! seeking;
  139. }
  140. case JackTransportRolling: /* JACK's timeout has expired */
  141. /* FIXME: what's the right thing to do here? */
  142. // request_locate( pos->frame );
  143. return 1;
  144. // return transport->frame == pos->frame;
  145. break;
  146. default:
  147. printf( "unknown transport state.\n" );
  148. }
  149. return 0;
  150. }
  151. /* THREAD: RT */
  152. void
  153. Engine::timebase ( jack_transport_state_t, jack_nframes_t, jack_position_t *pos, int )
  154. {
  155. position_info pi = timeline->solve_tempomap( pos->frame );
  156. pos->valid = JackPositionBBT;
  157. pos->beats_per_bar = pi.beats_per_bar;
  158. pos->beat_type = pi.beat_type;
  159. pos->beats_per_minute = pi.tempo;
  160. pos->bar = pi.bbt.bar + 1;
  161. pos->beat = pi.bbt.beat + 1;
  162. pos->tick = pi.bbt.tick;
  163. pos->ticks_per_beat = 1920.0; /* FIXME: wrong place for this */
  164. /* FIXME: fill this in */
  165. pos->bar_start_tick = 0;
  166. }
  167. /* THREAD: RT */
  168. int
  169. Engine::process ( nframes_t nframes )
  170. {
  171. /* FIXME: wrong place for this */
  172. _thread.set( "RT" );
  173. transport->poll();
  174. if ( freewheeling() )
  175. {
  176. /* freewheeling mode/export. We're actually running
  177. non-RT. Assume that everything is quiescent, locking is
  178. unecessary and do I/O synchronously */
  179. if ( timeline )
  180. timeline->process( nframes );
  181. /* because we're going faster than realtime. */
  182. timeline->wait_for_buffers();
  183. }
  184. else
  185. {
  186. if ( ! trylock() )
  187. {
  188. /* the data structures we need to access here (tracks and
  189. * their ports, but not track contents) may be in an
  190. * inconsistent state at the moment. Just punt and drop this
  191. * buffer. */
  192. ++_buffers_dropped;
  193. return 0;
  194. }
  195. /* handle chicken/egg problem */
  196. if ( timeline )
  197. /* this will initiate the process() call graph for the various
  198. * number and types of tracks, which will in turn send data out
  199. * the appropriate ports. */
  200. timeline->process( nframes );
  201. unlock();
  202. }
  203. return 0;
  204. }
  205. /* THREAD: RT */
  206. /** enter or leave freehweeling mode */
  207. void
  208. Engine::freewheeling ( bool yes )
  209. {
  210. if ( jack_set_freewheel( _client, yes ) )
  211. WARNING( "Unkown error while setting freewheeling mode" );
  212. }
  213. /* TRHEAD: RT */
  214. void
  215. Engine::thread_init ( void )
  216. {
  217. _thread.set( "RT" );
  218. }
  219. /* THREAD: RT */
  220. void
  221. Engine::shutdown ( void )
  222. {
  223. _zombified = true;
  224. }
  225. /** Connect to JACK */
  226. const char *
  227. Engine::init ( void )
  228. {
  229. if (( _client = jack_client_open ( APP_NAME, (jack_options_t)0, NULL )) == 0 )
  230. return NULL;
  231. #define set_callback( name ) jack_set_ ## name ## _callback( _client, &Engine:: name , this )
  232. set_callback( thread_init );
  233. set_callback( process );
  234. set_callback( xrun );
  235. set_callback( freewheel );
  236. set_callback( buffer_size );
  237. /* FIXME: should we wait to register this until after the project
  238. has been loaded (and we have disk threads running)? */
  239. set_callback( sync );
  240. jack_set_timebase_callback( _client, 0, &Engine::timebase, this );
  241. jack_on_shutdown( _client, &Engine::shutdown, this );
  242. jack_activate( _client );
  243. _sample_rate = frame_rate();
  244. MESSAGE( "Jack sample rate is %lu", (unsigned long)_sample_rate );
  245. timeline->_sample_rate = frame_rate();
  246. /* we don't need to create any ports until tracks are created */
  247. return jack_get_client_name( _client );
  248. }
  249. void
  250. Engine::request_locate ( nframes_t frame )
  251. {
  252. if ( timeline )
  253. timeline->seek( frame );
  254. }