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.

179 lines
4.9KB

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