aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--graphics/animation.cpp8
-rw-r--r--graphics/animation.h1
-rw-r--r--sound/audiostream.cpp8
-rw-r--r--sound/audiostream.h2
-rw-r--r--sound/flac.cpp12
-rw-r--r--sound/mp3.cpp5
-rw-r--r--sound/vorbis.cpp12
7 files changed, 32 insertions, 16 deletions
diff --git a/graphics/animation.cpp b/graphics/animation.cpp
index 7d8cc1d920..0b5aba1e99 100644
--- a/graphics/animation.cpp
+++ b/graphics/animation.cpp
@@ -36,13 +36,11 @@ BaseAnimationState::~BaseAnimationState() {
if (decoder)
mpeg2_close(decoder);
delete mpgfile;
- delete sndfile;
#ifndef BACKEND_8BIT
_sys->hideOverlay();
free(overlay);
#endif
- if (bgSoundStream)
- delete bgSoundStream;
+ delete bgSoundStream;
#endif
}
@@ -53,7 +51,6 @@ bool BaseAnimationState::init(const char *name) {
decoder = NULL;
mpgfile = NULL;
- sndfile = NULL;
bgSoundStream = NULL;
#ifdef BACKEND_8BIT
@@ -132,8 +129,7 @@ bool BaseAnimationState::init(const char *name) {
ticks = _sys->get_msecs();
// Play audio
- sndfile = new File();
- bgSoundStream = AudioStream::openStreamFile(name, sndfile);
+ bgSoundStream = AudioStream::openStreamFile(name);
if (bgSoundStream != NULL) {
_snd->playInputStream(&bgSound, bgSoundStream, false, 255, 0, -1, false);
diff --git a/graphics/animation.h b/graphics/animation.h
index 4877be24a8..4722ceb176 100644
--- a/graphics/animation.h
+++ b/graphics/animation.h
@@ -90,7 +90,6 @@ protected:
#endif
File *mpgfile;
- File *sndfile;
byte buffer[BUFFER_SIZE];
diff --git a/sound/audiostream.cpp b/sound/audiostream.cpp
index 73ff1204cd..5d7a3ae4e1 100644
--- a/sound/audiostream.cpp
+++ b/sound/audiostream.cpp
@@ -67,7 +67,7 @@ static const StreamFileFormat STREAM_FILEFORMATS[] = {
{ NULL, NULL, NULL } // Terminator
};
-AudioStream* AudioStream::openStreamFile(const char* filename, File *fileHandle)
+AudioStream* AudioStream::openStreamFile(const char* filename)
{
char buffer[1024];
const uint len = strlen(filename);
@@ -78,6 +78,7 @@ AudioStream* AudioStream::openStreamFile(const char* filename, File *fileHandle)
char *ext = &buffer[len+1];
AudioStream* stream = NULL;
+ File *fileHandle = new File();
for (int i = 0; i < ARRAYSIZE(STREAM_FILEFORMATS)-1 && stream == NULL; ++i) {
strcpy(ext, STREAM_FILEFORMATS[i].fileExtension);
@@ -85,9 +86,12 @@ AudioStream* AudioStream::openStreamFile(const char* filename, File *fileHandle)
if (fileHandle->isOpen())
stream = STREAM_FILEFORMATS[i].openStreamFile(fileHandle, fileHandle->size());
}
+
+ // Do not reference the file anymore. If the stream didn't incRef the file,
+ // the object will be deleted (and the file be closed).
+ fileHandle->decRef();
if (stream == NULL) {
- fileHandle->close();
debug(1, "AudioStream: Could not open compressed AudioFile %s", filename);
}
diff --git a/sound/audiostream.h b/sound/audiostream.h
index 2dbc1ae81c..6cc719d457 100644
--- a/sound/audiostream.h
+++ b/sound/audiostream.h
@@ -86,7 +86,7 @@ public:
* @return an Audiostream ready to use in case of success;
* NULL in case of an error (e.g. invalid/nonexisting file)
*/
- static AudioStream* openStreamFile(const char* filename, File *fileHandle);
+ static AudioStream* openStreamFile(const char* filename);
};
class AppendableAudioStream : public AudioStream {
diff --git a/sound/flac.cpp b/sound/flac.cpp
index 18e332ead9..15e04f34f6 100644
--- a/sound/flac.cpp
+++ b/sound/flac.cpp
@@ -166,6 +166,8 @@ FlacInputStream::FlacInputStream(File *sourceFile, const uint32 fileStart)
_fileInfo.fileStartPos = fileStart;
_fileInfo.filePos = fileStart;
_fileInfo.fileEndPos = sourceFile->size();
+
+ _fileInfo.fileHandle->incRef();
}
FlacInputStream::FlacInputStream(File *sourceFile, const uint32 fileStart, const uint32 fileStop)
@@ -186,6 +188,8 @@ FlacInputStream::FlacInputStream(File *sourceFile, const uint32 fileStart, const
_fileInfo.fileStartPos = fileStart;
_fileInfo.filePos = fileStart;
_fileInfo.fileEndPos = fileStop;
+
+ _fileInfo.fileHandle->incRef();
}
FlacInputStream::~FlacInputStream() {
@@ -195,6 +199,8 @@ FlacInputStream::~FlacInputStream() {
}
if (_preBuffer.bufData != NULL)
delete[] _preBuffer.bufData;
+
+ _fileInfo.fileHandle->decRef();
}
inline FLAC__SeekableStreamDecoderState FlacInputStream::getState() const {
@@ -789,10 +795,8 @@ void FlacTrackInfo::play(SoundMixer *mixer, PlayingSoundHandle *handle, int star
FlacTrackInfo::~FlacTrackInfo()
{
- if (_firstStream)
- delete _firstStream;
- if (_file)
- delete _file;
+ delete _firstStream;
+ delete _file;
}
DigitalTrackInfo* getFlacTrack(int track)
diff --git a/sound/mp3.cpp b/sound/mp3.cpp
index 27938b7aa0..a71290952c 100644
--- a/sound/mp3.cpp
+++ b/sound/mp3.cpp
@@ -97,6 +97,8 @@ MP3InputStream::MP3InputStream(File *file, mad_timer_t duration, uint size) {
// If a size is specified, we do not perform any further read operations
if (size) {
_file = 0;
+ } else {
+ _file->incRef();
}
}
@@ -106,6 +108,9 @@ MP3InputStream::~MP3InputStream() {
mad_stream_finish(&_stream);
free(_ptr);
+
+ if (_file)
+ _file->decRef();
}
bool MP3InputStream::init() {
diff --git a/sound/vorbis.cpp b/sound/vorbis.cpp
index 4773823520..3d48d12b1b 100644
--- a/sound/vorbis.cpp
+++ b/sound/vorbis.cpp
@@ -102,7 +102,7 @@ static int seek_wrap(void *datasource, ogg_int64_t offset, int whence) {
static int close_wrap(void *datasource) {
file_info *f = (file_info *) datasource;
- f->file->close();
+ f->file->decRef();
delete f;
return 0;
}
@@ -193,6 +193,8 @@ class VorbisInputStream : public AudioStream {
inline bool eosIntern() const;
public:
VorbisInputStream(OggVorbis_File *file, int duration);
+ ~VorbisInputStream();
+
int readBuffer(int16 *buffer, const int numSamples);
int16 read();
@@ -225,6 +227,10 @@ VorbisInputStream::VorbisInputStream(OggVorbis_File *file, int duration)
refill();
}
+VorbisInputStream::~VorbisInputStream() {
+ ov_clear(_ov_file);
+}
+
inline int16 VorbisInputStream::read() {
assert(!eosIntern());
@@ -309,8 +315,10 @@ AudioStream *makeVorbisStream(File *file, uint32 size) {
delete ov_file;
delete f;
return 0;
- } else
+ } else {
+ file->incRef();
return new VorbisInputStream(ov_file, 0);
+ }
}
#endif