From d1a2f52352f8c27254b78a69815ff6b3a27b2f9c Mon Sep 17 00:00:00 2001 From: Jonathan Moore Liles Date: Sat, 10 May 2008 20:40:43 -0500 Subject: [PATCH] Support optional mipmapping of peakfiles. Mipmap peaks for new captures. --- Timeline/Audio_File.H | 2 + Timeline/Audio_File_SF.C | 2 - Timeline/Audio_Region.C | 2 + Timeline/Peaks.C | 90 +++++++++++++++++++++++++++------------- Timeline/Peaks.H | 1 + 5 files changed, 67 insertions(+), 30 deletions(-) diff --git a/Timeline/Audio_File.H b/Timeline/Audio_File.H index b1a5c65..38847b7 100644 --- a/Timeline/Audio_File.H +++ b/Timeline/Audio_File.H @@ -93,6 +93,8 @@ public: virtual nframes_t read ( sample_t *buf, int channel, nframes_t start, nframes_t end ) = 0; virtual nframes_t write ( sample_t *buf, nframes_t len ) = 0; + virtual void finalize ( void ) { _peaks.finish_writing(); } + bool read_peaks( float fpp, nframes_t start, nframes_t end, int *peaks, Peak **pbuf, int *channels ); }; diff --git a/Timeline/Audio_File_SF.C b/Timeline/Audio_File_SF.C index dc38dad..6e7ddc2 100644 --- a/Timeline/Audio_File_SF.C +++ b/Timeline/Audio_File_SF.C @@ -158,8 +158,6 @@ Audio_File_SF::close ( void ) if ( _in ) sf_close( _in ); - _peaks.finish_writing(); - _in = NULL; } diff --git a/Timeline/Audio_Region.C b/Timeline/Audio_Region.C index 93aeaca..834ed04 100644 --- a/Timeline/Audio_Region.C +++ b/Timeline/Audio_Region.C @@ -842,6 +842,8 @@ Audio_Region::finalize ( nframes_t frame ) _clip->close(); _clip->open(); + _clip->finalize(); + /* FIXME: should we attempt to truncate the file? */ _range.length = frame - _range.start - _range.offset; diff --git a/Timeline/Peaks.C b/Timeline/Peaks.C index f4ba6f4..9abe50f 100644 --- a/Timeline/Peaks.C +++ b/Timeline/Peaks.C @@ -581,47 +581,29 @@ class Peak_Builder public: + /** generate additional cache levels for a peakfile with only 1 block (ie. that of a new capture) */ bool - run ( void ) + make_peaks_mipmap ( void ) { + if ( ! Peaks::mipmapped_peakfiles ) + return true; + Audio_File *_clip = _peaks->_clip; const char *filename = _clip->name(); - DMESSAGE( "building peaks for \"%s\"", filename ); - FILE *rfp; - if ( ! ( fp = fopen( peakname( filename ), "w+" ) ) ) - return false; - - _clip->seek( 0 ); + last_block_pos = sizeof( peakfile_block_header ); - Peak buf[ _clip->channels() ]; - - DMESSAGE( "building level 1 peak cache" ); - - write_block_header( Peaks::cache_minimum ); - - /* build first level from source */ - size_t len; - do { - len = _peaks->read_source_peaks( buf, 1, Peaks::cache_minimum ); - - fwrite( buf, sizeof( buf ), len, fp ); - } - while ( len ); - - /* reopen for reading */ - fclose( fp ); + /* open for reading */ rfp = fopen( peakname( filename ), "r" ); - -// rfp = freopen( peakname( filename ), "r", fp ); - /* open the file again for appending */ fp = fopen( peakname( filename ), "r+" ); fseek( fp, 0, SEEK_END ); + Peak buf[ _clip->channels() ]; + /* now build the remaining peak levels, each based on the * preceding level */ @@ -655,6 +637,45 @@ public: fclose( rfp ); fclose( fp ); + return true; + } + + bool + make_peaks ( void ) + { + Audio_File *_clip = _peaks->_clip; + + const char *filename = _clip->name(); + + DMESSAGE( "building peaks for \"%s\"", filename ); + + FILE *rfp; + + if ( ! ( fp = fopen( peakname( filename ), "w+" ) ) ) + return false; + + _clip->seek( 0 ); + + Peak buf[ _clip->channels() ]; + + DMESSAGE( "building level 1 peak cache" ); + + write_block_header( Peaks::cache_minimum ); + + /* build first level from source */ + size_t len; + do { + len = _peaks->read_source_peaks( buf, 1, Peaks::cache_minimum ); + + fwrite( buf, sizeof( buf ), len, fp ); + } + while ( len ); + + /* reopen for reading */ + fclose( fp ); + + make_peaks_mipmap(); + DMESSAGE( "done building peaks" ); return true; @@ -674,7 +695,15 @@ Peaks::make_peaks ( void ) const { Peak_Builder pb( this ); - return pb.run(); + return pb.make_peaks(); +} + +bool +Peaks::make_peaks_mipmap ( void ) const +{ + Peak_Builder pb( this ); + + return pb.make_peaks_mipmap(); } /** return normalization factor for a single peak, assuming the peak @@ -710,6 +739,11 @@ Peaks::finish_writing ( void ) delete _peak_writer; _peak_writer = NULL; } + + /* now fill in the rest of the cache */ + + if ( ! fork() ) + exit( make_peaks_mipmap() ); } void diff --git a/Timeline/Peaks.H b/Timeline/Peaks.H index d243ebb..4c5c132 100644 --- a/Timeline/Peaks.H +++ b/Timeline/Peaks.H @@ -101,6 +101,7 @@ public: bool current ( void ) const; bool make_peaks ( void ) const; + bool make_peaks_mipmap ( void ) const; void prepare_for_writing ( void ); void finish_writing ( void );