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.

232 lines
6.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. /* 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( true );
  45. }
  46. /* THREAD: IO */
  47. /** read /nframes/ from the attached track into /buf/ */
  48. void
  49. Playback_DS::read_block ( sample_t *buf, nframes_t nframes )
  50. {
  51. memset( buf, 0, nframes * sizeof( sample_t ) * channels() );
  52. /* stupid chicken/egg */
  53. if ( ! timeline )
  54. return;
  55. // printf( "IO: attempting to read block @ %lu\n", _frame );
  56. if ( ! track() )
  57. {
  58. // _frame += _nframes;
  59. return;
  60. }
  61. timeline->rdlock();
  62. if ( track()->play( buf, _frame, nframes, channels() ) )
  63. _frame += nframes;
  64. else
  65. /* error */;
  66. timeline->unlock();
  67. }
  68. #define AVOID_UNNECESSARY_COPYING 1
  69. /* THREAD: IO */
  70. void
  71. Playback_DS::disk_thread ( void )
  72. {
  73. printf( "IO thread running...\n" );
  74. /* buffer to hold the interleaved data returned by the track reader */
  75. sample_t *buf = new sample_t[ _nframes * channels() * _disk_io_blocks ];
  76. #ifndef AVOID_UNNECESSARY_COPYING
  77. sample_t *cbuf = new sample_t[ _nframes * _disk_io_blocks ];
  78. #endif
  79. int blocks_ready = 1;
  80. const nframes_t nframes = _nframes * _disk_io_blocks;
  81. while ( wait_for_block() )
  82. {
  83. // lock(); // for seeking
  84. if ( seek_pending() )
  85. {
  86. printf( "performing seek\n" );
  87. _frame = _pending_seek;
  88. _pending_seek = -1;
  89. blocks_ready = 1;
  90. /* finish flushing the buffer */
  91. /* for ( int i = channels(); i-- ) */
  92. /* jack_ringbuffer_write_advance( _rb[ i ], jack_ringbuffer_write_space( _rb[ i ] ) ); */
  93. }
  94. if ( blocks_ready < _disk_io_blocks )
  95. {
  96. ++blocks_ready;
  97. /* wait for more space */
  98. continue;
  99. }
  100. /* reset */
  101. blocks_ready = 1;
  102. read_block( buf, nframes );
  103. // unlock(); // for seeking
  104. /* deinterleave the buffer and stuff it into the per-channel ringbuffers */
  105. const size_t block_size = nframes * sizeof( sample_t );
  106. for ( int i = channels(); i--; )
  107. {
  108. #ifdef AVOID_UNNECESSARY_COPYING
  109. /* deinterleave direcectly into the ringbuffer to avoid
  110. * unnecessary copying */
  111. jack_ringbuffer_data_t rbd[2];
  112. memset( rbd, 0, sizeof( rbd ) );
  113. jack_ringbuffer_get_write_vector( _rb[ i ], rbd );
  114. if ( rbd[ 0 ].len >= block_size )
  115. {
  116. /* it'll all fit in one go */
  117. buffer_deinterleave_one_channel( (sample_t*)rbd[ 0 ].buf, buf, i, channels(), nframes );
  118. }
  119. else if ( rbd[ 0 ].len + rbd[ 1 ].len >= block_size )
  120. {
  121. /* there's enough space in the ringbuffer, but it's not contiguous */
  122. assert( ! ( rbd[ 0 ].len % sizeof( sample_t ) ) );
  123. // assert( ! ( rbd[ 1 ].len % sizeof( sample_t ) ) );
  124. const nframes_t f = rbd[ 0 ].len / sizeof( sample_t );
  125. /* do the first half */
  126. buffer_deinterleave_one_channel( (sample_t*)rbd[ 0 ].buf, buf, i, channels(), f );
  127. assert( rbd[ 1 ].len >= ( nframes - f ) * sizeof( sample_t ) );
  128. /* do the second half */
  129. buffer_deinterleave_one_channel( (sample_t*)rbd[ 1 ].buf, buf + f, i, channels(), nframes - f );
  130. }
  131. else
  132. ++_xruns;
  133. jack_ringbuffer_write_advance( _rb[ i ], block_size );
  134. #else
  135. buffer_deinterleave_one_channel( cbuf, buf, i, channels(), nframes );
  136. if ( jack_ringbuffer_write( _rb[ i ], (char*)cbuf, block_size ) < block_size )
  137. ++_xruns;
  138. #endif
  139. }
  140. }
  141. printf( "IO thread terminating.\n" );
  142. delete[] buf;
  143. #ifndef AVOID_UNNECESSARY_COPYING
  144. delete[] cbuf;
  145. #endif
  146. }
  147. /* THREAD: RT */
  148. /** take a single block from the ringbuffers and send it out the
  149. * attached track's ports */
  150. nframes_t
  151. Playback_DS::process ( nframes_t nframes )
  152. {
  153. const size_t block_size = nframes * sizeof( sample_t );
  154. // printf( "process: %lu %lu %lu\n", _frame, _frame + nframes, nframes );
  155. for ( int i = channels(); i--; )
  156. {
  157. void *buf = _th->output[ i ].buffer( nframes );
  158. if ( jack_ringbuffer_read( _rb[ i ], (char*)buf, block_size ) < block_size )
  159. {
  160. ++_xruns;
  161. memset( buf, 0, block_size );
  162. /* FIXME: we need to resync somehow */
  163. }
  164. /* TODO: figure out a way to stop IO while muted without losing sync */
  165. if ( _th->mute() || ( Track::soloing() && ! _th->solo() ) )
  166. buffer_fill_with_silence( (sample_t*)buf, nframes );
  167. }
  168. block_processed();
  169. /* FIXME: bogus */
  170. return nframes;
  171. }