2003-11-29 12:11:01 +00:00
|
|
|
/* ScummVM - Scumm Interpreter
|
2006-01-18 17:39:49 +00:00
|
|
|
* Copyright (C) 2003-2006 The ScummVM project
|
2003-11-29 12:11:01 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2005-10-18 01:30:26 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2003-11-29 12:11:01 +00:00
|
|
|
*
|
2006-02-11 10:05:31 +00:00
|
|
|
* $URL$
|
|
|
|
* $Id$
|
2003-11-29 12:11:01 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2005-06-24 15:23:51 +00:00
|
|
|
#include "common/stdafx.h"
|
2003-11-29 12:11:01 +00:00
|
|
|
|
|
|
|
#include "sound/audiocd.h"
|
|
|
|
#include "sound/mp3.h"
|
|
|
|
#include "sound/vorbis.h"
|
2004-02-22 14:11:16 +00:00
|
|
|
#include "sound/flac.h"
|
2006-09-23 00:42:35 +00:00
|
|
|
#include "engines/engine.h"
|
2003-11-29 12:11:01 +00:00
|
|
|
#include "common/file.h"
|
|
|
|
#include "common/util.h"
|
2005-01-10 22:06:49 +00:00
|
|
|
#include "common/system.h"
|
2003-11-29 12:11:01 +00:00
|
|
|
|
2006-05-09 14:16:43 +00:00
|
|
|
DECLARE_SINGLETON(Audio::AudioCDManager);
|
|
|
|
|
2006-04-29 22:33:31 +00:00
|
|
|
namespace Audio {
|
|
|
|
|
2004-02-22 14:11:16 +00:00
|
|
|
struct TrackFormat {
|
|
|
|
/** Decodername */
|
|
|
|
const char* decoderName;
|
2005-07-30 21:11:48 +00:00
|
|
|
/**
|
2004-02-22 14:11:16 +00:00
|
|
|
* Pointer to a function which tries to open the specified track - the only argument
|
|
|
|
* is the number of the track to be played.
|
2007-02-19 18:17:38 +00:00
|
|
|
* Returns either a DigitalTrackInfo object representing the requested track or null
|
2004-02-22 14:11:16 +00:00
|
|
|
* in case of an error
|
|
|
|
*/
|
|
|
|
DigitalTrackInfo* (*openTrackFunction)(int);
|
|
|
|
};
|
|
|
|
|
2007-02-20 13:50:20 +00:00
|
|
|
static const TrackFormat s_trackFormats[] = {
|
2005-07-30 21:11:48 +00:00
|
|
|
/* decoderName, openTrackFunction */
|
2007-02-24 23:40:28 +00:00
|
|
|
#ifdef USE_FLAC
|
|
|
|
{ "Flac", getFlacTrack },
|
|
|
|
#endif
|
2005-08-10 12:42:56 +00:00
|
|
|
#ifdef USE_VORBIS
|
2004-02-22 14:11:16 +00:00
|
|
|
{ "Ogg Vorbis", getVorbisTrack },
|
2005-08-10 06:16:26 +00:00
|
|
|
#endif
|
2004-02-22 14:11:16 +00:00
|
|
|
#ifdef USE_MAD
|
2006-03-03 15:46:39 +00:00
|
|
|
{ "MPEG Layer 3", getMP3Track },
|
2005-08-10 06:16:26 +00:00
|
|
|
#endif
|
2004-02-22 14:11:16 +00:00
|
|
|
|
|
|
|
{ NULL, NULL } // Terminator
|
|
|
|
};
|
|
|
|
|
2003-11-29 12:11:01 +00:00
|
|
|
|
|
|
|
AudioCDManager::AudioCDManager() {
|
2005-04-20 11:13:56 +00:00
|
|
|
memset(_cachedTracks, 0, sizeof(_cachedTracks));
|
|
|
|
memset(_trackInfo, 0, sizeof(_trackInfo));
|
2007-02-07 16:27:43 +00:00
|
|
|
_cd.playing = false;
|
|
|
|
_cd.track = 0;
|
|
|
|
_cd.start = 0;
|
|
|
|
_cd.duration = 0;
|
|
|
|
_cd.numLoops = 0;
|
2007-02-20 13:50:20 +00:00
|
|
|
_currentCacheIdx = 0;
|
2006-10-21 12:44:10 +00:00
|
|
|
_mixer = g_system->getMixer();
|
|
|
|
assert(_mixer);
|
2003-11-29 12:11:01 +00:00
|
|
|
}
|
|
|
|
|
2003-11-29 13:56:33 +00:00
|
|
|
void AudioCDManager::play(int track, int numLoops, int startFrame, int duration) {
|
2003-11-29 12:11:01 +00:00
|
|
|
if (numLoops != 0 || startFrame != 0) {
|
|
|
|
// Try to load the track from a .mp3/.ogg file, and if found, use
|
|
|
|
// that. If not found, attempt to do regular Audio CD playback of
|
|
|
|
// the requested track.
|
|
|
|
int index = getCachedTrack(track);
|
2003-11-29 13:56:33 +00:00
|
|
|
|
|
|
|
_cd.track = track;
|
|
|
|
_cd.numLoops = numLoops;
|
|
|
|
_cd.start = startFrame;
|
|
|
|
_cd.duration = duration;
|
|
|
|
|
2003-11-29 12:11:01 +00:00
|
|
|
if (index >= 0) {
|
2006-10-21 12:44:10 +00:00
|
|
|
_mixer->stopHandle(_cd.handle);
|
2003-11-29 12:11:01 +00:00
|
|
|
_cd.playing = true;
|
2007-02-24 23:19:53 +00:00
|
|
|
/*
|
|
|
|
FIXME: Seems numLoops == 0 and numLoops == 1 both indicate a single repetition,
|
|
|
|
while all other positive numbers indicate precisely the number of desired
|
|
|
|
repetitions. Finally, -1 means infinitely many
|
|
|
|
*/
|
|
|
|
numLoops = (numLoops < 1) ? numLoops + 1 : numLoops;
|
|
|
|
_trackInfo[index]->play(_mixer, &_cd.handle, numLoops, _cd.start, _cd.duration);
|
2003-11-29 12:11:01 +00:00
|
|
|
} else {
|
2004-09-28 20:19:37 +00:00
|
|
|
g_system->playCD(track, numLoops, startFrame, duration);
|
2005-04-20 11:03:36 +00:00
|
|
|
_cd.playing = false;
|
2003-11-29 12:11:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-11-29 13:56:33 +00:00
|
|
|
void AudioCDManager::stop() {
|
2003-11-29 12:11:01 +00:00
|
|
|
if (_cd.playing) {
|
2006-10-21 12:44:10 +00:00
|
|
|
_mixer->stopHandle(_cd.handle);
|
2003-11-29 12:11:01 +00:00
|
|
|
_cd.playing = false;
|
|
|
|
} else {
|
2004-09-28 20:19:37 +00:00
|
|
|
g_system->stopCD();
|
2003-11-29 12:11:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-11-29 23:40:21 +00:00
|
|
|
bool AudioCDManager::isPlaying() const {
|
2004-09-28 20:19:37 +00:00
|
|
|
return _cd.playing || g_system->pollCD();
|
2003-11-29 12:11:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioCDManager::updateCD() {
|
|
|
|
if (_cd.playing) {
|
2007-02-24 23:19:53 +00:00
|
|
|
// Check whether the audio track stopped playback
|
2006-10-21 12:44:10 +00:00
|
|
|
if (!_mixer->isSoundHandleActive(_cd.handle)) {
|
2007-02-24 23:19:53 +00:00
|
|
|
// FIXME: We do not update the numLoops parameter here (and in fact,
|
|
|
|
// currently can't do that). Luckily, only one engine ever checks
|
|
|
|
// this part of the AudioCD status, namely the SCUMM engine; and it
|
|
|
|
// only checks
|
|
|
|
_cd.playing = false;
|
2003-11-29 12:11:01 +00:00
|
|
|
}
|
|
|
|
} else {
|
2004-09-28 20:19:37 +00:00
|
|
|
g_system->updateCD();
|
2003-11-29 12:11:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-11-29 13:56:33 +00:00
|
|
|
AudioCDManager::Status AudioCDManager::getStatus() const {
|
|
|
|
// TODO: This could be improved for "real" CD playback.
|
2003-12-24 00:25:18 +00:00
|
|
|
// But to do that, we would have to extend the OSystem interface.
|
2003-11-29 13:56:33 +00:00
|
|
|
Status info = _cd;
|
2003-11-29 23:40:21 +00:00
|
|
|
info.playing = isPlaying();
|
2003-11-29 13:56:33 +00:00
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2003-11-29 12:11:01 +00:00
|
|
|
int AudioCDManager::getCachedTrack(int track) {
|
|
|
|
// See if we find the track in the cache
|
2007-02-20 13:50:20 +00:00
|
|
|
for (int i = 0; i < CACHE_TRACKS; i++)
|
2005-04-20 11:13:56 +00:00
|
|
|
if (_cachedTracks[i] == track) {
|
2007-02-20 13:50:20 +00:00
|
|
|
return _trackInfo[i] ? i : -1;
|
2003-11-29 12:11:01 +00:00
|
|
|
}
|
|
|
|
|
2007-02-20 13:50:20 +00:00
|
|
|
// 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;
|
2003-11-29 12:11:01 +00:00
|
|
|
|
2007-02-20 13:50:20 +00:00
|
|
|
if (newTrack != NULL) {
|
|
|
|
// We successfully loaded a digital track. Store it into _trackInfo.
|
2003-11-29 12:11:01 +00:00
|
|
|
|
2007-02-20 13:50:20 +00:00
|
|
|
currentIndex = _currentCacheIdx++;
|
|
|
|
_currentCacheIdx %= CACHE_TRACKS;
|
|
|
|
|
|
|
|
// First, delete the previous track info object
|
|
|
|
delete _trackInfo[currentIndex];
|
2003-11-29 12:11:01 +00:00
|
|
|
|
2007-02-20 13:50:20 +00:00
|
|
|
// Then, store the new track info object
|
|
|
|
_trackInfo[currentIndex] = newTrack;
|
2005-04-20 11:13:56 +00:00
|
|
|
_cachedTracks[currentIndex] = track;
|
2007-02-20 13:50:20 +00:00
|
|
|
} else {
|
|
|
|
debug(2, "Track %d not available in compressed format", track);
|
2005-04-13 19:50:58 +00:00
|
|
|
}
|
2003-11-29 12:11:01 +00:00
|
|
|
|
2007-02-20 13:50:20 +00:00
|
|
|
return currentIndex;
|
2003-11-29 12:11:01 +00:00
|
|
|
}
|
2006-04-29 22:33:31 +00:00
|
|
|
|
|
|
|
} // End of namespace Audio
|