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.

290 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. Track::Capture *c = _capture;
  146. _capture = NULL;
  147. /* now finalize the recording */
  148. track()->finalize( c, _stop_frame );
  149. delete c;
  150. _terminate = false;
  151. DMESSAGE( "capture thread gone" );
  152. }
  153. /** begin recording */
  154. void
  155. Record_DS::start ( nframes_t frame )
  156. {
  157. THREAD_ASSERT( UI );
  158. if ( _recording )
  159. {
  160. WARNING( "programming error: attempt to start recording while recording is still in progress" );
  161. return;
  162. }
  163. /* /\* FIXME: safe to do this here? *\/ */
  164. /* flush(); */
  165. _frame = frame;
  166. _capture = new Track::Capture;
  167. run();
  168. _recording = true;
  169. }
  170. /** finalize the recording process. */
  171. void
  172. Record_DS::stop ( nframes_t frame )
  173. {
  174. THREAD_ASSERT( UI );
  175. if ( ! _recording )
  176. {
  177. WARNING( "programming error: attempt to stop recording when no recording is being made" );
  178. return;
  179. }
  180. _recording = false;
  181. _stop_frame = frame;
  182. detach();
  183. DMESSAGE( "recording finished" );
  184. }
  185. /** read from the attached track's ports and stuff the ringbuffers */
  186. nframes_t
  187. Record_DS::process ( nframes_t nframes )
  188. {
  189. THREAD_ASSERT( RT );
  190. if ( ! _recording )
  191. return 0;
  192. const size_t block_size = nframes * sizeof( sample_t );
  193. for ( int i = channels(); i--; )
  194. {
  195. void *buf = track()->input[ i ].buffer( nframes );
  196. if ( jack_ringbuffer_write( _rb[ i ], (char*)buf, block_size ) < block_size )
  197. {
  198. ++_xruns;
  199. memset( buf, 0, block_size );
  200. /* FIXME: we need to resync somehow */
  201. }
  202. }
  203. block_processed();
  204. /* FIXME: bogus */
  205. return nframes;
  206. }