[Scummvm-cvs-logs] SF.net SVN: scummvm: [22497] scummvm/trunk/engines/kyra

lordhoto at users.sourceforge.net lordhoto at users.sourceforge.net
Wed May 17 11:08:09 CEST 2006


Revision: 22497
Author:   lordhoto
Date:     2006-05-17 11:07:02 -0700 (Wed, 17 May 2006)
ViewCVS:  http://svn.sourceforge.net/scummvm/?rev=22497&view=rev

Log Message:
-----------
- adds AUD file player (has still some problems with playing)
- adds a new sound class SoundDigital (only used for Kyra3) (needs more work though)

Modified Paths:
--------------
    scummvm/trunk/engines/kyra/kyra.h
    scummvm/trunk/engines/kyra/kyra3.cpp
    scummvm/trunk/engines/kyra/module.mk
    scummvm/trunk/engines/kyra/sound.cpp
    scummvm/trunk/engines/kyra/sound.h

Added Paths:
-----------
    scummvm/trunk/engines/kyra/sound_digital.cpp
Modified: scummvm/trunk/engines/kyra/kyra.h
===================================================================
--- scummvm/trunk/engines/kyra/kyra.h	2006-05-16 22:16:31 UTC (rev 22496)
+++ scummvm/trunk/engines/kyra/kyra.h	2006-05-17 18:07:02 UTC (rev 22497)
@@ -30,6 +30,7 @@
 
 class Movie;
 class Sound;
+class SoundDigital;
 class SeqPlayer;
 class Resource;
 class PAKFile;
@@ -1016,10 +1017,23 @@
 	~KyraEngine_v3();
 	
 	Movie *createWSAMovie();
+	
+	SoundDigital *soundDigital() { return _soundDigital; }
 
 	int setupGameFlags() { _game = GI_KYRA3; return 0; }
 	
 	int go();
+private:
+	int init();
+
+	SoundDigital *_soundDigital;
+	
+	// sound specific
+private:
+	void playMenuAudioFile();
+	
+	int _musicSoundChannel;
+	const char *_menuAudioFile;
 };
 
 } // End of namespace Kyra

Modified: scummvm/trunk/engines/kyra/kyra3.cpp
===================================================================
--- scummvm/trunk/engines/kyra/kyra3.cpp	2006-05-16 22:16:31 UTC (rev 22496)
+++ scummvm/trunk/engines/kyra/kyra3.cpp	2006-05-17 18:07:02 UTC (rev 22497)
@@ -23,11 +23,15 @@
 #include "kyra/kyra.h"
 #include "kyra/screen.h"
 #include "kyra/wsamovie.h"
+#include "kyra/sound.h"
 
 #include "common/system.h"
 
 namespace Kyra {
 KyraEngine_v3::KyraEngine_v3(OSystem *system) : KyraEngine(system) {
+	_soundDigital = 0;
+	_musicSoundChannel = -1;
+	_menuAudioFile = "TITLE1.AUD";
 }
 
 KyraEngine_v3::~KyraEngine_v3() {
@@ -37,6 +41,16 @@
 	return new WSAMovieV3(this);
 }
 
+int KyraEngine_v3::init() {
+	KyraEngine::init();
+	
+	_soundDigital = new SoundDigital(this, _mixer);
+	assert(_soundDigital);
+	assert(_soundDigital->init());
+	
+	return 0;
+}
+
 int KyraEngine_v3::go() {
 	_screen->_curPage = 0;
 	_screen->clearPage(0);
@@ -54,6 +68,7 @@
 	_screen->setScreenPalette(pal);
 	
 	// XXX
+	playMenuAudioFile();
 	
 	logo->setX(0); logo->setY(0);
 	logo->setDrawPage(0);
@@ -61,6 +76,7 @@
 		uint32 nextRun = _system->getMillis() + 3 * _tickLength;
 		logo->displayFrame(i);
 		_screen->updateScreen();
+		playMenuAudioFile();
 		delayUntil(nextRun);
 	}
 	
@@ -68,6 +84,7 @@
 		uint32 nextRun = _system->getMillis() + 3 * _tickLength;
 		logo->displayFrame(i);
 		_screen->updateScreen();
+		playMenuAudioFile();
 		delayUntil(nextRun);
 	}
 	
@@ -75,4 +92,15 @@
 
 	return 0;
 }
+
+void KyraEngine_v3::playMenuAudioFile() {
+	if (!_soundDigital->isPlaying(_musicSoundChannel)) {
+		Common::File *handle = new Common::File();
+		uint32 temp = 0;
+		_res->fileHandle(_menuAudioFile, &temp, *handle);
+		if (handle->isOpen()) {
+			_musicSoundChannel = _soundDigital->playSound(handle, -1);
+		}
+	}
 }
+}

Modified: scummvm/trunk/engines/kyra/module.mk
===================================================================
--- scummvm/trunk/engines/kyra/module.mk	2006-05-16 22:16:31 UTC (rev 22496)
+++ scummvm/trunk/engines/kyra/module.mk	2006-05-17 18:07:02 UTC (rev 22497)
@@ -17,6 +17,7 @@
 	seqplayer.o \
 	sequences_v1.o \
 	sound_adlib.o \
+	sound_digital.o \
 	sound.o \
 	sprites.o \
 	staticres.o \

Modified: scummvm/trunk/engines/kyra/sound.cpp
===================================================================
--- scummvm/trunk/engines/kyra/sound.cpp	2006-05-16 22:16:31 UTC (rev 22496)
+++ scummvm/trunk/engines/kyra/sound.cpp	2006-05-17 18:07:02 UTC (rev 22497)
@@ -52,6 +52,7 @@
 		strcpy(filenamebuffer, file);
 		strcat(filenamebuffer, _supportedCodes[i].fileext);
 
+		_compressHandle.close();
 		_engine->resource()->fileHandle(filenamebuffer, &fileSize, _compressHandle);
 		if (!_compressHandle.isOpen())
 			continue;

Modified: scummvm/trunk/engines/kyra/sound.h
===================================================================
--- scummvm/trunk/engines/kyra/sound.h	2006-05-16 22:16:31 UTC (rev 22496)
+++ scummvm/trunk/engines/kyra/sound.h	2006-05-17 18:07:02 UTC (rev 22497)
@@ -25,6 +25,7 @@
 
 #include "common/stdafx.h"
 #include "common/scummsys.h"
+#include "common/file.h"
 #include "sound/mididrv.h"
 #include "sound/midiparser.h"
 #include "sound/mixer.h"
@@ -211,6 +212,28 @@
 	Sound *_music, *_sfx;
 };
 
+#define SOUND_STREAMS 4
+
+class SoundDigital {
+public:
+	SoundDigital(KyraEngine *vm, Audio::Mixer *mixer);
+	~SoundDigital();
+
+	bool init();
+	
+	int playSound(Common::File *fileHandle, int channel = -1);
+	bool isPlaying(int channel);
+	void stopSound(int channel);
+private:
+	KyraEngine *_vm;
+	Audio::Mixer *_mixer;
+
+	struct Sound {
+		Common::File *fileHandle;
+		Audio::SoundHandle handle;
+	} _sounds[SOUND_STREAMS];
+};
+
 } // end of namespace Kyra
 
 #endif

Added: scummvm/trunk/engines/kyra/sound_digital.cpp
===================================================================
--- scummvm/trunk/engines/kyra/sound_digital.cpp	                        (rev 0)
+++ scummvm/trunk/engines/kyra/sound_digital.cpp	2006-05-17 18:07:02 UTC (rev 22497)
@@ -0,0 +1,342 @@
+/* ScummVM - Scumm Interpreter
+ * Copyright (C) 2006 The ScummVM project
+ *
+ * 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
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * $URL $
+ * $Id $
+ *
+ */
+
+#include "kyra/sound.h"
+
+#include "sound/audiostream.h"
+
+namespace Kyra {
+
+// Thanks to Torbj\xF6rn Andersson (eriktorbjorn) for his aud player on which
+// this code is based on
+
+// TODO: cleanup of whole AUDStream
+// FIXME: sound 'stutters' a bit, maybe a problem while converting int8 samples to int16?
+class AUDStream : public Audio::AudioStream {
+public:
+	AUDStream(Common::File *file);
+	~AUDStream();
+	
+	int readBuffer(int16 *buffer, const int numSamples);
+
+	bool isStereo() const { return false; }
+	bool endOfData() const { return _endOfData; }
+	
+	int getRate() const { return _rate; }
+private:
+	Common::File *_file;
+	bool _endOfData;
+	int _rate;
+	uint _processedSize;
+	uint _totalSize;
+	
+	int _bytesLeft;
+	
+	uint8 *_outBuffer;
+	int _outBufferOffset;
+	uint _outBufferSize;
+	
+	uint8 *_inBuffer;
+	uint _inBufferSize;
+	
+	int readChunk(int16 *buffer, const int maxSamples);
+	
+	static const int8 WSTable2Bit[];
+	static const int8 WSTable4Bit[];
+};
+
+const int8 AUDStream::WSTable2Bit[] = { -2, -1, 0, 1 };
+const int8 AUDStream::WSTable4Bit[] = {
+	-9, -8, -6, -5, -4, -3, -2, -1,
+	 0,  1,  2,  3,  4,  5,  6,  8
+};
+
+AUDStream::AUDStream(Common::File *file) : _file(0), _endOfData(true), _rate(0),
+	_processedSize(0), _totalSize(0), _bytesLeft(0), _outBuffer(0),
+	_outBufferOffset(0), _outBufferSize(0), _inBuffer(0), _inBufferSize(0) {
+#if defined(__SYMBIAN32__)
+	// Symbian can't share filehandles between different threads.
+	// So create a new file  and seek that to the other filehandles position
+	_file= new File;
+	_file->open(file->name());
+	_file->seek(file->pos());
+#else
+	_file = file;
+#endif
+	_file->incRef();
+	
+	_rate = _file->readUint16LE();
+	_totalSize = _file->readUint32LE();
+	// TODO?: add checks
+	int flags = _file->readByte();	// flags
+	int type = _file->readByte();	// type
+	
+	if (type == 1 && !flags) {
+		_endOfData = false;
+	} else
+		warning("No AUD file (rate: %d, size: %d, flags: 0x%X, type: %d)", _rate, _totalSize, flags, type);
+}
+
+AUDStream::~AUDStream() {
+	delete [] _outBuffer;
+	delete [] _inBuffer;
+
+	if (_file)
+		_file->decRef();
+#ifdef __SYMBIAN32__
+	delete _file;
+#endif
+}
+
+int AUDStream::readBuffer(int16 *buffer, const int numSamples) {
+	int samplesRead = 0, samplesLeft = numSamples;
+	
+	while (samplesLeft > 0 && !_endOfData) {
+		int samples = readChunk(buffer, samplesLeft);
+		samplesRead += samples;
+		samplesLeft -= samples;
+		buffer += samples;
+	}
+
+	return samplesRead;
+}
+
+inline int16 clip8BitSample(int16 sample) {
+	if (sample > 255)
+		return 255;
+	if (sample < 0)
+		return 0;
+	return sample;
+}
+
+int AUDStream::readChunk(int16 *buffer, const int maxSamples) {
+	int samplesProcessed = 0;
+
+	// if no bytes of the old chunk are left, read the next one
+	if (_bytesLeft <= 0) {
+		if (_processedSize > _totalSize) {
+			_endOfData = true;
+			return 0;
+		}
+
+		uint16 size = _file->readUint16LE();
+		uint16 outSize = _file->readUint16LE();
+		uint32 id = _file->readUint32LE();
+		
+		assert(id == 0x0000DEAF);
+		
+		_processedSize += 8 + size;
+		
+		_outBufferOffset = 0;
+		if (size == outSize) {
+			if (outSize > _outBufferSize) {
+				_outBufferSize = outSize;
+				delete [] _outBuffer;
+				_outBuffer = new uint8[_outBufferSize];
+				assert(_outBuffer);
+			}
+
+			_bytesLeft = size;
+			
+			_file->read(_outBuffer, _bytesLeft);
+		} else {
+			_bytesLeft = outSize;
+			
+			if (outSize > _outBufferSize) {
+				_outBufferSize = outSize;
+				delete [] _outBuffer;
+				_outBuffer = new uint8[_outBufferSize];
+				assert(_outBuffer);
+			}
+			
+			if (size > _inBufferSize) {
+				_inBufferSize = size;
+				delete [] _inBuffer;
+				_inBuffer = new uint8[_inBufferSize];
+				assert(_inBuffer);
+			}
+			
+			if (_file->read(_inBuffer, size) != size) {
+				_endOfData = true;
+				return 0;
+			}
+			
+			int16 curSample = 0;
+			byte code = 0;
+			int8 count = 0;
+			uint16 input = 0;
+			int j = 0;
+			int i = 0;
+			
+			while (outSize > 0) {
+				input = _inBuffer[i++] << 2;
+				code = (input >> 8) & 0xff;
+				count = (input & 0xff) >> 2;
+
+				switch (code) {
+				case 2:
+					if (count & 0x20) {
+						/* NOTE: count is signed! */
+						count <<= 3;
+						curSample += (count >> 3);
+						_outBuffer[j++] = curSample & 0xFF;
+						outSize--;
+					} else {
+						for (; count >= 0; count--) {
+							_outBuffer[j++] = _inBuffer[i++];
+							outSize--;
+						}
+						curSample = _inBuffer[i - 1];
+					}
+					break;
+				case 1:
+					for (; count >= 0; count--) {
+						code = _inBuffer[i++];
+
+						curSample += WSTable4Bit[code & 0x0f];
+						curSample = clip8BitSample(curSample);
+						_outBuffer[j++] = curSample;
+
+						curSample += WSTable4Bit[code >> 4];
+						curSample = clip8BitSample(curSample);
+						_outBuffer[j++] = curSample;
+
+						outSize -= 2;
+					}
+					break;
+				case 0:
+					for (; count >= 0; count--) {
+						code = (uint8)_inBuffer[i++];
+
+						curSample += WSTable2Bit[code & 0x03];
+						curSample = clip8BitSample(curSample);
+						_outBuffer[j++] = curSample & 0xFF;
+
+						curSample += WSTable2Bit[(code >> 2) & 0x03];
+						curSample = clip8BitSample(curSample);
+						_outBuffer[j++] = curSample & 0xFF;
+
+						curSample += WSTable2Bit[(code >> 4) & 0x03];
+						curSample = clip8BitSample(curSample);
+						_outBuffer[j++] = curSample & 0xFF;
+
+						curSample += WSTable2Bit[(code >> 6) & 0x03];
+						curSample = clip8BitSample(curSample);
+						_outBuffer[j++] = curSample & 0xFF;
+
+						outSize -= 4;
+					}
+					break;
+				default:
+					for (; count >= 0; count--) {
+						_outBuffer[j++] = curSample & 0xFF;
+						outSize--;
+					}
+				}
+			}
+		}
+	}
+	
+	// copies the chunk data to the output buffer
+	if (_bytesLeft > 0) {
+		int samples = MIN(_bytesLeft, maxSamples);
+		samplesProcessed += samples;
+		_bytesLeft -= samples;
+
+		while (samples--) {
+			int16 sample = (int8)_outBuffer[_outBufferOffset++];
+			*buffer++ = (sample << 8) ^ 0x8000;
+		}
+	}
+
+	return samplesProcessed;
+}
+
+#pragma mark -
+
+SoundDigital::SoundDigital(KyraEngine *vm, Audio::Mixer *mixer) : _vm(vm), _mixer(mixer), _sounds() {}
+
+SoundDigital::~SoundDigital() {
+	for (int i = 0; i < SOUND_STREAMS; ++i) {
+		stopSound(i);
+	}
+}
+
+bool SoundDigital::init() {
+	return true;
+}
+
+int SoundDigital::playSound(Common::File *fileHandle, int channel) {
+	Sound *use = 0;
+	if (channel != -1 && channel < SOUND_STREAMS) {
+		stopSound(channel);
+		use = &_sounds[channel];
+	} else {
+		for (int i = 0; i < SOUND_STREAMS; ++i) {
+			if (!_sounds[i].fileHandle) {
+				use = &_sounds[i];
+				break;
+			}
+		}
+		
+		if (!use) {
+			warning("no free sound channel");
+			return -1;
+		}
+	}
+	
+	Audio::AudioStream *stream = new AUDStream(fileHandle);
+	if (stream->endOfData()) {
+		delete stream;
+		delete fileHandle;
+
+		return -1;
+	}
+	
+	// TODO: set correct sound type from channel id
+	_mixer->playInputStream(Audio::Mixer::kPlainSoundType, &use->handle, stream);
+	use->fileHandle = fileHandle;
+	
+	return use - _sounds;
+}
+
+bool SoundDigital::isPlaying(int channel) {
+	if (channel == -1)
+		return false;
+
+	assert(channel >= 0 && channel < SOUND_STREAMS);
+
+	return _mixer->isSoundHandleActive(_sounds[channel].handle);
+}
+
+void SoundDigital::stopSound(int channel) {
+	if (isPlaying(channel)) {
+		_mixer->stopHandle(_sounds[channel].handle);
+	}
+	
+	if (_sounds[channel].fileHandle) {
+		delete _sounds[channel].fileHandle;
+		_sounds[channel].fileHandle = 0;			
+	}
+}
+
+} // end of namespace Kyra


Property changes on: scummvm/trunk/engines/kyra/sound_digital.cpp
___________________________________________________________________
Name: svn:mime-type
   + text/plain
Name: svn:keywords
   + Id,URL
Name: svn:eol-style
   + native


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