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.

316 lines
7.8KB

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