Add normalization function.

This commit is contained in:
Jonathan Moore Liles 2008-02-16 04:30:56 -06:00
parent a819cdd7b8
commit 1750df5f5e
5 changed files with 98 additions and 80 deletions

View File

@ -67,7 +67,7 @@ Region::trim ( enum trim_e t, int X )
case RIGHT:
{
int d = (x() + w()) - X;
_end -= d;
_end = _start + w() - d;
resize( x(), y(), w() - d, h() );
break;
}
@ -122,6 +122,9 @@ Region::handle ( int m )
ox = x() - X;
oy = y() - Y;
if ( Fl::event_button() == 2 )
normalize();
return 1;
}
return 0;
@ -210,5 +213,16 @@ Region::draw ( void )
// fl_pop_clip();
fl_color( FL_RED );
fl_line( x() - _start, y(), x() - _start, y() + h() );
fl_line( x() + w() - _end, y(), x() + w() - _end, y() + h() );
draw_label();
static char pat[200];
sprintf( pat, "start %lu, end %lu", _start, _end );
fl_draw( pat, x(), y() + h() / 2 );
}

View File

@ -23,4 +23,6 @@
struct Timeline {
Fl_Scroll *scroll;
float fpp; /* frames per pixel */
};

View File

@ -32,66 +32,9 @@ extern Fl_Color velocity_colors[];
Waveform::Waveform ( int X, int Y, int W, int H, const char *L ) : Fl_Widget( X, Y, W, H, L )
{
_scale = 1;
}
void
Waveform::bubble_draw ( void )
{
int inc = 10;
for ( tick_t x = 0; x < w() && x < _end; ++x )
{
float v1 = _peaks[ _start + (x * inc) ] / (float)127;
int lh1 = (float)(h() / 2) * fabs( v1 );
int ly1 = (h() / 2) - (lh1 / 2);
fl_color( selection_color() );
fl_color( velocity_colors[ 127 - (int)(127 * fabs( v1 )) ] );
fl_pie( x, y() + ly1, inc, lh1, 0, 360 );
fl_color( fl_darker( selection_color() ) );
fl_arc( x, y() + ly1, inc, lh1, 0, 360 );
}
}
int
Waveform::handle ( int m )
{
if ( m == FL_PUSH )
{
switch ( Fl::event_button() )
{
case 1:
_start += 100;
_end += 100;
redraw();
break;
case 3:
_start -= 100;
_end -= 100;
redraw();
break;
case 2:
selection_color( selection_color() + 10 );
redraw();
break;
default:
return 0;
}
return 1;
}
return 0;
}
int measure = 50;
void
@ -121,34 +64,28 @@ Waveform::draw ( int X, int Y, int W, int H )
int j;
float _scale = 2;
int start = (_start + (X - x())) * 2;
j = 0;
for ( int x = X; x < X + W; ++x )
{
float lo = _peaks[ start + j++ ] * _scale;
float hi = _peaks[ start + j++ ] * _scale;
float lo = _peaks[ start + j++ ];
float hi = _peaks[ start + j++ ];
int mid = Y + (H / 2);
if ( lo < -1.0 || hi > 1.0 )
fl_color( FL_RED );
else
fl_color( selection_color() );
// fl_color( velocity_colors[ 127 - (int)( 127 * fabs( hi - lo ) ) ] );
// FIXME: cache this stuff.
// fl_color( fl_color_average( selection_color(), fl_contrast( fl_darker( FL_BLUE ), selection_color() ), fabs( hi - lo ) ) );
fl_color( fl_color_average( FL_RED, selection_color(), fabs( hi - lo ) ) );
if ( lo < -0.5 || hi > 0.5 )
hi *= _scale;
lo *= _scale;
if ( lo < -1.0 || hi > 1.0 )
fl_color( FL_RED );
fl_line( x, mid + (H * lo), x, mid + (H * hi) );
fl_line( x, mid + (H / 2 * lo), x, mid + (H / 2 * hi) );
}
@ -163,7 +100,7 @@ Waveform::draw ( int X, int Y, int W, int H )
{
float v = _peaks[ start + j ] * _scale;
j += 2;
fl_vertex( x, Y + (H / 2) + ((float)H * v ));
fl_vertex( x, Y + (H / 2) + ((float)H / 2 * v ));
}
fl_end_line();
@ -175,7 +112,7 @@ Waveform::draw ( int X, int Y, int W, int H )
{
float v = _peaks[ start + j ] * _scale;
j += 2;
fl_vertex( x, Y + (H / 2) + ((float)H * v ));
fl_vertex( x, Y + (H / 2) + ((float)H / 2 * v ));
}
fl_end_line();
@ -186,3 +123,63 @@ Waveform::draw ( int X, int Y, int W, int H )
fl_pop_clip();
}
void
Waveform::downsample ( int s, int e, float *mhi, float *mlo )
{
*mhi = -1.0;
*mlo = 1.0;
int start = s * 2;
int end = e * 2;
for ( int j = start; j < end; )
{
float lo = _peaks[ j++ ];
float hi = _peaks[ j++ ];
if ( hi > *mhi )
*mhi = hi;
if ( lo < *mlo )
*mlo = lo;
}
}
void
Waveform::normalize ( void )
{
/* float mhi = -1.0; */
/* float mlo = 1.0; */
float mhi, mlo;
downsample( _start, _end, &mhi, &mlo );
/* int start = _start * 2; */
/* int end = _end * 2; */
/* for ( int j = start; j < end; ) */
/* { */
/* float lo = _peaks[ j++ ]; */
/* float hi = _peaks[ j++ ]; */
/* if ( hi > mhi ) */
/* mhi = hi; */
/* if ( lo < mlo ) */
/* mlo = lo; */
/* } */
_scale = 1.0f / (float)mhi;
if ( _scale * mlo < -1.0 )
_scale = 1 / fabs( mlo );
_scale = fabs( _scale );
// printf( "scale = %f, hi=%f, lo=%f\n", _scale, mhi, mlo );
redraw();
}

View File

@ -22,6 +22,8 @@
#include <FL/Fl_Widget.H>
#include <FL/fl_draw.H>
#include "Timeline.H"
typedef unsigned long tick_t;
class Waveform : public Fl_Widget
@ -43,20 +45,22 @@ public:
Waveform ( const Waveform & rhs ) : Fl_Widget( rhs.x(), rhs.y(), rhs.w(), rhs.h(), rhs.label() )
{
// Fl_Widget::resize( rhs.x(), rhs.y(), rhs.w(), rhs.h() );
_peaks = rhs._peaks;
_start = rhs._start;
_end = rhs._end;
_scale = rhs._scale;
}
int handle ( int m );
void bubble_draw ( void );
void draw ( void );
void draw ( int X, int Y, int W, int H );
void start ( tick_t s ) { _start = s; }
void end ( tick_t e ) { _end = e; }
void peaks ( float *p ) { _peaks = p; }
void normalize ( void );
void downsample ( int s, int e, float *mhi, float *mlo );
};

3
main.C
View File

@ -57,6 +57,7 @@ main ( int argc, char **argv )
Fl_Double_Window *main_window = new Fl_Double_Window( 0, 0, 800, 600 );
timeline.scroll = new Fl_Scroll( 0, 0, 800, 600 );
timeline.fpp = 100;
Fl_Pack *tracks = new Fl_Pack( 0, 0, 5000, 5000 );
tracks->type( Fl_Pack::VERTICAL );
@ -94,7 +95,7 @@ main ( int argc, char **argv )
wave->peaks( peaks );
wave->start( 0 );
wave->end( len );
wave->end( (len / sizeof( float )) / 2 );
wave->color( FL_CYAN );
wave->selection_color( fl_darker( FL_GRAY ) );