Work on peak reader (WIP). Resize regions on zoom.

This commit is contained in:
Jonathan Moore Liles 2008-02-17 03:00:38 -06:00
parent 720f024fa0
commit 09a19516f3
5 changed files with 115 additions and 41 deletions

92
Peaks.C
View File

@ -64,11 +64,71 @@ Peaks::read ( int X, float *hi, float *lo ) const
downsample( start, end, hi, lo ); downsample( start, end, hi, lo );
} }
static
int
sf_read_peaks ( SNDFILE *in, Peak *peaks, int npeaks, int chunksize )
{
float *fbuf = new float[ chunksize ];
size_t len;
int i;
for ( i = 0; i < npeaks; ++i )
{
/* read in a buffer */
len = sf_read_float( in, fbuf, chunksize );
float hi = -1.0;
float lo = 1.0;
for ( int j = len; j--; )
{
if ( fbuf[j] > hi )
hi = fbuf[j];
if ( fbuf[j] < lo )
lo = fbuf[j];
}
peaks[ i ].max = hi;
peaks[ i ].min = lo;
if ( len < chunksize )
break;
}
delete fbuf;
return i;
}
void void
Peaks::read_peaks ( int s, int e, float *mhi, float *mlo ) const Peaks::read_peaks ( int s, int e, float *mhi, float *mlo ) const
{ {
static Peak * peaks_read = NULL;
static nframes_t peaks_read_offset = 0;
const int buffer_size = BUFSIZ;
if ( ! peaks_read )
peaks_read = new Peak[ buffer_size ];
else
{
if ( s >= peaks_read_offset &&
e - peaks_read_offset < buffer_size )
goto done;
if ( e > peaks_read_offset + buffer_size )
{
printf( "hit buffer boundardy!\n" );
memmove( peaks_read, &peaks_read[ (s - peaks_read_offset) ], (buffer_size - (s - peaks_read_offset)) * sizeof( Peak ) );
peaks_read_offset = s;
goto done;
}
}
/* this could be faster, but who cares. Don't zoom in so far! */ /* this could be faster, but who cares. Don't zoom in so far! */
{
SNDFILE *in; SNDFILE *in;
SF_INFO si; SF_INFO si;
@ -76,37 +136,21 @@ Peaks::read_peaks ( int s, int e, float *mhi, float *mlo ) const
in = sf_open( _clip->name(), SFM_READ, &si ); in = sf_open( _clip->name(), SFM_READ, &si );
/* if ( si.channels != 1 ) */
/* abort(); */
/* if ( si.samplerate != timeline.sample_rate ) */
/* abort(); */
sf_seek( in, s, SEEK_SET ); sf_seek( in, s, SEEK_SET );
peaks_read_offset = s;
int chunksize = e - s; int chunksize = e - s;
float *fbuf = new float[ chunksize ]; printf( "read %d peaks\n", sf_read_peaks( in, peaks_read, buffer_size, chunksize ) );
size_t len;
/* read in a buffer */
len = sf_read_float( in, fbuf, chunksize );
Peak p;
p.max = -1.0;
p.min = 1.0;
for ( int i = 0; i < len; ++i )
{
if ( fbuf[i] > p.max )
p.max = fbuf[i];
if ( fbuf[i] < p.min )
p.min = fbuf[i];
}
sf_close( in ); sf_close( in );
}
delete fbuf; done:
// FIXME: should downsample here?
Peak p = peaks_read[ s - peaks_read_offset ];
*mhi = p.max; *mhi = p.max;
*mlo = p.min; *mlo = p.min;

View File

@ -44,6 +44,7 @@ Region::init ( void )
box( FL_PLASTIC_UP_BOX ); box( FL_PLASTIC_UP_BOX );
_track = NULL; _track = NULL;
_offset = 0;
} }
Region::Region ( const Region & rhs ) : Waveform( rhs ) Region::Region ( const Region & rhs ) : Waveform( rhs )
@ -55,6 +56,7 @@ Region::Region ( const Region & rhs ) : Waveform( rhs )
labelcolor( rhs.labelcolor() ); labelcolor( rhs.labelcolor() );
labeltype( rhs.labeltype() ); labeltype( rhs.labeltype() );
_offset = rhs._offset;
_track = rhs._track; _track = rhs._track;
} }
@ -82,7 +84,7 @@ Region::trim ( enum trim_e t, int X )
_start = timeline.x_to_ts( x() + d ); _start = timeline.x_to_ts( x() + d );
// _start += timeline.x_to_ts( d ); // _start += timeline.x_to_ts( d );
resize( x() + d, y(), w() - d, h() ); Fl_Widget::resize( x() + d, y(), w() - d, h() );
break; break;
} }
case RIGHT: case RIGHT:
@ -90,7 +92,7 @@ Region::trim ( enum trim_e t, int X )
int d = (x() + w()) - X; int d = (x() + w()) - X;
// _end = _start + w() - d; // _end = _start + w() - d;
_end = timeline.x_to_ts( _start + w() - d ); _end = timeline.x_to_ts( _start + w() - d );
resize( x(), y(), w() - d, h() ); Fl_Widget::resize( x(), y(), w() - d, h() );
break; break;
} }
default: default:
@ -225,14 +227,26 @@ Region::handle ( int m )
} }
/** must be called whenever zoom is adjusted */
void
Region::resize ( void )
{
int X = timeline.ts_to_x( _offset );
int W = timeline.ts_to_x( _end - _start );
if ( W )
Fl_Widget::resize( X, y(), W, h() );
}
void void
Region::draw ( void ) Region::draw ( void )
{ {
draw_box(); draw_box();
// fl_push_clip( x() + Fl::box_dx( box() ), y(), w() - Fl::box_dw( box() ), h() ); // fl_push_clip( x() + Fl::box_dx( box() ), y(), w() - Fl::box_dw( box() ), h() );
Waveform::draw(); Waveform::draw();
// fl_pop_clip(); // fl_pop_clip();

View File

@ -45,6 +45,8 @@ public:
int handle ( int m ); int handle ( int m );
void draw ( void ); void draw ( void );
void resize ( void );
Track * track ( void ) { return _track; } Track * track ( void ) { return _track; }
void track ( Track *t ) { _track = t; } void track ( Track *t ) { _track = t; }

View File

@ -20,11 +20,13 @@
#pragma once #pragma once
#include <FL/Fl_Scroll.H> #include <FL/Fl_Scroll.H>
#include <FL/Fl_Pack.H>
#include "Clip.H" #include "Clip.H"
struct Timeline { struct Timeline {
Fl_Scroll *scroll; Fl_Scroll *scroll;
Fl_Pack *tracks;
float fpp; /* frames per pixel */ float fpp; /* frames per pixel */

22
main.C
View File

@ -53,7 +53,18 @@ void
cb_zoom ( Fl_Widget *w, void *v ) cb_zoom ( Fl_Widget *w, void *v )
{ {
timeline.fpp = ((Fl_Slider*)w)->value(); timeline.fpp = ((Fl_Slider*)w)->value();
for ( int i = timeline.tracks->children(); i-- ; )
{
Fl_Group *track = (Fl_Group*)timeline.tracks->child( i );
for ( int j = track->children(); j-- ; )
((Region*)(track->child( j )))->resize();
}
timeline.scroll->redraw(); timeline.scroll->redraw();
printf( "%f\n", timeline.fpp );
} }
int int
@ -70,8 +81,8 @@ main ( int argc, char **argv )
timeline.sample_rate = 44100; timeline.sample_rate = 44100;
Fl_Pack *tracks = new Fl_Pack( 0, 0, 5000, 5000 ); timeline.tracks = new Fl_Pack( 0, 0, 5000, 5000 );
tracks->type( Fl_Pack::VERTICAL ); timeline.tracks->type( Fl_Pack::VERTICAL );
Fl::get_system_colors(); Fl::get_system_colors();
Fl::scheme( "plastic" ); Fl::scheme( "plastic" );
@ -87,7 +98,7 @@ main ( int argc, char **argv )
Region *wave = new Region( new Clip( "streambass8.wav" ) ); Region *wave = new Region( new Clip( "streambass8.wav" ) );
wave->resize( 0, 0, 500, 100 ); // wave->resize( 0, 0, 500, 100 );
// wave->peaks( peaks ); // wave->peaks( peaks );
wave->start( 0 ); wave->start( 0 );
@ -119,13 +130,14 @@ main ( int argc, char **argv )
track1->next( track2 ); track1->next( track2 );
track2->prev( track1 ); track2->prev( track1 );
tracks->end(); timeline.tracks->end();
timeline.scroll->end(); timeline.scroll->end();
Fl_Slider *zoom_slider = new Fl_Slider( 0, 0, 800, 24 ); Fl_Slider *zoom_slider = new Fl_Slider( 0, 0, 800, 24 );
zoom_slider->type( 1 ); zoom_slider->type( 1 );
zoom_slider->callback( cb_zoom, 0 ); zoom_slider->callback( cb_zoom, 0 );
zoom_slider->range( 1, 256 * 256 ); zoom_slider->range( 1, 1024 );
zoom_slider->step( 1 );
zoom_slider->value( 256 ); zoom_slider->value( 256 );
main_window->end(); main_window->end();