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-12 21:13:30 +02:00
|
|
|
#include "Engine.H" // for locking.
|
2008-04-07 10:12:01 +02:00
|
|
|
|
2008-04-12 21:13:30 +02:00
|
|
|
/**********/
|
|
|
|
/* Engine */
|
|
|
|
/**********/
|
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
|
2008-04-12 21:13:30 +02:00
|
|
|
thread (or vice-versa). The I/O thread syncronizes access with the
|
|
|
|
user thread via the Timeline mutex. The size of the buffer (in
|
|
|
|
seconds) must be set before any Disk_Stream objects are created;
|
|
|
|
that is, at startup time. The default is 5 seconds, which may or
|
|
|
|
may not be excessive depending on various external factors. */
|
|
|
|
|
2008-04-07 09:29:30 +02:00
|
|
|
/* FIXME: handle termination of IO thread in destructor */
|
2008-04-07 10:00:33 +02:00
|
|
|
/* FIXME: deal with (jack) buffer size changes */
|
|
|
|
/* FIXME: needs error handling everywhere! */
|
2008-04-12 21:13:30 +02:00
|
|
|
/* TODO: handle capture too */
|
|
|
|
|
|
|
|
float Disk_Stream::seconds_to_buffer = 1.0f;
|
|
|
|
// size_t Disk_Stream::disk_block_frames = 2048;
|
2008-04-07 10:00:33 +02:00
|
|
|
|
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-12 21:13:30 +02:00
|
|
|
_terminate = false;
|
|
|
|
_pending_seek = -1;
|
2008-04-07 10:00:33 +02:00
|
|
|
|
2008-04-09 04:20:44 +02:00
|
|
|
printf( "nframes %lu\n", nframes );
|
|
|
|
|
2008-04-12 21:13:30 +02:00
|
|
|
_total_blocks = frame_rate * seconds_to_buffer / nframes;
|
2008-04-07 10:00:33 +02:00
|
|
|
|
|
|
|
_nframes = nframes;
|
|
|
|
|
2008-04-12 21:13:30 +02:00
|
|
|
size_t bufsize = _total_blocks * nframes * sizeof( sample_t );
|
2008-04-07 10:00:33 +02:00
|
|
|
|
2008-04-10 05:44:02 +02:00
|
|
|
/* const int blocks = 64; */
|
|
|
|
/* const size_t bufsize = (blocks * (nframes * sizeof( sample_t ))) + sizeof( sample_t ); */
|
|
|
|
|
2008-04-07 12:00:16 +02:00
|
|
|
for ( int i = channels; i--; )
|
2008-04-09 02:05:15 +02:00
|
|
|
_rb.push_back( jack_ringbuffer_create( bufsize ) );
|
2008-04-07 10:00:33 +02:00
|
|
|
|
2008-04-12 21:13:30 +02:00
|
|
|
sem_init( &_blocks, 0, _total_blocks );
|
2008-04-07 10:00:33 +02:00
|
|
|
|
|
|
|
run();
|
|
|
|
}
|
|
|
|
|
2008-04-07 12:00:16 +02:00
|
|
|
Disk_Stream::~Disk_Stream ( )
|
2008-04-07 10:00:33 +02:00
|
|
|
{
|
2008-04-12 21:13:30 +02:00
|
|
|
|
|
|
|
/* stop the IO thread */
|
|
|
|
_terminate = true;
|
|
|
|
pthread_join( _thread, NULL );
|
|
|
|
|
|
|
|
/* it isn't safe to do all this with the RT thread running */
|
|
|
|
engine->lock();
|
|
|
|
|
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-12 21:13:30 +02:00
|
|
|
|
|
|
|
engine->unlock();
|
2008-04-07 10:00:33 +02:00
|
|
|
}
|
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-12 21:13:30 +02:00
|
|
|
/* to be called when the JACK buffer size changes. */
|
|
|
|
void
|
|
|
|
Disk_Stream::resize ( nframes_t nframes )
|
|
|
|
{
|
|
|
|
if ( nframes != _nframes )
|
|
|
|
/* FIXME: to something here! */;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Disk_Stream::seek_pending ( void )
|
|
|
|
{
|
|
|
|
return _pending_seek != (nframes_t)-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* THREAD: RT */
|
|
|
|
/** request that the IO thread perform a seek and rebuffer. This is
|
|
|
|
called for each Disk_Stream whenever the RT thread determines that
|
|
|
|
the transport has jumped to a new position. This is called *before*
|
|
|
|
process. */
|
|
|
|
void
|
|
|
|
Disk_Stream::seek ( nframes_t frame )
|
|
|
|
{
|
|
|
|
printf( "requesting seek\n" );
|
|
|
|
|
|
|
|
if ( seek_pending() )
|
|
|
|
printf( "seek error, attempt to seek while seek is pending\n" );
|
|
|
|
|
|
|
|
_pending_seek = frame;
|
|
|
|
|
|
|
|
/* flush buffers */
|
|
|
|
for ( int i = channels(); i--; )
|
|
|
|
jack_ringbuffer_read_advance( _rb[ i ], jack_ringbuffer_read_space( _rb[ i ] ) );
|
|
|
|
|
|
|
|
/* dirty hack... reset the semaphore. Should we just call sem_init
|
|
|
|
* again instead? */
|
|
|
|
|
|
|
|
/* sem_init( &_blocks, 0, _total_blocks ); */
|
|
|
|
|
|
|
|
int n;
|
|
|
|
sem_getvalue( &_blocks, &n );
|
|
|
|
|
|
|
|
n = _total_blocks - n;
|
|
|
|
|
|
|
|
while ( n-- )
|
|
|
|
sem_post( &_blocks );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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 */
|
2008-04-12 21:13:30 +02:00
|
|
|
/** read /nframes/ from the attached track into /buf/ */
|
2008-04-07 10:00:33 +02:00
|
|
|
void
|
2008-04-12 21:13:30 +02:00
|
|
|
Disk_Stream::read_block ( sample_t *buf, nframes_t nframes )
|
2008-04-07 09:29:30 +02:00
|
|
|
{
|
2008-04-09 02:05:15 +02:00
|
|
|
|
2008-04-13 00:55:25 +02:00
|
|
|
memset( buf, 0, nframes * sizeof( sample_t ) * channels() );
|
|
|
|
|
2008-04-09 02:05:15 +02:00
|
|
|
/* stupid chicken/egg */
|
|
|
|
if ( ! timeline )
|
|
|
|
return;
|
|
|
|
|
2008-04-09 04:20:44 +02:00
|
|
|
// printf( "IO: attempting to read block @ %lu\n", _frame );
|
2008-04-09 02:05:15 +02:00
|
|
|
|
|
|
|
if ( ! track() )
|
|
|
|
{
|
|
|
|
// _frame += _nframes;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
timeline->rdlock();
|
|
|
|
|
2008-04-12 21:13:30 +02:00
|
|
|
if ( track()->play( buf, _frame, nframes, channels() ) )
|
|
|
|
_frame += nframes;
|
2008-04-07 09:29:30 +02:00
|
|
|
else
|
|
|
|
/* error */;
|
2008-04-09 02:05:15 +02:00
|
|
|
|
|
|
|
timeline->unlock();
|
2008-04-07 09:29:30 +02:00
|
|
|
}
|
|
|
|
|
2008-04-12 21:13:30 +02:00
|
|
|
int
|
|
|
|
Disk_Stream::output_buffer_percent ( void )
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
|
|
|
sem_getvalue( &_blocks, &n );
|
|
|
|
|
|
|
|
return 100 - (n * 100 / _total_blocks);
|
|
|
|
}
|
|
|
|
|
2008-04-07 09:29:30 +02:00
|
|
|
/* 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() )
|
|
|
|
{
|
2008-04-09 02:05:15 +02:00
|
|
|
// printf( "IO: RT thread is ready for more data...\n" );
|
|
|
|
|
2008-04-13 00:55:25 +02:00
|
|
|
// printf( "IO: disk buffer is %3d%% full\r", output_buffer_percent() );
|
2008-04-12 21:13:30 +02:00
|
|
|
|
|
|
|
// lock(); // for seeking
|
|
|
|
|
|
|
|
if ( seek_pending() )
|
|
|
|
{
|
|
|
|
printf( "performing seek\n" );
|
|
|
|
_frame = _pending_seek;
|
|
|
|
_pending_seek = -1;
|
|
|
|
/* finish flushing the buffer */
|
|
|
|
|
|
|
|
/* for ( int i = channels(); i-- ) */
|
|
|
|
/* jack_ringbuffer_write_advance( _rb[ i ], jack_ringbuffer_write_space( _rb[ i ] ) ); */
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: should we not read from disk in larger-than-JACK-buffer blocks? */
|
|
|
|
read_block( buf, _nframes );
|
|
|
|
|
|
|
|
// unlock(); // for seeking
|
2008-04-07 09:29:30 +02:00
|
|
|
|
|
|
|
/* 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-09 04:20:44 +02:00
|
|
|
for ( unsigned int j = i; k < _nframes; j += channels() )
|
2008-04-07 09:29:30 +02:00
|
|
|
cbuf[ k++ ] = buf[ j ];
|
|
|
|
|
2008-04-10 05:44:02 +02:00
|
|
|
while ( jack_ringbuffer_write_space( _rb[ i ] ) < block_size )
|
|
|
|
{
|
|
|
|
printf( "IO: disk buffer overrun!\n" );
|
2008-04-12 21:13:30 +02:00
|
|
|
/* FIXME: is this *really* the right thing to do? */
|
2008-04-10 05:44:02 +02:00
|
|
|
usleep( 2000 );
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-12 21:13:30 +02:00
|
|
|
printf( "IO thread terminating.\n" );
|
|
|
|
|
2008-04-07 09:29:30 +02:00
|
|
|
delete[] buf;
|
|
|
|
delete[] cbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* THREAD: RT */
|
2008-04-12 21:13:30 +02:00
|
|
|
/** take a single block from the ringbuffers and send it out the
|
|
|
|
* attached track's ports */
|
2008-04-09 02:05:15 +02:00
|
|
|
nframes_t
|
|
|
|
Disk_Stream::process ( nframes_t nframes )
|
2008-04-07 09:29:30 +02:00
|
|
|
{
|
2008-04-10 05:44:02 +02:00
|
|
|
const size_t block_size = nframes * sizeof( sample_t );
|
|
|
|
|
|
|
|
// printf( "process: %lu %lu %lu\n", _frame, _frame + nframes, nframes );
|
2008-04-07 10:00:33 +02:00
|
|
|
|
2008-04-07 12:00:16 +02:00
|
|
|
for ( int i = channels(); i--; )
|
2008-04-07 10:00:33 +02:00
|
|
|
{
|
2008-04-10 05:44:02 +02:00
|
|
|
|
|
|
|
void *buf = _th->output[ i ].buffer( nframes );
|
2008-04-07 10:00:33 +02:00
|
|
|
|
2008-04-09 04:20:44 +02:00
|
|
|
if ( jack_ringbuffer_read( _rb[ i ], (char*)buf, block_size ) < block_size )
|
2008-04-10 05:44:02 +02:00
|
|
|
{
|
|
|
|
printf( "RT: buffer underrun (disk can't keep up).\n" );
|
|
|
|
memset( buf, 0, block_size );
|
2008-04-12 21:13:30 +02:00
|
|
|
/* FIXME: we need to resync somehow */
|
2008-04-10 05:44:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* /\* testing. *\/ */
|
|
|
|
/* FILE *fp = fopen( "testing.au", "a" ); */
|
|
|
|
/* fwrite( buf, block_size, 1, fp ); */
|
|
|
|
/* fclose( fp ); */
|
|
|
|
|
2008-04-07 10:00:33 +02:00
|
|
|
}
|
2008-04-07 09:29:30 +02:00
|
|
|
|
|
|
|
block_processed();
|
2008-04-09 02:05:15 +02:00
|
|
|
|
|
|
|
/* FIXME: bogus */
|
|
|
|
return nframes;
|
2008-04-07 09:29:30 +02:00
|
|
|
}
|