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.

151 lines
4.5KB

  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 = timeline->seek_pending();
  69. if ( ! r )
  70. seeking = false;
  71. return ! seeking;
  72. }
  73. case JackTransportRolling: /* JACK's timeout has expired */
  74. /* FIXME: what's the right thing to do here? */
  75. // request_locate( pos->frame );
  76. return 1;
  77. // return transport.frame == pos->frame;
  78. break;
  79. default:
  80. printf( "unknown transport state.\n" );
  81. }
  82. return 0;
  83. }
  84. /* THREAD: RT */
  85. int
  86. Engine::process ( nframes_t nframes )
  87. {
  88. transport.poll();
  89. if ( ! transport.rolling )
  90. /* FIXME: fill all ports with silence */
  91. return 0;
  92. if ( ! trylock() )
  93. {
  94. /* the data structures we need to access here (tracks and
  95. * their ports, but not track contents) may be in an
  96. * inconsistent state at the moment. Just punt and drop this
  97. * buffer. */
  98. ++_buffers_dropped;
  99. return 0;
  100. }
  101. /* handle chicken/egg problem */
  102. if ( timeline )
  103. /* this will initiate the process() call graph for the various
  104. * number and types of tracks, which will in turn send data out
  105. * the appropriate ports. */
  106. timeline->process( nframes );
  107. unlock();
  108. return 0;
  109. }
  110. int
  111. Engine::init ( void )
  112. {
  113. if (( _client = jack_client_open ( APP_NAME, (jack_options_t)0, NULL )) == 0 )
  114. return 0;
  115. jack_set_process_callback( _client, &Engine::process, this );
  116. /* FIXME: should we wait to register this until after the session
  117. has been loaded (and we have disk threads running)? */
  118. jack_set_sync_callback( _client, &Engine::sync, this );
  119. jack_activate( _client );
  120. /* we don't need to create any ports until tracks are created */
  121. return 1;
  122. }