Work on peak reader.

This commit is contained in:
Jonathan Moore Liles 2008-03-26 18:21:11 -05:00
parent 2abf28095c
commit 95a2896d53
6 changed files with 132 additions and 93 deletions

View File

@ -41,15 +41,10 @@ Audio_File::from_file ( const char * filename )
done: done:
a->_peaks = new Peaks;
a->_peaks = new Peaks[ a->channels() ]; a->_peaks->clip( a );
a->_peaks->open();
for ( int i = a->channels(); i-- ; )
{
a->_peaks[i].channel( i );
a->_peaks[i].clip( a );
a->_peaks[i].open();
}
_open_files[ string( filename ) ] = a; _open_files[ string( filename ) ] = a;

View File

@ -54,7 +54,7 @@ public:
static Audio_File *from_file ( const char *filename ); static Audio_File *from_file ( const char *filename );
Peaks const * peaks ( int channel ) { return &_peaks[ channel ]; } Peaks const * peaks ( ) { return _peaks; }
const char *name ( void ) const { return _filename; } const char *name ( void ) const { return _filename; }
nframes_t length ( void ) const { return _length; } nframes_t length ( void ) const { return _length; }
int channels ( void ) const { return _channels; } int channels ( void ) const { return _channels; }

View File

@ -91,16 +91,20 @@ Audio_File_SF::seek ( nframes_t offset )
sf_seek( _in, offset, SEEK_SET ); sf_seek( _in, offset, SEEK_SET );
} }
/* if channels is -1, then all channels are read into buffer
(interleaved). buf should be big enough to hold them all */
nframes_t nframes_t
Audio_File_SF::read ( sample_t *buf, int channel, nframes_t len ) Audio_File_SF::read ( sample_t *buf, int channel, nframes_t len )
{ {
if ( _channels == 1 ) if ( _channels == 1 || channel == -1 )
return sf_read_float ( _in, buf, len ); return sf_readf_float( _in, buf, len );
else
{
sample_t *tmp = new sample_t[ len * _channels ]; sample_t *tmp = new sample_t[ len * _channels ];
nframes_t rlen = sf_readf_float( _in, tmp, len ); nframes_t rlen = sf_readf_float( _in, tmp, len );
/* extract the requested channel */
for ( int i = channel; i < rlen; i += _channels ) for ( int i = channel; i < rlen; i += _channels )
*(buf++) = tmp[ i ]; *(buf++) = tmp[ i ];
@ -108,6 +112,7 @@ Audio_File_SF::read ( sample_t *buf, int channel, nframes_t len )
return rlen; return rlen;
} }
}
/** read samples from /start/ to /end/ into /buf/ */ /** read samples from /start/ to /end/ into /buf/ */
nframes_t nframes_t

View File

@ -123,19 +123,23 @@ Peak_Server::handle_request ( int s, const char *buf, int l )
send( s, &data, sizeof( data ), 0 ); send( s, &data, sizeof( data ), 0 );
for ( int i = 0; i < af->channels(); ++i ) const Peaks *pk = af->peaks();
{
const Peaks *pk = af->peaks( i );
int npeaks = pk->fill_buffer( fpp, start, end ); int npeaks = pk->fill_buffer( fpp, start, end );
if ( ! ( peaks == npeaks ) ) /* deinterlace and transmit */
printf( "wtf?! %d %d\n", peaks, npeaks ); int channels = af->channels();
// send( s, &peaks, sizeof( int ), 0 ); /* FIXME: really inefficient */
for ( int i = 0; i < channels; i++ )
for ( int j = i; j < npeaks * channels; j += channels )
send( s, pk->peakbuf() + j, sizeof( Peak ), 0 );
send( s, pk->peakbuf(), npeaks * sizeof( Peak ), 0 );
} /* for ( int i = 0; i < af->channels(); ++i ) */
/* { */
/* send( s, pk->peakbuf(), npeaks * sizeof( Peak ), 0 ); */
/* } */
break; break;
} }

View File

@ -70,7 +70,7 @@ Peaks::fill_buffer ( float fpp, int s, int e ) const
} }
void inline void
Peaks::downsample ( Peak *peaks, int s, int e, float *mhi, float *mlo ) const Peaks::downsample ( Peak *peaks, int s, int e, float *mhi, float *mlo ) const
{ {
*mhi = 0; *mhi = 0;
@ -91,11 +91,25 @@ Peaks::downsample ( Peak *peaks, int s, int e, float *mhi, float *mlo ) const
} }
} }
int int
Peaks::clip_read_peaks ( Peak *peaks, int npeaks, int chunksize ) const Peaks::read_peakfile_peaks ( Peak *peaks, nframes_t s, int npeaks, int chunksize ) const
{ {
sample_t *fbuf = new sample_t[ chunksize ]; int fd;
if ( ( fd = ::open( peakname( _clip->name() ), O_RDONLY ) ) < 0 )
{
printf( "failed to open peak file!" );
return 0;
}
Peak *pbuf = new Peak[ chunksize ];
/* get chunk size of peak file */
int pfchunksize;
::read( fd, &pfchunksize, sizeof( int ) );
/* locate to start position */
lseek( fd, SEEK_CUR, s / pfchunksize );
size_t len; size_t len;
@ -103,18 +117,70 @@ Peaks::clip_read_peaks ( Peak *peaks, int npeaks, int chunksize ) const
for ( i = 0; i < npeaks; ++i ) for ( i = 0; i < npeaks; ++i )
{ {
/* read in a buffer */ /* read in a buffer */
len = _clip->read( fbuf, _channel, chunksize ); len = ::read( fd, pbuf, chunksize );
Peak &p = peaks[ i ]; Peak &p = peaks[ i ];
// downsample( pbuf,
p.min = 0; p.min = 0;
p.max = 0; p.max = 0;
for ( int j = len; j--; ) /* for ( int j = len; j--; ) */
/* { */
/* if ( fbuf[ j ] > p.max ) */
/* p.max = fbuf[ j ]; */
/* if ( fbuf[ j ] < p.min ) */
/* p.min = fbuf[ j ]; */
/* } */
if ( len < chunksize )
break;
}
/* delete fbuf; */
_clip->close();
return i;
}
int
Peaks::read_source_peaks ( Peak *peaks, nframes_t s, int npeaks, int chunksize ) const
{ {
if ( fbuf[j] > p.max ) int channels = _clip->channels();
p.max = fbuf[j];
if ( fbuf[j] < p.min ) sample_t *fbuf = new sample_t[ chunksize * channels ];
p.min = fbuf[j];
size_t len;
_clip->open();
_clip->seek( s );
int i;
for ( i = 0; i < npeaks; ++i )
{
/* read in a buffer */
len = _clip->read( fbuf, -1, chunksize );
Peak *pk = peaks + (i * channels);
/* get the peak for each channel */
for ( int j = 0; j < channels; ++j )
{
Peak &p = pk[ j ];
p.min = 0;
p.max = 0;
for ( int k = j; k < len * channels; k += channels )
{
if ( fbuf[ k ] > p.max )
p.max = fbuf[ k ];
if ( fbuf[ k ] < p.min )
p.min = fbuf[ k ];
}
} }
if ( len < chunksize ) if ( len < chunksize )
@ -123,6 +189,8 @@ Peaks::clip_read_peaks ( Peak *peaks, int npeaks, int chunksize ) const
delete fbuf; delete fbuf;
_clip->close();
return i; return i;
} }
@ -131,21 +199,16 @@ Peaks::read_peaks ( int s, int e, int npeaks, int chunksize ) const
{ {
printf( "reading peaks %d @ %d\n", npeaks, chunksize ); printf( "reading peaks %d @ %d\n", npeaks, chunksize );
if ( _peakbuf.size < npeaks ) if ( _peakbuf.size < npeaks * _clip->channels() )
{ {
_peakbuf.size = npeaks; _peakbuf.size = npeaks * _clip->channels();
// printf( "reallocating peak buffer %li\n", _peakbuf.size ); // printf( "reallocating peak buffer %li\n", _peakbuf.size );
_peakbuf.buf = (peakdata*)realloc( _peakbuf.buf, sizeof( peakdata ) + (_peakbuf.size * sizeof( Peak )) ); _peakbuf.buf = (peakdata*)realloc( _peakbuf.buf, sizeof( peakdata ) + (_peakbuf.size * sizeof( Peak )) );
} }
_clip->open();
_clip->seek( s );
_peakbuf.offset = s; _peakbuf.offset = s;
_peakbuf.buf->chunksize = chunksize; _peakbuf.buf->chunksize = chunksize;
_peakbuf.len = clip_read_peaks( _peakbuf.buf->data, npeaks, chunksize ); _peakbuf.len = read_source_peaks( _peakbuf.buf->data, s, npeaks, chunksize );
_clip->close();
} }
/** Return the peak for the range of samples */ /** Return the peak for the range of samples */
@ -155,27 +218,13 @@ Peaks::peak ( nframes_t start, nframes_t end ) const
/* Is there a better way to return this? */ /* Is there a better way to return this? */
static Peak p; static Peak p;
if ( _fpp < _peaks->chunksize )
{
assert( _fpp == _peakbuf.buf->chunksize );
start = (start - _peakbuf.offset) / _peakbuf.buf->chunksize; start = (start - _peakbuf.offset) / _peakbuf.buf->chunksize;
end = (end - _peakbuf.offset) / _peakbuf.buf->chunksize; end = (end - _peakbuf.offset) / _peakbuf.buf->chunksize;
if ( end > _peakbuf.len ) if ( end > _peakbuf.len )
end = _peakbuf.len; end = _peakbuf.len;
// assert( _peakbuf.len > start );
downsample( _peakbuf.buf->data, start, end, &p.max, &p.min ); downsample( _peakbuf.buf->data, start, end, &p.max, &p.min );
}
else
{
start /= _peaks->chunksize;
end /= _peaks->chunksize;
downsample( _peaks->data, start, end, &p.max, &p.min );
}
return p; return p;
} }
@ -202,7 +251,7 @@ Peaks::peakname ( const char *filename ) const
{ {
static char file[512]; static char file[512];
snprintf( file, 512, "%s.peak-%d", filename, _channel ); snprintf( file, 512, "%s.peak", filename );
return (const char*)&file; return (const char*)&file;
} }
@ -227,13 +276,8 @@ Peaks::open ( void )
_len = st.st_size; _len = st.st_size;
} }
_peaks = (peakdata*)mmap( NULL, _len, PROT_READ, MAP_SHARED, fd, 0 );
::close( fd ); ::close( fd );
if ( _peaks == MAP_FAILED )
printf( "failed to create mapping!\n" );
_len = (_len - sizeof( int )) / sizeof( Peak ); _len = (_len - sizeof( int )) / sizeof( Peak );
return true; return true;
@ -286,11 +330,14 @@ Peaks::make_peaks ( int chunksize )
/* write chunksize first */ /* write chunksize first */
fwrite( &chunksize, sizeof( int ), 1, fp ); fwrite( &chunksize, sizeof( int ), 1, fp );
Peak peaks[ _clip->channels() ];
size_t len; size_t len;
nframes_t s = 0;
do { do {
Peak p; len = read_source_peaks( peaks, s, 1, chunksize );
len = clip_read_peaks( &p, 1, chunksize ); s += len;
fwrite( &p, sizeof( Peak ), 1, fp ); fwrite( peaks, sizeof( peaks ), 1, fp );
} }
while ( len ); while ( len );
@ -312,10 +359,10 @@ Peaks::normalization_factor( float fpp, nframes_t start, nframes_t end ) const
// fill_buffer( fpp, start, end ); // fill_buffer( fpp, start, end );
if ( end - start < _peaks->chunksize * 4 ) /* if ( end - start < _peaks->chunksize * 4 ) */
fill_buffer( _clip->length() / 4, start, end ); /* fill_buffer( _clip->length() / 4, start, end ); */
else /* else */
fill_buffer( _clip->length(), start, end ); /* fill_buffer( _clip->length(), start, end ); */
Peak p = peak( start, end ); Peak p = peak( start, end );

View File

@ -57,16 +57,14 @@ class Peaks
static peakbuffer _peakbuf; static peakbuffer _peakbuf;
Audio_File *_clip; Audio_File *_clip;
int _channel;
peakdata *_peaks;
size_t _len; size_t _len;
mutable float _fpp; mutable float _fpp;
void read_peaks ( int s, int e, int npeaks, int chunksize ) const; void read_peaks ( int s, int e, int npeaks, int chunksize ) const;
int clip_read_peaks ( Peak *peaks, int npeaks, int chunksize ) const; int read_source_peaks ( Peak *peaks, nframes_t s, int npeaks, int chunksize ) const;
int read_peakfile_peaks ( Peak *peaks, nframes_t s, int npeaks, int chunksize ) const;
const char *peakname ( const char *filename ) const; const char *peakname ( const char *filename ) const;
@ -77,28 +75,18 @@ public:
Peaks ( ) Peaks ( )
{ {
_peaks = new peakdata;
_peaks->chunksize = 0;
_len = 0; _len = 0;
_clip = NULL; _clip = NULL;
_channel = 0;
} }
Peaks ( Audio_File *c ) Peaks ( Audio_File *c )
{ {
_peaks = new peakdata;
_peaks->chunksize = 0;
_len = 0; _len = 0;
_clip = c; _clip = c;
_channel = 0;
} }
Peak *peakbuf ( void ) const { return Peaks::_peakbuf.buf->data; } Peak *peakbuf ( void ) const { return Peaks::_peakbuf.buf->data; }
void clip ( Audio_File *c ) { _clip = c; } void clip ( Audio_File *c ) { _clip = c; }
void channel ( int v ) { _channel = v; }
int fill_buffer ( float fpp, int s, int e ) const; int fill_buffer ( float fpp, int s, int e ) const;