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 */
|
2008-05-22 09:05:49 +02:00
|
|
|
#include "../Timeline.H" // for locking
|
|
|
|
#include "../Audio_Sequence.H"
|
|
|
|
#include "../Track.H"
|
|
|
|
|
2008-04-16 10:44:31 +02:00
|
|
|
#include "Port.H"
|
|
|
|
#include "Record_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"
|
2008-05-22 09:05:49 +02:00
|
|
|
#include "util/debug.h"
|
2008-06-02 03:13:18 +02:00
|
|
|
#include "util/Thread.H"
|
2008-05-03 06:44:48 +02:00
|
|
|
|
2008-05-23 04:23:41 +02:00
|
|
|
const Audio_Region *
|
|
|
|
Record_DS::capture_region ( void ) const
|
|
|
|
{
|
|
|
|
if ( _capture )
|
|
|
|
return _capture->region;
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-04-16 10:44:31 +02:00
|
|
|
/** write /nframes/ from buf to the capture file of the attached track */
|
|
|
|
void
|
|
|
|
Record_DS::write_block ( sample_t *buf, nframes_t nframes )
|
|
|
|
{
|
2008-06-02 03:13:18 +02:00
|
|
|
THREAD_ASSERT( Capture );
|
2008-04-16 10:44:31 +02:00
|
|
|
|
|
|
|
/* stupid chicken/egg */
|
2008-05-07 18:18:57 +02:00
|
|
|
if ( ! ( timeline && sequence() ) )
|
2008-04-16 10:44:31 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
// timeline->wrlock();
|
|
|
|
|
2008-05-23 04:23:41 +02:00
|
|
|
track()->write( _capture, buf, nframes );
|
2008-04-16 10:44:31 +02:00
|
|
|
|
2008-05-02 01:35:46 +02:00
|
|
|
_frames_written += nframes;
|
2008-04-16 10:44:31 +02:00
|
|
|
|
|
|
|
// timeline->unlock();
|
|
|
|
}
|
|
|
|
|
2008-05-02 06:12:51 +02:00
|
|
|
#define AVOID_UNNECESSARY_COPYING 1
|
|
|
|
|
2008-04-16 10:44:31 +02:00
|
|
|
void
|
|
|
|
Record_DS::disk_thread ( void )
|
|
|
|
{
|
2008-06-02 03:13:18 +02:00
|
|
|
_thread.name( "Capture" );
|
|
|
|
|
|
|
|
track()->record( _capture, _frame );
|
|
|
|
|
2008-05-03 06:44:48 +02:00
|
|
|
DMESSAGE( "capture thread running..." );
|
2008-04-16 10:44:31 +02:00
|
|
|
|
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() ];
|
2008-05-02 06:12:51 +02:00
|
|
|
#ifndef AVOID_UNNECESSARY_COPYING
|
2008-05-01 13:35:42 +02:00
|
|
|
sample_t *cbuf = new sample_t[ nframes ];
|
2008-05-02 06:12:51 +02:00
|
|
|
#endif
|
2008-04-30 05:50:58 +02:00
|
|
|
|
|
|
|
const size_t block_size = nframes * sizeof( sample_t );
|
2008-04-16 10:44:31 +02:00
|
|
|
|
2009-01-02 08:20:45 +01:00
|
|
|
int blocks_ready = 0;
|
2008-04-16 10:44:31 +02:00
|
|
|
|
|
|
|
while ( wait_for_block() )
|
|
|
|
{
|
2009-01-02 08:20:45 +01:00
|
|
|
if ( ++blocks_ready < _disk_io_blocks )
|
2008-04-30 05:50:58 +02:00
|
|
|
continue;
|
2009-01-02 08:20:45 +01:00
|
|
|
else
|
|
|
|
blocks_ready = 0;
|
2008-04-30 05:50:58 +02:00
|
|
|
|
|
|
|
/* pull data from the per-channel ringbuffers and interlace it */
|
2008-04-16 10:44:31 +02:00
|
|
|
for ( int i = channels(); i--; )
|
|
|
|
{
|
|
|
|
|
2008-05-02 06:12:51 +02:00
|
|
|
#ifdef AVOID_UNNECESSARY_COPYING
|
2008-04-16 10:44:31 +02:00
|
|
|
|
2008-05-02 06:12:51 +02:00
|
|
|
/* interleave direcectly from the ringbuffer to avoid
|
|
|
|
* unnecessary copying */
|
2008-04-16 10:44:31 +02:00
|
|
|
|
2008-05-02 06:12:51 +02:00
|
|
|
jack_ringbuffer_data_t rbd[2];
|
2008-04-16 10:44:31 +02:00
|
|
|
|
2008-05-02 06:12:51 +02:00
|
|
|
memset( rbd, 0, sizeof( rbd ) );
|
2008-04-16 10:44:31 +02:00
|
|
|
|
2008-05-02 06:12:51 +02:00
|
|
|
jack_ringbuffer_get_read_vector( _rb[ i ], rbd );
|
2008-04-16 10:44:31 +02:00
|
|
|
|
2008-05-02 06:12:51 +02:00
|
|
|
if ( rbd[ 0 ].len >= block_size )
|
|
|
|
{
|
|
|
|
/* it'll all fit in one go */
|
|
|
|
buffer_interleave_one_channel( buf, (sample_t*)rbd[ 0 ].buf, i, channels(), nframes );
|
|
|
|
}
|
|
|
|
else if ( rbd[ 0 ].len + rbd[ 1 ].len >= block_size )
|
|
|
|
{
|
|
|
|
/* there's enough space in the ringbuffer, but it's not contiguous */
|
2008-04-16 10:44:31 +02:00
|
|
|
|
2008-05-02 06:12:51 +02:00
|
|
|
assert( ! ( rbd[ 0 ].len % sizeof( sample_t ) ) );
|
2008-04-16 10:44:31 +02:00
|
|
|
|
2008-05-02 06:12:51 +02:00
|
|
|
const nframes_t f = rbd[ 0 ].len / sizeof( sample_t );
|
2008-04-16 10:44:31 +02:00
|
|
|
|
2008-05-02 06:12:51 +02:00
|
|
|
/* do the first half */
|
|
|
|
buffer_deinterleave_one_channel( (sample_t*)rbd[ 0 ].buf, buf, i, channels(), f );
|
|
|
|
buffer_interleave_one_channel( buf, (sample_t*)rbd[ 0 ].buf, i, channels(), f );
|
2008-04-16 10:44:31 +02:00
|
|
|
|
2008-05-02 06:12:51 +02:00
|
|
|
assert( rbd[ 1 ].len >= ( nframes - f ) * sizeof( sample_t ) );
|
2008-04-16 10:44:31 +02:00
|
|
|
|
2008-05-02 06:12:51 +02:00
|
|
|
/* do the second half */
|
|
|
|
buffer_interleave_one_channel( buf + f, (sample_t*)rbd[ 0 ].buf, i, channels(), nframes - f );
|
2008-04-16 10:44:31 +02:00
|
|
|
|
2008-05-02 06:12:51 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
++_xruns;
|
2008-04-16 10:44:31 +02:00
|
|
|
|
2008-05-02 06:12:51 +02:00
|
|
|
jack_ringbuffer_read_advance( _rb[ i ], block_size );
|
|
|
|
#else
|
|
|
|
if ( jack_ringbuffer_read( _rb[ i ], (char*)cbuf, block_size ) < block_size )
|
|
|
|
++_xruns;
|
2008-04-16 10:44:31 +02:00
|
|
|
|
2008-05-02 06:12:51 +02:00
|
|
|
buffer_interleave_one_channel( buf, cbuf, i, channels(), nframes );
|
|
|
|
#endif
|
2008-04-16 10:44:31 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-04-30 05:50:58 +02:00
|
|
|
write_block( buf, nframes );
|
2008-04-16 10:44:31 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-05-03 06:44:48 +02:00
|
|
|
DMESSAGE( "capture thread terminating" );
|
2008-05-02 01:35:46 +02:00
|
|
|
|
|
|
|
/* flush what remains in the buffer out to disk */
|
|
|
|
|
|
|
|
{
|
|
|
|
/* use JACk sized blocks for this last bit */
|
|
|
|
const nframes_t nframes = _nframes;
|
|
|
|
const size_t block_size = _nframes * sizeof( sample_t );
|
|
|
|
|
2008-05-02 06:12:51 +02:00
|
|
|
#ifdef AVOID_UNNECESSARY_COPYING
|
|
|
|
sample_t *cbuf = new sample_t[ nframes ];
|
|
|
|
#endif
|
|
|
|
|
2008-05-23 01:12:51 +02:00
|
|
|
while ( blocks_ready-- > 0 || ( ! sem_trywait( &_blocks ) && errno != EAGAIN ) )
|
2008-05-02 01:35:46 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
for ( int i = channels(); i--; )
|
|
|
|
{
|
|
|
|
jack_ringbuffer_read( _rb[ i ], (char*)cbuf, block_size );
|
|
|
|
|
|
|
|
buffer_interleave_one_channel( buf, cbuf, i, channels(), nframes );
|
|
|
|
}
|
|
|
|
|
|
|
|
const nframes_t frames_remaining = (_stop_frame - _frame ) - _frames_written;
|
|
|
|
|
|
|
|
if ( frames_remaining < nframes )
|
|
|
|
{
|
|
|
|
/* this is the last block, might be partial */
|
|
|
|
write_block( buf, frames_remaining );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
write_block( buf, nframes );
|
|
|
|
}
|
2008-05-02 06:12:51 +02:00
|
|
|
|
|
|
|
#ifdef AVOID_UNNECESSARY_COPYING
|
|
|
|
delete[] cbuf;
|
|
|
|
#endif
|
|
|
|
|
2008-05-02 01:35:46 +02:00
|
|
|
}
|
|
|
|
|
2008-04-16 10:44:31 +02:00
|
|
|
delete[] buf;
|
2008-05-02 06:12:51 +02:00
|
|
|
#ifndef AVOID_UNNECESSARY_COPYING
|
|
|
|
delete[] cbuf;
|
|
|
|
#endif
|
2008-05-23 04:23:41 +02:00
|
|
|
|
2008-05-28 03:45:46 +02:00
|
|
|
DMESSAGE( "finalzing capture" );
|
|
|
|
|
2008-07-18 03:58:33 +02:00
|
|
|
Track::Capture *c = _capture;
|
2008-05-23 04:23:41 +02:00
|
|
|
|
|
|
|
_capture = NULL;
|
|
|
|
|
2008-07-18 03:58:33 +02:00
|
|
|
/* now finalize the recording */
|
2009-01-02 08:20:45 +01:00
|
|
|
|
2008-07-18 03:58:33 +02:00
|
|
|
track()->finalize( c, _stop_frame );
|
|
|
|
|
|
|
|
delete c;
|
|
|
|
|
2008-05-23 04:23:41 +02:00
|
|
|
_terminate = false;
|
2008-05-28 03:45:46 +02:00
|
|
|
DMESSAGE( "capture thread gone" );
|
2008-04-16 10:44:31 +02:00
|
|
|
}
|
|
|
|
|
2008-04-16 17:35:25 +02:00
|
|
|
|
|
|
|
/** begin recording */
|
|
|
|
void
|
|
|
|
Record_DS::start ( nframes_t frame )
|
|
|
|
{
|
2008-06-02 03:13:18 +02:00
|
|
|
THREAD_ASSERT( UI );
|
2008-04-16 17:35:25 +02:00
|
|
|
|
|
|
|
if ( _recording )
|
|
|
|
{
|
2008-05-24 01:48:44 +02:00
|
|
|
WARNING( "programming error: attempt to start recording while recording is still in progress" );
|
2008-04-16 17:35:25 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-06-02 03:13:18 +02:00
|
|
|
/* /\* FIXME: safe to do this here? *\/ */
|
|
|
|
/* flush(); */
|
2008-04-18 02:26:46 +02:00
|
|
|
|
2009-01-02 08:20:45 +01:00
|
|
|
DMESSAGE( "recording started at frame %lu", (unsigned long)frame);
|
|
|
|
|
2008-04-16 17:35:25 +02:00
|
|
|
_frame = frame;
|
|
|
|
|
2008-05-23 04:23:41 +02:00
|
|
|
_capture = new Track::Capture;
|
|
|
|
|
2008-04-16 17:35:25 +02:00
|
|
|
run();
|
|
|
|
|
|
|
|
_recording = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/** finalize the recording process. */
|
|
|
|
void
|
|
|
|
Record_DS::stop ( nframes_t frame )
|
|
|
|
{
|
2008-06-02 03:13:18 +02:00
|
|
|
THREAD_ASSERT( UI );
|
|
|
|
|
2008-04-16 17:35:25 +02:00
|
|
|
if ( ! _recording )
|
|
|
|
{
|
2008-05-24 01:48:44 +02:00
|
|
|
WARNING( "programming error: attempt to stop recording when no recording is being made" );
|
2008-04-16 17:35:25 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-05-02 01:35:46 +02:00
|
|
|
_recording = false;
|
2008-04-16 20:08:00 +02:00
|
|
|
|
2008-05-02 01:35:46 +02:00
|
|
|
_stop_frame = frame;
|
2008-04-16 20:08:00 +02:00
|
|
|
|
2008-05-28 03:45:46 +02:00
|
|
|
detach();
|
2008-04-16 20:08:00 +02:00
|
|
|
|
2008-05-03 06:44:48 +02:00
|
|
|
DMESSAGE( "recording finished" );
|
2008-04-16 17:35:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-02 08:20:45 +01:00
|
|
|
#include "../Transport.H"
|
|
|
|
extern Transport *transport;
|
|
|
|
|
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-06-02 03:13:18 +02:00
|
|
|
THREAD_ASSERT( RT );
|
2008-04-16 17:35:25 +02:00
|
|
|
|
|
|
|
if ( ! _recording )
|
|
|
|
return 0;
|
|
|
|
|
2009-01-02 08:20:45 +01:00
|
|
|
if ( transport->frame < _frame )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* DMESSAGE( "recording actually happening at %lu (start frame %lu)", (unsigned long)transport->frame, (unsigned long)_frame); */
|
|
|
|
|
|
|
|
nframes_t offset = 0;
|
|
|
|
|
|
|
|
if ( _frame > transport->frame &&
|
|
|
|
_frame < transport->frame + nframes )
|
|
|
|
{
|
|
|
|
/* The record start frame falls somewhere within the current
|
|
|
|
buffer. We must discard the unneeded portion and only
|
|
|
|
stuff the part requested into the ringbuffer. */
|
|
|
|
|
|
|
|
offset = _frame - transport->frame;
|
|
|
|
|
|
|
|
/* DMESSAGE( "offset = %lu", (unsigned long)offset ); */
|
|
|
|
}
|
|
|
|
|
|
|
|
const size_t offset_size = offset * sizeof( sample_t );
|
|
|
|
const size_t block_size = ( nframes * sizeof( sample_t ) ) - offset_size;
|
2008-04-16 10:44:31 +02:00
|
|
|
|
|
|
|
for ( int i = channels(); i--; )
|
|
|
|
{
|
2009-01-02 08:20:45 +01:00
|
|
|
/* read the entire input buffer */
|
2008-05-07 18:18:57 +02:00
|
|
|
void *buf = track()->input[ i ].buffer( nframes );
|
2008-04-16 10:44:31 +02:00
|
|
|
|
2009-01-02 08:20:45 +01:00
|
|
|
/* if ( buffer_is_digital_black( (sample_t*)buf, nframes ) ) */
|
|
|
|
/* DWARNING( "recording an entirely blank buffer" ); */
|
|
|
|
|
|
|
|
/* FIXME: this results in a ringbuffer size that is no longer
|
|
|
|
necessarily a multiple of nframes... how will the other side
|
|
|
|
handle that? */
|
|
|
|
if ( jack_ringbuffer_write( _rb[ i ], (char*)buf + offset, block_size ) < block_size )
|
2008-04-16 10:44:31 +02:00
|
|
|
{
|
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 */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
block_processed();
|
|
|
|
|
|
|
|
/* FIXME: bogus */
|
|
|
|
return nframes;
|
|
|
|
}
|