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.

251 lines
7.2KB

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