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-19 06:16:21 +02:00
|
|
|
#include "Track.H"
|
|
|
|
#include "Audio_Sequence.H"
|
2008-04-07 12:00:16 +02:00
|
|
|
#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-16 07:03:10 +02:00
|
|
|
#include "dsp.h"
|
|
|
|
|
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
|
2008-04-16 02:08:48 +02:00
|
|
|
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 10:00:33 +02:00
|
|
|
/* FIXME: deal with (jack) buffer size changes */
|
|
|
|
/* FIXME: needs error handling everywhere! */
|
2008-04-16 02:08:48 +02:00
|
|
|
/* TODO: read/write data from/to disk in larger chunks to avoid
|
|
|
|
* excessive seeking. 256k is supposedly the sweetspot. */
|
2008-04-12 21:13:30 +02:00
|
|
|
|
2008-04-30 05:50:58 +02:00
|
|
|
float Disk_Stream::seconds_to_buffer = 5.0f;
|
|
|
|
//float Disk_Stream::seconds_to_buffer = 2.0f;
|
|
|
|
/* this is really only a rough estimate. The actual amount of data
|
|
|
|
read depends on many factors. Overlapping regions, for example, will
|
|
|
|
require more data to be read from disk, as will varying channel
|
|
|
|
counts.*/
|
|
|
|
size_t Disk_Stream::disk_io_kbytes = 256;
|
2008-04-07 10:00:33 +02:00
|
|
|
|
2008-04-19 06:16:21 +02:00
|
|
|
Disk_Stream::Disk_Stream ( Track *th, float frame_rate, nframes_t nframes, int channels ) : _th( th )
|
2008-04-07 10:00:33 +02:00
|
|
|
{
|
2008-04-30 05:50:58 +02:00
|
|
|
|
|
|
|
assert( channels );
|
|
|
|
|
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-30 06:42:28 +02:00
|
|
|
if ( disk_io_kbytes )
|
|
|
|
_disk_io_blocks = ( bufsize * channels ) / ( disk_io_kbytes * 1024 );
|
|
|
|
else
|
|
|
|
_disk_io_blocks = 1;
|
2008-04-30 05:50:58 +02:00
|
|
|
|
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
|
|
|
|
2008-04-16 10:44:31 +02:00
|
|
|
// run();
|
2008-04-07 10:00:33 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
/* 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-05-02 01:35:46 +02:00
|
|
|
|
|
|
|
/* THREAD: RT */
|
|
|
|
/** flush buffers and reset. Must only be called from the RT thread. */
|
|
|
|
void
|
|
|
|
Disk_Stream::flush ( bool is_output )
|
|
|
|
{
|
|
|
|
|
|
|
|
/* flush buffers */
|
|
|
|
for ( int i = channels(); i--; )
|
|
|
|
jack_ringbuffer_read_advance( _rb[ i ], jack_ringbuffer_read_space( _rb[ i ] ) );
|
|
|
|
|
|
|
|
|
|
|
|
/* sem_destroy( &_blocks ); */
|
|
|
|
|
|
|
|
/* if ( is_output ) */
|
|
|
|
/* sem_init( &_blocks, 0, _total_blocks ); */
|
|
|
|
/* else */
|
|
|
|
/* sem_init( &_blocks, 0, 0 ); */
|
|
|
|
|
|
|
|
if ( is_output )
|
|
|
|
{
|
|
|
|
|
|
|
|
int n;
|
|
|
|
sem_getvalue( &_blocks, &n );
|
|
|
|
|
|
|
|
n = _total_blocks - n;
|
|
|
|
|
|
|
|
while ( n-- )
|
|
|
|
sem_post( &_blocks );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sem_destroy( &_blocks );
|
|
|
|
|
|
|
|
sem_init( &_blocks, 0, 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-04-16 17:35:25 +02:00
|
|
|
/** stop the IO thread, block until it finishes. */
|
|
|
|
void
|
|
|
|
Disk_Stream::shutdown ( void )
|
|
|
|
{
|
|
|
|
_terminate = true;
|
2008-04-21 20:34:15 +02:00
|
|
|
|
|
|
|
/* try to wake the thread so it'll see that it's time to die */
|
|
|
|
block_processed();
|
|
|
|
|
|
|
|
if ( _thread )
|
|
|
|
pthread_join( _thread, NULL );
|
|
|
|
|
2008-04-19 21:24:26 +02:00
|
|
|
_terminate = false;
|
2008-04-16 17:35:25 +02:00
|
|
|
}
|
|
|
|
|
2008-04-19 06:16:21 +02:00
|
|
|
Audio_Sequence *
|
2008-04-07 12:00:16 +02:00
|
|
|
Disk_Stream::track ( void )
|
2008-04-07 10:12:01 +02:00
|
|
|
{
|
2008-04-19 06:16:21 +02:00
|
|
|
return (Audio_Sequence*)_th->track();
|
2008-04-07 10:12:01 +02:00
|
|
|
}
|
|
|
|
|
2008-04-07 09:29:30 +02:00
|
|
|
/** start Disk_Stream thread */
|
|
|
|
void
|
|
|
|
Disk_Stream::run ( void )
|
|
|
|
{
|
2008-04-16 10:44:31 +02:00
|
|
|
if ( pthread_create( &_thread, NULL, &Disk_Stream::disk_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! */;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-07 09:29:30 +02:00
|
|
|
/* static wrapper */
|
2008-04-07 12:00:16 +02:00
|
|
|
void *
|
2008-04-16 10:44:31 +02:00
|
|
|
Disk_Stream::disk_thread ( void *arg )
|
2008-04-07 09:29:30 +02:00
|
|
|
{
|
2008-04-16 10:44:31 +02:00
|
|
|
((Disk_Stream*)arg)->disk_thread();
|
2008-04-07 12:00:16 +02:00
|
|
|
|
|
|
|
return NULL;
|
2008-04-07 09:29:30 +02:00
|
|
|
}
|
|
|
|
|
2008-04-12 21:13:30 +02:00
|
|
|
int
|
2008-04-16 10:44:31 +02:00
|
|
|
Disk_Stream::buffer_percent ( void )
|
2008-04-12 21:13:30 +02:00
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
|
|
|
sem_getvalue( &_blocks, &n );
|
|
|
|
|
|
|
|
return 100 - (n * 100 / _total_blocks);
|
|
|
|
}
|