diff options
-rw-r--r-- | engines/agos/animation.cpp | 11 | ||||
-rw-r--r-- | engines/agos/sound.cpp | 25 | ||||
-rw-r--r-- | engines/scumm/he/sound_he.cpp | 7 | ||||
-rw-r--r-- | engines/sword1/music.cpp | 39 | ||||
-rw-r--r-- | engines/tucker/sequences.cpp | 25 | ||||
-rw-r--r-- | sound/adpcm.cpp | 22 | ||||
-rw-r--r-- | sound/wave.cpp | 44 | ||||
-rw-r--r-- | sound/wave.h | 4 |
8 files changed, 53 insertions, 124 deletions
diff --git a/engines/agos/animation.cpp b/engines/agos/animation.cpp index efcd78e482..e2511f335f 100644 --- a/engines/agos/animation.cpp +++ b/engines/agos/animation.cpp @@ -282,7 +282,6 @@ void MoviePlayerDXA::stopVideo() { } void MoviePlayerDXA::startSound() { - byte *buffer; uint32 offset, size; if (getSoundTag() == MKID_BE('WAVE')) { @@ -302,18 +301,12 @@ void MoviePlayerDXA::startSound() { offset = in.readUint32LE(); size = in.readUint32LE(); - buffer = (byte *)malloc(size); in.seek(offset, SEEK_SET); - in.read(buffer, size); + _bgSoundStream = Audio::makeWAVStream(in.readStream(size), true); in.close(); } else { - buffer = (byte *)malloc(size); - _fileStream->read(buffer, size); + _bgSoundStream = Audio::makeWAVStream(_fileStream->readStream(size), true); } - - Common::MemoryReadStream stream(buffer, size); - _bgSoundStream = Audio::makeWAVStream(&stream, false); - free(buffer); } else { _bgSoundStream = Audio::AudioStream::openStreamFile(baseName); } diff --git a/engines/agos/sound.cpp b/engines/agos/sound.cpp index 3008442ed2..b93dce62a2 100644 --- a/engines/agos/sound.cpp +++ b/engines/agos/sound.cpp @@ -31,7 +31,6 @@ #include "agos/agos.h" #include "agos/sound.h" -#include "sound/adpcm.h" #include "sound/audiostream.h" #include "sound/flac.h" #include "sound/mixer.h" @@ -782,34 +781,14 @@ void Sound::playVoiceData(byte *soundData, uint sound) { } void Sound::playSoundData(Audio::SoundHandle *handle, byte *soundData, uint sound, int pan, int vol, bool loop) { - byte *buffer, flags; - uint16 compType; - int blockAlign, rate; - - // TODO: Use makeWAVStream() in future, when makeADPCMStream() allows sound looping int size = READ_LE_UINT32(soundData + 4); Common::MemoryReadStream stream(soundData, size); - if (!Audio::loadWAVFromStream(stream, size, rate, flags, &compType, &blockAlign)) - error("playSoundData: Not a valid WAV data"); + Audio::AudioStream *sndStream = Audio::makeWAVStream(&stream, true, loop); convertVolume(vol); convertPan(pan); - if (loop == true) - flags |= Audio::Mixer::FLAG_LOOP; - - if (compType == 2) { - Audio::AudioStream *sndStream = Audio::makeADPCMStream(&stream, false, size, Audio::kADPCMMS, rate, (flags & Audio::Mixer::FLAG_STEREO) ? 2 : 1, blockAlign); - buffer = (byte *)malloc(size * 4); - size = sndStream->readBuffer((int16*)buffer, size * 2); - size *= 2; // 16bits. - delete sndStream; - } else { - buffer = (byte *)malloc(size); - memcpy(buffer, soundData + stream.pos(), size); - } - - _mixer->playRaw(Audio::Mixer::kSFXSoundType, handle, buffer, size, rate, flags | Audio::Mixer::FLAG_AUTOFREE, -1, vol, pan); + _mixer->playInputStream(Audio::Mixer::kSFXSoundType, handle, sndStream, -1, vol, pan); } void Sound::stopSfx5() { diff --git a/engines/scumm/he/sound_he.cpp b/engines/scumm/he/sound_he.cpp index 477f3cf0ad..254fecbe0d 100644 --- a/engines/scumm/he/sound_he.cpp +++ b/engines/scumm/he/sound_he.cpp @@ -658,6 +658,13 @@ void SoundHE::playHESound(int soundID, int heOffset, int heChannel, int heFlags) _heChannel[heChannel].timer = size * 1000 / rate; flags |= Audio::Mixer::FLAG_AUTOFREE; + + // makeADPCMStream returns a stream in native endianness, but LinearInputStream (and playRaw) + // is defaulted to Big Endian. If we're on a Little Endian system, set the LE flag. +#ifdef SCUMM_LITTLE_ENDIAN + flags |= Audio::Mixer::FLAG_LITTLE_ENDIAN; +#endif + _mixer->playRaw(type, &_heSoundChannels[heChannel], sound + heOffset, size - heOffset, rate, flags, soundID); } else { _mixer->playRaw(type, &_heSoundChannels[heChannel], ptr + stream.pos() + heOffset, size - heOffset, rate, flags, soundID); diff --git a/engines/sword1/music.cpp b/engines/sword1/music.cpp index e30ba96bb7..8c0315052f 100644 --- a/engines/sword1/music.cpp +++ b/engines/sword1/music.cpp @@ -120,43 +120,6 @@ int BaseAudioStream::readBuffer(int16 *buffer, const int numSamples) { return retVal; } -class WaveAudioStream : public BaseAudioStream { -public: - WaveAudioStream(Common::SeekableReadStream *source, bool loop); - virtual int readBuffer(int16 *buffer, const int numSamples); -private: - virtual void rewind(); -}; - -WaveAudioStream::WaveAudioStream(Common::SeekableReadStream *source, bool loop) : BaseAudioStream(source, loop) { - rewind(); - - if (_samplesLeft == 0) - _loop = false; -} - -void WaveAudioStream::rewind() { - int rate, size; - byte flags; - - _sourceStream->seek(0); - - if (Audio::loadWAVFromStream(*_sourceStream, size, rate, flags)) { - reinit(size, rate, flags); - } -} - -int WaveAudioStream::readBuffer(int16 *buffer, const int numSamples) { - int retVal = BaseAudioStream::readBuffer(buffer, numSamples); - - if (_bitsPerSample == 16) { - for (int i = 0; i < retVal; i++) { - buffer[i] = (int16)READ_LE_UINT16(buffer + i); - } - } - - return retVal; -} class AiffAudioStream : public BaseAudioStream { public: @@ -252,7 +215,7 @@ bool MusicHandle::play(const char *fileBase, bool loop) { if (!_audioSource) { sprintf(fileName, "%s.wav", fileBase); if (_file.open(fileName)) - _audioSource = new WaveAudioStream(&_file, loop); + _audioSource = Audio::makeWAVStream(&_file, false, loop ? 0 : 1); } if (!_audioSource) { diff --git a/engines/tucker/sequences.cpp b/engines/tucker/sequences.cpp index e8280c3038..d115b1a8d1 100644 --- a/engines/tucker/sequences.cpp +++ b/engines/tucker/sequences.cpp @@ -601,27 +601,24 @@ Audio::AudioStream *AnimationSequencePlayer::loadSound(int index, AnimationSound case kAnimationSoundType16BitsRAW: size = f.size(); rate = 22050; - flags = Audio::Mixer::FLAG_UNSIGNED; - if (type == kAnimationSoundType16BitsRAW) { + flags = Audio::Mixer::FLAG_UNSIGNED|Audio::Mixer::FLAG_AUTOFREE; + if (type == kAnimationSoundType16BitsRAW) flags = Audio::Mixer::FLAG_LITTLE_ENDIAN | Audio::Mixer::FLAG_16BITS; + + if (size != 0) { + uint8 *sampleData = (uint8 *)malloc(size); + if (sampleData) { + f.read(sampleData, size); + stream = Audio::makeLinearInputStream(sampleData, size, rate, flags, 0, 0); + } } break; case kAnimationSoundTypeWAV: case kAnimationSoundTypeLoopingWAV: - Audio::loadWAVFromStream(f, size, rate, flags); - if (type == kAnimationSoundTypeLoopingWAV) { - flags |= Audio::Mixer::FLAG_LOOP; - } + stream = Audio::makeWAVStream(&f, true, type == kAnimationSoundTypeLoopingWAV); break; } - if (size != 0) { - uint8 *sampleData = (uint8 *)malloc(size); - if (sampleData) { - f.read(sampleData, size); - flags |= Audio::Mixer::FLAG_AUTOFREE; - stream = Audio::makeLinearInputStream(sampleData, size, rate, flags, 0, 0); - } - } + } return stream; } diff --git a/sound/adpcm.cpp b/sound/adpcm.cpp index 5cf1171fcb..66ca216c6e 100644 --- a/sound/adpcm.cpp +++ b/sound/adpcm.cpp @@ -204,8 +204,8 @@ int ADPCMInputStream::readBufferOKI(int16 *buffer, const int numSamples) { for (samples = 0; samples < numSamples && !_stream->eos() && _stream->pos() < _endpos; samples += 2) { data = _stream->readByte(); - buffer[samples] = TO_LE_16(decodeOKI((data >> 4) & 0x0f)); - buffer[samples + 1] = TO_LE_16(decodeOKI(data & 0x0f)); + buffer[samples] = decodeOKI((data >> 4) & 0x0f); + buffer[samples + 1] = decodeOKI(data & 0x0f); } return samples; } @@ -241,8 +241,8 @@ int ADPCMInputStream::readBufferMSIMA1(int16 *buffer, const int numSamples) { for (; samples < numSamples && _blockPos < _blockAlign && !_stream->eos() && _stream->pos() < _endpos; samples += 2) { data = _stream->readByte(); _blockPos++; - buffer[samples] = TO_LE_16(decodeIMA(data & 0x0f)); - buffer[samples + 1] = TO_LE_16(decodeIMA((data >> 4) & 0x0f)); + buffer[samples] = decodeIMA(data & 0x0f); + buffer[samples + 1] = decodeIMA((data >> 4) & 0x0f); } } return samples; @@ -263,7 +263,7 @@ int ADPCMInputStream::readBufferMSIMA2(int16 *buffer, const int numSamples) { for (nibble = 0; nibble < 8; nibble++) { k = ((data & 0xf0000000) >> 28); - buffer[samples + channel + nibble * 2] = TO_LE_16(decodeIMA(k)); + buffer[samples + channel + nibble * 2] = decodeIMA(k); data <<= 4; } } @@ -302,13 +302,11 @@ int ADPCMInputStream::readBufferMS(int channels, int16 *buffer, const int numSam for (i = 0; i < channels; i++) _status.ch[i].sample1 = _stream->readSint16LE(); - for (i = 0; i < channels; i++) { - _status.ch[i].sample2 = _stream->readSint16LE(); - buffer[samples++] = TO_LE_16(_status.ch[i].sample2); - } + for (i = 0; i < channels; i++) + buffer[samples++] = _status.ch[i].sample2 = _stream->readSint16LE(); for (i = 0; i < channels; i++) - buffer[samples++] = TO_LE_16(_status.ch[i].sample1); + buffer[samples++] = _status.ch[i].sample1; _blockPos = channels * 7; } @@ -316,8 +314,8 @@ int ADPCMInputStream::readBufferMS(int channels, int16 *buffer, const int numSam for (; samples < numSamples && _blockPos < _blockAlign && !_stream->eos() && _stream->pos() < _endpos; samples += 2) { data = _stream->readByte(); _blockPos++; - buffer[samples] = TO_LE_16(decodeMS(&_status.ch[0], (data >> 4) & 0x0f)); - buffer[samples + 1] = TO_LE_16(decodeMS(&_status.ch[channels - 1], data & 0x0f)); + buffer[samples] = decodeMS(&_status.ch[0], (data >> 4) & 0x0f); + buffer[samples + 1] = decodeMS(&_status.ch[channels - 1], data & 0x0f); } } diff --git a/sound/wave.cpp b/sound/wave.cpp index 76ba5178a5..49189f3ea3 100644 --- a/sound/wave.cpp +++ b/sound/wave.cpp @@ -121,10 +121,8 @@ bool loadWAVFromStream(Common::SeekableReadStream &stream, int &size, int &rate, flags |= Audio::Mixer::FLAG_UNSIGNED; else if (bitsPerSample == 16) // 16 bit data is signed little endian flags |= (Audio::Mixer::FLAG_16BITS | Audio::Mixer::FLAG_LITTLE_ENDIAN); - else if (bitsPerSample == 4 && type == 17) // MS IMA ADPCM compressed. We decompress it - flags |= (Audio::Mixer::FLAG_16BITS | Audio::Mixer::FLAG_LITTLE_ENDIAN); - else if (bitsPerSample == 4 && type == 2) // MS ADPCM compressed. We decompress it - flags |= (Audio::Mixer::FLAG_16BITS | Audio::Mixer::FLAG_LITTLE_ENDIAN); + else if (bitsPerSample == 4 && (type == 2 || type == 17)) + flags |= Audio::Mixer::FLAG_16BITS; else { warning("getWavInfo: unsupported bitsPerSample %d", bitsPerSample); return false; @@ -163,9 +161,9 @@ bool loadWAVFromStream(Common::SeekableReadStream &stream, int &size, int &rate, return true; } -AudioStream *makeWAVStream(Common::SeekableReadStream *stream, bool disposeAfterUse) { +AudioStream *makeWAVStream(Common::SeekableReadStream *stream, bool disposeAfterUse, bool loop) { int size, rate; - byte *data, flags; + byte flags; uint16 type; int blockAlign; @@ -175,33 +173,25 @@ AudioStream *makeWAVStream(Common::SeekableReadStream *stream, bool disposeAfter return 0; } - if (type == 17) { // MS IMA ADPCM - Audio::AudioStream *sndStream = Audio::makeADPCMStream(stream, false, size, Audio::kADPCMMSIma, rate, (flags & Audio::Mixer::FLAG_STEREO) ? 2 : 1, blockAlign); - data = (byte *)malloc(size * 4); - assert(data); - size = sndStream->readBuffer((int16*)data, size * 2); - size *= 2; // 16bits. - delete sndStream; - } else if (type == 2) { // MS ADPCM - Audio::AudioStream *sndStream = Audio::makeADPCMStream(stream, false, size, Audio::kADPCMMS, rate, (flags & Audio::Mixer::FLAG_STEREO) ? 2 : 1, blockAlign); - data = (byte *)malloc(size * 4); - assert(data); - size = sndStream->readBuffer((int16*)data, size * 2); - size *= 2; // 16bits. - delete sndStream; - } else { - // Plain data. Just read everything at once. - // TODO: More elegant would be to wrap the stream. - data = (byte *)malloc(size); - assert(data); - stream->read(data, size); - } + if (type == 17) // MS IMA ADPCM + return makeADPCMStream(stream, disposeAfterUse, size, Audio::kADPCMMSIma, rate, (flags & Audio::Mixer::FLAG_STEREO) ? 2 : 1, blockAlign, loop ? 0 : 1); + else if (type == 2) // MS ADPCM + return makeADPCMStream(stream, disposeAfterUse, size, Audio::kADPCMMS, rate, (flags & Audio::Mixer::FLAG_STEREO) ? 2 : 1, blockAlign, loop ? 0 : 1); + + // Raw PCM. Just read everything at once. + // TODO: More elegant would be to wrap the stream. + byte *data = (byte *)malloc(size); + assert(data); + stream->read(data, size); if (disposeAfterUse) delete stream; // Since we allocated our own buffer for the data, we must set the autofree flag. flags |= Audio::Mixer::FLAG_AUTOFREE; + + if (loop) + flags |= Audio::Mixer::FLAG_LOOP; return makeLinearInputStream(data, size, rate, flags, 0, 0); } diff --git a/sound/wave.h b/sound/wave.h index cc3b463ba4..951a57b5f9 100644 --- a/sound/wave.h +++ b/sound/wave.h @@ -69,11 +69,13 @@ extern bool loadWAVFromStream( * * @param stream the SeekableReadStream from which to read the WAVE data * @param disposeAfterUse whether to delete the stream after use + * @param loop whether to loop the sound (infinitely) * @return a new AudioStream, or NULL, if an error occured */ AudioStream *makeWAVStream( Common::SeekableReadStream *stream, - bool disposeAfterUse = false); + bool disposeAfterUse = false, + bool loop = false); } // End of namespace Audio |