non/Timeline/Region.C

603 lines
15 KiB
C++
Raw Normal View History

2008-02-14 08:40:47 +01: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 "Track.H"
2008-02-14 08:40:47 +01:00
#include "Region.H"
#include "Timeline.H"
#include "Waveform.H"
2008-02-14 08:40:47 +01:00
#include <FL/fl_draw.H>
#include <FL/Fl.H>
#include <FL/Fl_Group.H>
#include <FL/Fl_Widget.H>
#include <FL/Fl_Box.H>
#include <stdio.h>
2008-02-14 08:40:47 +01:00
#include <algorithm>
//using std::algorithm;
using namespace std;
2008-02-21 17:20:36 +01:00
extern Timeline *timeline;
Fl_Boxtype Region::_box = FL_UP_BOX;
2008-02-20 13:45:02 +01:00
2008-02-21 08:38:31 +01:00
Fl_Color Region::_selection_color = FL_MAGENTA;
2008-02-21 10:48:53 +01:00
static Fl_Color fl_invert_color ( Fl_Color c )
{
unsigned char r, g, b;
Fl::get_color( c, r, g, b );
return fl_rgb_color( 255 - r, 255 - g, 255 - b );
}
2008-02-26 02:03:59 +01:00
#if 0
/* perhaps use map? */
map_PRIM ( set )
{
/* if ( narg % 2 != 0 ) */
/* printf( "invalid number of arguments\n" ); */
int id = atoi( arg );
map_ARG_NEXT( arg, end );
Logable *l = Loggable::find( id );
char **sa = malloc( sizeof( char * ) * narg + 1 );
for ( int i = 0; i < narg; ++i )
sa[ i ] = strdup( map_ARG_NEXT( arg, end ) );
l->set( sa );
map_RESULT( "" );
}
#endif
void
Region::init ( void )
2008-02-14 08:40:47 +01:00
{
_track = NULL;
_r->offset = 0;
_r->start = 0;
_r->end = 0;
_scale = 1.0f;
_clip = NULL;
2008-02-20 08:53:54 +01:00
_box_color = FL_CYAN;
_color = FL_BLUE;
2008-02-14 08:40:47 +01:00
}
2008-02-28 13:51:51 +01:00
/* copy constructor */
Region::Region ( const Region & rhs )
2008-02-16 08:46:43 +01:00
{
_r->offset = rhs._r->offset;
2008-04-02 08:49:24 +02:00
_track = rhs._track;
// _track = NULL;
2008-03-01 16:25:27 +01:00
_clip = rhs._clip;
_r->start = rhs._r->start;
_r->end = rhs._r->end;
2008-03-01 16:25:27 +01:00
_scale = rhs._scale;
_box_color = rhs._box_color;
_color = rhs._color;
log_create();
2008-02-16 08:46:43 +01:00
}
2008-03-31 12:00:47 +02:00
Track_Widget *
Region::clone ( const Track_Widget *r )
{
return new Region( *(Region*)r );
}
2008-02-28 13:51:51 +01:00
/* */
Region::Region ( Audio_File *c )
{
init();
_clip = c;
_r->end = _clip->length();
log_create();
}
2008-02-18 02:13:20 +01:00
2008-02-28 13:51:51 +01:00
/* used when DND importing */
Region::Region ( Audio_File *c, Track *t, nframes_t o )
2008-02-28 13:51:51 +01:00
{
init();
_clip = c;
_r->end = _clip->length();
2008-02-28 13:51:51 +01:00
_track = t;
_r->offset = o;
2008-02-28 13:51:51 +01:00
_track->add( this );
2008-03-01 16:25:27 +01:00
int sum = 0;
const char *s = rindex( _clip->name(), '/' );
for ( int i = strlen( s ); i--; )
sum += s[ i ];
while ( sum >> 8 )
sum = (sum & 0xFF) + (sum >> 8);
_color = (Fl_Color)sum;
/* _color = fl_color_average( FL_YELLOW, (Fl_Color)sum, 0.80 ); */
// _color = FL_YELLOW;
_box_color = FL_WHITE;
2008-02-28 13:51:51 +01:00
log_create();
}
2008-02-20 08:08:36 +01:00
void
Region::trim ( enum trim_e t, int X )
{
2008-03-05 03:12:54 +01:00
X -= _track->x();
redraw();
2008-02-20 08:08:36 +01:00
switch ( t )
{
case LEFT:
{
/* if ( d < 0 ) */
/* // _track->damage( FL_DAMAGE_EXPOSE, x() + d, y(), 1 - d, h() ); */
/* _track->damage( FL_DAMAGE_EXPOSE, x(), y(), w(), h() ); */
/* else */
/* _track->damage( FL_DAMAGE_EXPOSE, x(), y(), d, h() ); */
int d = X - ( abs_x() - scroll_x() );
2008-02-21 17:20:36 +01:00
long td = timeline->x_to_ts( d );
2008-02-18 02:13:20 +01:00
if ( td < 0 && _r->start < 0 - td )
td = 0 - _r->start;
if ( _r->start + td >= _r->end )
td = (_r->end - _r->start) - timeline->x_to_ts( 1 );
2008-02-24 03:25:58 +01:00
_r->start += td;
_r->offset += td;
2008-02-20 08:08:36 +01:00
break;
}
case RIGHT:
{
int d = (( abs_x() - scroll_x() ) + abs_w() ) - X;
/* _track->damage( FL_DAMAGE_EXPOSE, x() + w(), y(), d, h() ); */
2008-02-21 17:20:36 +01:00
long td = timeline->x_to_ts( d );
2008-02-18 02:13:20 +01:00
// printf( "%li %li\n", td, _r->end - _r->start );
2008-02-24 03:25:58 +01:00
if ( td >= 0 && _r->end - _r->start < td )
_r->end = _r->start + timeline->x_to_ts( 1 );
2008-02-24 03:25:58 +01:00
else
_r->end -= td;
2008-02-20 08:08:36 +01:00
break;
}
default:
return;
2008-02-16 05:23:58 +01:00
2008-02-20 08:08:36 +01:00
}
}
int
2008-02-20 07:35:29 +01:00
Region::handle ( int m )
{
static int ox, oy;
static enum trim_e trimming;
2008-02-20 07:35:29 +01:00
static bool copied = false;
static nframes_t os;
2008-03-05 03:12:54 +01:00
// int X = Fl::event_x() - _track->x();
int X = Fl::event_x();
2008-02-20 07:35:29 +01:00
int Y = Fl::event_y();
2008-02-21 03:06:39 +01:00
int ret;
2008-02-24 14:28:55 +01:00
Logger _log( this );
//log_r->start();
2008-02-21 03:06:39 +01:00
2008-02-20 07:35:29 +01:00
switch ( m )
{
case FL_ENTER:
2008-03-06 19:46:40 +01:00
Track_Widget::handle( m );
redraw();
break;
case FL_LEAVE:
2008-03-06 19:46:40 +01:00
Track_Widget::handle( m );
redraw();
break;
2008-02-20 07:35:29 +01:00
case FL_PUSH:
{
/* trimming / splitting */
if ( Fl::event_shift() && ! Fl::event_ctrl() )
2008-02-20 07:35:29 +01:00
{
switch ( Fl::event_button() )
{
2008-02-20 08:08:36 +01:00
case 1:
trim( trimming = LEFT, X );
begin_drag( Drag( x() - X, y() - Y ) );
_log.hold();
2008-02-20 08:08:36 +01:00
break;
case 3:
trim( trimming = RIGHT, X );
begin_drag( Drag( x() - X, y() - Y ) );
2008-03-03 22:36:33 +01:00
_log.hold();
2008-02-20 08:08:36 +01:00
break;
2008-02-24 08:48:07 +01:00
case 2:
{
/* split */
if ( ! copied )
{
Loggable::block_start();
2008-02-24 08:48:07 +01:00
Region *copy = new Region( *this );
trim( RIGHT, X );
copy->trim( LEFT, X );
_track->add( copy );
log_end();
Loggable::block_end();
2008-03-05 23:51:04 +01:00
return 0;
2008-02-24 08:48:07 +01:00
}
}
2008-02-20 07:35:29 +01:00
default:
return 0;
2008-02-24 08:42:41 +01:00
break;
2008-02-20 07:35:29 +01:00
}
2008-02-24 08:42:41 +01:00
2008-02-20 07:35:29 +01:00
fl_cursor( FL_CURSOR_WE );
return 1;
}
else
{
ox = x() - X;
oy = y() - Y;
/* for panning */
os = _r->start;
/* normalization and selection */
2008-03-05 23:51:04 +01:00
if ( Fl::event_button2() )
2008-02-21 03:10:44 +01:00
{
2008-03-05 23:51:04 +01:00
if ( Fl::event_ctrl() )
2008-02-21 10:51:07 +01:00
normalize();
else
2008-03-03 22:00:38 +01:00
{
2008-03-04 01:35:40 +01:00
if ( Track_Widget::current() == this )
{
if ( selected() )
deselect();
else
select();
}
2008-03-03 22:00:38 +01:00
}
2008-02-21 10:51:07 +01:00
2008-02-22 05:53:39 +01:00
redraw();
2008-02-24 08:42:41 +01:00
goto changed;
2008-02-21 03:10:44 +01:00
}
if ( Fl::event_button1() && Fl::event_ctrl() )
{
/* duplication */
return 1;
}
2008-03-05 23:51:04 +01:00
else
return Track_Widget::handle( m );
2008-02-20 07:35:29 +01:00
}
break;
}
case FL_RELEASE:
2008-03-05 03:12:54 +01:00
{
Track_Widget::handle( m );
2008-03-05 03:12:54 +01:00
2008-02-20 07:35:29 +01:00
copied = false;
if ( trimming != NO )
trimming = NO;
2008-03-05 03:12:54 +01:00
2008-02-24 08:42:41 +01:00
goto changed;
2008-03-05 03:12:54 +01:00
}
2008-02-20 07:35:29 +01:00
case FL_DRAG:
2008-03-05 03:12:54 +01:00
if ( ! _drag )
2008-02-24 14:28:55 +01:00
{
begin_drag( Drag( x() - X, y() - Y ) );
2008-02-24 14:28:55 +01:00
_log.hold();
}
/* panning */
2008-02-20 07:35:29 +01:00
if ( Fl::event_state() & FL_SHIFT &&
Fl::event_state() & FL_CTRL )
{
int d = (ox + X) - x();
2008-02-21 17:20:36 +01:00
long td = timeline->x_to_ts( d );
2008-02-20 07:35:29 +01:00
nframes_t W = _r->end - _r->start;
2008-02-20 12:48:42 +01:00
2008-02-20 07:35:29 +01:00
if ( td > 0 && os < td )
_r->start = 0;
2008-02-20 07:35:29 +01:00
else
_r->start = os - td;
2008-02-20 07:35:29 +01:00
_r->end = _r->start + W;
2008-02-20 12:48:42 +01:00
_track->redraw();
2008-02-20 07:35:29 +01:00
return 1;
}
/* trimming */
2008-02-20 08:08:36 +01:00
if ( Fl::event_state() & FL_SHIFT )
if ( trimming )
{
trim( trimming, X );
return 1;
}
else
return 0;
/* duplication */
2008-02-20 07:35:29 +01:00
if ( Fl::event_state() & FL_CTRL )
{
2008-03-04 04:46:48 +01:00
if ( _drag->state == 0 )
2008-02-20 07:35:29 +01:00
{
_track->add( new Region( *this ) );
2008-03-04 04:46:48 +01:00
_drag->state = 1;
2008-02-20 07:35:29 +01:00
return 1;
}
}
/* track jumping */
2008-03-03 22:36:33 +01:00
if ( ! selected() )
2008-02-20 07:35:29 +01:00
{
2008-03-03 22:36:33 +01:00
if ( Y > y() + h() )
2008-02-20 07:35:29 +01:00
{
Fl::copy( class_name(), strlen( class_name() ), 0 );
Fl::dnd();
2008-02-20 07:35:29 +01:00
}
2008-03-03 22:36:33 +01:00
else
if ( Y < y() )
{
Fl::copy( class_name(), strlen( class_name() ), 0 );
Fl::dnd();
2008-03-03 22:36:33 +01:00
}
}
2008-02-20 07:35:29 +01:00
2008-03-05 03:12:54 +01:00
ret = Track_Widget::handle( m );
2008-02-21 03:06:39 +01:00
return ret | 1;
2008-02-20 07:35:29 +01:00
default:
2008-02-21 03:06:39 +01:00
return Track_Widget::handle( m );
2008-02-20 07:35:29 +01:00
break;
}
2008-02-24 08:42:41 +01:00
changed:
return 1;
2008-02-20 07:35:29 +01:00
}
2008-02-14 08:40:47 +01:00
void
Region::draw_box( int X, int Y, int W, int H )
{
2008-04-02 08:49:24 +02:00
if ( ! shown() )
return;
/* dirty hack to keep the box from flipping to vertical at small sizes */
2008-02-14 08:40:47 +01:00
fl_push_clip( x(), Y, w(), H );
2008-02-21 08:38:31 +01:00
2008-03-03 22:00:38 +01:00
if ( selected() )
2008-02-23 07:59:10 +01:00
fl_draw_box( fl_down( box() ), x() - 10, y(), w() + 50, h(), _selection_color );
2008-02-21 10:48:53 +01:00
// fl_draw_box( fl_down( box() ), x() - 10, Y, w() + 50, H, fl_invert_color( _box_color ) );
2008-02-21 08:38:31 +01:00
else
2008-02-23 07:59:10 +01:00
fl_draw_box( box(), x() - 10, y(), w() + 50, h(), _box_color );
2008-02-21 08:38:31 +01:00
fl_pop_clip();
}
2008-02-21 01:25:21 +01:00
/* Draw (part of) region. OX is pixel offset from start of timeline, X
Y W and H are the portion of the widget to draw (arrived at by
intersection of the clip and relative to OX) */
2008-02-14 08:40:47 +01:00
void
Region::draw ( int X, int Y, int W, int H )
2008-02-14 08:40:47 +01:00
{
2008-04-02 08:49:24 +02:00
if ( ! shown() )
return;
2008-02-20 11:45:10 +01:00
if ( ! ( W > 0 && H > 0 ) )
return;
int OX = scroll_x();
int ox = timeline->ts_to_x( _r->offset );
2008-02-21 01:25:21 +01:00
if ( ox > OX + _track->w() ||
ox < OX && ox + abs_w() < OX )
2008-02-20 11:45:10 +01:00
return;
int rw = timeline->ts_to_x( _r->end - _r->start );
2008-02-20 11:45:10 +01:00
2008-04-07 09:29:30 +02:00
// nframes_t end = _r->offset + ( _r->end - _r->start );
2008-02-20 11:45:10 +01:00
/* calculate waveform offset due to scrolling */
nframes_t offset = 0;
2008-02-21 01:25:21 +01:00
if ( ox < OX )
2008-02-20 11:45:10 +01:00
{
2008-02-21 17:20:36 +01:00
offset = timeline->x_to_ts( OX - ox );
2008-02-20 11:45:10 +01:00
rw = timeline->ts_to_x( (_r->end - _r->start) - offset );
2008-02-20 11:45:10 +01:00
}
rw = min( rw, _track->w() );
int rx = x();
2008-02-20 08:53:54 +01:00
fl_push_clip( rx, Y, rw, H );
/* get actual peak data */
int channels;
int peaks;
Peak *pbuf;
2008-02-20 13:45:02 +01:00
const nframes_t start = _r->start + offset + timeline->x_to_ts( X - rx );
_clip->read_peaks( timeline->fpp(),
start,
start + timeline->x_to_ts( W ),
&peaks, &pbuf, &channels );
2008-02-14 08:40:47 +01:00
assert( pbuf );
2008-02-16 05:23:58 +01:00
int ch = (h() - Fl::box_dh( box() )) / channels;
for ( int i = 0; i < channels; ++i )
{
Peak *pb = pbuf + (peaks * i);
/* scale it */
for ( int j = peaks; j--; )
{
pb[ j ].min *= _scale;
pb[ j ].max *= _scale;
}
Waveform::draw( X, (y() + Fl::box_dy( box() )) + (i * ch), W, ch,
pb, peaks,
selected() ? fl_invert_color( _color ) : _color );
}
delete pbuf;
/* for ( int i = _clip->channels(); i--; ) */
/* Waveform::draw( rx, X, (y() + Fl::box_dy( box() )) + (i * ch), W, */
/* ch, _clip, i, timeline->fpp(), */
/* _r->start + offset, min( (_r->end - _r->start) - offset, _r->end), */
/* _scale, selected() ? fl_invert_color( _color ) : _color ); */
2008-02-14 08:40:47 +01:00
2008-03-12 17:06:13 +01:00
timeline->draw_measure_lines( rx, Y, rw, H, _box_color );
2008-02-20 13:45:02 +01:00
2008-02-20 08:53:54 +01:00
fl_color( FL_BLACK );
fl_line( rx, Y, rx, Y + H );
fl_line( rx + rw - 1, Y, rx + rw - 1, Y + H );
2008-02-16 05:23:58 +01:00
draw_label( _clip->name(), align() );
2008-03-09 05:19:05 +01:00
if ( current() )
{
/* draw length bubble */
char pat[40];
2008-03-07 00:21:57 +01:00
snprintf( pat, sizeof( pat ), "%dm:%.1fs", (int)(length() / timeline->sample_rate()) / 60, (double)length() / timeline->sample_rate() );
2008-02-20 11:45:10 +01:00
draw_label( pat, (Fl_Align)(FL_ALIGN_INSIDE | FL_ALIGN_CENTER), FL_GREEN );
}
2008-02-21 08:38:31 +01:00
2008-02-21 07:12:21 +01:00
fl_pop_clip();
2008-02-20 11:45:10 +01:00
2008-02-14 08:40:47 +01:00
}
2008-02-21 03:10:44 +01:00
void
Region::normalize ( void )
{
printf( "normalize: start=%lu end=%lu\n", _r->start, _r->end );
2008-02-21 03:10:44 +01:00
/* FIXME: figure out a way to do this via the peak server */
/* _scale = _clip->peaks( 0 )->normalization_factor( timeline->fpp(), _r->start, _r->end ); */
2008-02-21 13:57:33 +01:00
}
2008-04-07 09:29:30 +02:00
/** read the overlapping part of /channel/ at /pos/ for /nframes/ of
this region into /buf/, where /pos/ is in timeline frames */
/* this runs in the diskstream thread. */
/* FIXME: it is far more efficient to read all the channels from a
multichannel source at once... But how should we handle the case of a
mismatch between the number of channels in this region's source and
the number of channels on the track/buffer this data is being read
for? Would it not be better to simply buffer and deinterlace the
frames in the Audio_File class instead, so that sequential requests
for different channels at the same position avoid hitting the disk
again? */
/* FIXME: should fade-out/fade-ins not be handled here? */
nframes_t
2008-04-07 12:00:16 +02:00
Region::read ( sample_t *buf, nframes_t pos, nframes_t nframes, int channel ) const
2008-04-07 09:29:30 +02:00
{
const Range &r = _range;
/* do nothing if we aren't covered by this frame range */
const nframes_t length = r.end - r.start;
if ( ! ( pos > r.offset + length || r.offset + length < pos ) )
return 0;
/* calculate offsets into file and sample buffer */
nframes_t sofs, ofs, cnt;
if ( pos < r.offset )
{
sofs = 0;
ofs = r.offset - pos;
cnt = nframes - ofs;
}
else
{
ofs = 0;
sofs = pos - r.offset;
}
if ( sofs > nframes )
return 0;
const nframes_t start = ofs + r.start + sofs;
const nframes_t len = min( cnt, nframes - sofs );
const nframes_t end = start + len;
if ( len == 0 )
return 0;
/* now that we know how much and where to read, get on with it */
/* FIXME: seeking can be very expensive. Esp. with compressed
* formats. We should attempt to avoid it. But here or in the
* Audio_File class? */
cnt = _clip->read( buf + ofs, channel, start, end );
/* apply gain */
for ( int i = cnt; i--; )
buf[i] *= _scale;
return cnt;
}