From b162e0dc04fc1e1b0883442c4079a6728029eea8 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Tue, 20 Feb 2007 13:50:20 +0000 Subject: Cleaned up AudioCDManager::getCachedTrack (in particular, don't empty a slot in the track cache if we are not going to use it) svn-id: r25740 --- sound/audiocd.cpp | 48 ++++++++++++++++++++++++++---------------------- sound/audiocd.h | 6 ++++-- 2 files changed, 30 insertions(+), 24 deletions(-) diff --git a/sound/audiocd.cpp b/sound/audiocd.cpp index 3e8a995624..8ae811ef10 100644 --- a/sound/audiocd.cpp +++ b/sound/audiocd.cpp @@ -47,7 +47,7 @@ struct TrackFormat { DigitalTrackInfo* (*openTrackFunction)(int); }; -static const TrackFormat TRACK_FORMATS[] = { +static const TrackFormat s_trackFormats[] = { /* decoderName, openTrackFunction */ #ifdef USE_VORBIS { "Ogg Vorbis", getVorbisTrack }, @@ -71,7 +71,7 @@ AudioCDManager::AudioCDManager() { _cd.start = 0; _cd.duration = 0; _cd.numLoops = 0; - _currentCache = 0; + _currentCacheIdx = 0; _mixer = g_system->getMixer(); assert(_mixer); } @@ -142,36 +142,40 @@ AudioCDManager::Status AudioCDManager::getStatus() const { } int AudioCDManager::getCachedTrack(int track) { - int i; - // See if we find the track in the cache - for (i = 0; i < CACHE_TRACKS; i++) + for (int i = 0; i < CACHE_TRACKS; i++) if (_cachedTracks[i] == track) { - if (_trackInfo[i]) - return i; - else - return -1; + return _trackInfo[i] ? i : -1; } - int currentIndex = _currentCache++; - _currentCache %= CACHE_TRACKS; - // Not found, see if it exists + // The track is not already in the cache. Try and see if + // we can load it. + DigitalTrackInfo *newTrack = 0; + for (const TrackFormat *format = s_trackFormats; + format->openTrackFunction != NULL && newTrack == NULL; + ++format) { + newTrack = format->openTrackFunction(track); + } + + int currentIndex = -1; - // First, delete the previous track info object - delete _trackInfo[currentIndex]; - _trackInfo[currentIndex] = NULL; - _cachedTracks[currentIndex] = 0; + if (newTrack != NULL) { + // We successfully loaded a digital track. Store it into _trackInfo. - for (i = 0; i < ARRAYSIZE(TRACK_FORMATS)-1 && _trackInfo[currentIndex] == NULL; ++i) - _trackInfo[currentIndex] = TRACK_FORMATS[i].openTrackFunction(track); + currentIndex = _currentCacheIdx++; + _currentCacheIdx %= CACHE_TRACKS; + + // First, delete the previous track info object + delete _trackInfo[currentIndex]; - if (_trackInfo[currentIndex] != NULL) { + // Then, store the new track info object + _trackInfo[currentIndex] = newTrack; _cachedTracks[currentIndex] = track; - return currentIndex; + } else { + debug(2, "Track %d not available in compressed format", track); } - debug(2, "Track %d not available in compressed format", track); - return -1; + return currentIndex; } } // End of namespace Audio diff --git a/sound/audiocd.h b/sound/audiocd.h index 837edcff82..b23942c9f5 100644 --- a/sound/audiocd.h +++ b/sound/audiocd.h @@ -34,8 +34,10 @@ namespace Audio { class DigitalTrackInfo { public: + virtual ~DigitalTrackInfo() {} + virtual void play(Mixer *mixer, SoundHandle *handle, int startFrame, int duration) = 0; - virtual ~DigitalTrackInfo() { } +// virtual void stop(); }; @@ -80,7 +82,7 @@ private: }; int _cachedTracks[CACHE_TRACKS]; DigitalTrackInfo *_trackInfo[CACHE_TRACKS]; - int _currentCache; + int _currentCacheIdx; Mixer *_mixer; }; -- cgit v1.2.3