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.

184 lines
5.0KB

  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. #include "Disk_Stream.H"
  19. #include "Track_Header.H"
  20. #include "Audio_Track.H"
  21. #include "Port.H"
  22. // float Disk_Stream::seconds_to_buffer = 5.0f;
  23. float Disk_Stream::seconds_to_buffer = 1.0f;
  24. /* A Disk_Stream uses a separate I/O thread to stream a track's
  25. regions from disk into a ringbuffer, to be processed by the RT
  26. thread (or vice-versa). */
  27. /* FIXME: handle termination of IO thread in destructor */
  28. /* FIXME: could all of this not simply be included in the Track_Header
  29. class? */
  30. /* FIXME: deal with (jack) buffer size changes */
  31. /* FIXME: can this be made to actually handle capture? */
  32. /* FIXME: needs error handling everywhere! */
  33. Disk_Stream::Disk_Stream ( Track_Header *th, float frame_rate, nframes_t nframes, int channels ) : _th( th )
  34. {
  35. _frame = 0;
  36. _thread = 0;
  37. printf( "nframes %lu\n", nframes );
  38. const int blocks = frame_rate * seconds_to_buffer / nframes;
  39. _nframes = nframes;
  40. size_t bufsize = blocks * nframes * sizeof( sample_t );
  41. for ( int i = channels; i--; )
  42. _rb.push_back( jack_ringbuffer_create( bufsize ) );
  43. sem_init( &_blocks, 0, blocks );
  44. run();
  45. }
  46. Disk_Stream::~Disk_Stream ( )
  47. {
  48. _th = NULL;
  49. sem_destroy( &_blocks );
  50. for ( int i = channels(); i--; )
  51. jack_ringbuffer_free( _rb[ i ] );
  52. }
  53. Audio_Track *
  54. Disk_Stream::track ( void )
  55. {
  56. return (Audio_Track*)_th->track();
  57. }
  58. /** start Disk_Stream thread */
  59. void
  60. Disk_Stream::run ( void )
  61. {
  62. if ( pthread_create( &_thread, NULL, &Disk_Stream::io_thread, this ) != 0 )
  63. /* error */;
  64. }
  65. /* void */
  66. /* DIsk_Stream::shutdown ( void ) */
  67. /* { */
  68. /* pthread_join( &_thread, NULL ); */
  69. /* } */
  70. /* static wrapper */
  71. void *
  72. Disk_Stream::io_thread ( void *arg )
  73. {
  74. ((Disk_Stream*)arg)->io_thread();
  75. return NULL;
  76. }
  77. /* THREAD: IO */
  78. /** read a block of data from the track into /buf/ */
  79. void
  80. Disk_Stream::read_block ( sample_t *buf )
  81. {
  82. /* stupid chicken/egg */
  83. if ( ! timeline )
  84. return;
  85. // printf( "IO: attempting to read block @ %lu\n", _frame );
  86. if ( ! track() )
  87. {
  88. // _frame += _nframes;
  89. return;
  90. }
  91. timeline->rdlock();
  92. if ( track()->play( buf, _frame, _nframes, channels() ) )
  93. _frame += _nframes;
  94. else
  95. /* error */;
  96. timeline->unlock();
  97. }
  98. /* THREAD: IO */
  99. void
  100. Disk_Stream::io_thread ( void )
  101. {
  102. printf( "IO thread running...\n" );
  103. /* buffer to hold the interleaved data returned by the track reader */
  104. sample_t *buf = new sample_t[ _nframes * channels() ];
  105. /* buffer for a single channel */
  106. sample_t *cbuf = new sample_t[ _nframes ];
  107. const size_t block_size = _nframes * sizeof( sample_t );
  108. while ( wait_for_block() )
  109. {
  110. // printf( "IO: RT thread is ready for more data...\n" );
  111. read_block( buf );
  112. /* deinterleave the buffer and stuff it into the per-channel ringbuffers */
  113. for ( int i = channels(); i--; )
  114. {
  115. int k = 0;
  116. for ( unsigned int j = i; k < _nframes; j += channels() )
  117. cbuf[ k++ ] = buf[ j ];
  118. jack_ringbuffer_write( _rb[ i ], (char*)cbuf, block_size );
  119. }
  120. }
  121. delete[] buf;
  122. delete[] cbuf;
  123. }
  124. /* THREAD: RT */
  125. /** take a block from the ringbuffers and send it out the track's
  126. * ports */
  127. nframes_t
  128. Disk_Stream::process ( nframes_t nframes )
  129. {
  130. const size_t block_size = _nframes * sizeof( sample_t );
  131. for ( int i = channels(); i--; )
  132. {
  133. void *buf = (_th->output)[ i ].buffer( _nframes );
  134. /* FIXME: handle underrun */
  135. if ( jack_ringbuffer_read( _rb[ i ], (char*)buf, block_size ) < block_size )
  136. printf( "disktream (rt): buffer underrun!\n" );
  137. }
  138. block_processed();
  139. /* FIXME: bogus */
  140. return nframes;
  141. }