[Scummvm-cvs-logs] SF.net SVN: scummvm:[53834] scummvm/trunk/engines/toon

sylvaintv at users.sourceforge.net sylvaintv at users.sourceforge.net
Tue Oct 26 00:15:47 CEST 2010


Revision: 53834
          http://scummvm.svn.sourceforge.net/scummvm/?rev=53834&view=rev
Author:   sylvaintv
Date:     2010-10-25 22:15:47 +0000 (Mon, 25 Oct 2010)

Log Message:
-----------
TOON: Implemented Ambient SFX

For example used in arcade playing sounds, lullaby music,
toilet game win sound effects...

Modified Paths:
--------------
    scummvm/trunk/engines/toon/audio.cpp
    scummvm/trunk/engines/toon/audio.h
    scummvm/trunk/engines/toon/script_func.cpp
    scummvm/trunk/engines/toon/toon.cpp

Modified: scummvm/trunk/engines/toon/audio.cpp
===================================================================
--- scummvm/trunk/engines/toon/audio.cpp	2010-10-25 22:14:53 UTC (rev 53833)
+++ scummvm/trunk/engines/toon/audio.cpp	2010-10-25 22:15:47 UTC (rev 53834)
@@ -49,9 +49,18 @@
 	for (int32 i = 0; i < 4; i++) 
 		_audioPacks[i] = 0;
 
-	voiceMuted = false;
-	musicMuted = false;
-	sfxMuted = false;
+	for (int32 i = 0; i < 4; i++) {
+		_ambientSFXs[i]._delay = 0;
+		_ambientSFXs[i]._enabled = false;
+		_ambientSFXs[i]._id = -1;
+		_ambientSFXs[i]._channel = -1;
+		_ambientSFXs[i]._lastTimer = 0;
+		_ambientSFXs[i]._volume = 255;
+	}
+
+	_voiceMuted = false;
+	_musicMuted = false;
+	_sfxMuted = false;
 }
 
 AudioManager::~AudioManager(void) {
@@ -62,18 +71,18 @@
 
 void AudioManager::muteMusic(bool muted) {
 	setMusicVolume(muted ? 0 : 255);
-	musicMuted = muted;
+	_musicMuted = muted;
 }
 
 void AudioManager::muteVoice(bool muted) {
 	if(voiceStillPlaying() && _channels[2]) {
 		_channels[2]->setVolume(muted ? 0 : 255);
 	}
-	voiceMuted = muted;
+	_voiceMuted = muted;
 }
 
 void AudioManager::muteSfx(bool muted) {
-	sfxMuted = muted;
+	_sfxMuted = muted;
 }
 
 void AudioManager::removeInstance(AudioStreamInstance *inst) {
@@ -125,7 +134,7 @@
 	//if (!_channels[_currentMusicChannel])
 	//	delete _channels[_currentMusicChannel];
 	_channels[_currentMusicChannel] = new AudioStreamInstance(this, _mixer, srs, true);
-	_channels[_currentMusicChannel]->setVolume(musicMuted ? 0 : 255);
+	_channels[_currentMusicChannel]->setVolume(_musicMuted ? 0 : 255);
 	_channels[_currentMusicChannel]->play(true, Audio::Mixer::kMusicSoundType);
 }
 
@@ -152,11 +161,11 @@
 
 	_channels[2] = new AudioStreamInstance(this, _mixer, stream);
 	_channels[2]->play(false, Audio::Mixer::kSpeechSoundType);
-	_channels[2]->setVolume(voiceMuted ? 0 : 255);
+	_channels[2]->setVolume(_voiceMuted ? 0 : 255);
 
 }
 
-void AudioManager::playSFX(int32 id, int volume , bool genericSFX) {
+int32 AudioManager::playSFX(int32 id, int volume , bool genericSFX) {
 	debugC(4, kDebugAudio, "playSFX(%d, %d)", id, (genericSFX) ? 1 : 0);
 
 	// find a free SFX channel
@@ -168,16 +177,18 @@
 		stream = _audioPacks[3]->getStream(id, true);
 
 	if (stream->size() == 0)
-		return;
+		return -1;
 
 	for (int32 i = 3; i < 16; i++) {
 		if (!_channels[i]) {
 			_channels[i] = new AudioStreamInstance(this, _mixer, stream);
 			_channels[i]->play(false, Audio::Mixer::kSFXSoundType);
-			_channels[i]->setVolume(sfxMuted ? 0 : volume);
-			break;
+			_channels[i]->setVolume(_sfxMuted ? 0 : volume);
+			return i;
 		}
 	}
+
+	return -1;
 }
 
 void AudioManager::stopCurrentVoice() {
@@ -517,5 +528,94 @@
 	}
 }
 
+void AudioManager::startAmbientSFX(int32 id, int32 delay, int32 mode, int32 volume)
+{
+	int32 found = -1;
+	for (int32 i = 0; i < 4; i++) {
+		if (!_ambientSFXs[i]._enabled) {
+			found = i;
+			break;
+		}
+	}
+
+	if (found < 0) 
+		return;
+
+	_ambientSFXs[found]._lastTimer = _vm->getOldMilli() - 1;
+	_ambientSFXs[found]._delay = delay;
+	_ambientSFXs[found]._enabled = true;
+	_ambientSFXs[found]._mode = mode;
+	_ambientSFXs[found]._volume = volume;
+	_ambientSFXs[found]._id = id;
+	updateAmbientSFX();
+
+}
+
+void AudioManager::setAmbientSFXVolume(int32 id, int volume) {
+	for (int32 i = 0; i < 4; i++) {
+		AudioAmbientSFX* ambient = &_ambientSFXs[i];
+		if (ambient->_id == id && ambient->_enabled) {
+			ambient->_volume = volume;
+			if (ambient->_channel >= 0 && _channels[ambient->_channel] && _channels[ambient->_channel]->isPlaying()) {
+				_channels[ambient->_channel]->setVolume(volume);
+			}
+			break;
+		}
+	}
+}
+
+void AudioManager::killAmbientSFX(int32 id)
+{
+	for (int32 i = 0; i < 4; i++) {
+		AudioAmbientSFX* ambient = &_ambientSFXs[i];
+		if (ambient->_id == id && ambient->_enabled) {
+			ambient->_enabled = false;
+			ambient->_id = -1;
+
+			if (_channels[ambient->_channel]) {
+				_channels[ambient->_channel]->stop(false);
+			}
+		}
+
+	}
+}
+
+void AudioManager::killAllAmbientSFX()
+{
+	for (int32 i = 0; i < 4; i++) {
+		AudioAmbientSFX* ambient = &_ambientSFXs[i];
+		if (ambient->_enabled) {
+			ambient->_enabled = false;
+			ambient->_id = -1;
+
+			if (_channels[ambient->_channel] && _channels[ambient->_channel]->isPlaying()) {
+				_channels[ambient->_channel]->stop(false);
+			}
+		}
+	}
+}
+
+void AudioManager::updateAmbientSFX()
+{
+	if (_vm->getMoviePlayer()->isPlaying()) return;
+
+	for (int32 i = 0; i < 4; i++) {
+		AudioAmbientSFX* ambient = &_ambientSFXs[i];
+		if (ambient->_enabled && (ambient->_channel < 0 || !(_channels[ambient->_channel] && _channels[ambient->_channel]->isPlaying()))) {
+			if(ambient->_mode == 1) {
+				if (_vm->randRange(0, 32767) < ambient->_delay) {					
+					ambient->_channel = playSFX(ambient->_id, ambient->_volume, false);
+				}
+			} else {
+				if (_vm->getOldMilli() > ambient->_lastTimer) {
+					ambient->_channel = playSFX(ambient->_id, ambient->_volume, false);
+					ambient->_lastTimer = _vm->getOldMilli(); // + 60 * _vm->getTickLength() * ambient->_delay;
+				}
+			}
+		}
+	}
+}
+
+
 } // End of namespace Toon
 

Modified: scummvm/trunk/engines/toon/audio.h
===================================================================
--- scummvm/trunk/engines/toon/audio.h	2010-10-25 22:14:53 UTC (rev 53833)
+++ scummvm/trunk/engines/toon/audio.h	2010-10-25 22:15:47 UTC (rev 53834)
@@ -109,6 +109,16 @@
 	ToonEngine *_vm;
 };
 
+struct AudioAmbientSFX {
+	int32 _id;
+	int32 _volume;
+	int32 _lastTimer;
+	int32 _delay;
+	int32 _mode;
+	int32 _channel;
+	bool _enabled;
+};
+
 class AudioManager {
 public:
 	void removeInstance(AudioStreamInstance *inst); // called by destructor
@@ -120,17 +130,23 @@
 
 	void playMusic(Common::String dir, Common::String music);
 	void playVoice(int32 id, bool genericVoice);
-	void playSFX(int32 id, int volume, bool genericSFX);
+	int32 playSFX(int32 id, int volume, bool genericSFX);
 	void stopCurrentVoice();
 	void setMusicVolume(int32 volume);
 	void stopMusic();
 	void muteVoice(bool mute);
 	void muteMusic(bool mute);
 	void muteSfx(bool mute);
-	bool isVoiceMuted() { return voiceMuted; }
-	bool isMusicMuted() { return musicMuted; }	
-	bool isSfxMuted() { return sfxMuted; }
+	bool isVoiceMuted() { return _voiceMuted; }
+	bool isMusicMuted() { return _musicMuted; }	
+	bool isSfxMuted() { return _sfxMuted; }
 
+	void startAmbientSFX(int32 id, int32 delay, int32 mode, int32 volume);
+	void killAmbientSFX(int32 id);
+	void killAllAmbientSFX();
+	void updateAmbientSFX();
+	void setAmbientSFXVolume(int32 id, int volume);
+
 	void closeAudioPack(int32 id);
 	bool loadAudioPack(int32 id, Common::String indexFile, Common::String packFile);
 
@@ -148,9 +164,11 @@
 	Audio::Mixer *_mixer;
 
 protected:
-	bool voiceMuted;
-	bool musicMuted;
-	bool sfxMuted;
+	bool _voiceMuted;
+	bool _musicMuted;
+	bool _sfxMuted;
+
+	AudioAmbientSFX _ambientSFXs[4];
 };
 
 } // End of namespace Toon

Modified: scummvm/trunk/engines/toon/script_func.cpp
===================================================================
--- scummvm/trunk/engines/toon/script_func.cpp	2010-10-25 22:14:53 UTC (rev 53833)
+++ scummvm/trunk/engines/toon/script_func.cpp	2010-10-25 22:15:47 UTC (rev 53834)
@@ -1054,18 +1054,26 @@
 }
 
 int32 ScriptFunc::sys_Cmd_Set_Ambient_Sfx(EMCState *state) {
+	//printf("Ambient Sfx : %d %d %d %d\n", stackPos(0), stackPos(1), stackPos(2), stackPos(3));
+	_vm->getAudioManager()->startAmbientSFX(stackPos(0), stackPos(1), stackPos(2), stackPos(3));
 	return 0;
 }
 
 int32 ScriptFunc::sys_Cmd_Kill_Ambient_Sfx(EMCState *state) {
+	//printf("Kill Sfx : %d \n", stackPos(0));
+	_vm->getAudioManager()->killAmbientSFX(stackPos(0));
 	return 0;
 }
 
 int32 ScriptFunc::sys_Cmd_Set_Ambient_Sfx_Plus(EMCState *state) {
+	//printf("Ambient Sfx Plus: %d %d %d %d %d %d %d %d\n", stackPos(0), stackPos(1), stackPos(2), stackPos(3), stackPos(4), stackPos(5), stackPos(6), stackPos(7));
+	_vm->getAudioManager()->startAmbientSFX(stackPos(0), stackPos(1), stackPos(2), stackPos(3));
 	return 0;
 }
 
 int32 ScriptFunc::sys_Cmd_Set_Ambient_Volume(EMCState *state) {
+	//printf("Ambient Volume : %d %d \n", stackPos(0), stackPos(1));
+	_vm->getAudioManager()->setAmbientSFXVolume(stackPos(0), stackPos(1));
 	return 0;
 }
 

Modified: scummvm/trunk/engines/toon/toon.cpp
===================================================================
--- scummvm/trunk/engines/toon/toon.cpp	2010-10-25 22:14:53 UTC (rev 53833)
+++ scummvm/trunk/engines/toon/toon.cpp	2010-10-25 22:15:47 UTC (rev 53834)
@@ -161,7 +161,7 @@
 	// Wait after a specified number of script steps when executing a script
 	// to lower CPU usage
 	if (++_scriptStep >= 40) {
-		g_system->delayMillis(10);
+		g_system->delayMillis(1);
 		_scriptStep = 0;
 	}
 }
@@ -359,6 +359,7 @@
 	updateTimer(timeIncrement);
 	updateTimers();
 	updateScrolling(false, timeIncrement);
+	_audioManager->updateAmbientSFX();
 	_animationManager->update(timeIncrement);
 	_cursorAnimationInstance->update(timeIncrement);
 
@@ -815,6 +816,7 @@
 	updateAnimationSceneScripts(elapsedTime);
 	updateTimer(elapsedTime);
 	_animationManager->update(elapsedTime);
+	_audioManager->updateAmbientSFX();
 	render();
 
 	
@@ -1574,6 +1576,8 @@
 	strcpy(temp, createRoomFilename(Common::String::printf("%s.pak", _gameState->_locations[_gameState->_currentScene]._name).c_str()).c_str());
 	resources()->closePackage(temp);
 
+	_audioManager->killAllAmbientSFX();
+
 	_drew->stopWalk();
 	_flux->stopWalk();
 
@@ -2665,6 +2669,7 @@
 	}
 }
 
+
 void ToonEngine::playSFX(int32 id, int32 volume) {
 	if (id < 0)
 		_audioManager->playSFX(-id + 1, volume, true);


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