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.

303 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::request_locate ( nframes_t frame )
  69. {
  70. if ( timeline )
  71. timeline->seek( frame );
  72. }
  73. /* THREAD: RT */
  74. /** This is the jack xrun callback */
  75. int
  76. Engine::xrun ( void )
  77. {
  78. ++_xruns;
  79. return 0;
  80. }
  81. /* THREAD: RT */
  82. void
  83. Engine::freewheel ( bool starting )
  84. {
  85. _freewheeling = starting;
  86. if ( starting )
  87. DMESSAGE( "entering freewheeling mode" );
  88. else
  89. DMESSAGE( "leaving freewheeling mode" );
  90. }
  91. /* THREAD: RT (non-RT) */
  92. int
  93. Engine::buffer_size ( nframes_t nframes )
  94. {
  95. /* TODO: inform all disktreams the the buffer size has changed */
  96. timeline->resize_buffers( nframes );
  97. return 0;
  98. }
  99. /* THREAD: RT */
  100. /** This is the jack slow-sync callback. */
  101. int
  102. Engine::sync ( jack_transport_state_t state, jack_position_t *pos )
  103. {
  104. static bool seeking = false;
  105. switch ( state )
  106. {
  107. case JackTransportStopped: /* new position requested */
  108. /* JACK docs lie. This is only called when the transport
  109. is *really* stopped, not when starting a slow-sync
  110. cycle */
  111. transport->frame = pos->frame;
  112. return 1;
  113. case JackTransportStarting: /* this means JACK is polling slow-sync clients */
  114. {
  115. if ( ! seeking )
  116. {
  117. request_locate( pos->frame );
  118. seeking = true;
  119. }
  120. int r = true;
  121. if ( timeline )
  122. r = timeline->seek_pending();
  123. if ( ! r )
  124. seeking = false;
  125. return ! seeking;
  126. }
  127. case JackTransportRolling: /* JACK's timeout has expired */
  128. /* FIXME: what's the right thing to do here? */
  129. // request_locate( pos->frame );
  130. return 1;
  131. // return transport->frame == pos->frame;
  132. break;
  133. default:
  134. printf( "unknown transport state.\n" );
  135. }
  136. return 0;
  137. }
  138. /* THREAD: RT */
  139. void
  140. Engine::timebase ( jack_transport_state_t, jack_nframes_t, jack_position_t *pos, int )
  141. {
  142. position_info pi = timeline->solve_tempomap( pos->frame );
  143. pos->valid = JackPositionBBT;
  144. pos->beats_per_bar = pi.beats_per_bar;
  145. pos->beat_type = pi.beat_type;
  146. pos->beats_per_minute = pi.tempo;
  147. pos->bar = pi.bbt.bar + 1;
  148. pos->beat = pi.bbt.beat + 1;
  149. pos->tick = pi.bbt.tick;
  150. pos->ticks_per_beat = 1920.0; /* FIXME: wrong place for this */
  151. /* FIXME: fill this in */
  152. pos->bar_start_tick = 0;
  153. }
  154. /* THREAD: RT */
  155. int
  156. Engine::process ( nframes_t nframes )
  157. {
  158. /* FIXME: wrong place for this */
  159. _thread.set( "RT" );
  160. transport->poll();
  161. if ( freewheeling() )
  162. {
  163. /* freewheeling mode/export. We're actually running
  164. non-RT. Assume that everything is quiescent, locking is
  165. unecessary and do I/O synchronously */
  166. if ( timeline )
  167. timeline->process( nframes );
  168. /* because we're going faster than realtime. */
  169. timeline->wait_for_buffers();
  170. }
  171. else
  172. {
  173. if ( ! trylock() )
  174. {
  175. /* the data structures we need to access here (tracks and
  176. * their ports, but not track contents) may be in an
  177. * inconsistent state at the moment. Just punt and drop this
  178. * buffer. */
  179. ++_buffers_dropped;
  180. return 0;
  181. }
  182. /* handle chicken/egg problem */
  183. if ( timeline )
  184. /* this will initiate the process() call graph for the various
  185. * number and types of tracks, which will in turn send data out
  186. * the appropriate ports. */
  187. timeline->process( nframes );
  188. unlock();
  189. }
  190. return 0;
  191. }
  192. /** enter or leave freehweeling mode */
  193. void
  194. Engine::freewheeling ( bool yes )
  195. {
  196. if ( jack_set_freewheel( _client, yes ) )
  197. WARNING( "Unkown error while setting freewheeling mode" );
  198. }
  199. void
  200. Engine::thread_init ( void *arg )
  201. {
  202. ((Engine*)arg)->thread_init();
  203. }
  204. void
  205. Engine::thread_init ( void )
  206. {
  207. _thread.set( "RT" );
  208. }
  209. void
  210. Engine::shutdown ( void *arg )
  211. {
  212. ((Engine*)arg)->shutdown();
  213. }
  214. void
  215. Engine::shutdown ( void )
  216. {
  217. _zombified = true;
  218. }
  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. }