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.

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