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.

145 lines
4.2KB

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