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.

311 lines
7.6KB

  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 "util/Thread.H"
  26. Engine::Engine ( ) : _thread( "RT" )
  27. {
  28. _freewheeling = false;
  29. _zombified = false;
  30. _client = NULL;
  31. _buffers_dropped = 0;
  32. _xruns = 0;
  33. }
  34. /*******************/
  35. /* Static Wrappers */
  36. /*******************/
  37. int
  38. Engine::process ( nframes_t nframes, void *arg )
  39. {
  40. return ((Engine*)arg)->process( nframes );
  41. }
  42. int
  43. Engine::sync ( jack_transport_state_t state, jack_position_t *pos, void *arg )
  44. {
  45. return ((Engine*)arg)->sync( state, pos );
  46. }
  47. int
  48. Engine::xrun ( void *arg )
  49. {
  50. return ((Engine*)arg)->xrun();
  51. }
  52. void
  53. Engine::timebase ( jack_transport_state_t state, jack_nframes_t nframes, jack_position_t *pos, int new_pos, void *arg )
  54. {
  55. ((Engine*)arg)->timebase( state, nframes, pos, new_pos );
  56. }
  57. void
  58. Engine::freewheel ( int starting, void *arg )
  59. {
  60. ((Engine*)arg)->freewheel( starting );
  61. }
  62. int
  63. Engine::buffer_size ( nframes_t nframes, void *arg )
  64. {
  65. return ((Engine*)arg)->buffer_size( nframes );
  66. }
  67. void
  68. Engine::thread_init ( void *arg )
  69. {
  70. ((Engine*)arg)->thread_init();
  71. }
  72. void
  73. Engine::shutdown ( void *arg )
  74. {
  75. ((Engine*)arg)->shutdown();
  76. }
  77. /*************/
  78. /* Callbacks */
  79. /*************/
  80. /* THREAD: RT */
  81. /** This is the jack xrun callback */
  82. int
  83. Engine::xrun ( void )
  84. {
  85. ++_xruns;
  86. return 0;
  87. }
  88. /* THREAD: RT */
  89. void
  90. Engine::freewheel ( bool starting )
  91. {
  92. _freewheeling = starting;
  93. if ( starting )
  94. DMESSAGE( "entering freewheeling mode" );
  95. else
  96. DMESSAGE( "leaving freewheeling mode" );
  97. }
  98. /* THREAD: RT (non-RT) */
  99. int
  100. Engine::buffer_size ( nframes_t nframes )
  101. {
  102. timeline->resize_buffers( nframes );
  103. return 0;
  104. }
  105. /* THREAD: RT */
  106. /** This is the jack slow-sync callback. */
  107. int
  108. Engine::sync ( jack_transport_state_t state, jack_position_t *pos )
  109. {
  110. static bool seeking = false;
  111. switch ( state )
  112. {
  113. case JackTransportStopped: /* new position requested */
  114. /* JACK docs lie. This is only called when the transport
  115. is *really* stopped, not when starting a slow-sync
  116. cycle */
  117. transport->frame = pos->frame;
  118. return 1;
  119. case JackTransportStarting: /* this means JACK is polling slow-sync clients */
  120. {
  121. if ( ! seeking )
  122. {
  123. request_locate( pos->frame );
  124. seeking = true;
  125. }
  126. int r = true;
  127. if ( timeline )
  128. r = timeline->seek_pending();
  129. if ( ! r )
  130. seeking = false;
  131. return ! seeking;
  132. }
  133. case JackTransportRolling: /* JACK's timeout has expired */
  134. /* FIXME: what's the right thing to do here? */
  135. // request_locate( pos->frame );
  136. return 1;
  137. // return transport->frame == pos->frame;
  138. break;
  139. default:
  140. printf( "unknown transport state.\n" );
  141. }
  142. return 0;
  143. }
  144. /* THREAD: RT */
  145. void
  146. Engine::timebase ( jack_transport_state_t, jack_nframes_t, jack_position_t *pos, int )
  147. {
  148. position_info pi = timeline->solve_tempomap( pos->frame );
  149. pos->valid = JackPositionBBT;
  150. pos->beats_per_bar = pi.beats_per_bar;
  151. pos->beat_type = pi.beat_type;
  152. pos->beats_per_minute = pi.tempo;
  153. pos->bar = pi.bbt.bar + 1;
  154. pos->beat = pi.bbt.beat + 1;
  155. pos->tick = pi.bbt.tick;
  156. pos->ticks_per_beat = 1920.0; /* FIXME: wrong place for this */
  157. /* FIXME: fill this in */
  158. pos->bar_start_tick = 0;
  159. }
  160. /* THREAD: RT */
  161. int
  162. Engine::process ( nframes_t nframes )
  163. {
  164. /* FIXME: wrong place for this */
  165. _thread.set( "RT" );
  166. transport->poll();
  167. if ( freewheeling() )
  168. {
  169. /* freewheeling mode/export. We're actually running
  170. non-RT. Assume that everything is quiescent, locking is
  171. unecessary and do I/O synchronously */
  172. if ( timeline )
  173. timeline->process( nframes );
  174. /* because we're going faster than realtime. */
  175. timeline->wait_for_buffers();
  176. }
  177. else
  178. {
  179. if ( ! trylock() )
  180. {
  181. /* the data structures we need to access here (tracks and
  182. * their ports, but not track contents) may be in an
  183. * inconsistent state at the moment. Just punt and drop this
  184. * buffer. */
  185. ++_buffers_dropped;
  186. return 0;
  187. }
  188. /* handle chicken/egg problem */
  189. if ( timeline )
  190. /* this will initiate the process() call graph for the various
  191. * number and types of tracks, which will in turn send data out
  192. * the appropriate ports. */
  193. timeline->process( nframes );
  194. unlock();
  195. }
  196. return 0;
  197. }
  198. /* THREAD: RT */
  199. /** enter or leave freehweeling mode */
  200. void
  201. Engine::freewheeling ( bool yes )
  202. {
  203. if ( jack_set_freewheel( _client, yes ) )
  204. WARNING( "Unkown error while setting freewheeling mode" );
  205. }
  206. /* TRHEAD: RT */
  207. void
  208. Engine::thread_init ( void )
  209. {
  210. _thread.set( "RT" );
  211. }
  212. /* THREAD: RT */
  213. void
  214. Engine::shutdown ( void )
  215. {
  216. _zombified = true;
  217. }
  218. /** Connect to JACK */
  219. int
  220. Engine::init ( void )
  221. {
  222. if (( _client = jack_client_open ( APP_NAME, (jack_options_t)0, NULL )) == 0 )
  223. return 0;
  224. #define set_callback( name ) jack_set_ ## name ## _callback( _client, &Engine:: name , this )
  225. set_callback( thread_init );
  226. set_callback( process );
  227. set_callback( xrun );
  228. set_callback( freewheel );
  229. set_callback( buffer_size );
  230. /* FIXME: should we wait to register this until after the project
  231. has been loaded (and we have disk threads running)? */
  232. set_callback( sync );
  233. jack_set_timebase_callback( _client, 0, &Engine::timebase, this );
  234. jack_on_shutdown( _client, &Engine::shutdown, this );
  235. jack_activate( _client );
  236. _sample_rate = frame_rate();
  237. timeline->_sample_rate = frame_rate();
  238. /* we don't need to create any ports until tracks are created */
  239. return 1;
  240. }
  241. void
  242. Engine::request_locate ( nframes_t frame )
  243. {
  244. if ( timeline )
  245. timeline->seek( frame );
  246. }