Read peaks from source in chunks of exact size requirested.

Use one buffer for all source-read peaks.
This commit is contained in:
Jonathan Moore Liles 2008-02-17 14:41:59 -06:00
parent 09a19516f3
commit 0328ebfa2d
4 changed files with 89 additions and 76 deletions

1
Clip.H
View File

@ -26,6 +26,7 @@ typedef unsigned long nframes_t;
class Clip class Clip
{ {
const char *_filename; const char *_filename;
Peaks _peaks; Peaks _peaks;
nframes_t _length; /* length of clip in samples */ nframes_t _length; /* length of clip in samples */

111
Peaks.C
View File

@ -33,11 +33,35 @@
#include "Clip.H" #include "Clip.H"
#include "assert.h"
Peaks::peakbuffer Peaks::peakbuf;
/** Prepare a buffer of peaks from /s/ to /e/ for reading. Must be
* called before any calls to operator[] */
void
Peaks::fill_buffer ( int s, int e ) const
{
if ( timeline.fpp < _peaks->chunksize )
{
/* looks like we're going to have to switch to a higher resolution peak file
or read directly from the source */
read_peaks( s, e, e - s / timeline.fpp, timeline.fpp );
}
else
{
/* we'll just downsample on the fly in this case--no need for extra copying into
the buffer */
}
}
void void
Peaks::downsample ( int s, int e, float *mhi, float *mlo ) const Peaks::downsample ( int s, int e, float *mhi, float *mlo ) const
{ {
*mhi = -1.0; *mhi = 0;
*mlo = 1.0; *mlo = 0;
if ( e > _len ) if ( e > _len )
e = _len; e = _len;
@ -55,16 +79,6 @@ Peaks::downsample ( int s, int e, float *mhi, float *mlo ) const
} }
void
Peaks::read ( int X, float *hi, float *lo ) const
{
int start = X * timeline.fpp;
int end = (X + 1) * timeline.fpp;
downsample( start, end, hi, lo );
}
static static
int int
sf_read_peaks ( SNDFILE *in, Peak *peaks, int npeaks, int chunksize ) sf_read_peaks ( SNDFILE *in, Peak *peaks, int npeaks, int chunksize )
@ -79,20 +93,18 @@ sf_read_peaks ( SNDFILE *in, Peak *peaks, int npeaks, int chunksize )
/* read in a buffer */ /* read in a buffer */
len = sf_read_float( in, fbuf, chunksize ); len = sf_read_float( in, fbuf, chunksize );
float hi = -1.0; Peak &p = peaks[ i ];
float lo = 1.0; p.min = 0;
p.max = 0;
for ( int j = len; j--; ) for ( int j = len; j--; )
{ {
if ( fbuf[j] > hi ) if ( fbuf[j] > p.max )
hi = fbuf[j]; p.max = fbuf[j];
if ( fbuf[j] < lo ) if ( fbuf[j] < p.min )
lo = fbuf[j]; p.min = fbuf[j];
} }
peaks[ i ].max = hi;
peaks[ i ].min = lo;
if ( len < chunksize ) if ( len < chunksize )
break; break;
} }
@ -103,32 +115,19 @@ sf_read_peaks ( SNDFILE *in, Peak *peaks, int npeaks, int chunksize )
} }
void void
Peaks::read_peaks ( int s, int e, float *mhi, float *mlo ) const Peaks::read_peaks ( int s, int e, int npeaks, int chunksize ) const
{ {
static Peak * peaks_read = NULL; // printf( "reading peaks %d @ %d\n", npeaks, chunksize );
static nframes_t peaks_read_offset = 0;
const int buffer_size = BUFSIZ;
if ( ! peaks_read ) if ( peakbuf.size < npeaks )
peaks_read = new Peak[ buffer_size ];
else
{ {
if ( s >= peaks_read_offset && peakbuf.size = npeaks;
e - peaks_read_offset < buffer_size ) // printf( "reallocating peak buffer %li\n", peakbuf.size );
goto done; peakbuf.buf = (peakdata*)realloc( peakbuf.buf, sizeof( peakdata ) + (peakbuf.size * sizeof( Peak )) );
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! */ memset( peakbuf.buf->data, 0, peakbuf.size * sizeof( Peak ) );
{
SNDFILE *in; SNDFILE *in;
SF_INFO si; SF_INFO si;
@ -137,23 +136,12 @@ 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 );
sf_seek( in, s, SEEK_SET ); sf_seek( in, s, SEEK_SET );
peaks_read_offset = s;
int chunksize = e - s; peakbuf.offset = s;
peakbuf.buf->chunksize = chunksize;
printf( "read %d peaks\n", sf_read_peaks( in, peaks_read, buffer_size, chunksize ) ); peakbuf.len = sf_read_peaks( in, peakbuf.buf->data, npeaks, chunksize );
sf_close( in ); sf_close( in );
}
done:
// FIXME: should downsample here?
Peak p = peaks_read[ s - peaks_read_offset ];
*mhi = p.max;
*mlo = p.min;
} }
@ -168,10 +156,14 @@ Peaks::operator[] ( int X ) const
if ( timeline.fpp < _peaks->chunksize ) if ( timeline.fpp < _peaks->chunksize )
{ {
int start = timeline.x_to_ts( X ); assert( timeline.fpp == peakbuf.buf->chunksize );
int end = timeline.x_to_ts( X + 1 );
read_peaks( start, end, &p.max, &p.min ); int start = timeline.x_to_ts( X ) / peakbuf.buf->chunksize;
int i = start - (peakbuf.offset / peakbuf.buf->chunksize);
assert( peakbuf.len > i );
p = peakbuf.buf->data[ i ];
} }
else else
{ {
@ -213,8 +205,7 @@ Peaks::open ( const char *filename )
_len = st.st_size; _len = st.st_size;
} }
_peaks = (peakdata*)mmap( NULL, _len, PROT_READ, MAP_SHARED, fd, 0 );
_peaks = (peaks*)mmap( NULL, _len, PROT_READ, MAP_SHARED, fd, 0 );
::close( fd ); ::close( fd );

29
Peaks.H
View File

@ -21,6 +21,8 @@
#include <stdlib.h> #include <stdlib.h>
typedef unsigned long nframes_t;
struct Peak { struct Peak {
float min; float min;
float max; float max;
@ -31,25 +33,42 @@ class Clip;
class Peaks class Peaks
{ {
struct peaks { struct peakdata {
int chunksize; /* should always be a power of 2 */ int chunksize; /* should always be a power of 2 */
Peak data[]; Peak data[];
}; };
struct peakbuffer {
size_t size; /* total allocation size */
size_t len; /* number of peaks */
nframes_t offset; /* starting sample */
peakdata *buf;
peakbuffer ( )
{
size = len = 0;
}
};
static peakbuffer peakbuf;
Clip *_clip; Clip *_clip;
peaks *_peaks; peakdata *_peaks;
size_t _len; size_t _len;
void read_peaks ( int s, int e, float *mhi, float *mlo ) const; void read_peaks ( int s, int e, int npeaks, int chunksize ) const;
public: public:
Peaks ( Clip *c ) Peaks ( Clip *c )
{ {
_peaks = new peaks; _peaks = new peakdata;
_peaks->chunksize = 0; _peaks->chunksize = 0;
// _peaks->data = NULL; // _peaks->data = NULL;
@ -58,6 +77,8 @@ public:
_clip = c; _clip = c;
} }
void fill_buffer ( int s, int e ) const;
void downsample ( int s, int e, float *mhi, float *mlo ) const; void downsample ( int s, int e, float *mhi, float *mlo ) const;
void read ( int X, float *hi, float *lo ) const; void read ( int X, float *hi, float *lo ) const;
bool open ( const char *filename ); bool open ( const char *filename );

View File

@ -60,12 +60,6 @@ Waveform::draw ( void )
draw( X, y(), W, h() ); draw( X, y(), W, h() );
} }
void
Waveform::read_peaks ( int X, float *hi, float *lo )
{
_clip->peaks()->read( X, hi, lo );
}
void void
Waveform::draw ( int X, int Y, int W, int H ) Waveform::draw ( int X, int Y, int W, int H )
{ {
@ -79,6 +73,12 @@ Waveform::draw ( int X, int Y, int W, int H )
int start = timeline.ts_to_x( _start ) + (X - x() ); int start = timeline.ts_to_x( _start ) + (X - x() );
{
nframes_t start = timeline.x_to_ts( X - x() ) + _start;
_clip->peaks()->fill_buffer( start,
start + timeline.x_to_ts( W ) );
}
j = start; j = start;
for ( int x = X; x < X + W; ++x, ++j ) for ( int x = X; x < X + W; ++x, ++j )
{ {