2008-04-09 02:05:15 +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. */
|
|
|
|
|
/*******************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "Engine.H"
|
2008-05-22 09:05:49 +02:00
|
|
|
|
#include "../Transport.H"
|
2008-04-09 02:05:15 +02:00
|
|
|
|
|
2008-05-22 09:05:49 +02:00
|
|
|
|
#include "../Timeline.H" // for process()
|
|
|
|
|
#include "../Sequence_Widget.H" // for BBT and position info.
|
2008-04-09 02:05:15 +02:00
|
|
|
|
|
|
|
|
|
#define APP_NAME "Non-DAW" // FIXME: wrong place for this!
|
|
|
|
|
|
|
|
|
|
/* This is the home of the JACK process callback (does this *really*
|
|
|
|
|
need to be a class?) */
|
|
|
|
|
|
2008-06-02 03:13:18 +02:00
|
|
|
|
#include "util/Thread.H"
|
|
|
|
|
|
|
|
|
|
Engine::Engine ( ) : _thread( "RT" )
|
2008-04-09 02:05:15 +02:00
|
|
|
|
{
|
2008-05-17 23:37:41 +02:00
|
|
|
|
_freewheeling = false;
|
2008-06-11 02:53:38 +02:00
|
|
|
|
_zombified = false;
|
2008-04-09 02:05:15 +02:00
|
|
|
|
_client = NULL;
|
|
|
|
|
_buffers_dropped = 0;
|
2008-04-29 23:18:47 +02:00
|
|
|
|
_xruns = 0;
|
2008-04-09 02:05:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
2008-04-29 19:18:27 +02:00
|
|
|
|
/*******************/
|
|
|
|
|
/* Static Wrappers */
|
|
|
|
|
/*******************/
|
2008-04-09 02:05:15 +02:00
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
Engine::process ( nframes_t nframes, void *arg )
|
|
|
|
|
{
|
|
|
|
|
return ((Engine*)arg)->process( nframes );
|
|
|
|
|
}
|
|
|
|
|
|
2008-04-12 21:13:30 +02:00
|
|
|
|
int
|
|
|
|
|
Engine::sync ( jack_transport_state_t state, jack_position_t *pos, void *arg )
|
|
|
|
|
{
|
|
|
|
|
return ((Engine*)arg)->sync( state, pos );
|
|
|
|
|
}
|
|
|
|
|
|
2008-04-29 19:18:27 +02:00
|
|
|
|
int
|
|
|
|
|
Engine::xrun ( void *arg )
|
|
|
|
|
{
|
2008-04-29 19:37:40 +02:00
|
|
|
|
return ((Engine*)arg)->xrun();
|
2008-04-29 19:18:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
2008-05-14 22:00:56 +02:00
|
|
|
|
void
|
|
|
|
|
Engine::timebase ( jack_transport_state_t state, jack_nframes_t nframes, jack_position_t *pos, int new_pos, void *arg )
|
|
|
|
|
{
|
|
|
|
|
((Engine*)arg)->timebase( state, nframes, pos, new_pos );
|
|
|
|
|
}
|
|
|
|
|
|
2008-05-17 23:37:41 +02:00
|
|
|
|
void
|
|
|
|
|
Engine::freewheel ( int starting, void *arg )
|
|
|
|
|
{
|
|
|
|
|
((Engine*)arg)->freewheel( starting );
|
|
|
|
|
}
|
|
|
|
|
|
2008-05-20 06:30:08 +02:00
|
|
|
|
int
|
|
|
|
|
Engine::buffer_size ( nframes_t nframes, void *arg )
|
|
|
|
|
{
|
|
|
|
|
return ((Engine*)arg)->buffer_size( nframes );
|
|
|
|
|
}
|
|
|
|
|
|
2008-04-29 19:18:27 +02:00
|
|
|
|
|
|
|
|
|
|
2008-04-12 21:13:30 +02:00
|
|
|
|
void
|
|
|
|
|
Engine::request_locate ( nframes_t frame )
|
|
|
|
|
{
|
|
|
|
|
if ( timeline )
|
|
|
|
|
timeline->seek( frame );
|
|
|
|
|
}
|
|
|
|
|
|
2008-04-29 19:18:27 +02:00
|
|
|
|
/* THREAD: RT */
|
|
|
|
|
/** This is the jack xrun callback */
|
|
|
|
|
int
|
|
|
|
|
Engine::xrun ( void )
|
|
|
|
|
{
|
|
|
|
|
++_xruns;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2008-05-17 23:37:41 +02:00
|
|
|
|
/* THREAD: RT */
|
|
|
|
|
void
|
|
|
|
|
Engine::freewheel ( bool starting )
|
|
|
|
|
{
|
|
|
|
|
_freewheeling = starting;
|
|
|
|
|
|
2008-05-18 03:45:56 +02:00
|
|
|
|
if ( starting )
|
|
|
|
|
DMESSAGE( "entering freewheeling mode" );
|
|
|
|
|
else
|
|
|
|
|
DMESSAGE( "leaving freewheeling mode" );
|
2008-05-17 23:37:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
2008-05-20 06:30:08 +02:00
|
|
|
|
/* THREAD: RT (non-RT) */
|
|
|
|
|
int
|
|
|
|
|
Engine::buffer_size ( nframes_t nframes )
|
|
|
|
|
{
|
|
|
|
|
/* TODO: inform all disktreams the the buffer size has changed */
|
|
|
|
|
timeline->resize_buffers( nframes );
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2008-04-12 21:13:30 +02:00
|
|
|
|
/* THREAD: RT */
|
|
|
|
|
/** This is the jack slow-sync callback. */
|
|
|
|
|
int
|
|
|
|
|
Engine::sync ( jack_transport_state_t state, jack_position_t *pos )
|
|
|
|
|
{
|
|
|
|
|
static bool seeking = false;
|
|
|
|
|
|
|
|
|
|
switch ( state )
|
|
|
|
|
{
|
|
|
|
|
case JackTransportStopped: /* new position requested */
|
|
|
|
|
/* JACK docs lie. This is only called when the transport
|
|
|
|
|
is *really* stopped, not when starting a slow-sync
|
|
|
|
|
cycle */
|
2008-05-30 06:11:56 +02:00
|
|
|
|
transport->frame = pos->frame;
|
2008-04-12 21:13:30 +02:00
|
|
|
|
return 1;
|
2008-04-12 21:50:36 +02:00
|
|
|
|
case JackTransportStarting: /* this means JACK is polling slow-sync clients */
|
2008-04-12 21:13:30 +02:00
|
|
|
|
{
|
|
|
|
|
if ( ! seeking )
|
|
|
|
|
{
|
|
|
|
|
request_locate( pos->frame );
|
|
|
|
|
seeking = true;
|
|
|
|
|
}
|
|
|
|
|
|
2008-04-14 06:11:48 +02:00
|
|
|
|
int r = true;
|
|
|
|
|
|
|
|
|
|
if ( timeline )
|
|
|
|
|
r = timeline->seek_pending();
|
2008-04-12 21:13:30 +02:00
|
|
|
|
|
|
|
|
|
if ( ! r )
|
|
|
|
|
seeking = false;
|
|
|
|
|
|
|
|
|
|
return ! seeking;
|
|
|
|
|
}
|
|
|
|
|
case JackTransportRolling: /* JACK's timeout has expired */
|
|
|
|
|
/* FIXME: what's the right thing to do here? */
|
|
|
|
|
// request_locate( pos->frame );
|
|
|
|
|
return 1;
|
2008-04-22 04:47:29 +02:00
|
|
|
|
// return transport->frame == pos->frame;
|
2008-04-12 21:13:30 +02:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
printf( "unknown transport state.\n" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2008-05-14 22:00:56 +02:00
|
|
|
|
|
|
|
|
|
/* THREAD: RT */
|
|
|
|
|
void
|
2008-05-19 05:01:09 +02:00
|
|
|
|
Engine::timebase ( jack_transport_state_t, jack_nframes_t, jack_position_t *pos, int )
|
2008-05-14 22:00:56 +02:00
|
|
|
|
{
|
|
|
|
|
position_info pi = timeline->solve_tempomap( pos->frame );
|
|
|
|
|
|
|
|
|
|
pos->valid = JackPositionBBT;
|
2008-06-14 06:51:15 +02:00
|
|
|
|
|
2008-05-14 22:00:56 +02:00
|
|
|
|
pos->beats_per_bar = pi.beats_per_bar;
|
|
|
|
|
pos->beat_type = pi.beat_type;
|
|
|
|
|
pos->beats_per_minute = pi.tempo;
|
|
|
|
|
|
|
|
|
|
pos->bar = pi.bbt.bar + 1;
|
|
|
|
|
pos->beat = pi.bbt.beat + 1;
|
|
|
|
|
pos->tick = pi.bbt.tick;
|
|
|
|
|
pos->ticks_per_beat = 1920.0; /* FIXME: wrong place for this */
|
|
|
|
|
|
|
|
|
|
/* FIXME: fill this in */
|
|
|
|
|
pos->bar_start_tick = 0;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2008-04-09 02:05:15 +02:00
|
|
|
|
/* THREAD: RT */
|
|
|
|
|
int
|
|
|
|
|
Engine::process ( nframes_t nframes )
|
|
|
|
|
{
|
2008-06-02 03:13:18 +02:00
|
|
|
|
/* FIXME: wrong place for this */
|
|
|
|
|
_thread.set( "RT" );
|
|
|
|
|
|
2008-04-22 04:47:29 +02:00
|
|
|
|
transport->poll();
|
2008-04-09 02:05:15 +02:00
|
|
|
|
|
2008-05-18 03:45:56 +02:00
|
|
|
|
if ( freewheeling() )
|
2008-04-09 02:05:15 +02:00
|
|
|
|
{
|
2008-05-18 03:45:56 +02:00
|
|
|
|
/* freewheeling mode/export. We're actually running
|
|
|
|
|
non-RT. Assume that everything is quiescent, locking is
|
|
|
|
|
unecessary and do I/O synchronously */
|
|
|
|
|
if ( timeline )
|
|
|
|
|
timeline->process( nframes );
|
|
|
|
|
|
|
|
|
|
/* because we're going faster than realtime. */
|
|
|
|
|
timeline->wait_for_buffers();
|
2008-04-09 02:05:15 +02:00
|
|
|
|
}
|
2008-05-18 03:45:56 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if ( ! trylock() )
|
|
|
|
|
{
|
|
|
|
|
/* the data structures we need to access here (tracks and
|
|
|
|
|
* their ports, but not track contents) may be in an
|
|
|
|
|
* inconsistent state at the moment. Just punt and drop this
|
|
|
|
|
* buffer. */
|
|
|
|
|
++_buffers_dropped;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2008-04-09 02:05:15 +02:00
|
|
|
|
|
2008-05-18 03:45:56 +02:00
|
|
|
|
/* handle chicken/egg problem */
|
|
|
|
|
if ( timeline )
|
|
|
|
|
/* this will initiate the process() call graph for the various
|
|
|
|
|
* number and types of tracks, which will in turn send data out
|
|
|
|
|
* the appropriate ports. */
|
|
|
|
|
timeline->process( nframes );
|
2008-04-09 02:05:15 +02:00
|
|
|
|
|
2008-05-18 03:45:56 +02:00
|
|
|
|
unlock();
|
|
|
|
|
}
|
2008-04-09 02:05:15 +02:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2008-05-17 23:37:41 +02:00
|
|
|
|
/** enter or leave freehweeling mode */
|
|
|
|
|
void
|
|
|
|
|
Engine::freewheeling ( bool yes )
|
|
|
|
|
{
|
|
|
|
|
if ( jack_set_freewheel( _client, yes ) )
|
|
|
|
|
WARNING( "Unkown error while setting freewheeling mode" );
|
|
|
|
|
}
|
|
|
|
|
|
2008-06-02 03:13:18 +02:00
|
|
|
|
void
|
|
|
|
|
Engine::thread_init ( void *arg )
|
|
|
|
|
{
|
|
|
|
|
((Engine*)arg)->thread_init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
Engine::thread_init ( void )
|
|
|
|
|
{
|
|
|
|
|
_thread.set( "RT" );
|
|
|
|
|
}
|
|
|
|
|
|
2008-06-11 02:53:38 +02:00
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
Engine::shutdown ( void *arg )
|
|
|
|
|
{
|
|
|
|
|
((Engine*)arg)->shutdown();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
Engine::shutdown ( void )
|
|
|
|
|
{
|
|
|
|
|
_zombified = true;
|
|
|
|
|
}
|
|
|
|
|
|
2008-04-09 02:05:15 +02:00
|
|
|
|
int
|
|
|
|
|
Engine::init ( void )
|
|
|
|
|
{
|
|
|
|
|
if (( _client = jack_client_open ( APP_NAME, (jack_options_t)0, NULL )) == 0 )
|
|
|
|
|
return 0;
|
|
|
|
|
|
2008-04-29 19:18:27 +02:00
|
|
|
|
#define set_callback( name ) jack_set_ ## name ## _callback( _client, &Engine:: name , this )
|
2008-04-09 02:05:15 +02:00
|
|
|
|
|
2008-06-02 03:13:18 +02:00
|
|
|
|
set_callback( thread_init );
|
2008-04-29 19:18:27 +02:00
|
|
|
|
set_callback( process );
|
|
|
|
|
set_callback( xrun );
|
2008-05-17 23:37:41 +02:00
|
|
|
|
set_callback( freewheel );
|
2008-05-20 06:30:08 +02:00
|
|
|
|
set_callback( buffer_size );
|
2008-05-14 22:00:56 +02:00
|
|
|
|
|
2008-05-07 01:33:24 +02:00
|
|
|
|
/* FIXME: should we wait to register this until after the project
|
2008-04-12 21:13:30 +02:00
|
|
|
|
has been loaded (and we have disk threads running)? */
|
2008-04-29 19:18:27 +02:00
|
|
|
|
set_callback( sync );
|
2008-04-12 21:13:30 +02:00
|
|
|
|
|
2008-05-14 22:00:56 +02:00
|
|
|
|
jack_set_timebase_callback( _client, 0, &Engine::timebase, this );
|
|
|
|
|
|
2008-06-11 02:53:38 +02:00
|
|
|
|
jack_on_shutdown( _client, &Engine::shutdown, this );
|
|
|
|
|
|
2008-04-09 02:05:15 +02:00
|
|
|
|
jack_activate( _client );
|
|
|
|
|
|
2008-04-27 14:04:37 +02:00
|
|
|
|
_sample_rate = frame_rate();
|
|
|
|
|
|
2008-05-22 09:05:49 +02:00
|
|
|
|
timeline->_sample_rate = frame_rate();
|
|
|
|
|
|
2008-04-09 02:05:15 +02:00
|
|
|
|
/* we don't need to create any ports until tracks are created */
|
|
|
|
|
return 1;
|
|
|
|
|
}
|