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.

212 lines
6.3KB

  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. /* THREAD: IO */
  69. void
  70. Playback_DS::disk_thread ( void )
  71. {
  72. printf( "IO thread running...\n" );
  73. /* buffer to hold the interleaved data returned by the track reader */
  74. sample_t *buf = new sample_t[ _nframes * channels() * _disk_io_blocks ];
  75. int blocks_ready = 1;
  76. const nframes_t nframes = _nframes * _disk_io_blocks;
  77. while ( wait_for_block() )
  78. {
  79. // lock(); // for seeking
  80. if ( seek_pending() )
  81. {
  82. printf( "performing seek\n" );
  83. _frame = _pending_seek;
  84. _pending_seek = -1;
  85. blocks_ready = 1;
  86. /* finish flushing the buffer */
  87. /* for ( int i = channels(); i-- ) */
  88. /* jack_ringbuffer_write_advance( _rb[ i ], jack_ringbuffer_write_space( _rb[ i ] ) ); */
  89. }
  90. if ( blocks_ready < _disk_io_blocks )
  91. {
  92. ++blocks_ready;
  93. /* wait for more space */
  94. continue;
  95. }
  96. /* reset */
  97. blocks_ready = 1;
  98. read_block( buf, nframes );
  99. // unlock(); // for seeking
  100. /* deinterleave the buffer and stuff it into the per-channel ringbuffers */
  101. for ( int i = channels(); i--; )
  102. {
  103. /* deinterleave direcectly into the ringbuffer to avoid
  104. * unnecessary copying */
  105. jack_ringbuffer_data_t rbd[2];
  106. jack_ringbuffer_get_write_vector( _rb[ i ], rbd );
  107. const size_t block_size = nframes * sizeof( sample_t );
  108. if ( rbd[ 0 ].len >= block_size )
  109. /* it'll all fit in one go */
  110. buffer_deinterleave_one_channel( (sample_t*)rbd[ 0 ].buf, buf, i, channels(), nframes );
  111. else if ( rbd[ 0 ].len + rbd[ 1 ].len >= block_size )
  112. {
  113. /* there's enough space in the ringbuffer, but it's not contiguous */
  114. const nframes_t f = rbd[ 0 ].len / sizeof( sample_t );
  115. /* do the first half */
  116. buffer_deinterleave_one_channel( (sample_t*)rbd[ 0 ].buf, buf, i, channels(), f );
  117. /* do the second half */
  118. buffer_deinterleave_one_channel( (sample_t*)rbd[ 1 ].buf, buf + f, i, channels(), nframes - f );
  119. }
  120. else
  121. {
  122. ++_xruns;
  123. }
  124. /* buffer_deinterleave_one_channel( (sample_t*)rbd.buf, buf, i, channels(), _nframes ); */
  125. /* jack_ringbuffer_write( _rb[ i ], (char*)cbuf, block_size ); */
  126. jack_ringbuffer_write_advance( _rb[ i ], block_size );
  127. }
  128. }
  129. printf( "IO thread terminating.\n" );
  130. delete[] buf;
  131. }
  132. /* THREAD: RT */
  133. /** take a single block from the ringbuffers and send it out the
  134. * attached track's ports */
  135. nframes_t
  136. Playback_DS::process ( nframes_t nframes )
  137. {
  138. const size_t block_size = nframes * sizeof( sample_t );
  139. // printf( "process: %lu %lu %lu\n", _frame, _frame + nframes, nframes );
  140. for ( int i = channels(); i--; )
  141. {
  142. void *buf = _th->output[ i ].buffer( nframes );
  143. if ( jack_ringbuffer_read( _rb[ i ], (char*)buf, block_size ) < block_size )
  144. {
  145. ++_xruns;
  146. printf( "RT: buffer underrun (disk can't keep up).\n" );
  147. memset( buf, 0, block_size );
  148. /* FIXME: we need to resync somehow */
  149. }
  150. /* TODO: figure out a way to stop IO while muted without losing sync */
  151. if ( _th->mute() || ( Track::soloing() && ! _th->solo() ) )
  152. buffer_fill_with_silence( (sample_t*)buf, nframes );
  153. }
  154. block_processed();
  155. /* FIXME: bogus */
  156. return nframes;
  157. }