2008-04-16 10:44:31 +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. */
|
|
|
|
/*******************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
/* Handles streaming regions from disk to track outputs. */
|
|
|
|
|
|
|
|
/* FIXME: we shouldn't depend on these */
|
2008-05-22 09:05:49 +02:00
|
|
|
#include "../Timeline.H" // for locking
|
|
|
|
#include "../Audio_Sequence.H"
|
|
|
|
#include "../Track.H"
|
|
|
|
|
2009-12-25 01:57:34 +01:00
|
|
|
// #include "Port.H"
|
2008-04-16 10:44:31 +02:00
|
|
|
#include "Playback_DS.H"
|
2008-05-22 09:05:49 +02:00
|
|
|
#include "Engine.H"
|
2008-04-16 10:44:31 +02:00
|
|
|
#include "dsp.h"
|
|
|
|
|
2008-07-30 04:30:45 +02:00
|
|
|
#include "const.h"
|
2010-02-01 07:25:16 +01:00
|
|
|
#include "debug.h"
|
|
|
|
#include "Thread.H"
|
2013-02-23 02:18:52 +01:00
|
|
|
#include <unistd.h>
|
2008-05-03 06:44:48 +02:00
|
|
|
|
2008-04-16 10:44:31 +02:00
|
|
|
bool
|
|
|
|
Playback_DS::seek_pending ( void )
|
|
|
|
{
|
2013-09-06 09:15:43 +02:00
|
|
|
return _pending_seek || buffer_percent() < 50;
|
2008-04-16 10:44:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** 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
|
|
|
|
Playback_DS::seek ( nframes_t frame )
|
|
|
|
{
|
2008-06-02 03:13:18 +02:00
|
|
|
THREAD_ASSERT( RT );
|
|
|
|
|
2009-01-02 08:20:45 +01:00
|
|
|
/* FIXME: non-RT-safe IO */
|
|
|
|
DMESSAGE( "requesting seek to frame %lu", (unsigned long)frame );
|
2008-04-16 10:44:31 +02:00
|
|
|
|
|
|
|
if ( seek_pending() )
|
|
|
|
printf( "seek error, attempt to seek while seek is pending\n" );
|
|
|
|
|
2013-09-06 09:15:43 +02:00
|
|
|
_seek_frame = frame;
|
|
|
|
_pending_seek = true;
|
2008-04-16 10:44:31 +02:00
|
|
|
|
2013-09-06 09:15:43 +02:00
|
|
|
/* wake the IO thread */
|
|
|
|
block_processed();
|
2008-04-16 10:44:31 +02:00
|
|
|
}
|
|
|
|
|
2009-01-19 04:25:28 +01:00
|
|
|
/** set the playback delay to /frames/ frames. This be called prior to
|
|
|
|
a seek. */
|
|
|
|
void
|
|
|
|
Playback_DS::delay ( nframes_t frames )
|
|
|
|
{
|
|
|
|
_delay = frames;
|
|
|
|
}
|
|
|
|
|
2008-04-16 10:44:31 +02:00
|
|
|
/** read /nframes/ from the attached track into /buf/ */
|
|
|
|
void
|
|
|
|
Playback_DS::read_block ( sample_t *buf, nframes_t nframes )
|
|
|
|
{
|
2008-06-02 03:13:18 +02:00
|
|
|
THREAD_ASSERT( Playback );
|
2008-04-16 10:44:31 +02:00
|
|
|
|
|
|
|
memset( buf, 0, nframes * sizeof( sample_t ) * channels() );
|
|
|
|
|
|
|
|
// printf( "IO: attempting to read block @ %lu\n", _frame );
|
|
|
|
|
2013-02-23 02:18:52 +01:00
|
|
|
if ( !timeline )
|
|
|
|
return;
|
2008-04-16 10:44:31 +02:00
|
|
|
|
2013-09-06 09:15:43 +02:00
|
|
|
while ( timeline->tryrdlock() )
|
2009-01-19 04:25:28 +01:00
|
|
|
{
|
2013-09-06 09:15:43 +02:00
|
|
|
if ( _terminate )
|
2013-02-23 02:18:52 +01:00
|
|
|
return;
|
2013-09-06 09:15:43 +02:00
|
|
|
|
2013-02-23 02:18:52 +01:00
|
|
|
usleep( 1000 * 10 );
|
2009-01-19 04:25:28 +01:00
|
|
|
}
|
2013-09-06 09:15:43 +02:00
|
|
|
|
|
|
|
if ( sequence() )
|
|
|
|
{
|
|
|
|
/* FIXME: how does this work if _delay is not a multiple of bufsize? */
|
|
|
|
|
|
|
|
if ( _frame >= _delay )
|
|
|
|
{
|
|
|
|
if ( ! sequence()->play( buf, _frame - _delay, nframes, channels() ) )
|
|
|
|
WARNING( "Programming error?" );
|
|
|
|
}
|
|
|
|
|
|
|
|
_frame += nframes;
|
|
|
|
}
|
|
|
|
|
|
|
|
timeline->unlock();
|
2008-04-16 10:44:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Playback_DS::disk_thread ( void )
|
|
|
|
{
|
2008-06-02 03:13:18 +02:00
|
|
|
_thread.name( "Playback" );
|
|
|
|
|
2008-05-03 06:44:48 +02:00
|
|
|
DMESSAGE( "playback thread running" );
|
2008-04-16 10:44:31 +02:00
|
|
|
|
|
|
|
/* buffer to hold the interleaved data returned by the track reader */
|
2013-08-14 07:48:42 +02:00
|
|
|
sample_t *buf = buffer_alloc( _nframes * channels() * _disk_io_blocks );
|
2013-09-06 09:15:43 +02:00
|
|
|
sample_t *cbuf = buffer_alloc( _nframes );
|
2008-04-16 10:44:31 +02:00
|
|
|
|
2013-09-06 09:15:43 +02:00
|
|
|
const nframes_t nframes = _nframes;
|
|
|
|
nframes_t blocks_written;
|
2008-05-02 02:42:01 +02:00
|
|
|
|
2013-09-06 09:15:43 +02:00
|
|
|
while ( ! _terminate )
|
2008-04-16 10:44:31 +02:00
|
|
|
{
|
|
|
|
|
2013-09-06 09:15:43 +02:00
|
|
|
seek:
|
2008-04-16 10:44:31 +02:00
|
|
|
|
2013-09-06 09:15:43 +02:00
|
|
|
blocks_written = 0;
|
|
|
|
read_block( buf, nframes * _disk_io_blocks );
|
2008-04-16 10:44:31 +02:00
|
|
|
|
2013-09-06 09:15:43 +02:00
|
|
|
while ( blocks_written < _disk_io_blocks &&
|
|
|
|
wait_for_block() )
|
2008-04-30 05:50:58 +02:00
|
|
|
{
|
2013-09-06 09:15:43 +02:00
|
|
|
// lock(); // for seeking
|
|
|
|
|
|
|
|
if ( _pending_seek )
|
|
|
|
{
|
|
|
|
/* FIXME: non-RT-safe IO */
|
|
|
|
DMESSAGE( "performing seek to frame %lu", (unsigned long)_seek_frame );
|
|
|
|
|
|
|
|
_frame = _seek_frame;
|
|
|
|
_pending_seek = false;
|
2008-04-16 10:44:31 +02:00
|
|
|
|
2013-09-06 09:15:43 +02:00
|
|
|
flush();
|
|
|
|
|
|
|
|
goto seek;
|
|
|
|
}
|
2013-02-23 02:18:52 +01:00
|
|
|
|
2013-09-06 09:15:43 +02:00
|
|
|
/* might have received terminate signal while waiting for block */
|
|
|
|
if ( _terminate )
|
|
|
|
goto done;
|
|
|
|
|
2008-04-16 10:44:31 +02:00
|
|
|
// unlock(); // for seeking
|
2013-09-06 09:15:43 +02:00
|
|
|
|
|
|
|
/* deinterleave the buffer and stuff it into the per-channel ringbuffers */
|
2008-04-16 10:44:31 +02:00
|
|
|
|
2013-09-06 09:15:43 +02:00
|
|
|
const size_t block_size = nframes * sizeof( sample_t );
|
2008-05-02 04:19:12 +02:00
|
|
|
|
2013-09-06 09:15:43 +02:00
|
|
|
for ( int i = 0; i < channels(); i++ )
|
|
|
|
{
|
|
|
|
buffer_deinterleave_one_channel( cbuf,
|
|
|
|
buf + ( blocks_written * nframes * channels() ),
|
|
|
|
i,
|
|
|
|
channels(),
|
|
|
|
nframes );
|
2008-05-02 04:19:12 +02:00
|
|
|
|
2013-09-06 09:15:43 +02:00
|
|
|
while ( jack_ringbuffer_write_space( _rb[ i ] ) < block_size )
|
|
|
|
usleep( 100 * 1000 );
|
2008-05-02 02:42:01 +02:00
|
|
|
|
2013-09-06 09:15:43 +02:00
|
|
|
jack_ringbuffer_write( _rb[ i ], ((char*)cbuf), block_size );
|
2008-05-02 04:19:12 +02:00
|
|
|
}
|
|
|
|
|
2013-09-06 09:15:43 +02:00
|
|
|
blocks_written++;
|
|
|
|
}
|
2008-04-16 10:44:31 +02:00
|
|
|
}
|
|
|
|
|
2013-02-23 02:18:52 +01:00
|
|
|
done:
|
|
|
|
|
2008-05-03 06:44:48 +02:00
|
|
|
DMESSAGE( "playback thread terminating" );
|
2008-04-16 10:44:31 +02:00
|
|
|
|
2013-08-14 07:48:42 +02:00
|
|
|
free(buf);
|
|
|
|
free(cbuf);
|
2008-05-28 03:45:46 +02:00
|
|
|
|
2013-09-07 04:12:40 +02:00
|
|
|
// flush();
|
|
|
|
|
2008-05-28 03:45:46 +02:00
|
|
|
_terminate = false;
|
2013-02-23 02:18:52 +01:00
|
|
|
|
|
|
|
_thread.exit();
|
2008-04-16 10:44:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** take a single block from the ringbuffers and send it out the
|
|
|
|
* attached track's ports */
|
|
|
|
nframes_t
|
|
|
|
Playback_DS::process ( nframes_t nframes )
|
|
|
|
{
|
2008-06-02 03:13:18 +02:00
|
|
|
THREAD_ASSERT( RT );
|
|
|
|
|
2008-04-16 10:44:31 +02:00
|
|
|
const size_t block_size = nframes * sizeof( sample_t );
|
|
|
|
|
|
|
|
// printf( "process: %lu %lu %lu\n", _frame, _frame + nframes, nframes );
|
|
|
|
|
|
|
|
for ( int i = channels(); i--; )
|
|
|
|
{
|
2008-05-07 18:18:57 +02:00
|
|
|
void *buf = track()->output[ i ].buffer( nframes );
|
2008-04-16 10:44:31 +02:00
|
|
|
|
2013-08-26 06:45:40 +02:00
|
|
|
if ( engine->freewheeling() )
|
|
|
|
{
|
2013-09-06 09:15:43 +02:00
|
|
|
/* only ever read nframes at a time */
|
|
|
|
while ( jack_ringbuffer_read_space( _rb[i] ) < block_size )
|
|
|
|
usleep( 10 * 1000 );
|
|
|
|
|
|
|
|
jack_ringbuffer_read( _rb[ i ], ((char*)buf), block_size );
|
2013-08-26 06:45:40 +02:00
|
|
|
}
|
|
|
|
else
|
2013-09-06 09:15:43 +02:00
|
|
|
{
|
|
|
|
/* only ever read nframes at a time */
|
|
|
|
if ( jack_ringbuffer_read_space( _rb[i] ) < block_size )
|
2013-08-26 06:45:40 +02:00
|
|
|
{
|
|
|
|
++_xruns;
|
|
|
|
memset( buf, 0, block_size );
|
|
|
|
/* FIXME: we need to resync somehow */
|
|
|
|
}
|
2013-09-06 09:15:43 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
jack_ringbuffer_read( _rb[ i ], (char*)buf, block_size );
|
|
|
|
}
|
2008-04-16 10:44:31 +02:00
|
|
|
}
|
|
|
|
|
2008-04-25 07:15:08 +02:00
|
|
|
/* TODO: figure out a way to stop IO while muted without losing sync */
|
2008-05-07 18:18:57 +02:00
|
|
|
if ( track()->mute() || ( Track::soloing() && ! track()->solo() ) )
|
2008-04-25 07:15:08 +02:00
|
|
|
buffer_fill_with_silence( (sample_t*)buf, nframes );
|
2008-04-16 10:44:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
block_processed();
|
|
|
|
|
|
|
|
/* FIXME: bogus */
|
|
|
|
return nframes;
|
|
|
|
}
|