Browse Source

Read peaks from source in chunks of exact size requirested.

Use one buffer for all source-read peaks.
tags/non-daw-v1.1.0
Jonathan Moore Liles 17 years ago
parent
commit
0328ebfa2d
4 changed files with 89 additions and 76 deletions
  1. +1
    -0
      Clip.H
  2. +57
    -66
      Peaks.C
  3. +25
    -4
      Peaks.H
  4. +6
    -6
      Waveform.C

+ 1
- 0
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 */


+ 57
- 66
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;
*mlo = 1.0;
*mhi = 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;
float lo = 1.0;
Peak &p = peaks[ i ];
p.min = 0;
p.max = 0;


for ( int j = len; j--; ) for ( int j = len; j--; )
{ {
if ( fbuf[j] > hi )
hi = fbuf[j];
if ( fbuf[j] < lo )
lo = fbuf[j];
if ( fbuf[j] > p.max )
p.max = fbuf[j];
if ( fbuf[j] < p.min )
p.min = fbuf[j];
} }


peaks[ i ].max = hi;
peaks[ i ].min = lo;

if ( len < chunksize ) if ( len < chunksize )
break; break;
} }
@@ -103,57 +115,33 @@ 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;
static nframes_t peaks_read_offset = 0;
const int buffer_size = BUFSIZ;
// printf( "reading peaks %d @ %d\n", npeaks, chunksize );


if ( ! peaks_read )
peaks_read = new Peak[ buffer_size ];
else
if ( peakbuf.size < npeaks )
{ {
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;
}
peakbuf.size = npeaks;
// printf( "reallocating peak buffer %li\n", peakbuf.size );
peakbuf.buf = (peakdata*)realloc( peakbuf.buf, sizeof( peakdata ) + (peakbuf.size * sizeof( Peak )) );
} }


/* this could be faster, but who cares. Don't zoom in so far! */

{
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;
memset( peakbuf.buf->data, 0, peakbuf.size * sizeof( Peak ) );


int chunksize = e - s;

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


sf_close( in );
}
memset( &si, 0, sizeof( si ) );


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


// FIXME: should downsample here?
sf_seek( in, s, SEEK_SET );


Peak p = peaks_read[ s - peaks_read_offset ];
peakbuf.offset = s;
peakbuf.buf->chunksize = chunksize;
peakbuf.len = sf_read_peaks( in, peakbuf.buf->data, npeaks, chunksize );


*mhi = p.max;
*mlo = p.min;
sf_close( in );
} }




@@ -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 );
int end = timeline.x_to_ts( X + 1 );
assert( timeline.fpp == peakbuf.buf->chunksize );

int start = timeline.x_to_ts( X ) / peakbuf.buf->chunksize;
int i = start - (peakbuf.offset / peakbuf.buf->chunksize);


read_peaks( start, end, &p.max, &p.min );
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 = (peaks*)mmap( NULL, _len, PROT_READ, MAP_SHARED, fd, 0 );
_peaks = (peakdata*)mmap( NULL, _len, PROT_READ, MAP_SHARED, fd, 0 );


::close( fd ); ::close( fd );




+ 25
- 4
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 );


+ 6
- 6
Waveform.C 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 )
{ {


Loading…
Cancel
Save