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.

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