diff options
Diffstat (limited to 'sound/audiocd.cpp')
-rw-r--r-- | sound/audiocd.cpp | 48 |
1 files changed, 26 insertions, 22 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 |