2008-04-20 23:46:47 +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. */
|
|
|
|
/*******************************************************************************/
|
|
|
|
|
2008-04-28 20:17:46 +02:00
|
|
|
#include <FL/fl_ask.H>
|
2008-05-22 22:58:36 +02:00
|
|
|
#include <list>
|
|
|
|
using namespace std;
|
2008-04-28 20:17:46 +02:00
|
|
|
|
2008-04-20 23:46:47 +02:00
|
|
|
#include "Control_Sequence.H"
|
|
|
|
#include "Track.H"
|
2008-05-22 09:05:49 +02:00
|
|
|
#include "Engine/Port.H"
|
2008-04-28 16:20:17 +02:00
|
|
|
|
|
|
|
#include "Transport.H" // for transport->frame
|
|
|
|
|
2008-04-22 02:45:17 +02:00
|
|
|
bool Control_Sequence::draw_with_gradient = true;
|
|
|
|
bool Control_Sequence::draw_with_polygon = true;
|
|
|
|
bool Control_Sequence::draw_with_grid = true;
|
2008-04-21 05:05:25 +02:00
|
|
|
|
2008-05-05 07:29:39 +02:00
|
|
|
Control_Sequence::Control_Sequence ( Track *track ) : Sequence( 0 )
|
2008-04-21 05:05:25 +02:00
|
|
|
{
|
|
|
|
init();
|
|
|
|
|
|
|
|
_track = track;
|
|
|
|
|
|
|
|
if ( track )
|
|
|
|
track->add( this );
|
|
|
|
|
2008-05-22 09:05:49 +02:00
|
|
|
_output = new Port( Port::Output, track->name(), track->ncontrols(), "cv" );
|
2008-04-28 18:19:25 +02:00
|
|
|
|
2008-04-21 05:05:25 +02:00
|
|
|
log_create();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Control_Sequence::~Control_Sequence ( )
|
|
|
|
{
|
|
|
|
log_destroy();
|
2008-05-22 09:05:49 +02:00
|
|
|
|
|
|
|
delete _output;
|
2008-04-21 05:05:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Control_Sequence::init ( void )
|
|
|
|
{
|
|
|
|
_track = NULL;
|
2008-04-25 05:29:28 +02:00
|
|
|
_highlighted = false;
|
2008-04-28 21:04:16 +02:00
|
|
|
_output = NULL;
|
2008-04-21 05:05:25 +02:00
|
|
|
|
2008-04-23 17:41:35 +02:00
|
|
|
color( fl_darker( FL_YELLOW ) );
|
2008-04-21 05:05:25 +02:00
|
|
|
}
|
|
|
|
|
2008-04-20 23:46:47 +02:00
|
|
|
void
|
2008-04-25 08:34:08 +02:00
|
|
|
Control_Sequence::get ( Log_Entry &e ) const
|
2008-04-20 23:46:47 +02:00
|
|
|
{
|
2008-05-07 17:28:57 +02:00
|
|
|
e.add( ":track", _track );
|
|
|
|
e.add( ":name", name() );
|
2008-04-20 23:46:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Control_Sequence::set ( Log_Entry &e )
|
|
|
|
{
|
|
|
|
for ( int i = 0; i < e.size(); ++i )
|
|
|
|
{
|
|
|
|
const char *s, *v;
|
|
|
|
|
|
|
|
e.get( i, &s, &v );
|
|
|
|
|
2008-05-07 17:28:57 +02:00
|
|
|
if ( ! strcmp( ":track", s ) )
|
2008-04-20 23:46:47 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
sscanf( v, "%X", &i );
|
|
|
|
Track *t = (Track*)Loggable::find( i );
|
|
|
|
|
|
|
|
assert( t );
|
|
|
|
|
2008-05-22 09:05:49 +02:00
|
|
|
_output = new Port( Port::Output, t->name(), t->ncontrols(), "cv" );
|
|
|
|
|
2008-04-20 23:46:47 +02:00
|
|
|
t->add( this );
|
|
|
|
}
|
2008-05-07 17:28:57 +02:00
|
|
|
else if ( ! strcmp( ":name", s ) )
|
2008-05-07 04:32:37 +02:00
|
|
|
name( v );
|
2008-05-04 01:25:59 +02:00
|
|
|
|
2008-04-20 23:46:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-23 17:41:35 +02:00
|
|
|
void
|
|
|
|
Control_Sequence::draw_curve ( bool flip, bool filled )
|
|
|
|
{
|
|
|
|
|
|
|
|
const int bx = x();
|
|
|
|
const int by = y() + Fl::box_dy( box() );
|
|
|
|
const int bw = w();
|
|
|
|
const int bh = h() - Fl::box_dh( box() );
|
|
|
|
|
|
|
|
list <Sequence_Widget *>::const_iterator e = _widgets.end();
|
|
|
|
e--;
|
|
|
|
|
|
|
|
if ( _widgets.size() )
|
|
|
|
for ( list <Sequence_Widget *>::const_iterator r = _widgets.begin(); ; r++ )
|
|
|
|
{
|
|
|
|
const int ry = (*r)->y();
|
|
|
|
|
|
|
|
if ( r == _widgets.begin() )
|
|
|
|
{
|
|
|
|
if ( flip )
|
|
|
|
{
|
|
|
|
if ( filled )
|
|
|
|
fl_vertex( bx, by );
|
|
|
|
fl_vertex( bx, ry );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( filled )
|
|
|
|
fl_vertex( bx, bh + by );
|
|
|
|
fl_vertex( bx, ry );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-27 07:11:00 +02:00
|
|
|
fl_vertex( (*r)->line_x(), ry );
|
2008-04-23 17:41:35 +02:00
|
|
|
|
|
|
|
if ( r == e )
|
|
|
|
{
|
|
|
|
if ( flip )
|
|
|
|
{
|
|
|
|
fl_vertex( bx + bw, ry );
|
|
|
|
if ( filled )
|
|
|
|
fl_vertex( bx + bw, by );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fl_vertex( bx + bw, ry );
|
|
|
|
if ( filled )
|
|
|
|
fl_vertex( bx + bw, bh + by );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2008-04-25 05:29:28 +02:00
|
|
|
|
2008-04-20 23:46:47 +02:00
|
|
|
void
|
|
|
|
Control_Sequence::draw ( void )
|
|
|
|
{
|
|
|
|
|
|
|
|
if ( ! fl_not_clipped( x(), y(), w(), h() ) )
|
|
|
|
return;
|
|
|
|
|
|
|
|
fl_push_clip( x(), y(), w(), h() );
|
|
|
|
|
2008-04-23 05:40:31 +02:00
|
|
|
/* draw the box with the ends cut off. */
|
2008-04-25 04:50:21 +02:00
|
|
|
draw_box( box(), x() - Fl::box_dx( box() ), y(), w() + Fl::box_dw( box() ) + 1, h(), color() );
|
2008-04-23 05:40:31 +02:00
|
|
|
|
|
|
|
const int bx = x();
|
|
|
|
const int by = y() + Fl::box_dy( box() );
|
|
|
|
const int bw = w();
|
|
|
|
const int bh = h() - Fl::box_dh( box() );
|
2008-04-20 23:46:47 +02:00
|
|
|
|
|
|
|
int X, Y, W, H;
|
|
|
|
|
2008-04-23 05:40:31 +02:00
|
|
|
fl_clip_box( bx, by, bw, bh, X, Y, W, H );
|
2008-04-20 23:46:47 +02:00
|
|
|
|
2008-04-27 21:37:52 +02:00
|
|
|
bool active = active_r();
|
2008-04-25 08:58:08 +02:00
|
|
|
|
2008-04-27 21:37:52 +02:00
|
|
|
const Fl_Color color = active ? this->color() : fl_inactive( this->color() );
|
|
|
|
const Fl_Color selection_color = active ? this->selection_color() : fl_inactive( this->selection_color() );
|
2008-04-25 08:58:08 +02:00
|
|
|
|
2008-04-22 02:45:17 +02:00
|
|
|
if ( draw_with_gradient )
|
2008-04-22 01:45:54 +02:00
|
|
|
{
|
2008-04-27 21:37:52 +02:00
|
|
|
/* const Fl_Color c2 = fl_color_average( selection_color, FL_WHITE, 0.90f ); */
|
|
|
|
/* const Fl_Color c1 = fl_color_average( color, c2, 0.60f ); */
|
2008-04-23 17:41:35 +02:00
|
|
|
|
2008-04-27 21:37:52 +02:00
|
|
|
const Fl_Color c1 = fl_color_average( selection_color, FL_WHITE, 0.90f );
|
|
|
|
const Fl_Color c2 = fl_color_average( color, c1, 0.60f );
|
2008-04-22 01:45:54 +02:00
|
|
|
|
2008-04-23 05:40:31 +02:00
|
|
|
for ( int gy = 0; gy < bh; gy++ )
|
2008-04-22 01:45:54 +02:00
|
|
|
{
|
2008-04-23 17:41:35 +02:00
|
|
|
fl_color( fl_color_average( c1, c2, gy / (float)bh) );
|
2008-04-23 05:40:31 +02:00
|
|
|
fl_line( X, by + gy, X + W, by + gy );
|
2008-04-22 01:45:54 +02:00
|
|
|
}
|
|
|
|
}
|
2008-04-20 23:46:47 +02:00
|
|
|
|
2008-04-22 02:45:17 +02:00
|
|
|
if ( draw_with_grid )
|
|
|
|
{
|
2008-04-25 08:58:08 +02:00
|
|
|
fl_color( fl_darker( color ) );
|
2008-04-22 02:45:17 +02:00
|
|
|
|
2008-04-23 05:40:31 +02:00
|
|
|
const int inc = bh / 10;
|
2008-04-22 02:45:17 +02:00
|
|
|
if ( inc )
|
2008-04-23 05:40:31 +02:00
|
|
|
for ( int gy = 0; gy < bh; gy += inc )
|
|
|
|
fl_line( X, by + gy, X + W, by + gy );
|
2008-04-22 02:45:17 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-04-22 01:45:54 +02:00
|
|
|
|
2008-04-22 02:45:17 +02:00
|
|
|
if ( draw_with_polygon )
|
2008-04-23 17:41:35 +02:00
|
|
|
{
|
2008-04-27 21:37:52 +02:00
|
|
|
fl_color( draw_with_gradient ? color : fl_color_average( color, selection_color, 0.45f ) );
|
|
|
|
|
2008-04-22 01:45:54 +02:00
|
|
|
fl_begin_complex_polygon();
|
2008-04-23 17:41:35 +02:00
|
|
|
draw_curve( draw_with_gradient, true );
|
|
|
|
fl_end_complex_polygon();
|
2008-04-20 23:46:47 +02:00
|
|
|
|
2008-04-25 08:58:08 +02:00
|
|
|
fl_color( selection_color );
|
2008-04-23 17:41:35 +02:00
|
|
|
fl_line_style( FL_SOLID, 2 );
|
2008-04-20 23:46:47 +02:00
|
|
|
|
2008-04-23 17:41:35 +02:00
|
|
|
fl_begin_line();
|
|
|
|
draw_curve( draw_with_gradient, false );
|
|
|
|
fl_end_line();
|
|
|
|
}
|
2008-04-22 01:45:54 +02:00
|
|
|
else
|
2008-04-23 17:41:35 +02:00
|
|
|
{
|
2008-04-25 08:58:08 +02:00
|
|
|
// fl_color( fl_color_average( selection_color, color, 0.70f ) );
|
|
|
|
fl_color( selection_color );
|
2008-04-23 17:41:35 +02:00
|
|
|
fl_line_style( FL_SOLID, 2 );
|
|
|
|
|
|
|
|
fl_begin_line();
|
|
|
|
draw_curve( draw_with_gradient, false );
|
2008-04-22 01:45:54 +02:00
|
|
|
fl_end_line();
|
2008-04-23 17:41:35 +02:00
|
|
|
}
|
2008-04-20 23:46:47 +02:00
|
|
|
|
|
|
|
fl_line_style( FL_SOLID, 0 );
|
|
|
|
|
2008-04-25 08:58:08 +02:00
|
|
|
timeline->draw_measure_lines( x(), y(), w(), h(), color );
|
2008-04-20 23:46:47 +02:00
|
|
|
|
2008-04-25 05:29:28 +02:00
|
|
|
if ( _highlighted )
|
|
|
|
for ( list <Sequence_Widget *>::const_iterator r = _widgets.begin(); r != _widgets.end(); r++ )
|
|
|
|
(*r)->draw_box();
|
2008-05-01 07:29:46 +02:00
|
|
|
else
|
|
|
|
for ( list <Sequence_Widget *>::const_iterator r = _widgets.begin(); r != _widgets.end(); r++ )
|
|
|
|
if ( (*r)->selected() )
|
|
|
|
(*r)->draw_box();
|
2008-04-20 23:46:47 +02:00
|
|
|
|
|
|
|
fl_pop_clip();
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
Control_Sequence::handle ( int m )
|
|
|
|
{
|
2008-04-25 05:29:28 +02:00
|
|
|
switch ( m )
|
|
|
|
{
|
|
|
|
case FL_ENTER:
|
|
|
|
_highlighted = true;
|
2008-05-02 13:42:50 +02:00
|
|
|
fl_cursor( cursor() );
|
2008-04-25 05:29:28 +02:00
|
|
|
redraw();
|
|
|
|
return 1;
|
|
|
|
case FL_LEAVE:
|
|
|
|
_highlighted = false;
|
|
|
|
redraw();
|
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-04-20 23:46:47 +02:00
|
|
|
int r = Sequence::handle( m );
|
|
|
|
|
|
|
|
if ( r )
|
|
|
|
return r;
|
|
|
|
|
|
|
|
switch ( m )
|
|
|
|
{
|
|
|
|
case FL_PUSH:
|
|
|
|
{
|
2008-04-28 20:17:46 +02:00
|
|
|
Logger log( this );
|
|
|
|
|
2008-04-20 23:46:47 +02:00
|
|
|
if ( Fl::event_button1() )
|
|
|
|
{
|
|
|
|
Control_Point *r = new Control_Point( this, timeline->xoffset + timeline->x_to_ts( Fl::event_x() - x() ), (float)(Fl::event_y() - y()) / h() );
|
|
|
|
|
|
|
|
add( r );
|
|
|
|
}
|
2008-04-28 20:17:46 +02:00
|
|
|
else if ( Fl::event_button3() && ! ( Fl::event_state() & ( FL_ALT | FL_SHIFT | FL_CTRL ) ) )
|
|
|
|
{
|
|
|
|
|
|
|
|
Fl_Menu_Item menu[] =
|
|
|
|
{
|
|
|
|
{ "Rename" },
|
|
|
|
{ "Remove" },
|
|
|
|
{ 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
const Fl_Menu_Item *r = menu->popup( Fl::event_x(), Fl::event_y(), "Control Sequence" );
|
|
|
|
|
|
|
|
if ( r )
|
|
|
|
{
|
|
|
|
if ( r == &menu[ 0 ] )
|
|
|
|
{
|
|
|
|
const char *s = fl_input( "Input new name for control sequence:", name() );
|
|
|
|
|
|
|
|
if ( s )
|
|
|
|
name( s );
|
|
|
|
|
|
|
|
redraw();
|
|
|
|
}
|
|
|
|
else if ( r == &menu[ 1 ] )
|
|
|
|
{
|
|
|
|
/* TODO: remove */
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2008-04-20 23:46:47 +02:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2008-04-28 16:20:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**********/
|
|
|
|
/* Engine */
|
|
|
|
/**********/
|
|
|
|
|
|
|
|
|
|
|
|
static inline float
|
|
|
|
linear_interpolate ( float y1, float y2, float mu )
|
|
|
|
{
|
2008-04-29 04:21:17 +02:00
|
|
|
// return y1 + mu * ( y2 - y1 );
|
|
|
|
return y1 * ( 1.0f - mu ) + y2 * mu;
|
2008-04-28 16:20:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline float
|
|
|
|
sigmoid_interpolate ( float y1, float y2, float mu )
|
|
|
|
{
|
|
|
|
return linear_interpolate( y1, y2, ( 1 - cos( mu * M_PI ) ) / 2 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* THREAD: ?? */
|
|
|
|
/** fill buf with /nframes/ of interpolated control curve values
|
|
|
|
* starting at /frame/ */
|
|
|
|
nframes_t
|
|
|
|
Control_Sequence::play ( sample_t *buf, nframes_t frame, nframes_t nframes )
|
|
|
|
{
|
|
|
|
Control_Point *p2, *p1 = (Control_Point*)&_widgets.front();
|
|
|
|
|
|
|
|
nframes_t n = nframes;
|
|
|
|
|
|
|
|
for ( list <Sequence_Widget *>::const_iterator i = _widgets.begin();
|
|
|
|
i != _widgets.end(); ++i, p1 = p2 )
|
|
|
|
{
|
|
|
|
p2 = (Control_Point*)(*i);
|
|
|
|
|
|
|
|
if ( p2->when() < frame )
|
|
|
|
continue;
|
|
|
|
|
2008-04-29 04:21:17 +02:00
|
|
|
/* do incremental linear interpolation */
|
2008-04-28 16:20:17 +02:00
|
|
|
|
2008-04-29 04:21:17 +02:00
|
|
|
const nframes_t len = p2->when() - p1->when();
|
2008-04-28 16:20:17 +02:00
|
|
|
|
2008-04-29 04:21:17 +02:00
|
|
|
/* scale to -1.0 to 1.0 */
|
|
|
|
const float y1 = 1.0f - ( 2.0f * p1->control() );
|
|
|
|
const float y2 = 1.0f - ( 2.0f * p2->control() );
|
|
|
|
|
|
|
|
const nframes_t start = frame - p1->when();
|
|
|
|
const float incr = ( y2 - y1 ) / (float)len;
|
|
|
|
|
|
|
|
float v = y1 + start * incr;
|
|
|
|
|
|
|
|
for ( nframes_t i = start; i < len && n--; ++i, v += incr )
|
|
|
|
*(buf++) = v;
|
2008-04-28 16:20:17 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-04-29 04:21:17 +02:00
|
|
|
return nframes - n;
|
2008-04-28 16:20:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* THREAD: RT */
|
|
|
|
nframes_t
|
|
|
|
Control_Sequence::process ( nframes_t nframes )
|
|
|
|
{
|
2008-04-29 20:30:54 +02:00
|
|
|
if ( _output->connected() ) /* don't waste CPU on disconnected ports */
|
|
|
|
{
|
|
|
|
void *buf = _output->buffer( nframes );
|
2008-04-28 16:20:17 +02:00
|
|
|
|
2008-04-29 20:30:54 +02:00
|
|
|
return play( (sample_t*)buf, transport->frame, nframes );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return nframes;
|
2008-04-28 16:20:17 +02:00
|
|
|
}
|