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.

238 lines
6.3KB

  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 ( _freewheeling )
  80. FATAL( "Freewheeling mode is unimplemented" );
  81. }
  82. /* THREAD: RT */
  83. /** This is the jack slow-sync callback. */
  84. int
  85. Engine::sync ( jack_transport_state_t state, jack_position_t *pos )
  86. {
  87. static bool seeking = false;
  88. switch ( state )
  89. {
  90. case JackTransportStopped: /* new position requested */
  91. /* JACK docs lie. This is only called when the transport
  92. is *really* stopped, not when starting a slow-sync
  93. cycle */
  94. request_locate( pos->frame );
  95. return 1;
  96. case JackTransportStarting: /* this means JACK is polling slow-sync clients */
  97. {
  98. if ( ! seeking )
  99. {
  100. request_locate( pos->frame );
  101. seeking = true;
  102. }
  103. int r = true;
  104. if ( timeline )
  105. r = timeline->seek_pending();
  106. if ( ! r )
  107. seeking = false;
  108. return ! seeking;
  109. }
  110. case JackTransportRolling: /* JACK's timeout has expired */
  111. /* FIXME: what's the right thing to do here? */
  112. // request_locate( pos->frame );
  113. return 1;
  114. // return transport->frame == pos->frame;
  115. break;
  116. default:
  117. printf( "unknown transport state.\n" );
  118. }
  119. return 0;
  120. }
  121. /* THREAD: RT */
  122. void
  123. Engine::timebase ( jack_transport_state_t state, jack_nframes_t nframes, jack_position_t *pos, int new_pos )
  124. {
  125. position_info pi = timeline->solve_tempomap( pos->frame );
  126. pos->valid = JackPositionBBT;
  127. pos->beats_per_bar = pi.beats_per_bar;
  128. pos->beat_type = pi.beat_type;
  129. pos->beats_per_minute = pi.tempo;
  130. pos->bar = pi.bbt.bar + 1;
  131. pos->beat = pi.bbt.beat + 1;
  132. pos->tick = pi.bbt.tick;
  133. pos->ticks_per_beat = 1920.0; /* FIXME: wrong place for this */
  134. /* FIXME: fill this in */
  135. pos->bar_start_tick = 0;
  136. }
  137. /* THREAD: RT */
  138. int
  139. Engine::process ( nframes_t nframes )
  140. {
  141. transport->poll();
  142. if ( ! trylock() )
  143. {
  144. /* the data structures we need to access here (tracks and
  145. * their ports, but not track contents) may be in an
  146. * inconsistent state at the moment. Just punt and drop this
  147. * buffer. */
  148. ++_buffers_dropped;
  149. return 0;
  150. }
  151. /* if ( ! transport->rolling ) */
  152. /* timeline->silence( nframes ); */
  153. /* return 0; */
  154. /* handle chicken/egg problem */
  155. if ( timeline )
  156. /* this will initiate the process() call graph for the various
  157. * number and types of tracks, which will in turn send data out
  158. * the appropriate ports. */
  159. timeline->process( nframes );
  160. unlock();
  161. return 0;
  162. }
  163. /** enter or leave freehweeling mode */
  164. void
  165. Engine::freewheeling ( bool yes )
  166. {
  167. if ( jack_set_freewheel( _client, yes ) )
  168. WARNING( "Unkown error while setting freewheeling mode" );
  169. }
  170. int
  171. Engine::init ( void )
  172. {
  173. if (( _client = jack_client_open ( APP_NAME, (jack_options_t)0, NULL )) == 0 )
  174. return 0;
  175. #define set_callback( name ) jack_set_ ## name ## _callback( _client, &Engine:: name , this )
  176. set_callback( process );
  177. set_callback( xrun );
  178. set_callback( freewheel );
  179. /* FIXME: should we wait to register this until after the project
  180. has been loaded (and we have disk threads running)? */
  181. set_callback( sync );
  182. jack_set_timebase_callback( _client, 0, &Engine::timebase, this );
  183. jack_activate( _client );
  184. _sample_rate = frame_rate();
  185. /* we don't need to create any ports until tracks are created */
  186. return 1;
  187. }