From d7f020ee2e75b4df62e4bf64bb918ad60543751f Mon Sep 17 00:00:00 2001 From: Jonathan Moore Liles Date: Sun, 11 May 2008 19:31:41 -0500 Subject: [PATCH] Add mutex to Audio_File objects to prevent read_source_peaks() and IO thread from conflicting. --- Timeline/Audio_File.H | 4 +++- Timeline/Audio_File_SF.C | 17 +++++++++++++++-- Timeline/Audio_File_SF.H | 2 +- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/Timeline/Audio_File.H b/Timeline/Audio_File.H index 62a2623..c2804dc 100644 --- a/Timeline/Audio_File.H +++ b/Timeline/Audio_File.H @@ -29,9 +29,11 @@ #include #include +#include "Mutex.H" + class Peak_Writer; -class Audio_File +class Audio_File : protected Mutex { static std::map _open_files; diff --git a/Timeline/Audio_File_SF.C b/Timeline/Audio_File_SF.C index ed2d405..cb31f43 100644 --- a/Timeline/Audio_File_SF.C +++ b/Timeline/Audio_File_SF.C @@ -163,10 +163,12 @@ Audio_File_SF::close ( void ) void Audio_File_SF::seek ( nframes_t offset ) { + lock(); + if ( offset != _current_read ) - { sf_seek( _in, _current_read = offset, SEEK_SET | SFM_READ ); - } + + unlock(); } /* if channels is -1, then all channels are read into buffer @@ -179,6 +181,8 @@ Audio_File_SF::read ( sample_t *buf, int channel, nframes_t len ) // printf( "len = %lu, channels = %d\n", len, _channels ); + lock(); + nframes_t rlen; if ( _channels == 1 || channel == -1 ) @@ -198,6 +202,8 @@ Audio_File_SF::read ( sample_t *buf, int channel, nframes_t len ) _current_read += rlen; + unlock(); + return rlen; } @@ -207,12 +213,15 @@ Audio_File_SF::read ( sample_t *buf, int channel, nframes_t start, nframes_t end { assert( end > start ); + lock(); // open(); seek( start ); nframes_t len = read( buf, channel, end - start ); + unlock(); + // close(); return len; @@ -225,9 +234,13 @@ Audio_File_SF::write ( sample_t *buf, nframes_t nframes ) { _peaks.write( buf, nframes ); +// lock(); + nframes_t l = sf_writef_float( _in, buf, nframes ); _length += l; +// unlock(); + return l; } diff --git a/Timeline/Audio_File_SF.H b/Timeline/Audio_File_SF.H index 1fd2137..5f06c5c 100644 --- a/Timeline/Audio_File_SF.H +++ b/Timeline/Audio_File_SF.H @@ -29,7 +29,7 @@ class Audio_File_SF : public Audio_File /* used to avoid unnecessary seeking--libsndfile isn't smart * enough to do this for us */ - nframes_t _current_read; + volatile nframes_t _current_read; Audio_File_SF ( ) {