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.

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