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.

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