Reference count Audio_Files.

This commit is contained in:
Jonathan Moore Liles 2008-07-14 23:29:47 -05:00
parent b4e41435e1
commit ffaa7bff61
3 changed files with 36 additions and 2 deletions

View File

@ -134,7 +134,7 @@ Audio_Region::Audio_Region ( const Audio_Region & rhs ) : Sequence_Region( rhs )
{ {
// *((Sequence_Region*)this) = (Sequence_Region &)rhs; // *((Sequence_Region*)this) = (Sequence_Region &)rhs;
_clip = rhs._clip; _clip = rhs._clip->duplicate();
_scale = rhs._scale; _scale = rhs._scale;
_fade_in = rhs._fade_in; _fade_in = rhs._fade_in;
@ -192,6 +192,8 @@ Audio_Region::Audio_Region ( Audio_File *c, Sequence *t, nframes_t o )
Audio_Region::~Audio_Region ( ) Audio_Region::~Audio_Region ( )
{ {
log_destroy(); log_destroy();
_clip->release();
} }

View File

@ -29,7 +29,10 @@ std::map <std::string, Audio_File*> Audio_File::_open_files;
Audio_File::~Audio_File ( ) Audio_File::~Audio_File ( )
{ {
DMESSAGE( "Freeing Audio_File object for \"%s\"", _filename );
_open_files[ std::string( _filename ) ] = NULL; _open_files[ std::string( _filename ) ] = NULL;
if ( _filename ) if ( _filename )
free( _filename ); free( _filename );
} }
@ -80,7 +83,7 @@ Audio_File::filename ( void ) const
return realname( _filename ); return realname( _filename );
} }
/** attmpet to open any supported filetype */ /** attempt to open any supported filetype */
Audio_File * Audio_File *
Audio_File::from_file ( const char * filename ) Audio_File::from_file ( const char * filename )
{ {
@ -88,7 +91,11 @@ Audio_File::from_file ( const char * filename )
Audio_File *a; Audio_File *a;
if ( ( a = _open_files[ std::string( filename ) ] ) ) if ( ( a = _open_files[ std::string( filename ) ] ) )
{
++a->_refs;
return a; return a;
}
if ( ( a = Audio_File_SF::from_file( filename ) ) ) if ( ( a = Audio_File_SF::from_file( filename ) ) )
goto done; goto done;
@ -105,11 +112,31 @@ Audio_File::from_file ( const char * filename )
done: done:
ASSERT( ! _open_files[ std::string( filename ) ], "Programming errror" );
_open_files[ std::string( filename ) ] = a; _open_files[ std::string( filename ) ] = a;
a->_refs = 1;
return a; return a;
} }
Audio_File *
Audio_File::duplicate ( void )
{
++_refs;
return this;
}
/** release the resources assoicated with this audio file if no other
* references to it exist */
void
Audio_File::release ( void )
{
if ( --_refs == 0 )
delete this;
}
bool bool
Audio_File::read_peaks( float fpp, nframes_t start, nframes_t end, int *peaks, Peak **pbuf, int *channels ) Audio_File::read_peaks( float fpp, nframes_t start, nframes_t end, int *peaks, Peak **pbuf, int *channels )

View File

@ -34,6 +34,8 @@ class Peak_Writer;
class Audio_File : protected Mutex class Audio_File : protected Mutex
{ {
int _refs;
static std::map <std::string, Audio_File*> _open_files; static std::map <std::string, Audio_File*> _open_files;
/* not permitted */ /* not permitted */
@ -67,6 +69,7 @@ public:
_filename = NULL; _filename = NULL;
_samplerate = 0; _samplerate = 0;
_length = _channels = 0; _length = _channels = 0;
_refs = 0;
} }
virtual ~Audio_File ( ); virtual ~Audio_File ( );
@ -77,6 +80,8 @@ public:
static Audio_File *from_file ( const char *filename ); static Audio_File *from_file ( const char *filename );
void release ( void );
Audio_File *duplicate ( void );
Peaks const * peaks ( ) { return &_peaks; } Peaks const * peaks ( ) { return &_peaks; }
const char *filename ( void ) const; const char *filename ( void ) const;