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.

208 lines
5.5KB

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