non/Timeline/Timeline.C

706 lines
17 KiB
C++
Raw Normal View History

/*******************************************************************************/
/* 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 "Timeline.H"
#include "Tempo_Track.H"
2008-02-21 17:20:36 +01:00
#include "Time_Track.H"
#include "Audio_Track.H"
2008-03-07 05:47:52 +01:00
#include "Control_Track.H"
2008-02-26 05:58:15 +01:00
#include <FL/Fl_Scrollbar.H>
// #include <FL/Fl_Image.H>
// #include <FL/Fl_RGB_Image.H> // needed for alpha blending
#include "Track_Header.H"
2008-04-12 21:50:36 +02:00
const float UPDATE_FREQ = 0.02f;
#include "Playback_DS.H"
2008-04-12 21:50:36 +02:00
#include "Transport.H"
2008-02-26 05:58:15 +01:00
void
2008-03-06 23:45:23 +01:00
Timeline::cb_scroll ( Fl_Widget *w, void *v )
2008-02-26 05:58:15 +01:00
{
2008-03-06 23:45:23 +01:00
((Timeline*)v)->cb_scroll( w );
}
2008-02-26 05:58:15 +01:00
2008-03-06 23:45:23 +01:00
void
Timeline::cb_scroll ( Fl_Widget *w )
{
if ( w == vscroll )
2008-02-26 05:58:15 +01:00
{
2008-03-06 23:45:23 +01:00
tracks->position( tracks->x(), (rulers->y() + rulers->h()) - vscroll->value() );
2008-02-26 05:58:15 +01:00
2008-03-07 00:29:28 +01:00
yposition( vscroll->value() );
2008-02-26 05:58:15 +01:00
2008-03-07 05:47:52 +01:00
int rh = h() - rulers->h();
vscroll->value( vscroll->value(), 30, 0, max( tracks->h() - rh, rh) );
2008-02-26 05:58:15 +01:00
}
else
{
2008-03-06 23:45:23 +01:00
if ( hscroll->zoom_changed() )
{
2008-03-07 00:21:57 +01:00
_fpp = hscroll->zoom() * 1;
2008-02-27 21:04:17 +01:00
2008-03-07 00:21:57 +01:00
int maxx = ts_to_x( _length );
2008-03-06 23:45:23 +01:00
hscroll->range( 0, maxx );
2008-02-26 05:58:15 +01:00
2008-03-06 23:45:23 +01:00
redraw();
}
else
{
2008-03-07 00:29:28 +01:00
xposition( hscroll->value() );
2008-03-06 23:45:23 +01:00
}
}
2008-02-26 05:58:15 +01:00
}
2008-03-01 05:57:53 +01:00
Timeline::Timeline ( int X, int Y, int W, int H, const char* L ) : Fl_Overlay_Window( X, Y, W, H, L )
2008-02-21 17:20:36 +01:00
{
2008-02-23 07:59:10 +01:00
box( FL_FLAT_BOX );
2008-02-23 07:59:10 +01:00
xoffset = 0;
_enable_measure_lines = true;
2008-03-01 05:57:53 +01:00
X = Y = 0;
2008-02-21 17:20:36 +01:00
{
2008-02-28 21:10:41 +01:00
Scalebar *o = new Scalebar( X, Y + H - 18, W - 18, 18 );
2008-02-26 05:58:15 +01:00
2008-03-05 03:12:54 +01:00
o->range( 0, 48000 * 300 );
2008-02-26 05:58:15 +01:00
o->zoom_range( 2, 8192 );
o->zoom( 256 );
o->type( FL_HORIZONTAL );
2008-03-06 23:45:23 +01:00
o->callback( cb_scroll, this );
2008-02-26 05:58:15 +01:00
hscroll = o;
}
{
2008-02-28 21:10:41 +01:00
Fl_Scrollbar *o = new Fl_Scrollbar( X + W - 18, Y, 18, H - 18 );
2008-02-26 05:58:15 +01:00
o->type( FL_VERTICAL );
2008-02-29 06:14:40 +01:00
// o->step( 10 );
2008-03-06 23:45:23 +01:00
o->callback( cb_scroll, this );
2008-02-26 05:58:15 +01:00
vscroll = o;
}
{
Fl_Pack *o = new Fl_Pack( X + Track_Header::width(), Y, (W - Track_Header::width()) - vscroll->w(), H - hscroll->h(), "rulers" );
2008-02-21 17:20:36 +01:00
o->type( Fl_Pack::VERTICAL );
{
Tempo_Track *o = new Tempo_Track( 0, 0, 800, 24 );
o->color( FL_RED );
o->add( new Tempo_Point( 0, 120 ) );
o->add( new Tempo_Point( 56000, 250 ) );
o->label( "Tempo" );
o->align( FL_ALIGN_LEFT );
2008-02-21 17:20:36 +01:00
tempo_track = o;
// o->end();
2008-02-21 17:20:36 +01:00
}
{
Time_Track *o = new Time_Track( 0, 24, 800, 24 );
o->color( fl_color_average( FL_RED, FL_WHITE, 0.50f ) );
o->add( new Time_Point( 0, 4, 4 ) );
o->add( new Time_Point( 345344, 6, 8 ) );
o->label( "Time" );
o->align( FL_ALIGN_LEFT );
2008-02-21 17:20:36 +01:00
time_track = o;
// o->end();
2008-02-21 17:20:36 +01:00
}
2008-02-26 05:58:15 +01:00
o->size( o->w(), o->child( 0 )->h() * o->children() );
2008-02-21 17:20:36 +01:00
rulers = o;
o->end();
}
2008-02-26 05:58:15 +01:00
2008-02-21 17:20:36 +01:00
{
2008-02-26 05:58:15 +01:00
/* Fl_Scroll *o = new Fl_Scroll( 0, 24 * 2, 800, 600 - (24 * 3) ); */
/* o->type( Fl_Scroll::VERTICAL_ALWAYS ); */
2008-02-21 17:20:36 +01:00
2008-03-07 00:21:57 +01:00
_sample_rate = 44100;
_fpp = 256;
_length = _sample_rate * 60 * 2;
2008-02-21 17:20:36 +01:00
{
2008-02-26 05:58:15 +01:00
Fl_Pack *o = new Fl_Pack( X, rulers->y() + rulers->h(), W - vscroll->w(), 5000 );
2008-02-21 17:20:36 +01:00
o->type( Fl_Pack::VERTICAL );
2008-03-01 16:48:15 +01:00
o->spacing( 0 );
2008-02-21 17:20:36 +01:00
for ( int i = 1; i--; )
2008-02-21 17:20:36 +01:00
{
2008-03-01 15:23:59 +01:00
// Track_Header *t = new Track_Header( 0, 0, W, 75 );
Track_Header *t = new Track_Header( 0, 0, W, 30 );
Track *o = new Audio_Track( 0, 0, 1, 100 );
2008-03-07 05:47:52 +01:00
t->track( o );
t->add( new Audio_Track( 0, 0, 1, 100 ) );
t->add( new Audio_Track( 0, 0, 1, 100 ) );
t->add_control( new Control_Track( 0, 0, 1, 100 ) );
t->color( (Fl_Color)rand() );
2008-03-07 05:47:52 +01:00
}
2008-02-21 17:20:36 +01:00
tracks = o;
o->end();
}
2008-02-26 05:58:15 +01:00
/* scroll = o; */
/* o->end(); */
2008-02-21 17:20:36 +01:00
}
2008-04-12 21:50:36 +02:00
/* make sure scrollbars are on top */
add( vscroll );
add( hscroll );
2008-02-26 05:58:15 +01:00
vscroll->range( 0, tracks->h() );
2008-02-23 07:59:10 +01:00
redraw();
2008-02-21 17:20:36 +01:00
end();
2008-04-12 21:50:36 +02:00
Fl::add_timeout( UPDATE_FREQ, update_cb, this );
2008-02-21 17:20:36 +01:00
}
float
Timeline::beats_per_minute ( nframes_t when ) const
{
return tempo_track->beats_per_minute( when );
}
int
Timeline::beats_per_bar ( nframes_t when ) const
{
time_sig t = time_track->time( when );
return t.beats_per_bar;
}
void
Timeline::beats_per_minute ( nframes_t when, float bpm )
{
tempo_track->add( new Tempo_Point( when, bpm ) );
}
2008-02-28 21:36:46 +01:00
/** return the absolute pixel of the nearest measure line to /x/ */
int
Timeline::nearest_line ( int ix )
{
for ( int x = ix - 10; x < ix + 10; ++x )
{
2008-03-07 00:21:57 +01:00
const int measure = ts_to_x( (double)(_sample_rate * 60) / beats_per_minute( x_to_ts( x - Track_Header::width() ) + xoffset ));
2008-02-28 21:36:46 +01:00
// const int abs_x = ts_to_x( xoffset ) + x;
if ( 0 == x % measure )
return x;
}
return -1;
}
/** draw appropriate measure lines inside the given bounding box */
/* FIXME: this function *really* needs to be optimized. Currently it
searched both the time and tempo lists once for every horiontal
pixel and performs a number of calculations--this is slow. */
void
Timeline::draw_measure_lines ( int X, int Y, int W, int H, Fl_Color color )
{
if ( ! _enable_measure_lines )
return;
2008-03-01 16:48:15 +01:00
// fl_line_style( FL_DASH, 2 );
fl_line_style( FL_DASH, 0 );
2008-03-01 16:48:15 +01:00
Fl_Color beat = fl_color_average( FL_BLACK, color, 0.65f );
Fl_Color bar = fl_color_average( FL_RED, color, 0.65f );
int measure;
for ( int x = X; x < X + W; ++x )
{
2008-03-07 00:21:57 +01:00
measure = ts_to_x( (double)(_sample_rate * 60) / beats_per_minute( x_to_ts( x - Track_Header::width() ) + xoffset ));
2008-02-21 13:57:33 +01:00
const int abs_x = ts_to_x( xoffset ) + x - Track_Header::width();
2008-02-28 17:43:21 +01:00
2008-02-28 19:34:10 +01:00
if ( 0 == abs_x % measure )
2008-02-28 17:43:21 +01:00
{
int bpb = beats_per_bar( x_to_ts( x -Track_Header::width() ) + xoffset );
2008-02-28 17:43:21 +01:00
2008-02-28 19:34:10 +01:00
if ( 0 == (abs_x / measure) % bpb )
{
if ( measure * bpb < 8 )
break;
fl_color( bar );
}
else
{
if ( measure < 8 )
continue;
fl_color( beat );
}
2008-02-28 17:43:21 +01:00
2008-02-21 13:57:33 +01:00
fl_line( x, Y, x, Y + H );
2008-02-28 19:34:10 +01:00
}
}
fl_line_style( FL_SOLID, 0 );
}
2008-02-28 19:34:10 +01:00
void
2008-03-07 00:29:28 +01:00
Timeline::xposition ( int X )
2008-02-28 19:34:10 +01:00
{
2008-03-07 00:29:28 +01:00
// _old_xposition = xoffset;
2008-02-28 19:34:10 +01:00
xoffset = x_to_ts( X );
damage( FL_DAMAGE_SCROLL );
}
2008-03-07 00:29:28 +01:00
void
Timeline::yposition ( int Y )
{
// _old_yposition = _yposition;
_yposition = Y;
damage( FL_DAMAGE_SCROLL );
}
2008-02-28 19:34:10 +01:00
void
Timeline::draw_clip ( void * v, int X, int Y, int W, int H )
{
Timeline *tl = (Timeline *)v;
// printf( "draw_clip: %d,%d %dx%d\n", X, Y, W, H );
fl_push_clip( X, Y, W, H );
fl_color( rand() );
fl_rectf( X, Y, X + W, Y + H );
2008-02-28 19:34:10 +01:00
tl->draw_child( *tl->rulers );
/* headers */
fl_push_clip( tl->tracks->x(), tl->rulers->y() + tl->rulers->h(), Track_Header::width(), tl->h() - tl->rulers->h() - tl->hscroll->h() );
tl->draw_child( *tl->tracks );
fl_pop_clip();
/* track bodies */
fl_push_clip( tl->tracks->x() + Track_Header::width(), tl->rulers->y() + tl->rulers->h(), tl->tracks->w() - Track_Header::width(), tl->h() - tl->rulers->h() - tl->hscroll->h() );
tl->draw_child( *tl->tracks );
fl_pop_clip();
// tl->draw_child( *tl->tracks );
2008-02-28 19:34:10 +01:00
fl_pop_clip();
}
static unsigned char *rect_image;
2008-02-28 19:34:10 +01:00
void
Timeline::draw ( void )
{
int X, Y, W, H;
X = tracks->x() + Fl::box_dx( tracks->child( 0 )->box() ) + 1;
Y = tracks->y();
W = tracks->w() - Fl::box_dw( tracks->child( 0 )->box() ) - 1;
H = tracks->h();
/* if ( damage() & FL_DAMAGE_USER1 ) */
/* { */
/* /\* save the rectangle so we can draw it (darkened) in the overlay *\/ */
/* Rectangle &r = _selection; */
/* make_current(); */
/* rect_image = fl_read_image( NULL, r.x, r.y, r.w, r.h, 0 ); */
/* return; */
/* } */
2008-03-01 16:48:15 +01:00
if ( (damage() & FL_DAMAGE_ALL)
||
damage() & FL_DAMAGE_EXPOSE )
2008-02-28 19:34:10 +01:00
{
2008-03-01 05:57:53 +01:00
draw_box( box(), 0, 0, w(), h(), color() );
2008-02-28 19:34:10 +01:00
fl_push_clip( 0, rulers->y(), w(), rulers->h() );
2008-02-28 19:34:10 +01:00
draw_child( *rulers );
fl_pop_clip();
2008-03-01 16:48:15 +01:00
fl_push_clip( tracks->x(), rulers->y() + rulers->h(), tracks->w(), hscroll->y() - (rulers->y() + rulers->h()) );
2008-02-28 19:34:10 +01:00
draw_child( *tracks );
fl_pop_clip();
draw_child( *hscroll );
draw_child( *vscroll );
2008-03-05 20:06:41 +01:00
redraw_overlay();
/* Rectangle &r = _selection; */
/* unsigned char *data = fl_read_image( NULL, r.x, r.y, r.w, r.h, 0 ); */
/* Fl_RGB_Image bi( data, r.w, r.h, 3 ); */
/* bi.color_average( FL_BLACK, 0.50f ); */
/* bi.draw( r.x, r.y ); */
/* delete[] data; */
/* if ( r.w && r.h ) */
/* { */
/* const unsigned char data[] = { 0, 127, 0, 96, */
/* 0, 96, 0, 127 }; */
/* Fl_RGB_Image bi( data, 2, 2, 2 ); */
/* Fl_Image *bi2 = bi.copy( r.w, r.h ); */
/* bi2->draw( r.x, r.y ); */
/* delete bi2; */
/* } */
2008-02-28 19:34:10 +01:00
return;
}
if ( damage() & FL_DAMAGE_CHILD )
{
2008-03-01 16:48:15 +01:00
// draw_box( box(), 0, 0, w(), h(), color() );
fl_push_clip( rulers->x(), rulers->y(), rulers->w() - vscroll->w(), rulers->h() );
2008-02-28 19:34:10 +01:00
update_child( *rulers );
fl_pop_clip();
fl_push_clip( tracks->x(), rulers->y() + rulers->h(), tracks->w(), hscroll->y() - (rulers->y() + rulers->h()) );
2008-02-28 19:34:10 +01:00
update_child( *tracks );
fl_pop_clip();
update_child( *hscroll );
update_child( *vscroll );
}
if ( damage() & FL_DAMAGE_SCROLL )
{
int dx = ts_to_x( _old_xposition ) - ts_to_x( xoffset );
2008-03-07 00:29:28 +01:00
int dy = _old_yposition - _yposition;
2008-02-28 19:34:10 +01:00
if ( ! dy )
fl_scroll( X + Track_Header::width(), rulers->y(), rulers->w() - Fl::box_dw( rulers->child(0)->box() ), rulers->h(), dx, 0, draw_clip, this );
2008-02-28 19:34:10 +01:00
Y = rulers->y() + rulers->h();
H = h() - rulers->h() - hscroll->h();
if ( dy == 0 )
fl_scroll( X + Track_Header::width(), Y, W - Track_Header::width(), H, dx, dy, draw_clip, this );
else
fl_scroll( X, Y, W, H, dx, dy, draw_clip, this );
2008-02-28 19:34:10 +01:00
_old_xposition = xoffset;
2008-03-07 00:29:28 +01:00
_old_yposition = _yposition;
2008-02-28 19:34:10 +01:00
}
2008-02-28 19:34:10 +01:00
}
2008-04-12 21:50:36 +02:00
void
Timeline::draw_playhead ( void )
{
int x = ( ts_to_x( transport.frame ) - ts_to_x( xoffset ) ) + tracks->x() + Track_Header::width();
2008-04-12 23:47:33 +02:00
if ( x < tracks->x() + Track_Header::width() || x > tracks->x() + tracks->w() )
2008-04-12 21:50:36 +02:00
return;
fl_color( FL_RED );
2008-04-12 23:47:33 +02:00
int y = rulers->y() + rulers->h();
int h = this->h() - hscroll->h();
fl_rectf( x - 2, y, 5, 2 );
fl_line( x, y, x, h );
2008-04-12 21:50:36 +02:00
}
void
Timeline::redraw_playhead ( void )
{
static nframes_t last_playhead = -1;
if ( last_playhead != transport.frame )
{
redraw_overlay();
last_playhead = transport.frame;
}
2008-04-12 21:50:36 +02:00
}
/** called so many times a second to redraw the playhead etc. */
void
Timeline::update_cb ( void *arg )
{
Fl::repeat_timeout( UPDATE_FREQ, update_cb, arg );
Timeline *tl = (Timeline *)arg;
tl->redraw_playhead();
}
2008-03-01 05:57:53 +01:00
void
Timeline::draw_overlay ( void )
{
2008-04-12 21:50:36 +02:00
draw_playhead();
if ( ! ( _selection.w && _selection.h ) )
return;
2008-03-01 05:57:53 +01:00
fl_push_clip( tracks->x() + Track_Header::width(), rulers->y() + rulers->h(), tracks->w() - Track_Header::width(), h() - rulers->h() - hscroll->h() );
const Rectangle &r = _selection;
fl_color( FL_BLACK );
2008-03-07 00:21:57 +01:00
fl_line_style( FL_SOLID, 2 );
2008-03-07 00:21:57 +01:00
fl_rect( r.x + 2, r.y + 2, r.w, r.h );
fl_color( FL_MAGENTA );
fl_line_style( FL_DASH, 2 );
fl_rect( r.x, r.y, r.w, r.h );
2008-03-07 00:21:57 +01:00
fl_line( r.x, r.y, r.x + r.w, r.y + r.h );
fl_line( r.x + r.w, r.y, r.x, r.y + r.h );
/* fl_overlay_rect( r.x, r.y, r.w, r.h ); */
2008-03-01 05:57:53 +01:00
2008-03-05 20:06:41 +01:00
fl_line_style( FL_SOLID, 0 );
2008-03-01 05:57:53 +01:00
/* const unsigned char data[] = { 127, 127, 127, 96, */
/* 127, 96, 127, 40 }; */
/* Fl_RGB_Image bi( data, 2, 2, 2 ); */
/* Fl_Image *bi2 = bi.copy( r.w, r.h ); */
/* bi2->draw( r.x, r.y ); */
/* delete bi2; */
/* unsigned char *data = fl_read_image( NULL, r.x, r.y, r.w, r.h, 0 ); */
/* Fl_RGB_Image bi( rect_image, r.w, r.h, 3 ); */
/* bi.color_average( FL_BLACK, 0.50f ); */
/* bi.draw( r.x, r.y ); */
/* delete[] rect_image; */
/* rect_image = NULL; */
fl_pop_clip();
2008-03-05 20:06:41 +01:00
}
2008-03-01 05:57:53 +01:00
2008-03-05 20:06:41 +01:00
// #include "Track_Widget.H"
2008-03-01 05:57:53 +01:00
2008-03-05 20:06:41 +01:00
/** select all widgets in inside rectangle /r/ */
void
Timeline::select( const Rectangle &r )
{
const int Y = r.y;
2008-03-05 20:06:41 +01:00
for ( int i = tracks->children(); i-- ; )
{
Track_Header *t = (Track_Header*)tracks->child( i );
2008-03-01 05:57:53 +01:00
if ( ! ( t->y() > Y + r.h || t->y() + t->h() < Y ) )
2008-03-05 20:06:41 +01:00
t->track()->select_range( r.x, r.w );
}
2008-03-01 05:57:53 +01:00
}
2008-02-28 19:34:10 +01:00
int
Timeline::handle ( int m )
{
2008-03-05 20:06:41 +01:00
static Drag *drag = NULL;
2008-03-01 15:23:59 +01:00
2008-02-28 19:34:10 +01:00
switch ( m )
{
case FL_KEYBOARD:
{
switch ( Fl::event_key() )
{
case FL_Delete:
{
2008-03-03 22:00:38 +01:00
Track_Widget::delete_selected();
return 1;
}
default:
return Fl_Overlay_Window::handle( m );
}
2008-03-01 15:23:59 +01:00
2008-03-05 20:06:41 +01:00
return 0;
}
default:
{
int r = Fl_Overlay_Window::handle( m );
2008-03-01 15:23:59 +01:00
if ( m != FL_RELEASE && r )
2008-03-05 20:06:41 +01:00
return r;
2008-03-01 15:23:59 +01:00
2008-03-05 20:06:41 +01:00
const int X = Fl::event_x();
const int Y = Fl::event_y();
2008-03-01 15:23:59 +01:00
2008-03-05 20:06:41 +01:00
switch ( m )
{
case FL_PUSH:
{
take_focus();
if ( ! Fl::event_button1() )
return 0;
assert( ! drag );
2008-03-05 20:06:41 +01:00
drag = new Drag( X - x(), Y - y() );
_selection.x = drag->x;
_selection.y = drag->y;
break;
}
case FL_DRAG:
{
int ox = X - drag->x;
int oy = Y - drag->y;
if ( ox < 0 )
_selection.x = X;
if ( oy < 0 )
_selection.y = Y;
_selection.w = abs( ox );
_selection.h = abs( oy );
2008-03-05 20:06:41 +01:00
break;
}
case FL_RELEASE:
{
delete drag;
drag = NULL;
2008-03-01 15:23:59 +01:00
2008-03-05 20:06:41 +01:00
select( _selection );
2008-03-01 15:23:59 +01:00
2008-03-05 20:06:41 +01:00
_selection.w = _selection.h = 0;
break;
}
default:
return 0;
break;
}
2008-03-01 15:23:59 +01:00
2008-03-05 20:06:41 +01:00
redraw_overlay();
return 1;
}
2008-02-28 19:34:10 +01:00
}
2008-03-01 15:23:59 +01:00
2008-02-28 19:34:10 +01:00
}
/**********/
/* Engine */
/**********/
/** call process() on each track header */
nframes_t
Timeline::process ( nframes_t nframes )
{
for ( int i = tracks->children(); i-- ; )
{
Track_Header *t = (Track_Header*)tracks->child( i );
t->process( nframes );
}
/* FIXME: BOGUS */
return nframes;
}
/* THREAD: RT */
void
Timeline::seek ( nframes_t frame )
{
for ( int i = tracks->children(); i-- ; )
{
Track_Header *t = (Track_Header*)tracks->child( i );
t->seek( frame );
}
}
/* THREAD: RT */
int
Timeline::seek_pending ( void )
{
int r = 0;
for ( int i = tracks->children(); i-- ; )
{
Track_Header *t = (Track_Header*)tracks->child( i );
if ( t->playback_ds )
r += t->playback_ds->buffer_percent() < 50;
}
}