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.

249 lines
6.7KB

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