From 9e47efc585b2572127542212756dbe01fbd0ff7b Mon Sep 17 00:00:00 2001 From: Jonathan Moore Liles Date: Thu, 28 Feb 2008 06:35:19 -0600 Subject: [PATCH] Don't open the same file twice. --- Audio_File.C | 8 ++++++++ Audio_File.H | 5 +++++ Audio_File_SF.C | 8 +------- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/Audio_File.C b/Audio_File.C index ac7a60b..18bc6a3 100644 --- a/Audio_File.C +++ b/Audio_File.C @@ -20,6 +20,8 @@ #include "Audio_File.H" #include "Audio_File_SF.H" +map Audio_File::_open_files; + /** attmpet to open any supported filetype */ Audio_File * Audio_File::from_file ( const char * filename ) @@ -27,6 +29,9 @@ Audio_File::from_file ( const char * filename ) Audio_File *a; + if ( ( a = _open_files[ string( filename ) ] ) ) + return a; + if ( ( a = Audio_File_SF::from_file( filename ) ) ) goto done; @@ -36,6 +41,7 @@ Audio_File::from_file ( const char * filename ) done: + a->_peaks = new Peaks[ a->channels() ]; for ( int i = a->channels(); i-- ; ) @@ -45,5 +51,7 @@ done: a->_peaks[i].open(); } + _open_files[ string( filename ) ] = a; + return a; } diff --git a/Audio_File.H b/Audio_File.H index 19659fc..5aee6ca 100644 --- a/Audio_File.H +++ b/Audio_File.H @@ -27,8 +27,13 @@ typedef float sample_t; #include "Peaks.H" +#include +#include +using namespace std; + class Audio_File { + static map _open_files; protected: diff --git a/Audio_File_SF.C b/Audio_File_SF.C index caf8b5c..30dfb9e 100644 --- a/Audio_File_SF.C +++ b/Audio_File_SF.C @@ -41,12 +41,6 @@ Audio_File_SF::from_file ( const char *filename ) return NULL; } -/* if ( si.channels != 1 ) */ -/* { */ -/* printf( "error: incompatible format\n" ); */ -/* goto invalid; */ -/* } */ - if ( si.samplerate != timeline->sample_rate ) { printf( "error: samplerate mismatch!\n" ); @@ -55,7 +49,7 @@ Audio_File_SF::from_file ( const char *filename ) c = new Audio_File_SF; - c->_filename = filename; + c->_filename = strdup( filename ); c->_length = si.frames; c->_channels = si.channels;