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.

180 lines
4.9KB

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