aboutsummaryrefslogtreecommitdiff
path: root/engines/saga/music.cpp
diff options
context:
space:
mode:
authorFilippos Karapetis2007-07-14 13:41:03 +0000
committerFilippos Karapetis2007-07-14 13:41:03 +0000
commit009b100094363f1e1f167d9792a35af8a3c63793 (patch)
treee994e4f868636f1369de7248c36dd798618391e3 /engines/saga/music.cpp
parent9efa640f9c8f32b5c1a0b6b40621c55ef4b2b730 (diff)
downloadscummvm-rg350-009b100094363f1e1f167d9792a35af8a3c63793.tar.gz
scummvm-rg350-009b100094363f1e1f167d9792a35af8a3c63793.tar.bz2
scummvm-rg350-009b100094363f1e1f167d9792a35af8a3c63793.zip
Cleanup of the digital music code. Now all kinds of music (uncompressed and compressed) are handled by the same class, DigitalMusicInputStream
svn-id: r28069
Diffstat (limited to 'engines/saga/music.cpp')
-rw-r--r--engines/saga/music.cpp140
1 files changed, 69 insertions, 71 deletions
diff --git a/engines/saga/music.cpp b/engines/saga/music.cpp
index 950903f4bb..66f5de79c1 100644
--- a/engines/saga/music.cpp
+++ b/engines/saga/music.cpp
@@ -38,17 +38,9 @@ namespace Saga {
#define BUFFER_SIZE 4096
-// I haven't decided yet if it's a good idea to make looping part of the audio
-// stream class, or if I should use a "wrapper" class, like I did for Broken
-// Sword 2, to make it easier to add support for compressed music... but I'll
-// worry about that later.
-// Update by md5: Apparently, it wasn't a good idea. Compressed digital music
-// is handled outside of this class, so looping does not work for compressed
-// digital music yet
-// TODO/FIXME: Add looping support for compressed digital music
-
-class RAWInputStream : public Audio::AudioStream {
+class DigitalMusicInputStream : public Audio::AudioStream {
private:
+ Audio::AudioStream *_stream;
ResourceContext *_context;
Common::File *_file;
uint32 _filePos;
@@ -67,7 +59,8 @@ private:
}
public:
- RAWInputStream(SagaEngine *vm, ResourceContext *context, uint32 resourceId, bool looping, uint32 loopStart);
+ DigitalMusicInputStream(SagaEngine *vm, ResourceContext *context, uint32 resourceId, bool looping, uint32 loopStart);
+ ~DigitalMusicInputStream();
int readBuffer(int16 *buffer, const int numSamples);
@@ -76,17 +69,62 @@ public:
int getRate() const { return _musicInfo->frequency; }
};
-RAWInputStream::RAWInputStream(SagaEngine *vm, ResourceContext *context, uint32 resourceId, bool looping, uint32 loopStart)
+DigitalMusicInputStream::DigitalMusicInputStream(SagaEngine *vm, ResourceContext *context, uint32 resourceId, bool looping, uint32 loopStart)
: _context(context), _finished(false), _looping(looping), _bufferEnd(_buf + BUFFER_SIZE) {
ResourceData * resourceData;
+ byte compressedHeader[10];
+ GameSoundTypes soundType;
resourceData = vm->_resource->getResourceData(context, resourceId);
_file = context->getFile(resourceData);
_musicInfo = vm->getMusicInfo();
if (_musicInfo == NULL) {
- error("RAWInputStream() wrong musicInfo");
+ error("DigitalMusicInputStream() wrong musicInfo");
+ }
+
+ _stream = NULL;
+
+ if (vm->getFeatures() & GF_COMPRESSED_SOUNDS) {
+ // Read compressed header to determine compression type
+ _file->seek((long)resourceData->offset, SEEK_SET);
+ _file->read(compressedHeader, 9);
+
+ if (compressedHeader[0] == char(0)) {
+ soundType = kSoundMP3;
+ } else if (compressedHeader[0] == char(1)) {
+ soundType = kSoundOGG;
+ } else if (compressedHeader[0] == char(2)) {
+ soundType = kSoundFLAC;
+ }
+
+ switch (soundType) {
+#ifdef USE_MAD
+ case kSoundMP3:
+ debug(1, "Playing MP3 compressed digital music");
+ _stream = Audio::makeMP3Stream(_file, resourceData->size);
+ break;
+#endif
+#ifdef USE_VORBIS
+ case kSoundOGG:
+ debug(1, "Playing OGG compressed digital music");
+ _stream = Audio::makeVorbisStream(_file, resourceData->size);
+ break;
+#endif
+#ifdef USE_FLAC
+ case kSoundFLAC:
+ debug(1, "Playing FLAC compressed digital music");
+ _stream = Audio::makeFlacStream(_file, resourceData->size);
+ break;
+#endif
+ default:
+ // Unknown compression
+ error("Trying to play a compressed digital music, but the compression is not known");
+ break;
+ }
+
+ resourceData->offset += 9; // Skip compressed header
}
// Determine the end position
@@ -100,10 +138,23 @@ RAWInputStream::RAWInputStream(SagaEngine *vm, ResourceContext *context, uint32
refill();
}
-int RAWInputStream::readBuffer(int16 *buffer, const int numSamples) {
+DigitalMusicInputStream::~DigitalMusicInputStream() {
+ delete _stream;
+}
+
+int DigitalMusicInputStream::readBuffer(int16 *buffer, const int numSamples) {
+ // TODO/FIXME: Add looping support for compressed digital music
+ //if (!_looping && _stream != NULL)
+ if (_stream != NULL)
+ return _stream->readBuffer(buffer, numSamples);
+
int samples = 0;
while (samples < numSamples && !eosIntern()) {
- const int len = MIN(numSamples - samples, (int) (_bufferEnd - _pos));
+ int len = 0;
+ if (_stream != NULL)
+ len = _stream->readBuffer(buffer, numSamples);
+ else
+ len = MIN(numSamples - samples, (int) (_bufferEnd - _pos));
memcpy(buffer, _pos, len * 2);
buffer += len;
_pos += len;
@@ -115,7 +166,7 @@ int RAWInputStream::readBuffer(int16 *buffer, const int numSamples) {
return samples;
}
-void RAWInputStream::refill() {
+void DigitalMusicInputStream::refill() {
if (_finished)
return;
@@ -417,61 +468,8 @@ void Music::play(uint32 resourceId, MusicFlags flags) {
loopStart = 4 * 18727;
}
- if (!(_vm->getFeatures() & GF_COMPRESSED_SOUNDS)) {
- // uncompressed digital music
- audioStream = new RAWInputStream(_vm, _digitalMusicContext, resourceId - 9, flags == MUSIC_LOOP, loopStart);
- } else {
- // compressed digital music
- ResourceData * musicResourceData;
- Common::File *_file;
- byte compressedHeader[10];
- GameSoundTypes soundType;
-
- musicResourceData = _vm->_resource->getResourceData(_digitalMusicContext, resourceId - 9);
- _file = _digitalMusicContext->getFile(musicResourceData);
-
- if (_vm->getMusicInfo() == NULL) {
- error("RAWInputStream() wrong musicInfo");
- }
-
- _file->seek((long)musicResourceData->offset, SEEK_SET);
-
- _file->read(compressedHeader, 9);
-
- if (compressedHeader[0] == char(0)) {
- soundType = kSoundMP3;
- } else if (compressedHeader[0] == char(1)) {
- soundType = kSoundOGG;
- } else if (compressedHeader[0] == char(2)) {
- soundType = kSoundFLAC;
- }
-
- switch (soundType) {
-#ifdef USE_MAD
- case kSoundMP3:
- debug(1, "Playing MP3 compressed digital music");
- audioStream = Audio::makeMP3Stream(_file, musicResourceData->size);
- break;
-#endif
-#ifdef USE_VORBIS
- case kSoundOGG:
- debug(1, "Playing OGG compressed digital music");
- audioStream = Audio::makeVorbisStream(_file, musicResourceData->size);
- break;
-#endif
-#ifdef USE_FLAC
- case kSoundFLAC:
- debug(1, "Playing FLAC compressed digital music");
- audioStream = Audio::makeFlacStream(_file, musicResourceData->size);
- break;
-#endif
- default:
- // Unknown compression
- error("Trying to play a compressed digital music, but the compression is not known");
- break;
- }
-
- }
+ // digital music
+ audioStream = new DigitalMusicInputStream(_vm, _digitalMusicContext, resourceId - 9, flags == MUSIC_LOOP, loopStart);
}
}
}