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 from track inputs to disk */
|
|
|
|
|
|
|
|
/* 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 "Record_DS.H"
|
|
|
|
|
|
|
|
#include "dsp.h"
|
|
|
|
|
|
|
|
/* THREAD: IO */
|
|
|
|
/** write /nframes/ from buf to the capture file of the attached track */
|
|
|
|
void
|
|
|
|
Record_DS::write_block ( sample_t *buf, nframes_t nframes )
|
|
|
|
{
|
|
|
|
|
|
|
|
/* stupid chicken/egg */
|
|
|
|
if ( ! ( timeline && track() ) )
|
|
|
|
return;
|
|
|
|
|
|
|
|
// timeline->wrlock();
|
|
|
|
|
2008-04-18 02:26:46 +02:00
|
|
|
_th->write( buf, nframes );
|
2008-04-16 10:44:31 +02:00
|
|
|
|
|
|
|
// track()->record( buf, _frame, nframes, channels() );
|
|
|
|
|
|
|
|
// timeline->unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* THREAD: IO */
|
|
|
|
void
|
|
|
|
Record_DS::disk_thread ( void )
|
|
|
|
{
|
|
|
|
|
|
|
|
printf( "IO thread running...\n" );
|
|
|
|
|
2008-04-30 05:50:58 +02:00
|
|
|
const nframes_t nframes = _nframes * _disk_io_blocks;
|
|
|
|
|
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() ];
|
|
|
|
sample_t *cbuf = new sample_t[ nframes ];
|
|
|
|
|
|
|
|
const size_t block_size = nframes * sizeof( sample_t );
|
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
|
|
|
|
|
|
|
while ( wait_for_block() )
|
|
|
|
{
|
|
|
|
|
2008-04-30 05:50:58 +02:00
|
|
|
if ( blocks_ready < _disk_io_blocks )
|
|
|
|
{
|
|
|
|
++blocks_ready;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
blocks_ready = 1;
|
|
|
|
|
|
|
|
|
|
|
|
/* pull data from the per-channel ringbuffers and interlace it */
|
2008-04-16 10:44:31 +02:00
|
|
|
for ( int i = channels(); i--; )
|
|
|
|
{
|
2008-04-30 01:48:11 +02:00
|
|
|
|
|
|
|
/* while ( jack_ringbuffer_read_space( _rb[ i ] ) < block_size ) */
|
|
|
|
/* { */
|
|
|
|
/* printf( "IO: disk buffer underrun!\n" ); */
|
|
|
|
/* /\* FIXME: is this *really* the right thing to do? *\/ */
|
|
|
|
/* usleep( 2000 ); */
|
|
|
|
/* } */
|
2008-04-16 10:44:31 +02:00
|
|
|
|
|
|
|
/* FIXME: avoid this copy */
|
|
|
|
|
|
|
|
jack_ringbuffer_read( _rb[ i ], (char*)cbuf, block_size );
|
|
|
|
|
2008-04-30 05:50:58 +02:00
|
|
|
buffer_interleave_one_channel( buf, cbuf, i, channels(), nframes );
|
2008-04-16 10:44:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* /\* deinterleave direcectly into the ringbuffer to avoid */
|
|
|
|
/* * unnecessary copying *\/ */
|
|
|
|
|
|
|
|
/* jack_ringbuffer_data_t rbd[2]; */
|
|
|
|
|
|
|
|
/* jack_ringbuffer_get_write_vector( _rb[ i ], rbd ); */
|
|
|
|
|
|
|
|
/* if ( rbd[ 0 ].len >= _nframes ) */
|
|
|
|
/* /\* it'll all fit in one go *\/ */
|
|
|
|
/* buffer_deinterleave_one_channel( (sample_t*)rbd[ 0 ].buf, buf, i, channels(), _nframes ); */
|
|
|
|
/* else if ( rbd[ 1 ].len ) */
|
|
|
|
/* { */
|
|
|
|
/* /\* there's enough space in the ringbuffer, but it's not contiguous *\/ */
|
|
|
|
|
|
|
|
/* /\* do the first half *\/ */
|
|
|
|
/* const nframes_t f = rbd[ 1 ].len / sizeof( sample_t ); */
|
|
|
|
|
|
|
|
/* buffer_deinterleave_one_channel( (sample_t*)rbd[ 0 ].buf, buf, i, channels(), f ); */
|
|
|
|
|
|
|
|
/* assert( rbd[ 1 ].len >= (_nframes - f) * sizeof( sample_t ) ); */
|
|
|
|
|
|
|
|
/* /\* do the second half *\/ */
|
|
|
|
/* buffer_deinterleave_one_channel( (sample_t*)rbd[ 1 ].buf, buf + f, i, channels(), _nframes - f ); */
|
|
|
|
/* } */
|
|
|
|
/* else */
|
|
|
|
/* printf( "programming error: expected more space in ringbuffer\n" ); */
|
|
|
|
|
|
|
|
/* /\* buffer_deinterleave_one_channel( (sample_t*)rbd.buf, buf, i, channels(), _nframes ); *\/ */
|
|
|
|
/* /\* jack_ringbuffer_write( _rb[ i ], (char*)cbuf, block_size ); *\/ */
|
|
|
|
|
|
|
|
/* jack_ringbuffer_write_advance( _rb[ i ], _nframes * sizeof( sample_t ) ); */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-04-30 05:50:58 +02:00
|
|
|
write_block( buf, nframes );
|
2008-04-16 10:44:31 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
printf( "IO thread terminating.\n" );
|
|
|
|
|
2008-04-16 17:35:25 +02:00
|
|
|
delete[] cbuf;
|
2008-04-16 10:44:31 +02:00
|
|
|
delete[] buf;
|
|
|
|
}
|
|
|
|
|
2008-04-16 17:35:25 +02:00
|
|
|
|
|
|
|
/** begin recording */
|
|
|
|
/* FIXME: we need to make note of the exact frame we were on when recording began */
|
|
|
|
void
|
|
|
|
Record_DS::start ( nframes_t frame )
|
|
|
|
{
|
|
|
|
/* FIXME: flush buffers here? */
|
|
|
|
|
|
|
|
if ( _recording )
|
|
|
|
{
|
|
|
|
printf( "programming error: attempt to start recording while recording is still in progress\n" );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-04-18 02:26:46 +02:00
|
|
|
|
2008-04-16 17:35:25 +02:00
|
|
|
_frame = frame;
|
|
|
|
|
2008-04-18 02:26:46 +02:00
|
|
|
_th->record( frame );
|
|
|
|
|
2008-04-16 17:35:25 +02:00
|
|
|
run();
|
|
|
|
|
|
|
|
_recording = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/** finalize the recording process. */
|
|
|
|
void
|
|
|
|
Record_DS::stop ( nframes_t frame )
|
|
|
|
{
|
|
|
|
|
|
|
|
if ( ! _recording )
|
|
|
|
{
|
|
|
|
printf( "programming error: attempt to stop recording when no recording is being made\n" );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
shutdown();
|
|
|
|
|
|
|
|
/* FIXME: flush buffers here? */
|
2008-04-16 20:08:00 +02:00
|
|
|
|
2008-04-18 02:26:46 +02:00
|
|
|
/* char *name = strdup( _af->name() ); */
|
|
|
|
/* delete _af; */
|
|
|
|
/* _af = NULL; */
|
2008-04-16 17:35:25 +02:00
|
|
|
|
2008-04-18 02:26:46 +02:00
|
|
|
/* Audio_File *af = Audio_File::from_file( name ); */
|
2008-04-16 20:08:00 +02:00
|
|
|
|
2008-04-18 02:26:46 +02:00
|
|
|
/* if ( ! af ) */
|
|
|
|
/* printf( "impossible!\n" ); */
|
2008-04-16 20:08:00 +02:00
|
|
|
|
2008-04-18 02:26:46 +02:00
|
|
|
/* new Region( af, track(), _frame ); */
|
2008-04-16 20:08:00 +02:00
|
|
|
|
2008-04-18 02:26:46 +02:00
|
|
|
/* track()->redraw(); */
|
2008-04-16 20:08:00 +02:00
|
|
|
|
2008-04-16 17:35:25 +02:00
|
|
|
_recording = false;
|
|
|
|
|
2008-04-18 02:26:46 +02:00
|
|
|
_th->stop( frame );
|
|
|
|
|
2008-04-16 17:35:25 +02:00
|
|
|
printf( "recording finished\n" );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-16 10:44:31 +02:00
|
|
|
/* THREAD: RT */
|
2008-04-16 17:35:25 +02:00
|
|
|
/** read from the attached track's ports and stuff the ringbuffers */
|
2008-04-16 10:44:31 +02:00
|
|
|
nframes_t
|
|
|
|
Record_DS::process ( nframes_t nframes )
|
|
|
|
{
|
2008-04-16 17:35:25 +02:00
|
|
|
|
|
|
|
if ( ! _recording )
|
|
|
|
return 0;
|
|
|
|
|
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--; )
|
|
|
|
{
|
|
|
|
void *buf = _th->input[ i ].buffer( nframes );
|
|
|
|
|
|
|
|
if ( jack_ringbuffer_write( _rb[ i ], (char*)buf, block_size ) < block_size )
|
|
|
|
{
|
|
|
|
printf( "RT: buffer overrun (disk can't keep up).\n" );
|
|
|
|
memset( buf, 0, block_size );
|
|
|
|
/* FIXME: we need to resync somehow */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
block_processed();
|
|
|
|
|
|
|
|
/* FIXME: bogus */
|
|
|
|
return nframes;
|
|
|
|
}
|