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.

240 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 "../Track.H"
  19. // #include "Audio_Sequence.H"
  20. class Audio_Sequence;
  21. // #include "Port.H"
  22. #include "Engine.H" // for locking.
  23. #include "Disk_Stream.H"
  24. #include "dsp.h"
  25. #include "const.h"
  26. #include "util/debug.h"
  27. /**********/
  28. /* Engine */
  29. /**********/
  30. /* A Disk_Stream uses a separate I/O thread to stream a track's
  31. regions from disk into a ringbuffer to be processed by the RT
  32. thread (or vice-versa). The I/O thread syncronizes access with the
  33. user thread via the Timeline mutex. The size of the buffer (in
  34. seconds) must be set before any Disk_Stream objects are created;
  35. that is, at startup time. The default is 5 seconds, which may or
  36. may not be excessive depending on various external factors. */
  37. float Disk_Stream::seconds_to_buffer = 2.0f;
  38. /* this is really only a rough estimate. The actual amount of data
  39. read depends on many factors. Overlapping regions, for example, will
  40. require more data to be read from disk, as will varying channel
  41. counts.*/
  42. size_t Disk_Stream::disk_io_kbytes = 256;
  43. Disk_Stream::Disk_Stream ( Track *track, float frame_rate, nframes_t nframes, int channels ) : _track( track )
  44. {
  45. assert( channels );
  46. _frame = 0;
  47. _terminate = false;
  48. _pending_seek = -1;
  49. _xruns = 0;
  50. _frame_rate = frame_rate;
  51. _resize_buffers( nframes, channels );
  52. sem_init( &_blocks, 0, _total_blocks );
  53. }
  54. Disk_Stream::~Disk_Stream ( )
  55. {
  56. /* it isn't safe to do all this with the RT thread running */
  57. engine->lock();
  58. shutdown();
  59. _track = NULL;
  60. sem_destroy( &_blocks );
  61. for ( int i = channels(); i--; )
  62. jack_ringbuffer_free( _rb[ i ] );
  63. engine->unlock();
  64. }
  65. /** flush buffers and reset. Must only be called from the RT thread. */
  66. void
  67. Disk_Stream::base_flush ( bool is_output )
  68. {
  69. THREAD_ASSERT( RT );
  70. /* flush buffers */
  71. for ( int i = _rb.size(); i--; )
  72. jack_ringbuffer_read_advance( _rb[ i ], jack_ringbuffer_read_space( _rb[ i ] ) );
  73. /* sem_destroy( &_blocks ); */
  74. /* if ( is_output ) */
  75. /* sem_init( &_blocks, 0, _total_blocks ); */
  76. /* else */
  77. /* sem_init( &_blocks, 0, 0 ); */
  78. if ( is_output )
  79. {
  80. int n;
  81. sem_getvalue( &_blocks, &n );
  82. n = _total_blocks - n;
  83. while ( n-- )
  84. sem_post( &_blocks );
  85. }
  86. else
  87. {
  88. sem_destroy( &_blocks );
  89. sem_init( &_blocks, 0, 0 );
  90. }
  91. }
  92. /** signal thread to terminate, then detach it */
  93. void
  94. Disk_Stream::detach ( void )
  95. {
  96. _terminate = true;
  97. block_processed();
  98. _thread.detach();
  99. }
  100. /** stop the IO thread. */
  101. void
  102. Disk_Stream::shutdown ( void )
  103. {
  104. _terminate = true;
  105. /* try to wake the thread so it'll see that it's time to die */
  106. block_processed();
  107. if ( _thread.running() )
  108. _thread.join();
  109. }
  110. Track *
  111. Disk_Stream::track ( void ) const
  112. {
  113. return _track;
  114. }
  115. Audio_Sequence *
  116. Disk_Stream::sequence ( void ) const
  117. {
  118. return (Audio_Sequence*)_track->sequence();
  119. }
  120. /** start Disk_Stream thread */
  121. void
  122. Disk_Stream::run ( void )
  123. {
  124. ASSERT( ! _thread.running(), "Thread is already running" );
  125. if ( ! _thread.clone( &Disk_Stream::disk_thread, this ) )
  126. FATAL( "Could not create IO thread!" );
  127. }
  128. void
  129. Disk_Stream::_resize_buffers ( nframes_t nframes, int channels )
  130. {
  131. for ( int i = _rb.size(); i--; )
  132. jack_ringbuffer_free( _rb[ i ] );
  133. _rb.clear();
  134. _nframes = nframes;
  135. _total_blocks = _frame_rate * seconds_to_buffer / nframes;
  136. size_t bufsize = _total_blocks * nframes * sizeof( sample_t );
  137. if ( disk_io_kbytes )
  138. _disk_io_blocks = ( bufsize * channels ) / ( disk_io_kbytes * 1024 );
  139. else
  140. _disk_io_blocks = 1;
  141. for ( int i = channels; i--; )
  142. _rb.push_back( jack_ringbuffer_create( bufsize ) );
  143. }
  144. /* THREAD: RT (non-RT) */
  145. /* to be called when the JACK buffer size changes. */
  146. void
  147. Disk_Stream::resize_buffers ( nframes_t nframes )
  148. {
  149. if ( nframes != _nframes )
  150. {
  151. DMESSAGE( "resizing buffers" );
  152. const bool was_running = _thread.running();
  153. if ( was_running )
  154. shutdown();
  155. flush();
  156. _resize_buffers( nframes, channels() );
  157. if ( was_running )
  158. run();
  159. }
  160. }
  161. /* static wrapper */
  162. void *
  163. Disk_Stream::disk_thread ( void *arg )
  164. {
  165. ((Disk_Stream*)arg)->disk_thread();
  166. return NULL;
  167. }
  168. int
  169. Disk_Stream::buffer_percent ( void )
  170. {
  171. int n;
  172. sem_getvalue( &_blocks, &n );
  173. return 100 - (n * 100 / _total_blocks);
  174. #include "const.h"
  175. }