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.

228 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"
  21. #include "Engine.H"
  22. #include "Audio_Track.H"
  23. #include "Track_Header.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 buffers */
  45. for ( int i = channels(); i--; )
  46. jack_ringbuffer_read_advance( _rb[ i ], jack_ringbuffer_read_space( _rb[ i ] ) );
  47. /* dirty hack... reset the semaphore. Should we just call sem_init
  48. * again instead? */
  49. /* sem_init( &_blocks, 0, _total_blocks ); */
  50. int n;
  51. sem_getvalue( &_blocks, &n );
  52. n = _total_blocks - n;
  53. while ( n-- )
  54. sem_post( &_blocks );
  55. }
  56. /* THREAD: IO */
  57. /** read /nframes/ from the attached track into /buf/ */
  58. void
  59. Playback_DS::read_block ( sample_t *buf, nframes_t nframes )
  60. {
  61. memset( buf, 0, nframes * sizeof( sample_t ) * channels() );
  62. /* stupid chicken/egg */
  63. if ( ! timeline )
  64. return;
  65. // printf( "IO: attempting to read block @ %lu\n", _frame );
  66. if ( ! track() )
  67. {
  68. // _frame += _nframes;
  69. return;
  70. }
  71. timeline->rdlock();
  72. if ( track()->play( buf, _frame, nframes, channels() ) )
  73. _frame += nframes;
  74. else
  75. /* error */;
  76. timeline->unlock();
  77. }
  78. /* THREAD: IO */
  79. void
  80. Playback_DS::disk_thread ( void )
  81. {
  82. printf( "IO thread running...\n" );
  83. /* buffer to hold the interleaved data returned by the track reader */
  84. sample_t *buf = new sample_t[ _nframes * channels() ];
  85. const size_t block_size = _nframes * sizeof( sample_t );
  86. while ( wait_for_block() )
  87. {
  88. // printf( "IO: RT thread is ready for more data...\n" );
  89. // printf( "IO: disk buffer is %3d%% full\r", output_buffer_percent() );
  90. // lock(); // for seeking
  91. if ( seek_pending() )
  92. {
  93. printf( "performing seek\n" );
  94. _frame = _pending_seek;
  95. _pending_seek = -1;
  96. /* finish flushing the buffer */
  97. /* for ( int i = channels(); i-- ) */
  98. /* jack_ringbuffer_write_advance( _rb[ i ], jack_ringbuffer_write_space( _rb[ i ] ) ); */
  99. }
  100. /* FIXME: should we not read from disk in larger-than-JACK-buffer blocks? */
  101. read_block( buf, _nframes );
  102. // unlock(); // for seeking
  103. /* deinterleave the buffer and stuff it into the per-channel ringbuffers */
  104. for ( int i = channels(); i--; )
  105. {
  106. while ( jack_ringbuffer_write_space( _rb[ i ] ) < block_size )
  107. {
  108. printf( "IO: disk buffer overrun!\n" );
  109. /* FIXME: is this *really* the right thing to do? */
  110. usleep( 2000 );
  111. }
  112. /* deinterleave direcectly into the ringbuffer to avoid
  113. * unnecessary copying */
  114. jack_ringbuffer_data_t rbd[2];
  115. jack_ringbuffer_get_write_vector( _rb[ i ], rbd );
  116. if ( rbd[ 0 ].len >= _nframes )
  117. /* it'll all fit in one go */
  118. buffer_deinterleave_one_channel( (sample_t*)rbd[ 0 ].buf, buf, i, channels(), _nframes );
  119. else if ( rbd[ 1 ].len )
  120. {
  121. /* there's enough space in the ringbuffer, but it's not contiguous */
  122. /* do the first half */
  123. const nframes_t f = rbd[ 1 ].len / sizeof( sample_t );
  124. buffer_deinterleave_one_channel( (sample_t*)rbd[ 0 ].buf, buf, i, channels(), f );
  125. assert( rbd[ 1 ].len >= (_nframes - f) * sizeof( sample_t ) );
  126. /* do the second half */
  127. buffer_deinterleave_one_channel( (sample_t*)rbd[ 1 ].buf, buf + f, i, channels(), _nframes - f );
  128. }
  129. else
  130. printf( "programming error: expected more space in ringbuffer\n" );
  131. /* buffer_deinterleave_one_channel( (sample_t*)rbd.buf, buf, i, channels(), _nframes ); */
  132. /* jack_ringbuffer_write( _rb[ i ], (char*)cbuf, block_size ); */
  133. jack_ringbuffer_write_advance( _rb[ i ], _nframes * sizeof( sample_t ) );
  134. }
  135. }
  136. printf( "IO thread terminating.\n" );
  137. delete[] buf;
  138. }
  139. /* THREAD: RT */
  140. /** take a single block from the ringbuffers and send it out the
  141. * attached track's ports */
  142. nframes_t
  143. Playback_DS::process ( nframes_t nframes )
  144. {
  145. const size_t block_size = nframes * sizeof( sample_t );
  146. // printf( "process: %lu %lu %lu\n", _frame, _frame + nframes, nframes );
  147. for ( int i = channels(); i--; )
  148. {
  149. void *buf = _th->output[ i ].buffer( nframes );
  150. if ( jack_ringbuffer_read( _rb[ i ], (char*)buf, block_size ) < block_size )
  151. {
  152. printf( "RT: buffer underrun (disk can't keep up).\n" );
  153. memset( buf, 0, block_size );
  154. /* FIXME: we need to resync somehow */
  155. }
  156. /* /\* testing. *\/ */
  157. /* FILE *fp = fopen( "testing.au", "a" ); */
  158. /* fwrite( buf, block_size, 1, fp ); */
  159. /* fclose( fp ); */
  160. }
  161. block_processed();
  162. /* FIXME: bogus */
  163. return nframes;
  164. }