File: audio.cpp

package info (click to toggle)
freespace2 24.0.2%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: trixie
  • size: 43,188 kB
  • sloc: cpp: 583,107; ansic: 21,729; python: 1,174; sh: 464; makefile: 248; xml: 181
file content (422 lines) | stat: -rw-r--r-- 11,658 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
//
//

#include "audio.h"

#include "gamesnd/gamesnd.h"
#include "menuui/credits.h"
#include "menuui/mainhallmenu.h"
#include "mission/missionmessage.h"
#include "missionui/missionbrief.h"
#include "render/3d.h"
#include "weapon/weapon.h"
#include "scripting/api/objs/audio_stream.h"
#include "scripting/api/objs/enums.h"
#include "scripting/api/objs/sound.h"
#include "scripting/api/objs/vecmath.h"
#include "sound/audiostr.h"

extern float Master_event_music_volume;

namespace scripting {
namespace api {

//**********LIBRARY: Audio
ADE_LIB(l_Audio, "Audio", "ad", "Sound/Music Library");

ADE_VIRTVAR(MasterVoiceVolume,
	l_Audio,
	nullptr,
	"The current master voice volume. This property is read-only.",
	"number",
	"The volume in the range from 0 to 1")
{
	if (ADE_SETTING_VAR) {
		LuaError(L, "This property is read-only!");
	}
	return ade_set_args(L, "f", Master_voice_volume);
}

ADE_VIRTVAR(MasterEventMusicVolume,
	l_Audio,
	nullptr,
	"The current master event music volume. This property is read-only.",
	"number",
	"The volume in the range from 0 to 1")
{
	if (ADE_SETTING_VAR) {
		LuaError(L, "This property is read-only!");
	}
	return ade_set_args(L, "f", Master_event_music_volume);
}

ADE_VIRTVAR(MasterEffectsVolume,
	l_Audio,
	nullptr,
	"The current master effects volume. This property is read-only.",
	"number",
	"The volume in the range from 0 to 1")
{
	if (ADE_SETTING_VAR) {
		LuaError(L, "This property is read-only!");
	}
	return ade_set_args(L, "f", Master_sound_volume);
}

ADE_FUNC(getSoundentry, l_Audio, "string/number", "Return a sound entry matching the specified index or name. If you are using a number then the first valid index is 1", "soundentry", "soundentry or invalid handle on error")
{
	gamesnd_id index;

	if (lua_isnumber(L, 1))
	{
		int idx = -1;
		if(!ade_get_args(L, "i", &idx))
			return ade_set_error(L, "o", l_SoundEntry.Set(sound_entry_h()));

		index = gamesnd_get_by_tbl_index(idx);
	}
	else
	{
		const char* s = nullptr;
		if(!ade_get_args(L, "s", &s))
			return ade_set_error(L, "o", l_SoundEntry.Set(sound_entry_h()));

		if (s == NULL)
			return ade_set_error(L, "o", l_SoundEntry.Set(sound_entry_h()));

		index = gamesnd_get_by_name(s);
	}

	if (!index.isValid())
	{
		return ade_set_args(L, "o", l_SoundEntry.Set(sound_entry_h()));
	}
	else
	{
		return ade_set_args(L, "o", l_SoundEntry.Set(sound_entry_h(index)));
	}
}

ADE_FUNC(loadSoundfile, l_Audio, "string filename", "Loads the specified sound file", "soundfile", "A soundfile handle")
{
	const char* fileName = nullptr;

	if (!ade_get_args(L, "s", &fileName))
		return ade_set_error(L, "o", l_Soundfile.Set(soundfile_h(sound_load_id::invalid())));

	game_snd_entry tmp_gse;
	strcpy_s(tmp_gse.filename, fileName);
	auto n = snd_load(&tmp_gse, nullptr, 0);

	return ade_set_error(L, "o", l_Soundfile.Set(soundfile_h(n)));
}

ADE_FUNC(playSound, l_Audio, "soundentry", "Plays the specified sound entry handle", "sound", "A handle to the playing sound")
{
	sound_entry_h *seh = NULL;

	if (!ade_get_args(L, "o", l_SoundEntry.GetPtr(&seh)))
		return ade_set_error(L, "o", l_Sound.Set(sound_h()));

	if (seh == NULL || !seh->IsValid())
		return ade_set_error(L, "o", l_Sound.Set(sound_h()));

	auto handle = snd_play(seh->Get());

	if (!handle.isValid()) {
		return ade_set_args(L, "o", l_Sound.Set(sound_h()));
	}
	else
	{
		return ade_set_args(L, "o", l_Sound.Set(sound_h(seh->idx, handle)));
	}
}

ADE_FUNC(playLoopingSound, l_Audio, "soundentry", "Plays the specified sound as a looping sound", "sound", "A handle to the playing sound or invalid handle if playback failed")
{
	sound_entry_h *seh = NULL;

	if (!ade_get_args(L, "o", l_SoundEntry.GetPtr(&seh)))
		return ade_set_error(L, "o", l_Sound.Set(sound_h()));

	if (seh == NULL || !seh->IsValid())
		return ade_set_error(L, "o", l_Sound.Set(sound_h()));

	auto handle = snd_play_looping(seh->Get());

	if (!handle.isValid()) {
		return ade_set_args(L, "o", l_Sound.Set(sound_h()));
	}
	else
	{
		return ade_set_args(L, "o", l_Sound.Set(sound_h(seh->idx, handle)));
	}
}

ADE_FUNC(play3DSound, l_Audio, "soundentry, [vector source, vector listener]",
         "Plays the specified sound entry handle. Source if by default 0, 0, 0 and listener is by default the current "
         "viewposition",
         "sound3D", "A handle to the playing sound")
{
	sound_entry_h *seh = NULL;
	vec3d *source = &vmd_zero_vector;
	vec3d *listener = &View_position;

	if (!ade_get_args(L, "o|oo", l_SoundEntry.GetPtr(&seh), l_Vector.GetPtr(&source), l_Vector.GetPtr(&listener)))
		return ade_set_error(L, "o", l_Sound3D.Set(sound_h()));

	if (seh == NULL || !seh->IsValid())
		return ade_set_error(L, "o", l_Sound3D.Set(sound_h()));

	auto handle = snd_play_3d(seh->Get(), source, listener);

	if (!handle.isValid()) {
		return ade_set_args(L, "o", l_Sound3D.Set(sound_h()));
	}
	else
	{
		return ade_set_args(L, "o", l_Sound3D.Set(sound_h(seh->idx, handle)));
	}
}

ADE_FUNC(playGameSound,
	l_Audio,
	"sound index, [number Panning = 0.0 /* -1.0 left to 1.0 right */, number Volume = 100 /* in percent */, number "
	"Priority = 0 /* 0-3 */, boolean VoiceMessage = false]",
	"Plays a sound from #Game Sounds in sounds.tbl. A priority of 0 indicates that the song must play; 1-3 will "
	"specify the maximum number of that sound that can be played",
	"boolean",
	"True if sound was played, false if not (Replaced with a sound instance object in the future)")
{
	int idx;
	float pan=0.0f;
	float vol=100.0f;
	int pri=0;
	bool voice_msg = false;
	if(!ade_get_args(L, "i|ffib", &idx, &pan, &vol, &pri, &voice_msg))
		return ADE_RETURN_NIL;

	if(idx < 0)
		return ADE_RETURN_FALSE;

	if(pri < 0 || pri > 3)
		pri = 0;

	CLAMP(pan, -1.0f, 1.0f);
	CLAMP(vol, 0.0f, 100.0f);

	auto gamesnd_idx = gamesnd_get_by_tbl_index(idx);

	if (gamesnd_idx.isValid()) {
		auto sound_handle = snd_play(gamesnd_get_game_sound(gamesnd_idx), pan, vol * 0.01f, pri, voice_msg);
		return ade_set_args(L, "b", sound_handle.isValid());
	} else {
		LuaError(L, "Invalid sound index %i (Snds[%i]) in playGameSound()", idx, gamesnd_idx.value());
		return ADE_RETURN_FALSE;
	}
}

ADE_FUNC(playInterfaceSound, l_Audio, "sound index", "Plays a sound from #Interface Sounds in sounds.tbl", "boolean", "True if sound was played, false if not")
{
	int idx;
	if(!ade_get_args(L, "i", &idx))
		return ade_set_error(L, "b", false);

	auto gamesnd_idx = gamesnd_get_by_iface_tbl_index(idx);

	if (gamesnd_idx.isValid()) {
		gamesnd_play_iface(gamesnd_idx);
		return ade_set_args(L, "b", true);
	} else {
		LuaError(L, "Invalid sound index %i (Snds[%i]) in playInterfaceSound()", idx, gamesnd_idx.value());
		return ADE_RETURN_FALSE;
	}
}

ADE_FUNC(playInterfaceSoundByName, l_Audio, "string name",
         "Plays a sound from #Interface Sounds in sounds.tbl by specifying the name of the sound entry. Sounds using "
         "the retail sound syntax can be accessed by specifying the index number as a string.",
         "boolean", "True if sound was played, false if not")
{
	const char* name;
	if (!ade_get_args(L, "s", &name))
		return ade_set_error(L, "b", false);

	auto gamesnd_idx = gamesnd_get_by_iface_name(name);

	if (gamesnd_idx.isValid()) {
		gamesnd_play_iface(gamesnd_idx);
		return ade_set_args(L, "b", true);
	} else {
		LuaError(L, "Invalid sound name %s in playInterfaceSoundByName()", name);
		return ADE_RETURN_FALSE;
	}
}

ADE_FUNC(playMusic, l_Audio, "string Filename, [number volume = 1.0, boolean looping = true]", "Plays a music file using FS2Open's builtin music system. Volume is currently ignored, uses players music volume setting. Files passed to this function are looped by default.", "number", "Audiohandle of the created audiostream, or -1 on failure")
{
	const char* s;
	float volume = 1.0f;
	bool loop = true;
	if (!ade_get_args(L, "s|fb", &s, &volume, &loop))
		return ade_set_error(L, "i", -1);

	int ah = audiostream_open(s, ASF_MENUMUSIC);
	if(ah < 0)
		return ade_set_error(L, "i", -1);

	// didn't remove the volume parameter because it'll break the API
	volume = Master_event_music_volume;

	audiostream_play(ah, volume, loop ? 1 : 0);
	return ade_set_args(L, "i", ah);
}

ADE_FUNC(stopMusic,
	l_Audio,
	"number audiohandle, [boolean fade = false, string music_type /* briefing|credits|mainhall */]",
	"Stops a playing music file, provided audiohandle is valid. If the 3rd arg is set to one of "
	"briefing,credits,mainhall then that music will be stopped despite the audiohandle given.",
	nullptr,
	nullptr)
{
	int ah;
	bool fade = false;
	const char* music_type = nullptr;

	if(!ade_get_args(L, "i|bs", &ah, &fade, &music_type))
		return ADE_RETURN_NIL;

	if (ah >= MAX_AUDIO_STREAMS || ah < 0 )
		return ADE_RETURN_NIL;

	if (music_type == NULL) {
		audiostream_close_file(ah, fade);
	} else {
		if (!stricmp(music_type, "briefing"))	{
			briefing_stop_music(fade);
		} else if (!stricmp(music_type, "credits")) {
			credits_stop_music(fade);
		} else if (!stricmp(music_type, "mainhall")) {
			main_hall_stop_music(fade);
		} else {
			LuaError(L, "Invalid music type (%s) passed to stopMusic", music_type);
		}
	}

	return ADE_RETURN_NIL;
}

ADE_FUNC(pauseMusic,
	l_Audio,
	"number audiohandle, boolean pause",
	"Pauses or unpauses a playing music file, provided audiohandle is valid. The boolean argument should be true to "
	"pause and false to unpause. If the audiohandle is -1, *all* audio streams are paused or unpaused.",
	nullptr,
	nullptr)
{
	int ah;
	bool pause;

	if(!ade_get_args(L, "ib", &ah, &pause))
		return ADE_RETURN_NIL;

	if (ah >= 0 && ah < MAX_AUDIO_STREAMS)
	{
		if (pause)
			audiostream_pause(ah, true);
		else
			audiostream_unpause(ah, true);
	}
	else if (ah == -1)
	{
		if (pause)
			audiostream_pause_all(true);
		else
			audiostream_unpause_all(true);
	}

	return ADE_RETURN_NIL;
}

ADE_FUNC(openAudioStream,
	l_Audio,
	"string fileName, enumeration stream_type /* AUDIOSTREAM_* values */",
	"Opens an audio stream of the specified file and type. An audio stream is meant for more long time sounds since "
	"they are streamed from the file instead of loaded in its entirety.",
	"audio_stream",
	"A handle to the opened stream or invalid on error")
{
	const char* fileName = nullptr;
	enum_h streamTypeEnum;
	if (!ade_get_args(L, "so", &fileName, l_Enum.Get(&streamTypeEnum))) {
		return ade_set_args(L, "o", l_AudioStream.Set(-1));
	}

	int streamType;
	switch (streamTypeEnum.index) {
	case LE_ASF_EVENTMUSIC:
		streamType = ASF_EVENTMUSIC;
		break;
	case LE_ASF_MENUMUSIC:
		streamType = ASF_MENUMUSIC;
		break;
	case LE_ASF_VOICE:
		streamType = ASF_VOICE;
		break;
	default:
		LuaError(L, "Invalid audio stream type %d.", streamTypeEnum.index);
		return ade_set_args(L, "o", l_AudioStream.Set(-1));
	}

	int ah = audiostream_open(fileName, streamType);
	if (ah < 0)
		return ade_set_args(L, "o", l_AudioStream.Set(-1));

	return ade_set_args(L, "o", l_AudioStream.Set(ah));
}

ADE_FUNC(pauseWeaponSounds,
	l_Audio,
	"boolean pause",
	"Pauses or unpauses all weapon sounds. The boolean argument should be true to pause and false to unpause.",
	nullptr,
	nullptr)
{
	bool pause;

	if (!ade_get_args(L, "b", &pause))
		return ADE_RETURN_NIL;

	if (pause) {
		weapon_pause_sounds();
	} else {
		weapon_unpause_sounds();
	}

	return ADE_RETURN_NIL;
}

ADE_FUNC(pauseVoiceMessages,
	l_Audio,
	"boolean pause",
	"Pauses or unpauses all voice message sounds. The boolean argument should be true to pause and false to unpause.",
	nullptr,
	nullptr)
{
	bool pause;

	if (!ade_get_args(L, "b", &pause))
		return ADE_RETURN_NIL;

	if (pause) {
		message_pause_all();
	} else {
		message_resume_all();
	}

	return ADE_RETURN_NIL;
}

} // namespace api
} // namespace scripting