[Scummvm-git-logs] scummvm master -> 34cbff9dc26363ed3c8e518b13bf54af395771da

dreammaster dreammaster at scummvm.org
Sun Jul 25 01:52:08 UTC 2021


This automated email contains information about 2 new commits which have been
pushed to the 'scummvm' repo located at https://github.com/scummvm/scummvm .

Summary:
13f605e18d AGS: Implement music playback in AGSWaves, SFX simplification
34cbff9dc2 COMMON: Add roundUpCapacity to MemoryWriteStreamDynamic


Commit: 13f605e18dd77f1372ef42ecb664cd56c302b988
    https://github.com/scummvm/scummvm/commit/13f605e18dd77f1372ef42ecb664cd56c302b988
Author: Paul Gilbert (dreammaster at scummvm.org)
Date: 2021-07-24T17:32:11-07:00

Commit Message:
AGS: Implement music playback in AGSWaves, SFX simplification

I was havbing a bunch of troubles trying to get the SFX loading
to work like the original, particularly when to free audio streams.
In the end, I decided to load sound effects from scratch each time
they're needed.

Changed paths:
    engines/ags/plugins/ags_waves/ags_waves.cpp
    engines/ags/plugins/ags_waves/ags_waves.h
    engines/ags/plugins/ags_waves/sound.cpp
    engines/ags/plugins/ags_waves/vars.h


diff --git a/engines/ags/plugins/ags_waves/ags_waves.cpp b/engines/ags/plugins/ags_waves/ags_waves.cpp
index f7e7463721..e567c57a75 100644
--- a/engines/ags/plugins/ags_waves/ags_waves.cpp
+++ b/engines/ags/plugins/ags_waves/ags_waves.cpp
@@ -98,8 +98,22 @@ void AGSWaves::AGS_EngineStartup(IAGSEngine *engine) {
 	SCRIPT_METHOD(TintProper, AGSWaves::TintProper);
 	SCRIPT_METHOD(GetWalkbehindBaserine, AGSWaves::GetWalkbehindBaserine);
 	SCRIPT_METHOD(SetWalkbehindBaserine, AGSWaves::SetWalkbehindBaserine);
+
+	engine->RequestEventHook(AGSE_PREGUIDRAW);
+	engine->RequestEventHook(AGSE_PRESCREENDRAW);
+	engine->RequestEventHook(AGSE_SAVEGAME);
+	engine->RequestEventHook(AGSE_RESTOREGAME);
+	engine->RequestEventHook(AGSE_ENTERROOM);
+}
+
+AGSWaves::~AGSWaves() {
+	stopAllSounds();
+}
+
+void AGS_EngineShutdown() {
 }
 
+
 int64 AGSWaves::AGS_EngineOnEvent(int event, NumberPtr data) {
 	switch (event) {
 	case AGSE_PREGUIDRAW:
@@ -107,7 +121,7 @@ int64 AGSWaves::AGS_EngineOnEvent(int event, NumberPtr data) {
 		break;
 
 	case AGSE_RESTOREGAME: {
-		_mixer->stopAll();
+		stopAllSounds();
 
 		Serializer s(_engine, data, true);
 		for (int j = 0; j < 500 - 1; ++j) {
@@ -121,6 +135,7 @@ int64 AGSWaves::AGS_EngineOnEvent(int event, NumberPtr data) {
 	case AGSE_SAVEGAME: {
 		Serializer s(_engine, data, true);
 		for (int j = 0; j < 500 - 1; ++j) {
+			SFX[j]._playing = _mixer->isSoundHandleActive(SFX[j]._soundHandle);
 			s.syncAsInt(SFX[j]._repeat);
 			s.syncAsInt(SFX[j]._volume);
 			s.syncAsInt(SFX[j]._playing);
@@ -137,10 +152,7 @@ int64 AGSWaves::AGS_EngineOnEvent(int event, NumberPtr data) {
 		break;
 
 	case AGSE_ENTERROOM:
-		for (int j = 0; j < 500 - 1; ++j) {
-			if (!_mixer->isSoundHandleActive(SFX[j]._soundHandle))
-				UnloadSFX(j);
-		}
+		stopAllSounds();
 		break;
 
 	default:
diff --git a/engines/ags/plugins/ags_waves/ags_waves.h b/engines/ags/plugins/ags_waves/ags_waves.h
index a6f359947f..b5c8269f1d 100644
--- a/engines/ags/plugins/ags_waves/ags_waves.h
+++ b/engines/ags/plugins/ags_waves/ags_waves.h
@@ -24,6 +24,7 @@
 #define AGS_PLUGINS_AGS_WAVES_AGS_WAVES_H
 
 #include "audio/mixer.h"
+#include "common/fs.h"
 #include "ags/plugins/ags_plugin.h"
 #include "ags/plugins/ags_waves/vars.h"
 
@@ -156,17 +157,16 @@ private:
 	void PlaySFX(int SoundToPlay, int repeat);
 
 	/**
-	 * Loads a sound file to memory
-	 * @param i		Sound number to load
+	 * Stops a playing sound effect
 	 */
-	void LoadSFX(int i);
+	void StopSFX(int sfxNum);
 
 	/**
-	 * Unloads a sound file from memory
-	 * @param i		Sound number to unload
+	 * Loads a ScummVM OGG stream for playback
 	 */
-	void UnloadSFX(int i);
+	Audio::AudioStream *loadOGG(const Common::FSNode &fsNode);
 
+	void stopAllSounds();
 	void GlitchFix();
 	void ApplyFilter(int SetFrequency);
 	void SetFilterFrequency(int setFrequency);
@@ -174,7 +174,7 @@ private:
 
 public:
 	AGSWaves();
-	virtual ~AGSWaves() {}
+	virtual ~AGSWaves();
 
 	const char *AGS_GetPluginName() override;
 	void AGS_EngineStartup(IAGSEngine *engine) override;
diff --git a/engines/ags/plugins/ags_waves/sound.cpp b/engines/ags/plugins/ags_waves/sound.cpp
index be79ff8d44..6e413dac5e 100644
--- a/engines/ags/plugins/ags_waves/sound.cpp
+++ b/engines/ags/plugins/ags_waves/sound.cpp
@@ -37,11 +37,13 @@ void AGSWaves::SFX_Play(ScriptMethodParams &params) {
 	PARAMS2(int, sfxNum, int, repeat);
 
 	SoundEffect &effect = SFX[sfxNum];
-	if (effect._stream == nullptr) {
-		LoadSFX(sfxNum);
-	}
+	_mixer->stopHandle(effect._soundHandle);
+
+	Common::FSNode fsNode = ::AGS::g_vm->getGameFolder().getChild(
+		"sounds").getChild(Common::String::format("sound%d.sfx", sfxNum));
+
+	Audio::AudioStream *sound = loadOGG(fsNode);
 
-	Audio::AudioStream *sound = effect._stream;
 	if (sound != nullptr) {
 		effect._volume = 255;
 
@@ -52,12 +54,10 @@ void AGSWaves::SFX_Play(ScriptMethodParams &params) {
 
 			// -1 for infinite, >0 number of successive repeats
 			Audio::LoopingAudioStream *las =
-				new Audio::LoopingAudioStream(sas, repeat + 1, DisposeAfterUse::NO);
-			_mixer->playStream(Audio::Mixer::kSFXSoundType, &effect._soundHandle, las,
-				-1, 255, 0, DisposeAfterUse::YES);
+				new Audio::LoopingAudioStream(sas, repeat + 1);
+			_mixer->playStream(Audio::Mixer::kSFXSoundType, &effect._soundHandle, las);
 		} else {
-			_mixer->playStream(Audio::Mixer::kSFXSoundType, &effect._soundHandle, sound,
-				-1, effect._volume, 0, DisposeAfterUse::NO);
+			_mixer->playStream(Audio::Mixer::kSFXSoundType, &effect._soundHandle, sound);
 		}
 
 		if (OGG_Filter && effect._filter && effect._volume > 1) {
@@ -65,7 +65,6 @@ void AGSWaves::SFX_Play(ScriptMethodParams &params) {
 		}
 
 		effect._repeat = repeat;
-		effect._playing = true;
 	}
 }
 
@@ -73,18 +72,15 @@ void AGSWaves::SFX_SetVolume(ScriptMethodParams &params) {
 	PARAMS2(int, sfxNum, int, volume);
 
 	SoundEffect &effect = SFX[sfxNum];
-
-	if (effect._stream != nullptr) {
-		_mixer->setChannelVolume(effect._soundHandle, volume);
-		effect._volume = volume;
-	}
+	_mixer->setChannelVolume(effect._soundHandle, volume);
+	effect._volume = volume;
 }
 
 void AGSWaves::SFX_GetVolume(ScriptMethodParams &params) {
 	PARAMS1(int, sfxNum);
 
 	SoundEffect &effect = SFX[sfxNum];
-	params._result = effect._stream ? effect._volume : 0;
+	params._result = _mixer->getChannelVolume(effect._soundHandle);
 }
 
 void AGSWaves::Music_Play(ScriptMethodParams &params) {
@@ -103,17 +99,7 @@ void AGSWaves::Music_GetVolume(ScriptMethodParams &params) {
 
 void AGSWaves::SFX_Stop(ScriptMethodParams &params) {
 	PARAMS1(int, sfxNum); //, int, fademsOUT);
-
-	SoundEffect &effect = SFX[sfxNum];
-
-	if (effect._stream != nullptr) {
-		_mixer->stopHandle(effect._soundHandle);
-		UnloadSFX(sfxNum);
-
-		effect._playing = false;
-		effect._repeat = 0;
-		effect._channel = -2;
-	}
+	StopSFX(sfxNum);
 }
 
 void AGSWaves::SFX_SetPosition(ScriptMethodParams &params) {
@@ -121,42 +107,40 @@ void AGSWaves::SFX_SetPosition(ScriptMethodParams &params) {
 
 	SoundEffect &effect = SFX[sfxNum];
 
-	if (effect._stream != nullptr) {
-		if (_mixer->isSoundHandleActive(effect._soundHandle)) {
-			int angle = 0;
-			int dist = 0;
+	if (_mixer->isSoundHandleActive(effect._soundHandle)) { 
+		int angle = 0;
+		int dist = 0;
 
-			if (xS != 0 && yS != 0) {
-				int pid = _engine->GetPlayerCharacter();
-				playerCharacter = _engine->GetCharacter(pid);
+		if (xS != 0 && yS != 0) {
+			int pid = _engine->GetPlayerCharacter();
+			playerCharacter = _engine->GetCharacter(pid);
 
-				int x1 = Character_GetX((intptr_t)playerCharacter);
-				int y1 = Character_GetY((intptr_t)playerCharacter);
+			int x1 = Character_GetX((intptr_t)playerCharacter);
+			int y1 = Character_GetY((intptr_t)playerCharacter);
 
-				int x2 = xS;
-				int y2 = yS;
+			int x2 = xS;
+			int y2 = yS;
 
-				int defx = (x1 - x2) * (x1 - x2);
-				int defy = (y1 - y2) * (y1 - y2);
+			int defx = (x1 - x2) * (x1 - x2);
+			int defy = (y1 - y2) * (y1 - y2);
 
-				float SquareRoot = sqrt(float(defx + defy));
-				dist = int(SquareRoot) - intensity;
-				if (dist > 255) dist = 255;
-				if (dist < 0)  dist = 0;
+			float SquareRoot = sqrt(float(defx + defy));
+			dist = int(SquareRoot) - intensity;
+			if (dist > 255) dist = 255;
+			if (dist < 0)  dist = 0;
 
-				float xDiff = float(x2 - x1);
-				float yDiff = float(y2 - y1);
-				float at2 = atan2(yDiff, xDiff);
+			float xDiff = float(x2 - x1);
+			float yDiff = float(y2 - y1);
+			float at2 = atan2(yDiff, xDiff);
 
-				float angles = (at2 * 360.0 / PI);
-				angle = int(angles);//%360;
-			}
-
-			// TODO: Change Mix_SetPosition to ScummVM equivalent
-			//Mix_SetPosition(id, angle, dist);
-			(void)angle;
-			(void)dist;
+			float angles = (at2 * 360.0 / PI);
+			angle = int(angles);//%360;
 		}
+
+		// TODO: Change Mix_SetPosition to ScummVM equivalent
+		//Mix_SetPosition(id, angle, dist);
+		(void)angle;
+		(void)dist;
 	}
 }
 
@@ -166,8 +150,8 @@ void AGSWaves::SFX_SetGlobalVolume(ScriptMethodParams &params) {
 }
 
 void AGSWaves::Load_SFX(ScriptMethodParams &params) {
-	PARAMS1(int, sfxNum);
-	LoadSFX(sfxNum);
+//	PARAMS1(int, sfxNum);
+//	LoadSFX(sfxNum);
 }
 
 void AGSWaves::Audio_Apply_Filter(ScriptMethodParams &params) {
@@ -192,29 +176,35 @@ void AGSWaves::SFX_Filter(ScriptMethodParams &params) {
 	SFX[sfxNum]._filter = enable;
 }
 
-
-void AGSWaves::LoadSFX(int i) {
-	Common::FSNode fsNode = ::AGS::g_vm->getGameFolder().getChild(
-		"sounds").getChild(Common::String::format("sound%d.sfx", i));
-
+Audio::AudioStream *AGSWaves::loadOGG(const Common::FSNode &fsNode) {
 #ifdef USE_VORBIS
 	if (fsNode.exists()) {
 		Common::File *soundFile = new Common::File();
 		if (!soundFile->open(fsNode))
 			error("Failed to open");
 
-		SFX[i]._stream = Audio::makeVorbisStream(soundFile, DisposeAfterUse::YES);
-		assert(SFX[i]._stream);
+		Audio::AudioStream *stream = Audio::makeVorbisStream(soundFile, DisposeAfterUse::YES);
+		assert(stream);
+		return stream;
 	}
 #endif
+
+	return nullptr;
 }
 
-void AGSWaves::UnloadSFX(int i) {
-	if (SFX[i]._stream != nullptr) {
-		_mixer->stopHandle(SFX[i]._soundHandle);
-		delete SFX[i]._stream;
-		SFX[i]._stream = nullptr;
-	}
+void AGSWaves::StopSFX(int sfxNum) {
+	SoundEffect &effect = SFX[sfxNum];
+	_mixer->stopHandle(effect._soundHandle);
+	effect._playing = 0;
+	effect._repeat = 0;
+	effect._channel = -2;
+}
+
+void AGSWaves::stopAllSounds() {
+	for (int i = 0; i < 500; ++i)
+		StopSFX(i);
+
+	_mixer->stopHandle(MFXStream._soundHandle);
 }
 
 void AGSWaves::GlitchFix() {
@@ -237,8 +227,19 @@ void AGSWaves::MusicPlay(int MusicToPlay, int repeat, int fadeinMS, int fadeoutM
 		return;
 	}
 
+	// Stop any previous music
+	_mixer->stopHandle(MFXStream._soundHandle);
+
+	// Load OGG file for music
+	Common::FSNode fsNode = ::AGS::g_vm->getGameFolder().getChild(
+		"Music").getChild(Common::String::format("music%d.mfx", MusicToPlay));
+	Audio::AudioStream *musicStream = loadOGG(fsNode);
+	if (!musicStream)
+		return;
+
 	bool samefile = currentMusic != MusicToPlay;
-	if (forceplay) samefile = true;
+	if (forceplay)
+		samefile = true;
 
 	if (samefile) {
 		currentMusicRepeat = repeat;
@@ -248,7 +249,9 @@ void AGSWaves::MusicPlay(int MusicToPlay, int repeat, int fadeinMS, int fadeoutM
 		if (!MFXStream.Switch) {
 			MFXStream.Channel = 0;
 
-			warning("TODO: OGGplayMusic(MusicLoads[MusicToPlay].musicPath, 0, repeat, 0, fixclick);");
+			_mixer->playStream(Audio::Mixer::kMusicSoundType,
+				&MFXStream._soundHandle, musicStream);
+
 			MFXStream.ID = MusicToPlay;
 			MFXStream.FadeTime = (fadeinMS / 1000) * 40;
 			MFXStream.FadeRate = (float)_mixer->getVolumeForSoundType(Audio::Mixer::kMusicSoundType)
@@ -260,7 +263,8 @@ void AGSWaves::MusicPlay(int MusicToPlay, int repeat, int fadeinMS, int fadeoutM
 			MFXStream.HaltedOne = false;
 			MFXStream.Channel = 1;
 
-			warning("TODO: OGGplayMusic(MusicLoads[MusicToPlay].musicPath, 0, repeat, 1, fixclick);");
+			_mixer->playStream(Audio::Mixer::kMusicSoundType,
+				&MFXStream._soundHandle, musicStream);
 
 			MFXStream.ID = MusicToPlay;
 			MFXStream.FadeTime = (fadeoutMS / 1000) * 40;
diff --git a/engines/ags/plugins/ags_waves/vars.h b/engines/ags/plugins/ags_waves/vars.h
index 93ec9d7202..1f8f566ae7 100644
--- a/engines/ags/plugins/ags_waves/vars.h
+++ b/engines/ags/plugins/ags_waves/vars.h
@@ -72,20 +72,15 @@ typedef int (*SCAPI_CHARACTER_GETX)(AGSCharacter *ch);
 typedef int (*SCAPI_CHARACTER_GETY)(AGSCharacter *ch);
 typedef int (*SCAPI_CHARACTER_ID) (AGSCharacter *ch);
 
-struct getMus {
-	char musicPath[1024];
-};
-
 //WAVE SOUNDS FILES
 struct SoundEffect {
-	Audio::AudioStream *_stream;
 	Audio::SoundHandle _soundHandle;
-	int _repeat;
-	int _volume;
-	bool _playing;
-	int _allow;
-	int _channel;
-	int _filter;
+	int _repeat = 0;
+	int _volume = 0;
+	int _allow = 0;
+	int _channel = 0;
+	int _filter = 0;
+	int _playing = 0;
 };
 
 struct Aud {
@@ -97,49 +92,50 @@ struct Aud {
 };
 
 struct Mus {
-	int ID;
-	int FadeTime;
-	float FadeRate;
-	float FadeVolume;
-	int Channel;
-	bool Switch;
-	bool HaltedZero;
-	bool HaltedOne;
+	int ID = 0;
+	int FadeTime = 0;
+	float FadeRate = 0;
+	float FadeVolume = 0;
+	int Channel = 0;
+	bool Switch = 0;
+	bool HaltedZero = 0;
+	bool HaltedOne = 0;
+	Audio::SoundHandle _soundHandle;
 };
 
 struct RainParticle {
-	int x;
-	int y;
-	int fx;
-	int fy;
-	int life;
-	int trans;
-	bool active;
-	int translay;
-	int transhold;
+	int x = 0;
+	int y = 0;
+	int fx = 0;
+	int fy = 0;
+	int life = 0;
+	int trans = 0;
+	bool active = 0;
+	int translay = 0;
+	int transhold = 0;
 };
 
 struct MusicStream {
-	int volume;
-	const char *Filename;
-	int repeat;
-	stb_vorbis *Vorbis;
-	bool fix_click;
+	int volume = 0;
+	const char *Filename = nullptr;
+	int repeat = 0;
+	stb_vorbis *Vorbis = 0;
+	bool fix_click = false;
 };
 
 struct DustParticle {
-	int x;
-	int y;
-	int transp;
-	int life;
-	bool active;
-	int dx;
-	int dy;
-	int mlay;
-	int timlay;
-	int movedport;
-	int translay;
-	int translayHold;
+	int x = 0;
+	int y = 0;
+	int transp = 0;
+	int life = 0;
+	bool active = false;
+	int dx = 0;
+	int dy = 0;
+	int mlay = 0;
+	int timlay = 0;
+	int movedport = 0;
+	int translay = 0;
+	int translayHold = 0;
 };
 
 struct Vars {
@@ -152,7 +148,6 @@ struct Vars {
 	PluginMethod Character_GetY;
 	PluginMethod Character_ID;
 
-	getMus MusicLoads[80];
 	SoundEffect SFX[500];
 	RainParticle RainParticles[400];
 	RainParticle RainParticlesFore[400];
@@ -192,9 +187,9 @@ struct Vars {
 	MusicStream globalStream[2];
 	SDL_AudioDeviceID getDevice[2];
 	bool AudioEnabled = false;
-	float ix, iy, ua;
+	float ix = 0, iy = 0, ua = 0;
 	float b_time[5];
-	float d_time;
+	float d_time = 0;
 
 	// Y-coordinate first because we use horizontal scanlines
 	uint32 texture[texHeight][texWidth];


Commit: 34cbff9dc26363ed3c8e518b13bf54af395771da
    https://github.com/scummvm/scummvm/commit/34cbff9dc26363ed3c8e518b13bf54af395771da
Author: Paul Gilbert (dreammaster at scummvm.org)
Date: 2021-07-24T18:45:56-07:00

Commit Message:
COMMON: Add roundUpCapacity to MemoryWriteStreamDynamic

Changed paths:
    common/memstream.h


diff --git a/common/memstream.h b/common/memstream.h
index 2ef3dfb06b..7be976bc99 100644
--- a/common/memstream.h
+++ b/common/memstream.h
@@ -204,6 +204,16 @@ protected:
 
 		_size = new_len;
 	}
+
+	/** Round up capacity to the next power of 2.
+	  * A minimal capacity of 8 is used.
+	  */
+	static size_t roundUpCapacity(size_t capacity) {
+		size_t capa = 8;
+		while (capa < capacity)
+			capa <<= 1;
+		return capa;
+	}
 public:
 	explicit MemoryWriteStreamDynamic(DisposeAfterUse::Flag disposeMemory) : _capacity(0), _size(0), _ptr(nullptr), _data(nullptr), _pos(0), _disposeMemory(disposeMemory) {}
 
@@ -213,7 +223,9 @@ public:
 	}
 
 	uint32 write(const void *dataPtr, uint32 dataSize) override {
-		ensureCapacity(_pos + dataSize);
+		if ((_size + dataSize) >= _capacity)
+			ensureCapacity(roundUpCapacity(_pos + dataSize));
+
 		memcpy(_ptr, dataPtr, dataSize);
 		_ptr += dataSize;
 		_pos += dataSize;




More information about the Scummvm-git-logs mailing list