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.

212 lines
5.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. /* This is the home of the JACK process callback */
  23. #include "const.h"
  24. #include "util/debug.h"
  25. #include "util/Thread.H"
  26. Engine::Engine ( ) : _thread( "RT" )
  27. {
  28. _buffers_dropped = 0;
  29. }
  30. Engine::~Engine ( )
  31. {
  32. /* We have to deactivate here in order to avoid our process
  33. callback is being invoked after we're already destroyed, but
  34. before the base class is */
  35. deactivate();
  36. }
  37. /*************/
  38. /* Callbacks */
  39. /*************/
  40. /* THREAD: RT */
  41. /** This is the jack xrun callback */
  42. int
  43. Engine::xrun ( void )
  44. {
  45. return 0;
  46. }
  47. /* THREAD: RT */
  48. void
  49. Engine::freewheel ( bool starting )
  50. {
  51. if ( starting )
  52. DMESSAGE( "entering freewheeling mode" );
  53. else
  54. DMESSAGE( "leaving freewheeling mode" );
  55. }
  56. /* THREAD: RT (non-RT) */
  57. int
  58. Engine::buffer_size ( nframes_t nframes )
  59. {
  60. timeline->resize_buffers( nframes );
  61. return 0;
  62. }
  63. /* THREAD: RT */
  64. /** This is the jack slow-sync callback. */
  65. int
  66. Engine::sync ( jack_transport_state_t state, jack_position_t *pos )
  67. {
  68. static bool seeking = false;
  69. switch ( state )
  70. {
  71. case JackTransportStopped: /* new position requested */
  72. /* JACK docs lie. This is only called when the transport
  73. is *really* stopped, not when starting a slow-sync
  74. cycle */
  75. transport->frame = pos->frame;
  76. return 1;
  77. case JackTransportStarting: /* this means JACK is polling slow-sync clients */
  78. {
  79. if ( ! seeking )
  80. {
  81. request_locate( pos->frame );
  82. seeking = true;
  83. }
  84. int r = true;
  85. if ( timeline )
  86. r = timeline->seek_pending();
  87. if ( ! r )
  88. seeking = false;
  89. return ! seeking;
  90. }
  91. case JackTransportRolling: /* JACK's timeout has expired */
  92. /* FIXME: what's the right thing to do here? */
  93. // request_locate( pos->frame );
  94. return 1;
  95. // return transport->frame == pos->frame;
  96. break;
  97. default:
  98. printf( "unknown transport state.\n" );
  99. }
  100. return 0;
  101. }
  102. /* THREAD: RT */
  103. void
  104. Engine::timebase ( jack_transport_state_t, jack_nframes_t, jack_position_t *pos, int )
  105. {
  106. position_info pi = timeline->solve_tempomap( pos->frame );
  107. pos->valid = JackPositionBBT;
  108. pos->beats_per_bar = pi.beats_per_bar;
  109. pos->beat_type = pi.beat_type;
  110. pos->beats_per_minute = pi.tempo;
  111. pos->bar = pi.bbt.bar + 1;
  112. pos->beat = pi.bbt.beat + 1;
  113. pos->tick = pi.bbt.tick;
  114. pos->ticks_per_beat = 1920.0; /* FIXME: wrong place for this */
  115. /* FIXME: fill this in */
  116. pos->bar_start_tick = 0;
  117. }
  118. /* THREAD: RT */
  119. int
  120. Engine::process ( nframes_t nframes )
  121. {
  122. /* FIXME: wrong place for this */
  123. _thread.set( "RT" );
  124. transport->poll();
  125. if ( freewheeling() )
  126. {
  127. /* freewheeling mode/export. We're actually running
  128. non-RT. Assume that everything is quiescent, locking is
  129. unecessary and do I/O synchronously */
  130. if ( timeline )
  131. timeline->process( nframes );
  132. /* because we're going faster than realtime. */
  133. timeline->wait_for_buffers();
  134. }
  135. else
  136. {
  137. if ( ! trylock() )
  138. {
  139. /* the data structures we need to access here (tracks and
  140. * their ports, but not track contents) may be in an
  141. * inconsistent state at the moment. Just punt and drop this
  142. * buffer. */
  143. ++_buffers_dropped;
  144. return 0;
  145. }
  146. /* handle chicken/egg problem */
  147. if ( timeline )
  148. /* this will initiate the process() call graph for the various
  149. * number and types of tracks, which will in turn send data out
  150. * the appropriate ports. */
  151. timeline->process( nframes );
  152. unlock();
  153. }
  154. return 0;
  155. }
  156. /* TRHEAD: RT */
  157. void
  158. Engine::thread_init ( void )
  159. {
  160. _thread.set( "RT" );
  161. }
  162. /* THREAD: RT */
  163. void
  164. Engine::shutdown ( void )
  165. {
  166. }
  167. void
  168. Engine::request_locate ( nframes_t frame )
  169. {
  170. if ( timeline )
  171. timeline->seek( frame );
  172. }