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.

256 lines
7.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 from track inputs to disk */
  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 "Record_DS.H"
  26. #include "dsp.h"
  27. /* THREAD: IO */
  28. /** write /nframes/ from buf to the capture file of the attached track */
  29. void
  30. Record_DS::write_block ( sample_t *buf, nframes_t nframes )
  31. {
  32. /* stupid chicken/egg */
  33. if ( ! ( timeline && track() ) )
  34. return;
  35. // timeline->wrlock();
  36. _th->write( buf, nframes );
  37. _frames_written += nframes;
  38. // timeline->unlock();
  39. }
  40. /* THREAD: IO */
  41. void
  42. Record_DS::disk_thread ( void )
  43. {
  44. printf( "IO thread running...\n" );
  45. const nframes_t nframes = _nframes * _disk_io_blocks;
  46. /* buffer to hold the interleaved data returned by the track reader */
  47. sample_t *buf = new sample_t[ nframes * channels() ];
  48. sample_t *cbuf = new sample_t[ nframes ];
  49. const size_t block_size = nframes * sizeof( sample_t );
  50. int blocks_ready = 1;
  51. while ( wait_for_block() )
  52. {
  53. if ( blocks_ready < _disk_io_blocks )
  54. {
  55. ++blocks_ready;
  56. continue;
  57. }
  58. blocks_ready = 1;
  59. /* pull data from the per-channel ringbuffers and interlace it */
  60. for ( int i = channels(); i--; )
  61. {
  62. /* FIXME: avoid this copy */
  63. if ( jack_ringbuffer_read( _rb[ i ], (char*)cbuf, block_size ) < block_size )
  64. {
  65. ++_xruns;
  66. /* FIXME: what now? */
  67. printf( "Record_DS: underrun!\n" );
  68. }
  69. buffer_interleave_one_channel( buf, cbuf, i, channels(), nframes );
  70. /* /\* deinterleave direcectly into the ringbuffer to avoid */
  71. /* * unnecessary copying *\/ */
  72. /* jack_ringbuffer_data_t rbd[2]; */
  73. /* jack_ringbuffer_get_write_vector( _rb[ i ], rbd ); */
  74. /* if ( rbd[ 0 ].len >= _nframes ) */
  75. /* /\* it'll all fit in one go *\/ */
  76. /* buffer_deinterleave_one_channel( (sample_t*)rbd[ 0 ].buf, buf, i, channels(), _nframes ); */
  77. /* else if ( rbd[ 1 ].len ) */
  78. /* { */
  79. /* /\* there's enough space in the ringbuffer, but it's not contiguous *\/ */
  80. /* /\* do the first half *\/ */
  81. /* const nframes_t f = rbd[ 1 ].len / sizeof( sample_t ); */
  82. /* buffer_deinterleave_one_channel( (sample_t*)rbd[ 0 ].buf, buf, i, channels(), f ); */
  83. /* assert( rbd[ 1 ].len >= (_nframes - f) * sizeof( sample_t ) ); */
  84. /* /\* do the second half *\/ */
  85. /* buffer_deinterleave_one_channel( (sample_t*)rbd[ 1 ].buf, buf + f, i, channels(), _nframes - f ); */
  86. /* } */
  87. /* else */
  88. /* printf( "programming error: expected more space in ringbuffer\n" ); */
  89. /* /\* buffer_deinterleave_one_channel( (sample_t*)rbd.buf, buf, i, channels(), _nframes ); *\/ */
  90. /* /\* jack_ringbuffer_write( _rb[ i ], (char*)cbuf, block_size ); *\/ */
  91. /* jack_ringbuffer_write_advance( _rb[ i ], _nframes * sizeof( sample_t ) ); */
  92. }
  93. write_block( buf, nframes );
  94. }
  95. printf( "IO thread terminating.\n" );
  96. /* flush what remains in the buffer out to disk */
  97. {
  98. /* use JACk sized blocks for this last bit */
  99. const nframes_t nframes = _nframes;
  100. const size_t block_size = _nframes * sizeof( sample_t );
  101. while ( blocks_ready-- > 0 || ! sem_trywait( &_blocks ) && errno != EAGAIN )
  102. {
  103. for ( int i = channels(); i--; )
  104. {
  105. jack_ringbuffer_read( _rb[ i ], (char*)cbuf, block_size );
  106. buffer_interleave_one_channel( buf, cbuf, i, channels(), nframes );
  107. }
  108. const nframes_t frames_remaining = (_stop_frame - _frame ) - _frames_written;
  109. if ( frames_remaining < nframes )
  110. {
  111. /* this is the last block, might be partial */
  112. write_block( buf, frames_remaining );
  113. break;
  114. }
  115. else
  116. write_block( buf, nframes );
  117. }
  118. }
  119. delete[] cbuf;
  120. delete[] buf;
  121. }
  122. /** begin recording */
  123. void
  124. Record_DS::start ( nframes_t frame )
  125. {
  126. if ( _recording )
  127. {
  128. printf( "programming error: attempt to start recording while recording is still in progress\n" );
  129. return;
  130. }
  131. /* FIXME: safe to do this here? */
  132. flush( false );
  133. _frame = frame;
  134. _th->record( frame );
  135. run();
  136. _recording = true;
  137. }
  138. /** finalize the recording process. */
  139. void
  140. Record_DS::stop ( nframes_t frame )
  141. {
  142. if ( ! _recording )
  143. {
  144. printf( "programming error: attempt to stop recording when no recording is being made\n" );
  145. return;
  146. }
  147. _recording = false;
  148. /* FIXME: we may still have data in the buffers waiting to be
  149. * written to disk... We should flush it out before stopping... */
  150. _stop_frame = frame;
  151. shutdown();
  152. /* FIXME: flush buffers here? */
  153. _th->stop( frame );
  154. printf( "recording finished\n" );
  155. }
  156. /* THREAD: RT */
  157. /** read from the attached track's ports and stuff the ringbuffers */
  158. nframes_t
  159. Record_DS::process ( nframes_t nframes )
  160. {
  161. if ( ! _recording )
  162. return 0;
  163. const size_t block_size = nframes * sizeof( sample_t );
  164. // printf( "process: %lu %lu %lu\n", _frame, _frame + nframes, nframes );
  165. for ( int i = channels(); i--; )
  166. {
  167. void *buf = _th->input[ i ].buffer( nframes );
  168. if ( jack_ringbuffer_write( _rb[ i ], (char*)buf, block_size ) < block_size )
  169. {
  170. ++_xruns;
  171. printf( "RT: buffer overrun (disk can't keep up).\n" );
  172. memset( buf, 0, block_size );
  173. /* FIXME: we need to resync somehow */
  174. }
  175. }
  176. block_processed();
  177. /* FIXME: bogus */
  178. return nframes;
  179. }