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.

158 lines
4.5KB

  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[ i ] = 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. if ( track()->play( buf, _frame, _nframes, channels() ) )
  81. _frame += _nframes;
  82. else
  83. /* error */;
  84. }
  85. /* THREAD: IO */
  86. void
  87. Disk_Stream::io_thread ( void )
  88. {
  89. printf( "IO thread running...\n" );
  90. /* buffer to hold the interleaved data returned by the track reader */
  91. sample_t *buf = new sample_t[ _nframes * channels() ];
  92. /* buffer for a single channel */
  93. sample_t *cbuf = new sample_t[ _nframes ];
  94. const size_t block_size = _nframes * sizeof( sample_t );
  95. while ( wait_for_block() )
  96. {
  97. read_block( buf );
  98. /* deinterleave the buffer and stuff it into the per-channel ringbuffers */
  99. for ( int i = channels(); i--; )
  100. {
  101. int k = 0;
  102. for ( unsigned int j = i; j < _nframes; j += channels() )
  103. cbuf[ k++ ] = buf[ j ];
  104. jack_ringbuffer_write( _rb[ i ], (char*)cbuf, block_size );
  105. }
  106. }
  107. delete[] buf;
  108. delete[] cbuf;
  109. }
  110. /* THREAD: RT */
  111. /** take a block from the ringbuffers and send it out the track's
  112. * ports */
  113. void
  114. Disk_Stream::process ( void )
  115. {
  116. const size_t block_size = _nframes * sizeof( sample_t );
  117. for ( int i = channels(); i--; )
  118. {
  119. sample_t *buf = (_th->output)[ i ].buffer( _nframes );
  120. /* FIXME: handle underrun */
  121. jack_ringbuffer_read( _rb[ i ], (char*)buf, block_size );
  122. }
  123. block_processed();
  124. }