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 */
|
|
|
|
#include "Timeline.H"
|
|
|
|
#include "Engine.H"
|
2008-04-19 06:16:21 +02:00
|
|
|
#include "Audio_Sequence.H"
|
|
|
|
#include "Track.H"
|
2008-04-16 10:44:31 +02:00
|
|
|
#include "Port.H"
|
|
|
|
#include "Playback_DS.H"
|
|
|
|
|
|
|
|
|
|
|
|
#include "dsp.h"
|
|
|
|
|
2008-05-03 06:44:48 +02:00
|
|
|
#include "debug.h"
|
|
|
|
|
2008-04-16 10:44:31 +02:00
|
|
|
bool
|
|
|
|
Playback_DS::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
|
|
|
|
Playback_DS::seek ( nframes_t frame )
|
|
|
|
{
|
2008-05-03 06:44:48 +02:00
|
|
|
DMESSAGE( "requesting seek" );
|
2008-04-16 10:44:31 +02:00
|
|
|
|
|
|
|
if ( seek_pending() )
|
|
|
|
printf( "seek error, attempt to seek while seek is pending\n" );
|
|
|
|
|
|
|
|
_pending_seek = frame;
|
|
|
|
|
2008-05-20 06:30:08 +02:00
|
|
|
flush();
|
2008-04-16 10:44:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* THREAD: IO */
|
|
|
|
/** read /nframes/ from the attached track into /buf/ */
|
|
|
|
void
|
|
|
|
Playback_DS::read_block ( sample_t *buf, nframes_t nframes )
|
|
|
|
{
|
|
|
|
|
|
|
|
memset( buf, 0, nframes * sizeof( sample_t ) * channels() );
|
|
|
|
|
|
|
|
/* stupid chicken/egg */
|
|
|
|
if ( ! timeline )
|
|
|
|
return;
|
|
|
|
|
|
|
|
// printf( "IO: attempting to read block @ %lu\n", _frame );
|
|
|
|
|
2008-05-07 18:18:57 +02:00
|
|
|
if ( ! sequence() )
|
2008-04-16 10:44:31 +02:00
|
|
|
{
|
|
|
|
// _frame += _nframes;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
timeline->rdlock();
|
|
|
|
|
2008-05-07 18:18:57 +02:00
|
|
|
if ( sequence()->play( buf, _frame, nframes, channels() ) )
|
2008-04-16 10:44:31 +02:00
|
|
|
_frame += nframes;
|
|
|
|
else
|
2008-05-19 05:01:09 +02:00
|
|
|
WARNING( "Programming error?" );
|
2008-04-16 10:44:31 +02:00
|
|
|
|
|
|
|
timeline->unlock();
|
|
|
|
}
|
|
|
|
|
2008-05-02 04:19:12 +02:00
|
|
|
#define AVOID_UNNECESSARY_COPYING 1
|
|
|
|
|
2008-04-16 10:44:31 +02:00
|
|
|
/* THREAD: IO */
|
|
|
|
void
|
|
|
|
Playback_DS::disk_thread ( void )
|
|
|
|
{
|
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 */
|
2008-04-30 05:50:58 +02:00
|
|
|
sample_t *buf = new sample_t[ _nframes * channels() * _disk_io_blocks ];
|
2008-05-02 04:19:12 +02:00
|
|
|
#ifndef AVOID_UNNECESSARY_COPYING
|
|
|
|
sample_t *cbuf = new sample_t[ _nframes * _disk_io_blocks ];
|
|
|
|
#endif
|
2008-04-16 10:44:31 +02:00
|
|
|
|
2008-04-30 05:50:58 +02:00
|
|
|
int blocks_ready = 1;
|
2008-04-16 10:44:31 +02:00
|
|
|
|
2008-05-02 02:42:01 +02:00
|
|
|
const nframes_t nframes = _nframes * _disk_io_blocks;
|
|
|
|
|
2008-04-16 10:44:31 +02:00
|
|
|
while ( wait_for_block() )
|
|
|
|
{
|
|
|
|
|
|
|
|
// lock(); // for seeking
|
|
|
|
|
|
|
|
if ( seek_pending() )
|
|
|
|
{
|
2008-05-03 06:44:48 +02:00
|
|
|
DMESSAGE( "performing seek" );
|
|
|
|
|
2008-04-16 10:44:31 +02:00
|
|
|
_frame = _pending_seek;
|
|
|
|
_pending_seek = -1;
|
2008-04-30 05:50:58 +02:00
|
|
|
blocks_ready = 1;
|
2008-04-16 10:44:31 +02:00
|
|
|
/* finish flushing the buffer */
|
|
|
|
|
|
|
|
/* for ( int i = channels(); i-- ) */
|
|
|
|
/* jack_ringbuffer_write_advance( _rb[ i ], jack_ringbuffer_write_space( _rb[ i ] ) ); */
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-04-30 05:50:58 +02:00
|
|
|
if ( blocks_ready < _disk_io_blocks )
|
|
|
|
{
|
|
|
|
++blocks_ready;
|
|
|
|
/* wait for more space */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* reset */
|
|
|
|
blocks_ready = 1;
|
|
|
|
|
|
|
|
read_block( buf, nframes );
|
2008-04-16 10:44:31 +02:00
|
|
|
|
|
|
|
// unlock(); // for seeking
|
|
|
|
|
|
|
|
/* deinterleave the buffer and stuff it into the per-channel ringbuffers */
|
|
|
|
|
2008-05-02 04:19:12 +02:00
|
|
|
const size_t block_size = nframes * sizeof( sample_t );
|
|
|
|
|
2008-04-16 10:44:31 +02:00
|
|
|
for ( int i = channels(); i--; )
|
|
|
|
{
|
2008-05-02 04:19:12 +02:00
|
|
|
|
|
|
|
#ifdef AVOID_UNNECESSARY_COPYING
|
2008-04-16 10:44:31 +02:00
|
|
|
/* deinterleave direcectly into the ringbuffer to avoid
|
|
|
|
* unnecessary copying */
|
|
|
|
|
|
|
|
jack_ringbuffer_data_t rbd[2];
|
|
|
|
|
2008-05-02 04:19:12 +02:00
|
|
|
memset( rbd, 0, sizeof( rbd ) );
|
2008-04-16 10:44:31 +02:00
|
|
|
|
2008-05-02 04:19:12 +02:00
|
|
|
jack_ringbuffer_get_write_vector( _rb[ i ], rbd );
|
2008-05-02 02:42:01 +02:00
|
|
|
|
|
|
|
if ( rbd[ 0 ].len >= block_size )
|
2008-05-02 04:19:12 +02:00
|
|
|
{
|
2008-04-16 10:44:31 +02:00
|
|
|
/* it'll all fit in one go */
|
2008-04-30 05:50:58 +02:00
|
|
|
buffer_deinterleave_one_channel( (sample_t*)rbd[ 0 ].buf, buf, i, channels(), nframes );
|
2008-05-02 04:19:12 +02:00
|
|
|
}
|
2008-05-02 02:42:01 +02:00
|
|
|
else if ( rbd[ 0 ].len + rbd[ 1 ].len >= block_size )
|
2008-04-16 10:44:31 +02:00
|
|
|
{
|
|
|
|
/* there's enough space in the ringbuffer, but it's not contiguous */
|
|
|
|
|
2008-05-02 04:19:12 +02:00
|
|
|
assert( ! ( rbd[ 0 ].len % sizeof( sample_t ) ) );
|
|
|
|
// assert( ! ( rbd[ 1 ].len % sizeof( sample_t ) ) );
|
|
|
|
|
2008-04-30 05:50:58 +02:00
|
|
|
const nframes_t f = rbd[ 0 ].len / sizeof( sample_t );
|
2008-04-16 10:44:31 +02:00
|
|
|
|
2008-05-02 02:42:01 +02:00
|
|
|
/* do the first half */
|
2008-04-16 10:44:31 +02:00
|
|
|
buffer_deinterleave_one_channel( (sample_t*)rbd[ 0 ].buf, buf, i, channels(), f );
|
|
|
|
|
2008-05-02 04:19:12 +02:00
|
|
|
assert( rbd[ 1 ].len >= ( nframes - f ) * sizeof( sample_t ) );
|
|
|
|
|
2008-04-16 10:44:31 +02:00
|
|
|
/* do the second half */
|
2008-04-30 05:50:58 +02:00
|
|
|
buffer_deinterleave_one_channel( (sample_t*)rbd[ 1 ].buf, buf + f, i, channels(), nframes - f );
|
2008-04-16 10:44:31 +02:00
|
|
|
}
|
|
|
|
else
|
2008-05-01 13:35:42 +02:00
|
|
|
++_xruns;
|
2008-04-16 10:44:31 +02:00
|
|
|
|
2008-05-02 02:42:01 +02:00
|
|
|
jack_ringbuffer_write_advance( _rb[ i ], block_size );
|
2008-05-02 04:19:12 +02:00
|
|
|
#else
|
|
|
|
buffer_deinterleave_one_channel( cbuf, buf, i, channels(), nframes );
|
2008-05-02 06:12:51 +02:00
|
|
|
|
|
|
|
if ( jack_ringbuffer_write( _rb[ i ], (char*)cbuf, block_size ) < block_size )
|
|
|
|
++_xruns;
|
2008-05-02 04:19:12 +02:00
|
|
|
#endif
|
2008-04-16 10:44:31 +02:00
|
|
|
}
|
2008-05-02 04:19:12 +02:00
|
|
|
|
2008-04-16 10:44:31 +02:00
|
|
|
}
|
|
|
|
|
2008-05-03 06:44:48 +02:00
|
|
|
DMESSAGE( "playback thread terminating" );
|
2008-04-16 10:44:31 +02:00
|
|
|
|
|
|
|
delete[] buf;
|
2008-05-02 04:19:12 +02:00
|
|
|
#ifndef AVOID_UNNECESSARY_COPYING
|
|
|
|
delete[] cbuf;
|
|
|
|
#endif
|
2008-04-16 10:44:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* THREAD: RT */
|
|
|
|
/** take a single block from the ringbuffers and send it out the
|
|
|
|
* attached track's ports */
|
|
|
|
nframes_t
|
|
|
|
Playback_DS::process ( nframes_t nframes )
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
if ( jack_ringbuffer_read( _rb[ i ], (char*)buf, block_size ) < block_size )
|
|
|
|
{
|
2008-05-01 13:35:42 +02:00
|
|
|
++_xruns;
|
2008-04-16 10:44:31 +02:00
|
|
|
memset( buf, 0, block_size );
|
|
|
|
/* FIXME: we need to resync somehow */
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|