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.

154 lines
4.6KB

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