[Scummvm-cvs-logs] SF.net SVN: scummvm:[45095] scummvm/trunk

fingolfin at users.sourceforge.net fingolfin at users.sourceforge.net
Thu Oct 15 00:37:05 CEST 2009


Revision: 45095
          http://scummvm.svn.sourceforge.net/scummvm/?rev=45095&view=rev
Author:   fingolfin
Date:     2009-10-14 22:37:05 +0000 (Wed, 14 Oct 2009)

Log Message:
-----------
Patch #2834677: Wave/ADPCM Endianness Fixes

Modified Paths:
--------------
    scummvm/trunk/engines/agos/animation.cpp
    scummvm/trunk/engines/agos/sound.cpp
    scummvm/trunk/engines/scumm/he/sound_he.cpp
    scummvm/trunk/engines/sword1/music.cpp
    scummvm/trunk/engines/tucker/sequences.cpp
    scummvm/trunk/sound/adpcm.cpp
    scummvm/trunk/sound/wave.cpp
    scummvm/trunk/sound/wave.h

Modified: scummvm/trunk/engines/agos/animation.cpp
===================================================================
--- scummvm/trunk/engines/agos/animation.cpp	2009-10-14 21:48:52 UTC (rev 45094)
+++ scummvm/trunk/engines/agos/animation.cpp	2009-10-14 22:37:05 UTC (rev 45095)
@@ -282,7 +282,6 @@
 }
 
 void MoviePlayerDXA::startSound() {
-	byte *buffer;
 	uint32 offset, size;
 
 	if (getSoundTag() == MKID_BE('WAVE')) {
@@ -302,18 +301,12 @@
 			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);
 	}

Modified: scummvm/trunk/engines/agos/sound.cpp
===================================================================
--- scummvm/trunk/engines/agos/sound.cpp	2009-10-14 21:48:52 UTC (rev 45094)
+++ scummvm/trunk/engines/agos/sound.cpp	2009-10-14 22:37:05 UTC (rev 45095)
@@ -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::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() {

Modified: scummvm/trunk/engines/scumm/he/sound_he.cpp
===================================================================
--- scummvm/trunk/engines/scumm/he/sound_he.cpp	2009-10-14 21:48:52 UTC (rev 45094)
+++ scummvm/trunk/engines/scumm/he/sound_he.cpp	2009-10-14 22:37:05 UTC (rev 45095)
@@ -658,6 +658,13 @@
 				_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);

Modified: scummvm/trunk/engines/sword1/music.cpp
===================================================================
--- scummvm/trunk/engines/sword1/music.cpp	2009-10-14 21:48:52 UTC (rev 45094)
+++ scummvm/trunk/engines/sword1/music.cpp	2009-10-14 22:37:05 UTC (rev 45095)
@@ -120,44 +120,7 @@
 	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:
 	AiffAudioStream(Common::SeekableReadStream *source, bool loop);
@@ -252,7 +215,7 @@
 	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) {

Modified: scummvm/trunk/engines/tucker/sequences.cpp
===================================================================
--- scummvm/trunk/engines/tucker/sequences.cpp	2009-10-14 21:48:52 UTC (rev 45094)
+++ scummvm/trunk/engines/tucker/sequences.cpp	2009-10-14 22:37:05 UTC (rev 45095)
@@ -601,27 +601,24 @@
 		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;
 }

Modified: scummvm/trunk/sound/adpcm.cpp
===================================================================
--- scummvm/trunk/sound/adpcm.cpp	2009-10-14 21:48:52 UTC (rev 45094)
+++ scummvm/trunk/sound/adpcm.cpp	2009-10-14 22:37:05 UTC (rev 45095)
@@ -204,8 +204,8 @@
 
 	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 @@
 		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 @@
 
 			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 @@
 			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 @@
 		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);
 		}
 	}
 

Modified: scummvm/trunk/sound/wave.cpp
===================================================================
--- scummvm/trunk/sound/wave.cpp	2009-10-14 21:48:52 UTC (rev 45094)
+++ scummvm/trunk/sound/wave.cpp	2009-10-14 22:37:05 UTC (rev 45095)
@@ -121,10 +121,8 @@
 		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 @@
 	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 @@
 		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);
 }

Modified: scummvm/trunk/sound/wave.h
===================================================================
--- scummvm/trunk/sound/wave.h	2009-10-14 21:48:52 UTC (rev 45094)
+++ scummvm/trunk/sound/wave.h	2009-10-14 22:37:05 UTC (rev 45095)
@@ -69,11 +69,13 @@
  *
  * @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
 


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Scummvm-git-logs mailing list