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.

282 lines
7.5KB

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