Update peaks when necessary.

This commit is contained in:
Jonathan Moore Liles 2008-02-16 20:59:45 -06:00
parent 31b449c24b
commit 0c9be68bcd
3 changed files with 39 additions and 15 deletions

47
Peaks.C
View File

@ -105,28 +105,26 @@ Peaks::open ( const char *filename )
{ {
int fd; int fd;
try_again: make_peaks( filename, 256 );
if ( ( fd = ::open( peakname( filename ), O_RDONLY ) ) < 0 ) if ( ( fd = ::open( peakname( filename ), O_RDONLY ) ) < 0 )
{
/* generate peaks here */
if ( make_peaks( filename, 256 ) )
goto try_again;
else
return false; return false;
}
{ {
struct stat st; struct stat st;
fstat( fd, &st ); fstat( fd, &st );
_len = st.st_size; _len = st.st_size;
} }
_peaks = (peaks*)mmap( NULL, _len, PROT_READ, MAP_SHARED, fd, 0 ); _peaks = (peaks*)mmap( NULL, _len, PROT_READ, MAP_SHARED, fd, 0 );
::close( fd ); ::close( fd );
if ( _peaks == MAP_FAILED ) if ( _peaks == MAP_FAILED )
printf( "failed to create mapping! " ); printf( "failed to create mapping!\n" );
_len = (_len - sizeof( int )) / sizeof( Peak ); _len = (_len - sizeof( int )) / sizeof( Peak );
@ -141,15 +139,40 @@ long_to_float( long *buf, int len )
*((float*)buf) = *buf / 32768; *((float*)buf) = *buf / 32768;
} }
/** returns false if peak file for /filename/ is out of date */
bool
Peaks::current ( const char *filename )
{
int sfd, pfd;
if ( ( sfd = ::open( filename, O_RDONLY ) ) < 0 )
return true;
if ( ( pfd = ::open( peakname( filename ), O_RDONLY ) ) < 0 )
return false;
struct stat sst, pst;
fstat( sfd, &sst );
fstat( pfd, &pst );
close( sfd );
close( pfd );
return sst.st_mtime <= pst.st_mtime;
}
/** build peaks file for /filename/ if necessary */
bool bool
Peaks::make_peaks ( const char *filename, int chunksize ) Peaks::make_peaks ( const char *filename, int chunksize )
{ {
if ( current( filename ) )
return true;
SNDFILE *in; SNDFILE *in;
// sox_format_init();
SF_INFO si; SF_INFO si;
memset( &si, 0, sizeof( si ) ); memset( &si, 0, sizeof( si ) );
in = sf_open( filename, SFM_READ, &si ); in = sf_open( filename, SFM_READ, &si );

View File

@ -54,6 +54,7 @@ public:
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 );
bool current ( const char *filename );
bool make_peaks ( const char *filename, int chunksize ); bool make_peaks ( const char *filename, int chunksize );
Peak & operator[] ( int X ) const; Peak & operator[] ( int X ) const;

View File

@ -46,7 +46,7 @@ public:
Waveform ( Clip *c ) : Fl_Widget( 0, 0, 500, 100, "" ) Waveform ( Clip *c ) : Fl_Widget( 0, 0, 500, 100, "" )
{ {
_clip = c; _clip = c;
_scale = 1;
label( _clip->name() ); label( _clip->name() );
_end = _clip->length(); _end = _clip->length();