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.

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