Browse Source

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

tags/non-daw-v1.1.0
Jonathan Moore Liles 16 years ago
parent
commit
09a19516f3
5 changed files with 108 additions and 34 deletions
  1. +71
    -27
      Peaks.C
  2. +16
    -2
      Region.C
  3. +2
    -0
      Region.H
  4. +2
    -0
      Timeline.H
  5. +17
    -5
      main.C

+ 71
- 27
Peaks.C View File

@@ -64,49 +64,93 @@ Peaks::read ( int X, float *hi, float *lo ) const
downsample( start, end, hi, lo );
}

void
Peaks::read_peaks ( int s, int e, float *mhi, float *mlo ) const

static
int
sf_read_peaks ( SNDFILE *in, Peak *peaks, int npeaks, int chunksize )
{
/* this could be faster, but who cares. Don't zoom in so far! */
float *fbuf = new float[ chunksize ];

SNDFILE *in;
SF_INFO si;
size_t len;

memset( &si, 0, sizeof( si ) );
int i;
for ( i = 0; i < npeaks; ++i )
{
/* read in a buffer */
len = sf_read_float( in, fbuf, chunksize );

in = sf_open( _clip->name(), SFM_READ, &si );
float hi = -1.0;
float lo = 1.0;

/* if ( si.channels != 1 ) */
/* abort(); */
/* if ( si.samplerate != timeline.sample_rate ) */
/* abort(); */
for ( int j = len; j--; )
{
if ( fbuf[j] > hi )
hi = fbuf[j];
if ( fbuf[j] < lo )
lo = fbuf[j];
}

sf_seek( in, s, SEEK_SET );
peaks[ i ].max = hi;
peaks[ i ].min = lo;

int chunksize = e - s;
if ( len < chunksize )
break;
}

float *fbuf = new float[ chunksize ];
delete fbuf;

size_t len;
return i;
}

/* read in a buffer */
len = sf_read_float( in, fbuf, chunksize );
void
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;

Peak p;
p.max = -1.0;
p.min = 1.0;
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! */

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];
SNDFILE *in;
SF_INFO si;

memset( &si, 0, sizeof( si ) );

in = sf_open( _clip->name(), SFM_READ, &si );

sf_seek( in, s, SEEK_SET );
peaks_read_offset = s;

int chunksize = e - s;

printf( "read %d peaks\n", sf_read_peaks( in, peaks_read, buffer_size, chunksize ) );

sf_close( in );
}

sf_close( in );
done:

delete fbuf;
// FIXME: should downsample here?

Peak p = peaks_read[ s - peaks_read_offset ];

*mhi = p.max;
*mlo = p.min;


+ 16
- 2
Region.C View File

@@ -44,6 +44,7 @@ Region::init ( void )
box( FL_PLASTIC_UP_BOX );

_track = NULL;
_offset = 0;
}

Region::Region ( const Region & rhs ) : Waveform( rhs )
@@ -55,6 +56,7 @@ Region::Region ( const Region & rhs ) : Waveform( rhs )
labelcolor( rhs.labelcolor() );
labeltype( rhs.labeltype() );

_offset = rhs._offset;
_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( d );

resize( x() + d, y(), w() - d, h() );
Fl_Widget::resize( x() + d, y(), w() - d, h() );
break;
}
case RIGHT:
@@ -90,7 +92,7 @@ Region::trim ( enum trim_e t, int X )
int d = (x() + w()) - X;
// _end = _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;
}
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
Region::draw ( void )
{

draw_box();

// fl_push_clip( x() + Fl::box_dx( box() ), y(), w() - Fl::box_dw( box() ), h() );


Waveform::draw();

// fl_pop_clip();


+ 2
- 0
Region.H View File

@@ -45,6 +45,8 @@ public:

int handle ( int m );
void draw ( void );
void resize ( void );


Track * track ( void ) { return _track; }
void track ( Track *t ) { _track = t; }


+ 2
- 0
Timeline.H View File

@@ -20,11 +20,13 @@
#pragma once

#include <FL/Fl_Scroll.H>
#include <FL/Fl_Pack.H>

#include "Clip.H"

struct Timeline {
Fl_Scroll *scroll;
Fl_Pack *tracks;

float fpp; /* frames per pixel */



+ 17
- 5
main.C View File

@@ -53,7 +53,18 @@ void
cb_zoom ( Fl_Widget *w, void *v )
{
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();

printf( "%f\n", timeline.fpp );
}

int
@@ -70,8 +81,8 @@ main ( int argc, char **argv )

timeline.sample_rate = 44100;

Fl_Pack *tracks = new Fl_Pack( 0, 0, 5000, 5000 );
tracks->type( Fl_Pack::VERTICAL );
timeline.tracks = new Fl_Pack( 0, 0, 5000, 5000 );
timeline.tracks->type( Fl_Pack::VERTICAL );

Fl::get_system_colors();
Fl::scheme( "plastic" );
@@ -87,7 +98,7 @@ main ( int argc, char **argv )

Region *wave = new Region( new Clip( "streambass8.wav" ) );

wave->resize( 0, 0, 500, 100 );
// wave->resize( 0, 0, 500, 100 );

// wave->peaks( peaks );
wave->start( 0 );
@@ -119,13 +130,14 @@ main ( int argc, char **argv )
track1->next( track2 );
track2->prev( track1 );

tracks->end();
timeline.tracks->end();
timeline.scroll->end();

Fl_Slider *zoom_slider = new Fl_Slider( 0, 0, 800, 24 );
zoom_slider->type( 1 );
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 );

main_window->end();


Loading…
Cancel
Save