File: sound.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (295 lines) | stat: -rw-r--r-- 8,358 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * 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 3 of the License, or
 * 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, see <http://www.gnu.org/licenses/>.
 *
 */

#include "audio/decoders/vorbis.h"
#include "common/util.h"
#include "ags/plugins/ags_waves/ags_waves.h"
#include "ags/shared/util/stdio_compat.h"
#include "ags/ags.h"

namespace AGS3 {
namespace Plugins {
namespace AGSWaves {

//const float PI = 3.14159265f;

void AGSWaves::SFX_Play(ScriptMethodParams &params) {
	PARAMS2(int, sfxNum, int, repeat);

	SoundEffect &effect = SFX[sfxNum];
	if (_mixer->isSoundHandleActive(effect._soundHandle)) {
		if (effect._allow == 1) {
			// In this case we should start the sound on a new channel, not stopping
			// the one currently playing.
			warning("TODO: play overlapping sound with SFX_Play");
		}
		return;
	}
	_mixer->stopHandle(effect._soundHandle);

	Common::ArchiveMemberPtr member = getFile(Common::String::format("sounds/sound%d.sfx", sfxNum).c_str());

	Audio::AudioStream *sound = loadOGG(member);

	if (sound != nullptr) {
		effect._volume = 255;

		playStream(Audio::Mixer::kSFXSoundType, &effect._soundHandle, sound, repeat);

		if (OGG_Filter && effect._filter && effect._volume > 1) {
			warning("TODO: Mix_RegisterEffect(grabChan, LPEffect, NULL, NULL);");
		}

		effect._repeat = repeat;
	} else {
		debug(0, "AGSWaves::SFX_Play couldn't load sfx %d", sfxNum);
	}
}

void AGSWaves::SFX_SetVolume(ScriptMethodParams &params) {
	PARAMS2(int, sfxNum, int, volume);

	SoundEffect &effect = SFX[sfxNum];
	_mixer->setChannelVolume(effect._soundHandle, volume);
	effect._volume = volume;
}

void AGSWaves::SFX_GetVolume(ScriptMethodParams &params) {
	PARAMS1(int, sfxNum);

	SoundEffect &effect = SFX[sfxNum];
	params._result = _mixer->getChannelVolume(effect._soundHandle);
}

void AGSWaves::Music_Play(ScriptMethodParams &params) {
	PARAMS6(int, MFX, int, repeat, int, fadeinMS, int, fadeoutMS, int, Position, bool, fixclick);
	MusicPlay(MFX, repeat, fadeinMS, fadeoutMS, Position, false, fixclick);
}

void AGSWaves::Music_SetVolume(ScriptMethodParams &params) {
	PARAMS1(int, volume);
	_mixer->setVolumeForSoundType(Audio::Mixer::kMusicSoundType, volume);
}

void AGSWaves::Music_GetVolume(ScriptMethodParams &params) {
	params._result = _mixer->getVolumeForSoundType(Audio::Mixer::kMusicSoundType);
}

void AGSWaves::SFX_Stop(ScriptMethodParams &params) {
	PARAMS1(int, sfxNum); //, int, fademsOUT);
	StopSFX(sfxNum);
}

void AGSWaves::SFX_SetPosition(ScriptMethodParams &params) {
#if 0
	PARAMS4(int, sfxNum, int, xS, int, yS, int, intensity);

	SoundEffect &effect = SFX[sfxNum];

	if (_mixer->isSoundHandleActive(effect._soundHandle)) {
		int angle = 0;
		int dist = 0;

		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 x2 = xS;
			int y2 = yS;

			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 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;
	}
#endif
	debug(0, "TODO: SFX_Setposition positional sound not yet implemented");
}

void AGSWaves::SFX_SetGlobalVolume(ScriptMethodParams &params) {
	PARAMS1(int, volume);
	_mixer->setVolumeForSoundType(Audio::Mixer::kPlainSoundType, volume);
}

void AGSWaves::Load_SFX(ScriptMethodParams &params) {
//	PARAMS1(int, sfxNum);
//	LoadSFX(sfxNum);
}

void AGSWaves::Audio_Apply_Filter(ScriptMethodParams &params) {
	PARAMS1(int, Frequency);

	GlitchFix();
	ApplyFilter(Frequency);
}

void AGSWaves::Audio_Remove_Filter(ScriptMethodParams &params) {
}

void AGSWaves::SFX_AllowOverlap(ScriptMethodParams &params) {
	PARAMS2(int, sfxNum, int, allow);
	SFX[sfxNum]._allow = allow;
}

void AGSWaves::SFX_Filter(ScriptMethodParams &params) {
	PARAMS2(int, sfxNum, int, enable);

	// THIS ENABLES/DISABLES the SFX LOW PASS FILTER,
	// I think by default all sound effects are affected by low pass, but there are some that i've manually disabled from being affected by it with this command
	SFX[sfxNum]._filter = enable;
}

Audio::AudioStream *AGSWaves::loadOGG(const Common::ArchiveMemberPtr member) {
#ifdef USE_VORBIS
	if (member) {
		Audio::AudioStream *stream = Audio::makeVorbisStream(member->createReadStream(), DisposeAfterUse::YES);
		return stream;
	}
#endif

	return nullptr;
}

void AGSWaves::playStream(Audio::Mixer::SoundType type, Audio::SoundHandle *handle, Audio::AudioStream *stream, int repeat) {
	if (!handle || !stream)
		return;

	if (repeat != 0) {
		Audio::SeekableAudioStream *sas =
			dynamic_cast<Audio::SeekableAudioStream *>(stream);
		assert(sas);

		// -1 for infinite, >0 number of successive repeats
		Audio::LoopingAudioStream *las =
			new Audio::LoopingAudioStream(sas, repeat + 1);
		_mixer->playStream(type, handle, las);
	} else {
		_mixer->playStream(type, handle, stream);
	}
}

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() {
	// TODO: Implementation
}

void AGSWaves::ApplyFilter(int setFrequency) {
	// THIS TURNS ON THE LOW PASS FILTER
	OGG_Filter = true;
	GeneralAudio.FilterFrequency = setFrequency;
	SetFilterFrequency(setFrequency);
}

void AGSWaves::SetFilterFrequency(int setFrequency) {
	// TODO: Implementation
}

void AGSWaves::MusicPlay(int MusicToPlay, int repeat, int fadeinMS, int fadeoutMS, int pos, bool forceplay, bool fixclick) {
	if (GeneralAudio.Disabled) {
		return;
	}

	// Stop any previous music
	_mixer->stopHandle(MFXStream._soundHandle);

	// Load OGG file for music
	Common::ArchiveMemberPtr member = getFile(Common::String::format("music/music%d.mfx", MusicToPlay).c_str());
	Audio::AudioStream *musicStream = loadOGG(member);
	if (!musicStream)
		return;

	bool samefile = currentMusic != MusicToPlay;
	if (forceplay)
		samefile = true;

	if (samefile) {
		currentMusicRepeat = repeat;
		currentMusicFadein = fadeinMS;
		currentMusic = MusicToPlay;

		if (!MFXStream.Switch) {
			MFXStream.Channel = 0;

			playStream(Audio::Mixer::kMusicSoundType,
				&MFXStream._soundHandle, musicStream, repeat);

			MFXStream.ID = MusicToPlay;
			MFXStream.FadeTime = (fadeinMS / 1000) * 40;
			MFXStream.FadeRate = (float)_mixer->getVolumeForSoundType(Audio::Mixer::kMusicSoundType)
				/ (float)MFXStream.FadeTime;
			MFXStream.FadeVolume = 0.0;
			MFXStream.HaltedZero = false;
			//MusicVolCanBeAdjusted=true;
		} else {
			MFXStream.HaltedOne = false;
			MFXStream.Channel = 1;

			playStream(Audio::Mixer::kMusicSoundType,
				&MFXStream._soundHandle, musicStream, repeat);

			MFXStream.ID = MusicToPlay;
			MFXStream.FadeTime = (fadeoutMS / 1000) * 40;
			MFXStream.FadeVolume = 0.0;//float(MusicGetVolume());
			MFXStream.FadeRate = (float)_mixer->getVolumeForSoundType(Audio::Mixer::kMusicSoundType)
				/ (float)MFXStream.FadeTime;
			//MusicVolCanBeAdjusted=false;
		}

		MFXStream.Switch = !MFXStream.Switch;
	}
}

} // namespace AGSWaves
} // namespace Plugins
} // namespace AGS3