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.

229 lines
5.7KB

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