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
6.9KB

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