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.

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