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.

221 lines
6.4KB

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