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.

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