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.

287 lines
7.6KB

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