non/Timeline/Disk_Stream.C

158 lines
4.5 KiB
C++
Raw Normal View History

2008-04-07 09:29:30 +02:00
/*******************************************************************************/
/* Copyright (C) 2008 Jonathan Moore Liles */
/* */
/* This program is free software; you can redistribute it and/or modify it */
/* under the terms of the GNU General Public License as published by the */
/* Free Software Foundation; either version 2 of the License, or (at your */
/* option) any later version. */
/* */
/* This program is distributed in the hope that it will be useful, but WITHOUT */
/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for */
/* more details. */
/* */
/* You should have received a copy of the GNU General Public License along */
/* with This program; see the file COPYING. If not,write to the Free Software */
/* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
/*******************************************************************************/
2008-04-07 12:00:16 +02:00
#include "Disk_Stream.H"
2008-04-07 10:12:01 +02:00
#include "Track_Header.H"
2008-04-07 12:00:16 +02:00
#include "Audio_Track.H"
#include "Port.H"
2008-04-07 10:12:01 +02:00
2008-04-07 12:00:16 +02:00
float Disk_Stream::seconds_to_buffer = 5.0f;
2008-04-07 09:29:30 +02:00
/* A Disk_Stream uses a separate I/O thread to stream a track's
regions from disk into a ringbuffer, to be processed by the RT
thread (or vice-versa). */
/* FIXME: handle termination of IO thread in destructor */
/* FIXME: could all of this not simply be included in the Track_Header
2008-04-07 10:00:33 +02:00
class? */
/* FIXME: deal with (jack) buffer size changes */
/* FIXME: can this be made to actually handle capture? */
/* FIXME: needs error handling everywhere! */
2008-04-07 12:00:16 +02:00
Disk_Stream::Disk_Stream ( Track_Header *th, float frame_rate, nframes_t nframes, int channels ) : _th( th )
2008-04-07 10:00:33 +02:00
{
_frame = 0;
2008-04-07 12:00:16 +02:00
_thread = 0;
2008-04-07 10:00:33 +02:00
const int blocks = frame_rate * seconds_to_buffer / nframes;
_nframes = nframes;
size_t bufsize = blocks * nframes * sizeof( sample_t );
2008-04-07 12:00:16 +02:00
for ( int i = channels; i--; )
2008-04-07 10:00:33 +02:00
_rb[ i ] = jack_ringbuffer_create( bufsize );
sem_init( &_blocks, 0, blocks );
run();
}
2008-04-07 12:00:16 +02:00
Disk_Stream::~Disk_Stream ( )
2008-04-07 10:00:33 +02:00
{
_th = NULL;
sem_destroy( &_blocks );
2008-04-07 12:00:16 +02:00
for ( int i = channels(); i--; )
2008-04-07 10:00:33 +02:00
jack_ringbuffer_free( _rb[ i ] );
}
2008-04-07 09:29:30 +02:00
2008-04-07 10:12:01 +02:00
Audio_Track *
2008-04-07 12:00:16 +02:00
Disk_Stream::track ( void )
2008-04-07 10:12:01 +02:00
{
return (Audio_Track*)_th->track();
}
2008-04-07 09:29:30 +02:00
/** start Disk_Stream thread */
void
Disk_Stream::run ( void )
{
2008-04-07 12:00:16 +02:00
if ( pthread_create( &_thread, NULL, &Disk_Stream::io_thread, this ) != 0 )
2008-04-07 09:29:30 +02:00
/* error */;
}
2008-04-07 12:00:16 +02:00
/* void */
/* DIsk_Stream::shutdown ( void ) */
/* { */
/* pthread_join( &_thread, NULL ); */
/* } */
2008-04-07 09:29:30 +02:00
/* static wrapper */
2008-04-07 12:00:16 +02:00
void *
2008-04-07 09:29:30 +02:00
Disk_Stream::io_thread ( void *arg )
{
((Disk_Stream*)arg)->io_thread();
2008-04-07 12:00:16 +02:00
return NULL;
2008-04-07 09:29:30 +02:00
}
/* THREAD: IO */
/** read a block of data from the track into /buf/ */
2008-04-07 10:00:33 +02:00
void
2008-04-07 09:29:30 +02:00
Disk_Stream::read_block ( sample_t *buf )
{
2008-04-07 10:12:01 +02:00
if ( track()->play( buf, _frame, _nframes, channels() ) )
2008-04-07 10:00:33 +02:00
_frame += _nframes;
2008-04-07 09:29:30 +02:00
else
/* error */;
}
/* THREAD: IO */
void
Disk_Stream::io_thread ( void )
{
printf( "IO thread running...\n" );
/* buffer to hold the interleaved data returned by the track reader */
sample_t *buf = new sample_t[ _nframes * channels() ];
/* buffer for a single channel */
sample_t *cbuf = new sample_t[ _nframes ];
const size_t block_size = _nframes * sizeof( sample_t );
while ( wait_for_block() )
{
read_block( buf );
/* deinterleave the buffer and stuff it into the per-channel ringbuffers */
2008-04-07 12:00:16 +02:00
for ( int i = channels(); i--; )
2008-04-07 09:29:30 +02:00
{
int k = 0;
2008-04-07 12:00:16 +02:00
for ( unsigned int j = i; j < _nframes; j += channels() )
2008-04-07 09:29:30 +02:00
cbuf[ k++ ] = buf[ j ];
2008-04-07 12:00:16 +02:00
jack_ringbuffer_write( _rb[ i ], (char*)cbuf, block_size );
2008-04-07 09:29:30 +02:00
}
}
delete[] buf;
delete[] cbuf;
}
/* THREAD: RT */
2008-04-07 10:00:33 +02:00
/** take a block from the ringbuffers and send it out the track's
* ports */
2008-04-07 09:29:30 +02:00
void
2008-04-07 12:00:16 +02:00
Disk_Stream::process ( void )
2008-04-07 09:29:30 +02:00
{
2008-04-07 10:00:33 +02:00
const size_t block_size = _nframes * sizeof( sample_t );
2008-04-07 12:00:16 +02:00
for ( int i = channels(); i--; )
2008-04-07 10:00:33 +02:00
{
2008-04-07 12:00:16 +02:00
sample_t *buf = (_th->output)[ i ].buffer( _nframes );
2008-04-07 10:00:33 +02:00
2008-04-07 12:00:16 +02:00
/* FIXME: handle underrun */
jack_ringbuffer_read( _rb[ i ], (char*)buf, block_size );
2008-04-07 10:00:33 +02:00
}
2008-04-07 09:29:30 +02:00
block_processed();
}