non/Timeline/Region.C

953 lines
24 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. */
/*******************************************************************************/
2008-04-19 06:16:21 +02:00
#include "Sequence.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 "dsp.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 <FL/Fl_Menu_Item.H>
#include <stdio.h>
2008-02-14 08:40:47 +01:00
#include <algorithm>
2008-04-14 06:01:08 +02:00
// using std::algorithm;
using namespace std;
2008-02-21 17:20:36 +01:00
extern Timeline *timeline;
2008-04-14 10:55:22 +02:00
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;
_fade_in.length = 256;
_fade_in.type = Fade::Sigmoid;
_fade_out = _fade_in;
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
{
2008-04-19 06:16:21 +02:00
*((Sequence_Widget*)this) = (Sequence_Widget &)rhs;
2008-03-01 16:25:27 +01:00
_clip = rhs._clip;
_scale = rhs._scale;
_fade_in = rhs._fade_in;
_fade_out = rhs._fade_out;
log_create();
2008-02-16 08:46:43 +01:00
}
2008-04-19 06:16:21 +02:00
Sequence_Widget *
Region::clone ( const Sequence_Widget *r )
2008-03-31 12:00:47 +02:00
{
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 */
2008-04-19 06:16:21 +02:00
Region::Region ( Audio_File *c, Sequence *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(), '/' );
if ( ! s )
s = _clip->name();
2008-03-01 16:25:27 +01:00
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
}
}
/* convert a screen x coord into an offset into the region */
#define x_to_offset( X ) ( timeline->x_to_ts( scroll_x() + ( (X) - _track->x() ) ) - _r->offset )
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-04-19 06:16:21 +02:00
Sequence_Widget::handle( m );
redraw();
break;
case FL_LEAVE:
2008-04-19 06:16:21 +02:00
Sequence_Widget::handle( m );
redraw();
break;
case FL_KEYBOARD:
{
if ( Fl::event_key() == FL_F + 3 )
{
nframes_t offset = x_to_offset( X );
if ( offset < length() )
_fade_in.length = offset;
printf( "setting fade in length to %lu\n", _fade_in.length );
}
else
if ( Fl::event_key() == FL_F + 4 )
{
long offset = length() - x_to_offset( X );
if ( offset > 0 )
_fade_out.length = offset;
printf( "setting fade out length to %lu\n", _fade_in.length );
}
redraw();
return 1;
}
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-04-19 06:16:21 +02:00
if ( Sequence_Widget::current() == this )
2008-03-04 01:35:40 +01:00
{
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
}
else if ( Fl::event_button1() && Fl::event_ctrl() )
{
/* duplication */
return 1;
}
else if ( Fl::event_button3() && ! Fl::event_ctrl() )
{
/* context menu */
Fade::fade_type_e it = _fade_in.type;
Fade::fade_type_e ot = _fade_out.type;
Fl_Menu_Item menu[] =
{
{ "Fade", 0, 0, 0, FL_SUBMENU },
{ "In", 0, 0, 0, FL_SUBMENU },
{ "Linear", 0, 0, 0, FL_MENU_RADIO | ( it == Fade::Linear ? FL_MENU_VALUE : 0 ) },
{ "Sigmoid", 0, 0, 0, FL_MENU_RADIO | ( it == Fade::Sigmoid ? FL_MENU_VALUE : 0 ) },
{ "Logarithmic", 0, 0, 0, FL_MENU_RADIO | ( it == Fade::Logarithmic ? FL_MENU_VALUE : 0 ) },
{ "Parabolic", 0, 0, 0, FL_MENU_RADIO | ( it == Fade::Parabolic ? FL_MENU_VALUE : 0 ) },
{ 0 },
{ "Out", 0, 0, 0, FL_SUBMENU },
{ "Linear", 0, 0, 0, FL_MENU_RADIO | ( ot == Fade::Linear ? FL_MENU_VALUE : 0 ) },
{ "Sigmoid", 0, 0, 0, FL_MENU_RADIO | ( ot == Fade::Sigmoid ? FL_MENU_VALUE : 0 ) },
{ "Logarothmic", 0, 0, 0, FL_MENU_RADIO | ( ot == Fade::Logarithmic ? FL_MENU_VALUE : 0 ) },
{ "Parabolic", 0, 0, 0, FL_MENU_RADIO | ( ot == Fade::Parabolic ? FL_MENU_VALUE : 0 ) },
{ 0 },
{ 0 },
{ 0 },
};
const Fl_Menu_Item *r = menu->popup( X, Y, "Region" );
if ( r )
{
if ( r > &menu[1] && r < &menu[6] )
_fade_in.type = (Fade::fade_type_e)(int)(r - &menu[2]);
else if ( r > &menu[7] && r < &menu[12] )
_fade_out.type = (Fade::fade_type_e)(int)(r - &menu[8]);
}
return 1;
}
2008-03-05 23:51:04 +01:00
else
2008-04-19 06:16:21 +02:00
return Sequence_Widget::handle( m );
2008-02-20 07:35:29 +01:00
}
break;
}
case FL_RELEASE:
2008-03-05 03:12:54 +01:00
{
2008-04-19 06:16:21 +02:00
Sequence_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-04-19 06:16:21 +02:00
ret = Sequence_Widget::handle( m );
2008-02-21 03:06:39 +01:00
return ret | 1;
2008-02-20 07:35:29 +01:00
default:
2008-04-19 06:16:21 +02:00
return Sequence_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
/** Draws the curve for a single fade. /X/ and /W/ repersent the
2008-04-16 07:50:32 +02:00
portion of the region covered by this draw, which may or may not
cover the fade in question. */
void
2008-04-15 10:53:36 +02:00
Region::draw_fade ( const Fade &fade, Fade::fade_dir_e dir, bool line, int X, int W )
{
const int dy = y() + Fl::box_dy( box() );
const int dh = h() - Fl::box_dh( box() );
const int height = dh;
const int width = timeline->ts_to_x( fade.length );
2008-04-02 08:49:24 +02:00
fl_color( fl_lighter( FL_BLACK ) );
2008-02-14 08:40:47 +01:00
fl_push_matrix();
2008-02-21 08:38:31 +01:00
if ( dir == Fade::In )
fl_translate( line_x(), dy );
2008-02-21 08:38:31 +01:00
else
{
fl_translate( line_x() + abs_w(), dy );
/* flip */
fl_scale( -1.0, 1.0 );
}
2008-04-15 10:53:36 +02:00
fl_scale( width, height );
if ( line )
fl_begin_line();
2008-04-15 10:53:36 +02:00
else
fl_begin_polygon();
fl_vertex( 0.0, 0.0 );
fl_vertex( 0.0, 1.0 );
2008-04-16 07:50:32 +02:00
// if ( draw_real_fade_curve )
{
nframes_t tsx = timeline->x_to_ts( 1 );
nframes_t ts = 0;
for ( int i = 0; i < width; ++i, ts += tsx )
fl_vertex( i / (float)width, 1.0f - fade.gain( ts / (float)fade.length ) );
}
2008-04-15 16:01:21 +02:00
fl_vertex( 1.0, 0.0 );
2008-04-15 10:53:36 +02:00
if ( line )
fl_end_line();
2008-04-15 10:53:36 +02:00
else
fl_end_polygon();
fl_pop_matrix();
}
void
Region::draw_box( void )
{
/* dirty hack to keep the box from flipping to vertical at small sizes */
fl_push_clip( x(), y(), w(), h() );
if ( selected() )
fl_draw_box( fl_down( box() ), x() - 10, y(), w() + 50, h(), _selection_color );
// fl_draw_box( fl_down( box() ), x() - 10, Y, w() + 50, H, fl_invert_color( _box_color ) );
else
fl_draw_box( box(), x() - 10, y(), w() + 50, h(), _box_color );
/* draw fades */
draw_fade( _fade_in, Fade::In, false, x(), w() );
draw_fade( _fade_out, Fade::Out, false, x(), w() );
fl_pop_clip();
}
2008-04-17 15:27:35 +02:00
/** Draw (part of) region. X, Y, W and H are the rectangle we're clipped to. */
2008-02-14 08:40:47 +01:00
void
Region::draw ( void )
2008-02-14 08:40:47 +01:00
{
2008-04-17 17:19:04 +02:00
/* intersect clip with region */
int X, Y, W, H;
2008-04-17 17:19:04 +02:00
fl_clip_box( x(), y(), w(), h(), X, Y, W, H );
2008-02-20 11:45:10 +01:00
if ( ! ( W > 0 && H > 0 ) )
/* no coverage */
2008-02-20 11:45:10 +01:00
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-04-17 15:27:35 +02:00
/* not in viewport */
2008-02-20 11:45:10 +01:00
return;
int rw = timeline->ts_to_x( _r->end - _r->start );
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
2008-04-17 17:19:04 +02:00
rw -= OX - ox;
2008-02-20 11:45:10 +01:00
}
rw = min( rw, _track->w() );
int rx = x();
fl_push_clip( rx, Y, rw, H );
2008-02-20 08:53:54 +01:00
/* get actual peak data */
int channels;
int peaks;
Peak *pbuf;
2008-02-20 13:45:02 +01:00
2008-02-14 08:40:47 +01:00
2008-04-17 15:27:35 +02:00
// const nframes_t start = _r->start + offset + timeline->x_to_ts( X - rx );
nframes_t start = _r->start + offset;
2008-02-16 05:23:58 +01:00
2008-04-17 17:19:04 +02:00
/* compensate for ??? */
if ( X - rx > 0 )
start += timeline->x_to_ts( X - rx );
2008-04-17 15:27:35 +02:00
if ( _clip->read_peaks( timeline->fpp(),
start,
start + timeline->x_to_ts( W ),
2008-04-17 15:27:35 +02:00
&peaks, &pbuf, &channels ) )
{
2008-04-17 15:27:35 +02:00
assert( pbuf );
/* draw fade curve outlines--this is only here because of crossfades */
draw_fade( _fade_in, Fade::In, true, X, W );
draw_fade( _fade_out, Fade::Out, true, X, W );
int ch = (h() - Fl::box_dh( box() )) / channels;
2008-04-17 18:12:22 +02:00
/* scale it */
for ( int j = peaks * channels; j--; )
{
2008-04-17 18:12:22 +02:00
pbuf[ j ].min *= _scale;
pbuf[ j ].max *= _scale;
}
2008-04-17 15:27:35 +02:00
2008-04-17 18:12:22 +02:00
for ( int i = 0; i < channels; ++i )
{
// Peak *pb = pbuf + (peaks * i);
/* int fw = timeline->ts_to_x( fade.length ); */
/* /\* if ( draw_fade_waveform ) *\/ */
/* for ( int j = min( fw, peaks ); j--; ) */
/* { */
/* const float g = fade.gain( j * timeline->fpp() ); */
/* pb[ j ].min *= g; */
/* pb[ j ].max *= g; */
/* } */
Waveform::draw( X,
2008-04-17 17:19:04 +02:00
(y() + Fl::box_dy( box() )) + (i * ch),
W,
2008-04-17 17:19:04 +02:00
ch,
2008-04-17 18:12:22 +02:00
pbuf + i, peaks, channels,
2008-04-17 15:27:35 +02:00
selected() ? fl_invert_color( _color ) : _color );
}
}
2008-04-14 10:55:22 +02: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
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
2008-04-14 06:01:08 +02:00
2008-04-14 10:55:22 +02:00
2008-04-14 06:01:08 +02:00
/**********/
/* Engine */
/**********/
/** Apply a (portion of) fade from /start/ to /end/ assuming a
2008-04-14 06:01:08 +02:00
* buffer size of /nframes/. /start/ and /end/ are relative to the
* given buffer, and /start/ may be negative. */
void
Region::Fade::apply ( sample_t *buf, Region::Fade::fade_dir_e dir, long start, nframes_t end, nframes_t nframes ) const
2008-04-14 06:01:08 +02:00
{
2008-04-16 07:50:32 +02:00
// printf( "apply fade %s: start=%ld end=%lu\n", dir == Fade::Out ? "out" : "in", start, end );
2008-04-14 06:01:08 +02:00
if ( ! nframes )
return;
2008-04-16 07:50:32 +02:00
const nframes_t i = start > 0 ? start : 0;
const nframes_t e = end > nframes ? nframes : end;
assert( i < nframes );
2008-04-16 07:50:32 +02:00
const float inc = increment();
float fi = ( i - start ) / (float)length;
// buf += i;
buf = &buf[ i ];
2008-04-16 07:50:32 +02:00
nframes_t n = e - i;
2008-04-14 06:01:08 +02:00
assert( i + n <= nframes );
if ( dir == Fade::Out )
2008-04-16 07:50:32 +02:00
{
fi = 1.0f - fi;
for ( ; n--; fi -= inc )
*(buf++) *= gain( fi );
}
2008-04-14 06:01:08 +02:00
else
2008-04-16 07:50:32 +02:00
for ( ; n--; fi += inc )
*(buf++) *= gain( fi );
2008-04-14 06:01:08 +02:00
}
#if 0
/** Compute the gain value (0 to 1f) for a fade-in/out curve of /type/
* (LINEAR, QUADRAIC, CUBIC), of /nframes/ in length at point
* /offset/ */
static inline
float gain_on_curve ( int type, int dir, nframes_t nframes, nframes_t offset, nframes_t length )
{
float a, b;
/* FIXME: these first two sections should *definitely* be cached */
/* calculate coefficients */
if ( dir == FADE_OUT )
{
a = -1.0f / (double)nframes;
/* fixme why would we need to know the clip length? */
b = length / (double)nframes;
// b = nframes;
}
else
{
a = 1.0f / (double)nframes;
b = 0.0f;
}
float c[4];
/* interpolate points */
switch ( type )
{
case Linear:
c[1] = a;
c[0] = b;
break;
case Quadratic:
c[2] = a * a;
c[1] = 2.0f * a * b;
c[0] = b * b;
break;
case Cubic:
{
const float a2 = a * a;
const float b2 = b * b;
c[3] = a * a2;
c[2] = 3.0f * a2 * b;
c[1] = 3.0f * a * b2;
c[0] = b * b2;
break;
}
default:
printf( "unknown curve order\n" );
}
/* now get the gain for the given point */
const float f = offset;
const float f2 = f * f;
float g = 1.0f;
switch ( type )
{
case Linear:
g *= c[1] * f + c[0];
break;
case Quadratic:
g *= c[2] * f2 + c[1] * f + c[0];
break;
case Cubic:
g *= c[3] * f2 * f + c[2] * f2 + c[1] * f + c[0];
break;
}
printf( "gain for %lu is %f\n", offset, g );
return g;
}
#endif
/* THREAD: IO */
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
2008-04-16 07:50:32 +02:00
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? */
2008-04-07 09:29:30 +02:00
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;
2008-04-07 09:29:30 +02:00
const nframes_t length = r.end - r.start;
/* do nothing if we aren't covered by this frame range */
if ( pos > r.offset + length || pos + nframes < r.offset )
2008-04-07 09:29:30 +02:00
return 0;
/* calculate offsets into file and sample buffer */
nframes_t sofs, ofs, cnt;
2008-04-09 04:20:44 +02:00
cnt = nframes;
2008-04-07 09:29:30 +02:00
if ( pos < r.offset )
{
sofs = 0;
ofs = r.offset - pos;
2008-04-09 04:20:44 +02:00
cnt -= ofs;
2008-04-07 09:29:30 +02:00
}
else
{
ofs = 0;
sofs = pos - r.offset;
}
2008-04-09 04:20:44 +02:00
if ( ofs >= nframes )
2008-04-07 09:29:30 +02:00
return 0;
2008-04-09 04:20:44 +02:00
// const nframes_t start = ofs + r.start + sofs;
const nframes_t start = r.start + sofs;
const nframes_t len = min( cnt, nframes - ofs );
2008-04-07 09:29:30 +02:00
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? */
2008-04-09 04:20:44 +02:00
// printf( "reading region ofs = %lu, sofs = %lu, %lu-%lu\n", ofs, sofs, start, end );
2008-04-07 09:29:30 +02:00
cnt = _clip->read( buf + ofs, channel, start, end );
/* apply gain */
buffer_apply_gain( buf, cnt, _scale );
2008-04-09 04:20:44 +02:00
2008-04-14 06:01:08 +02:00
/* perform declicking if necessary */
/* FIXME: keep the declick defults someplace else */
Fade declick;
declick.length = 256;
declick.type = Fade::Linear;
2008-04-14 06:01:08 +02:00
{
Fade fade;
2008-04-14 06:01:08 +02:00
fade = declick < _fade_in ? _fade_in : declick;
2008-04-14 06:01:08 +02:00
/* do fade in if necessary */
if ( sofs < fade.length )
{
const long d = 0 - sofs;
2008-04-14 06:01:08 +02:00
fade.apply( buf + ofs, Fade::In, d, d + fade.length, cnt - ofs );
}
fade = declick < _fade_out ? _fade_out : declick;
2008-04-14 06:01:08 +02:00
/* do fade out if necessary */
// if ( start + cnt + fade.length > r.end )
if ( start + fade.length > r.end )
{
const nframes_t d = r.end - start;
fade.apply( buf, Fade::Out, cnt + (long)d - fade.length, cnt + d, cnt );
}
}
2008-04-09 04:20:44 +02:00
// printf( "read %lu frames\n", cnt );
2008-04-07 09:29:30 +02:00
return cnt;
}
/* THREAD: IO */
/** write /nframes/ from /buf/ to source. /buf/ is interleaved and
must match the channel layout of the write source! */
nframes_t
Region::write ( sample_t *buf, nframes_t nframes )
{
nframes_t l = _clip->write( buf, nframes );
_range.end += l;
/* FIXME: too much? */
// _track->damage( FL_DAMAGE_EXPOSE, x() + w(), y(), 10/* FIXME: guess */, h() );
redraw();
return l;
}
2008-04-19 21:21:49 +02:00
/** finalize region capture. Assumes that this *is* a captured region
and that no other regions refer to the same source */
bool
Region::finalize ( void )
{
_clip->close();
_clip->open();
return true;
}