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.

246 lines
7.3KB

  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. /* Handles streaming regions from disk to track outputs. */
  19. /* FIXME: we shouldn't depend on these */
  20. #include "Timeline.H"
  21. #include "Engine.H"
  22. #include "Audio_Sequence.H"
  23. #include "Track.H"
  24. #include "Port.H"
  25. #include "Playback_DS.H"
  26. #include "dsp.h"
  27. bool
  28. Playback_DS::seek_pending ( void )
  29. {
  30. return _pending_seek != (nframes_t)-1;
  31. }
  32. /* THREAD: RT */
  33. /** request that the IO thread perform a seek and rebuffer. This is
  34. called for each Disk_Stream whenever the RT thread determines that
  35. the transport has jumped to a new position. This is called *before*
  36. process. */
  37. void
  38. Playback_DS::seek ( nframes_t frame )
  39. {
  40. printf( "requesting seek\n" );
  41. if ( seek_pending() )
  42. printf( "seek error, attempt to seek while seek is pending\n" );
  43. _pending_seek = frame;
  44. /* flush buffers */
  45. for ( int i = channels(); i--; )
  46. jack_ringbuffer_read_advance( _rb[ i ], jack_ringbuffer_read_space( _rb[ i ] ) );
  47. /* dirty hack... reset the semaphore. Should we just call sem_init
  48. * again instead? */
  49. /* sem_init( &_blocks, 0, _total_blocks ); */
  50. int n;
  51. sem_getvalue( &_blocks, &n );
  52. n = _total_blocks - n;
  53. while ( n-- )
  54. sem_post( &_blocks );
  55. }
  56. /* THREAD: IO */
  57. /** read /nframes/ from the attached track into /buf/ */
  58. void
  59. Playback_DS::read_block ( sample_t *buf, nframes_t nframes )
  60. {
  61. memset( buf, 0, nframes * sizeof( sample_t ) * channels() );
  62. /* stupid chicken/egg */
  63. if ( ! timeline )
  64. return;
  65. // printf( "IO: attempting to read block @ %lu\n", _frame );
  66. if ( ! track() )
  67. {
  68. // _frame += _nframes;
  69. return;
  70. }
  71. timeline->rdlock();
  72. if ( track()->play( buf, _frame, nframes, channels() ) )
  73. _frame += nframes;
  74. else
  75. /* error */;
  76. timeline->unlock();
  77. }
  78. /* THREAD: IO */
  79. void
  80. Playback_DS::disk_thread ( void )
  81. {
  82. printf( "IO thread running...\n" );
  83. /* buffer to hold the interleaved data returned by the track reader */
  84. sample_t *buf = new sample_t[ _nframes * channels() * _disk_io_blocks ];
  85. // const size_t block_size = _nframes * sizeof( sample_t );
  86. int blocks_ready = 1;
  87. while ( wait_for_block() )
  88. {
  89. // printf( "IO: RT thread is ready for more data...\n" );
  90. // printf( "IO: disk buffer is %3d%% full\r", output_buffer_percent() );
  91. // lock(); // for seeking
  92. if ( seek_pending() )
  93. {
  94. printf( "performing seek\n" );
  95. _frame = _pending_seek;
  96. _pending_seek = -1;
  97. blocks_ready = 1;
  98. /* finish flushing the buffer */
  99. /* for ( int i = channels(); i-- ) */
  100. /* jack_ringbuffer_write_advance( _rb[ i ], jack_ringbuffer_write_space( _rb[ i ] ) ); */
  101. }
  102. if ( blocks_ready < _disk_io_blocks )
  103. {
  104. ++blocks_ready;
  105. /* wait for more space */
  106. continue;
  107. }
  108. /* reset */
  109. blocks_ready = 1;
  110. const nframes_t nframes = _nframes * _disk_io_blocks;
  111. /* FIXME: should we not read from disk in larger-than-JACK-buffer blocks? */
  112. read_block( buf, nframes );
  113. // unlock(); // for seeking
  114. /* deinterleave the buffer and stuff it into the per-channel ringbuffers */
  115. for ( int i = channels(); i--; )
  116. {
  117. /* while ( jack_ringbuffer_write_space( _rb[ i ] ) < block_size ) */
  118. /* { */
  119. /* printf( "IO: disk buffer overrun!\n" ); */
  120. /* /\* FIXME: is this *really* the right thing to do? *\/ */
  121. /* usleep( 2000 ); */
  122. /* } */
  123. /* deinterleave direcectly into the ringbuffer to avoid
  124. * unnecessary copying */
  125. jack_ringbuffer_data_t rbd[2];
  126. jack_ringbuffer_get_write_vector( _rb[ i ], rbd );
  127. if ( rbd[ 0 ].len >= nframes )
  128. /* it'll all fit in one go */
  129. buffer_deinterleave_one_channel( (sample_t*)rbd[ 0 ].buf, buf, i, channels(), nframes );
  130. else if ( rbd[ 1 ].len )
  131. {
  132. /* there's enough space in the ringbuffer, but it's not contiguous */
  133. /* do the first half */
  134. const nframes_t f = rbd[ 0 ].len / sizeof( sample_t );
  135. buffer_deinterleave_one_channel( (sample_t*)rbd[ 0 ].buf, buf, i, channels(), f );
  136. assert( rbd[ 1 ].len >= (nframes - f) * sizeof( sample_t ) );
  137. /* do the second half */
  138. buffer_deinterleave_one_channel( (sample_t*)rbd[ 1 ].buf, buf + f, i, channels(), nframes - f );
  139. }
  140. else
  141. {
  142. ++_xruns;
  143. printf( "programming error: expected more space in ringbuffer\n" );
  144. }
  145. /* buffer_deinterleave_one_channel( (sample_t*)rbd.buf, buf, i, channels(), _nframes ); */
  146. /* jack_ringbuffer_write( _rb[ i ], (char*)cbuf, block_size ); */
  147. jack_ringbuffer_write_advance( _rb[ i ], nframes * sizeof( sample_t ) );
  148. }
  149. }
  150. printf( "IO thread terminating.\n" );
  151. delete[] buf;
  152. }
  153. /* THREAD: RT */
  154. /** take a single block from the ringbuffers and send it out the
  155. * attached track's ports */
  156. nframes_t
  157. Playback_DS::process ( nframes_t nframes )
  158. {
  159. const size_t block_size = nframes * sizeof( sample_t );
  160. // printf( "process: %lu %lu %lu\n", _frame, _frame + nframes, nframes );
  161. for ( int i = channels(); i--; )
  162. {
  163. void *buf = _th->output[ i ].buffer( nframes );
  164. if ( jack_ringbuffer_read( _rb[ i ], (char*)buf, block_size ) < block_size )
  165. {
  166. ++_xruns;
  167. printf( "RT: buffer underrun (disk can't keep up).\n" );
  168. memset( buf, 0, block_size );
  169. /* FIXME: we need to resync somehow */
  170. }
  171. /* TODO: figure out a way to stop IO while muted without losing sync */
  172. if ( _th->mute() || ( Track::soloing() && ! _th->solo() ) )
  173. buffer_fill_with_silence( (sample_t*)buf, nframes );
  174. }
  175. block_processed();
  176. /* FIXME: bogus */
  177. return nframes;
  178. }