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.

319 lines
8.8KB

  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 = 0;
  66. while ( wait_for_block() )
  67. {
  68. if ( ++blocks_ready < _disk_io_blocks )
  69. continue;
  70. else
  71. blocks_ready = 0;
  72. /* pull data from the per-channel ringbuffers and interlace it */
  73. for ( int i = channels(); i--; )
  74. {
  75. #ifdef AVOID_UNNECESSARY_COPYING
  76. /* interleave direcectly from the ringbuffer to avoid
  77. * unnecessary copying */
  78. jack_ringbuffer_data_t rbd[2];
  79. memset( rbd, 0, sizeof( rbd ) );
  80. jack_ringbuffer_get_read_vector( _rb[ i ], rbd );
  81. if ( rbd[ 0 ].len >= block_size )
  82. {
  83. /* it'll all fit in one go */
  84. buffer_interleave_one_channel( buf, (sample_t*)rbd[ 0 ].buf, i, channels(), nframes );
  85. }
  86. else if ( rbd[ 0 ].len + rbd[ 1 ].len >= block_size )
  87. {
  88. /* there's enough space in the ringbuffer, but it's not contiguous */
  89. assert( ! ( rbd[ 0 ].len % sizeof( sample_t ) ) );
  90. const nframes_t f = rbd[ 0 ].len / sizeof( sample_t );
  91. /* do the first half */
  92. buffer_deinterleave_one_channel( (sample_t*)rbd[ 0 ].buf, buf, i, channels(), f );
  93. buffer_interleave_one_channel( buf, (sample_t*)rbd[ 0 ].buf, i, channels(), f );
  94. assert( rbd[ 1 ].len >= ( nframes - f ) * sizeof( sample_t ) );
  95. /* do the second half */
  96. buffer_interleave_one_channel( buf + f, (sample_t*)rbd[ 0 ].buf, i, channels(), nframes - f );
  97. }
  98. else
  99. ++_xruns;
  100. jack_ringbuffer_read_advance( _rb[ i ], block_size );
  101. #else
  102. if ( jack_ringbuffer_read( _rb[ i ], (char*)cbuf, block_size ) < block_size )
  103. ++_xruns;
  104. buffer_interleave_one_channel( buf, cbuf, i, channels(), nframes );
  105. #endif
  106. }
  107. write_block( buf, nframes );
  108. }
  109. DMESSAGE( "capture thread terminating" );
  110. /* flush what remains in the buffer out to disk */
  111. {
  112. /* use JACk sized blocks for this last bit */
  113. const nframes_t nframes = _nframes;
  114. const size_t block_size = _nframes * sizeof( sample_t );
  115. #ifdef AVOID_UNNECESSARY_COPYING
  116. sample_t *cbuf = new sample_t[ nframes ];
  117. #endif
  118. while ( blocks_ready-- > 0 || ( ! sem_trywait( &_blocks ) && errno != EAGAIN ) )
  119. {
  120. for ( int i = channels(); i--; )
  121. {
  122. jack_ringbuffer_read( _rb[ i ], (char*)cbuf, block_size );
  123. buffer_interleave_one_channel( buf, cbuf, i, channels(), nframes );
  124. }
  125. const nframes_t frames_remaining = (_stop_frame - _frame ) - _frames_written;
  126. if ( frames_remaining < nframes )
  127. {
  128. /* this is the last block, might be partial */
  129. write_block( buf, frames_remaining );
  130. break;
  131. }
  132. else
  133. write_block( buf, nframes );
  134. }
  135. #ifdef AVOID_UNNECESSARY_COPYING
  136. delete[] cbuf;
  137. #endif
  138. }
  139. delete[] buf;
  140. #ifndef AVOID_UNNECESSARY_COPYING
  141. delete[] cbuf;
  142. #endif
  143. DMESSAGE( "finalzing capture" );
  144. Track::Capture *c = _capture;
  145. _capture = NULL;
  146. /* now finalize the recording */
  147. track()->finalize( c, _stop_frame );
  148. delete c;
  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. DMESSAGE( "recording started at frame %lu", (unsigned long)frame);
  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. #include "../Transport.H"
  186. extern Transport *transport;
  187. /** read from the attached track's ports and stuff the ringbuffers */
  188. nframes_t
  189. Record_DS::process ( nframes_t nframes )
  190. {
  191. THREAD_ASSERT( RT );
  192. if ( ! _recording )
  193. return 0;
  194. if ( transport->frame < _frame )
  195. return 0;
  196. /* DMESSAGE( "recording actually happening at %lu (start frame %lu)", (unsigned long)transport->frame, (unsigned long)_frame); */
  197. nframes_t offset = 0;
  198. if ( _frame > transport->frame &&
  199. _frame < transport->frame + nframes )
  200. {
  201. /* The record start frame falls somewhere within the current
  202. buffer. We must discard the unneeded portion and only
  203. stuff the part requested into the ringbuffer. */
  204. offset = _frame - transport->frame;
  205. /* DMESSAGE( "offset = %lu", (unsigned long)offset ); */
  206. }
  207. const size_t offset_size = offset * sizeof( sample_t );
  208. const size_t block_size = ( nframes * sizeof( sample_t ) ) - offset_size;
  209. for ( int i = channels(); i--; )
  210. {
  211. /* read the entire input buffer */
  212. void *buf = track()->input[ i ].buffer( nframes );
  213. /* if ( buffer_is_digital_black( (sample_t*)buf, nframes ) ) */
  214. /* DWARNING( "recording an entirely blank buffer" ); */
  215. /* FIXME: this results in a ringbuffer size that is no longer
  216. necessarily a multiple of nframes... how will the other side
  217. handle that? */
  218. if ( jack_ringbuffer_write( _rb[ i ], (char*)buf + offset, block_size ) < block_size )
  219. {
  220. ++_xruns;
  221. memset( buf, 0, block_size );
  222. /* FIXME: we need to resync somehow */
  223. }
  224. }
  225. block_processed();
  226. /* FIXME: bogus */
  227. return nframes;
  228. }