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.

235 lines
6.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"
  21. #include "Engine.H"
  22. #include "Audio_Sequence.H"
  23. #include "Track.H"
  24. #include "Port.H"
  25. #include "Record_DS.H"
  26. #include "dsp.h"
  27. /* THREAD: IO */
  28. /** write /nframes/ from buf to the capture file of the attached track */
  29. void
  30. Record_DS::write_block ( sample_t *buf, nframes_t nframes )
  31. {
  32. /* stupid chicken/egg */
  33. if ( ! ( timeline && track() ) )
  34. return;
  35. // timeline->wrlock();
  36. _th->write( buf, nframes );
  37. // track()->record( buf, _frame, nframes, channels() );
  38. // timeline->unlock();
  39. }
  40. /* THREAD: IO */
  41. void
  42. Record_DS::disk_thread ( void )
  43. {
  44. printf( "IO thread running...\n" );
  45. const nframes_t nframes = _nframes * _disk_io_blocks;
  46. /* buffer to hold the interleaved data returned by the track reader */
  47. sample_t *buf = new sample_t[ nframes * channels() ];
  48. sample_t *cbuf = new sample_t[ nframes ];
  49. const size_t block_size = nframes * sizeof( sample_t );
  50. int blocks_ready = 1;
  51. while ( wait_for_block() )
  52. {
  53. if ( blocks_ready < _disk_io_blocks )
  54. {
  55. ++blocks_ready;
  56. continue;
  57. }
  58. blocks_ready = 1;
  59. /* pull data from the per-channel ringbuffers and interlace it */
  60. for ( int i = channels(); i--; )
  61. {
  62. /* while ( jack_ringbuffer_read_space( _rb[ i ] ) < block_size ) */
  63. /* { */
  64. /* printf( "IO: disk buffer underrun!\n" ); */
  65. /* /\* FIXME: is this *really* the right thing to do? *\/ */
  66. /* usleep( 2000 ); */
  67. /* } */
  68. /* FIXME: avoid this copy */
  69. jack_ringbuffer_read( _rb[ i ], (char*)cbuf, block_size );
  70. buffer_interleave_one_channel( buf, cbuf, i, channels(), nframes );
  71. /* /\* deinterleave direcectly into the ringbuffer to avoid */
  72. /* * unnecessary copying *\/ */
  73. /* jack_ringbuffer_data_t rbd[2]; */
  74. /* jack_ringbuffer_get_write_vector( _rb[ i ], rbd ); */
  75. /* if ( rbd[ 0 ].len >= _nframes ) */
  76. /* /\* it'll all fit in one go *\/ */
  77. /* buffer_deinterleave_one_channel( (sample_t*)rbd[ 0 ].buf, buf, i, channels(), _nframes ); */
  78. /* else if ( rbd[ 1 ].len ) */
  79. /* { */
  80. /* /\* there's enough space in the ringbuffer, but it's not contiguous *\/ */
  81. /* /\* do the first half *\/ */
  82. /* const nframes_t f = rbd[ 1 ].len / sizeof( sample_t ); */
  83. /* buffer_deinterleave_one_channel( (sample_t*)rbd[ 0 ].buf, buf, i, channels(), f ); */
  84. /* assert( rbd[ 1 ].len >= (_nframes - f) * sizeof( sample_t ) ); */
  85. /* /\* do the second half *\/ */
  86. /* buffer_deinterleave_one_channel( (sample_t*)rbd[ 1 ].buf, buf + f, i, channels(), _nframes - f ); */
  87. /* } */
  88. /* else */
  89. /* printf( "programming error: expected more space in ringbuffer\n" ); */
  90. /* /\* buffer_deinterleave_one_channel( (sample_t*)rbd.buf, buf, i, channels(), _nframes ); *\/ */
  91. /* /\* jack_ringbuffer_write( _rb[ i ], (char*)cbuf, block_size ); *\/ */
  92. /* jack_ringbuffer_write_advance( _rb[ i ], _nframes * sizeof( sample_t ) ); */
  93. }
  94. write_block( buf, nframes );
  95. }
  96. printf( "IO thread terminating.\n" );
  97. delete[] cbuf;
  98. delete[] buf;
  99. }
  100. /** begin recording */
  101. /* FIXME: we need to make note of the exact frame we were on when recording began */
  102. void
  103. Record_DS::start ( nframes_t frame )
  104. {
  105. /* FIXME: flush buffers here? */
  106. if ( _recording )
  107. {
  108. printf( "programming error: attempt to start recording while recording is still in progress\n" );
  109. return;
  110. }
  111. _frame = frame;
  112. _th->record( frame );
  113. run();
  114. _recording = true;
  115. }
  116. /** finalize the recording process. */
  117. void
  118. Record_DS::stop ( nframes_t frame )
  119. {
  120. if ( ! _recording )
  121. {
  122. printf( "programming error: attempt to stop recording when no recording is being made\n" );
  123. return;
  124. }
  125. shutdown();
  126. /* FIXME: flush buffers here? */
  127. /* char *name = strdup( _af->name() ); */
  128. /* delete _af; */
  129. /* _af = NULL; */
  130. /* Audio_File *af = Audio_File::from_file( name ); */
  131. /* if ( ! af ) */
  132. /* printf( "impossible!\n" ); */
  133. /* new Region( af, track(), _frame ); */
  134. /* track()->redraw(); */
  135. _recording = false;
  136. _th->stop( frame );
  137. printf( "recording finished\n" );
  138. }
  139. /* THREAD: RT */
  140. /** read from the attached track's ports and stuff the ringbuffers */
  141. nframes_t
  142. Record_DS::process ( nframes_t nframes )
  143. {
  144. if ( ! _recording )
  145. return 0;
  146. const size_t block_size = nframes * sizeof( sample_t );
  147. // printf( "process: %lu %lu %lu\n", _frame, _frame + nframes, nframes );
  148. for ( int i = channels(); i--; )
  149. {
  150. void *buf = _th->input[ i ].buffer( nframes );
  151. if ( jack_ringbuffer_write( _rb[ i ], (char*)buf, block_size ) < block_size )
  152. {
  153. printf( "RT: buffer overrun (disk can't keep up).\n" );
  154. memset( buf, 0, block_size );
  155. /* FIXME: we need to resync somehow */
  156. }
  157. }
  158. block_processed();
  159. /* FIXME: bogus */
  160. return nframes;
  161. }