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.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. #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. _client = NULL;
  28. _buffers_dropped = 0;
  29. _xruns = 0;
  30. }
  31. /*******************/
  32. /* Static Wrappers */
  33. /*******************/
  34. int
  35. Engine::process ( nframes_t nframes, void *arg )
  36. {
  37. return ((Engine*)arg)->process( nframes );
  38. }
  39. int
  40. Engine::sync ( jack_transport_state_t state, jack_position_t *pos, void *arg )
  41. {
  42. return ((Engine*)arg)->sync( state, pos );
  43. }
  44. int
  45. Engine::xrun ( void *arg )
  46. {
  47. return ((Engine*)arg)->xrun();
  48. }
  49. void
  50. Engine::timebase ( jack_transport_state_t state, jack_nframes_t nframes, jack_position_t *pos, int new_pos, void *arg )
  51. {
  52. ((Engine*)arg)->timebase( state, nframes, pos, new_pos );
  53. }
  54. void
  55. Engine::request_locate ( nframes_t frame )
  56. {
  57. if ( timeline )
  58. timeline->seek( frame );
  59. }
  60. /* THREAD: RT */
  61. /** This is the jack xrun callback */
  62. int
  63. Engine::xrun ( void )
  64. {
  65. ++_xruns;
  66. return 0;
  67. }
  68. /* THREAD: RT */
  69. /** This is the jack slow-sync callback. */
  70. int
  71. Engine::sync ( jack_transport_state_t state, jack_position_t *pos )
  72. {
  73. static bool seeking = false;
  74. switch ( state )
  75. {
  76. case JackTransportStopped: /* new position requested */
  77. /* JACK docs lie. This is only called when the transport
  78. is *really* stopped, not when starting a slow-sync
  79. cycle */
  80. request_locate( pos->frame );
  81. return 1;
  82. case JackTransportStarting: /* this means JACK is polling slow-sync clients */
  83. {
  84. if ( ! seeking )
  85. {
  86. request_locate( pos->frame );
  87. seeking = true;
  88. }
  89. int r = true;
  90. if ( timeline )
  91. r = timeline->seek_pending();
  92. if ( ! r )
  93. seeking = false;
  94. return ! seeking;
  95. }
  96. case JackTransportRolling: /* JACK's timeout has expired */
  97. /* FIXME: what's the right thing to do here? */
  98. // request_locate( pos->frame );
  99. return 1;
  100. // return transport->frame == pos->frame;
  101. break;
  102. default:
  103. printf( "unknown transport state.\n" );
  104. }
  105. return 0;
  106. }
  107. /* THREAD: RT */
  108. void
  109. Engine::timebase ( jack_transport_state_t state, jack_nframes_t nframes, jack_position_t *pos, int new_pos )
  110. {
  111. position_info pi = timeline->solve_tempomap( pos->frame );
  112. pos->valid = JackPositionBBT;
  113. pos->beats_per_bar = pi.beats_per_bar;
  114. pos->beat_type = pi.beat_type;
  115. pos->beats_per_minute = pi.tempo;
  116. pos->bar = pi.bbt.bar + 1;
  117. pos->beat = pi.bbt.beat + 1;
  118. pos->tick = pi.bbt.tick;
  119. pos->ticks_per_beat = 1920.0; /* FIXME: wrong place for this */
  120. /* FIXME: fill this in */
  121. pos->bar_start_tick = 0;
  122. }
  123. /* THREAD: RT */
  124. int
  125. Engine::process ( nframes_t nframes )
  126. {
  127. transport->poll();
  128. if ( ! trylock() )
  129. {
  130. /* the data structures we need to access here (tracks and
  131. * their ports, but not track contents) may be in an
  132. * inconsistent state at the moment. Just punt and drop this
  133. * buffer. */
  134. ++_buffers_dropped;
  135. return 0;
  136. }
  137. /* if ( ! transport->rolling ) */
  138. /* timeline->silence( nframes ); */
  139. /* return 0; */
  140. /* handle chicken/egg problem */
  141. if ( timeline )
  142. /* this will initiate the process() call graph for the various
  143. * number and types of tracks, which will in turn send data out
  144. * the appropriate ports. */
  145. timeline->process( nframes );
  146. unlock();
  147. return 0;
  148. }
  149. int
  150. Engine::init ( void )
  151. {
  152. if (( _client = jack_client_open ( APP_NAME, (jack_options_t)0, NULL )) == 0 )
  153. return 0;
  154. #define set_callback( name ) jack_set_ ## name ## _callback( _client, &Engine:: name , this )
  155. set_callback( process );
  156. set_callback( xrun );
  157. /* FIXME: should we wait to register this until after the project
  158. has been loaded (and we have disk threads running)? */
  159. set_callback( sync );
  160. jack_set_timebase_callback( _client, 0, &Engine::timebase, this );
  161. jack_activate( _client );
  162. _sample_rate = frame_rate();
  163. /* we don't need to create any ports until tracks are created */
  164. return 1;
  165. }