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.

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