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 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
|
//
//
#include "sound.h"
#include "vecmath.h"
#include "gamesnd/gamesnd.h"
namespace scripting {
namespace api {
sound_entry_h::sound_entry_h() {
}
sound_entry_h::sound_entry_h(gamesnd_id n_idx) {
idx = n_idx;
}
game_snd* sound_entry_h::Get() const {
if (!this->isValid())
return NULL;
return gamesnd_get_game_sound(idx);
}
bool sound_entry_h::isValid() const {
return gamesnd_game_sound_valid(idx);
}
//**********HANDLE: SoundEntry
ADE_OBJ(l_SoundEntry, sound_entry_h, "soundentry", "sounds.tbl table entry handle");
ADE_VIRTVAR(DefaultVolume, l_SoundEntry, "number", "The default volume of this game sound. If the sound entry has a volume range then the maximum volume will be returned by this.", "number", "Volume in the range from 1 to 0 or -1 on error")
{
sound_entry_h *seh = NULL;
float newVal = -1.0f;
if (!ade_get_args(L, "o|f", l_SoundEntry.GetPtr(&seh), &newVal))
return ade_set_error(L, "f", -1.0f);
if (seh == NULL || !seh->isValid())
return ade_set_error(L, "f", -1.0f);
if (ADE_SETTING_VAR)
{
if (seh->Get() != NULL)
{
CAP(newVal, 0.0f, 1.0f);
seh->Get()->volume_range = ::util::UniformFloatRange(newVal);
}
}
return ade_set_args(L, "f", seh->Get()->volume_range.max());
}
ADE_FUNC(getFilename, l_SoundEntry, NULL, "Returns the filename of this sound. If the sound has multiple entries then the filename of the first entry will be returned.", "string", "filename or empty string on error")
{
sound_entry_h *seh = NULL;
if (!ade_get_args(L, "o", l_SoundEntry.GetPtr(&seh)))
return ade_set_error(L, "s", "");
if (seh == NULL || !seh->isValid() || seh->Get()->sound_entries.empty())
return ade_set_error(L, "s", "");
return ade_set_args(L, "s", seh->Get()->sound_entries[0].filename);
}
ADE_FUNC(getDuration, l_SoundEntry, NULL, "Returns the length of the sound in seconds. If the sound has multiple entries or a pitch range then the maximum duration of the sound will be returned.", "number", "the length, or -1 on error")
{
sound_entry_h *seh = NULL;
if (!ade_get_args(L, "o", l_SoundEntry.GetPtr(&seh)))
return ade_set_error(L, "f", -1.0f);
if (seh == NULL || !seh->isValid())
return ade_set_error(L, "f", -1.0f);
return ade_set_args(L, "f", (i2fl(gamesnd_get_max_duration(seh->Get())) / 1000.0f));
}
ADE_FUNC(get3DValues,
l_SoundEntry,
"vector Position, [number radius=0.0]",
"Computes the volume and the panning of the sound when it would be played from the specified position.<br>"
"If range is given then the volume will diminish when the listener is within that distance to the source.<br>"
"The position of the listener is always the the current viewing position.",
"number, number",
"The volume and the panning, in that sequence, or both -1 on error")
{
sound_entry_h *seh = NULL;
vec3d *sourcePos = NULL;
float radius = 0.0f;
float vol = 0.0f;
float pan = 0.0f;
if (!ade_get_args(L, "oo|f", l_SoundEntry.GetPtr(&seh), l_Vector.GetPtr(&sourcePos), &radius))
return ade_set_error(L, "ff", -1.0f, -1.0f);
if (seh == NULL || !seh->isValid())
return ade_set_error(L, "ff", -1.0f, -1.0f);
int result = snd_get_3d_vol_and_pan(seh->Get(), sourcePos, &vol, &pan, radius);
if (result < 0)
{
return ade_set_args(L, "ff", -1.0f, -1.0f);
}
else
{
return ade_set_args(L, "ff", vol, pan);
}
}
ADE_FUNC(isValid, l_SoundEntry, nullptr, "Detects whether handle is valid", "boolean", "true if valid, false if handle is invalid, nil if a syntax/type error occurs")
{
sound_entry_h *seh;
if(!ade_get_args(L, "o", l_SoundEntry.GetPtr(&seh)))
return ADE_RETURN_NIL;
return ade_set_args(L, "b", seh->isValid());
}
ADE_FUNC(tryLoad, l_SoundEntry, nullptr, "Detects whether handle references a sound that can be loaded", "boolean", "true if a load succeeded, false if not, nil if a syntax/type error occurs")
{
sound_entry_h *seh;
if (!ade_get_args(L, "o", l_SoundEntry.GetPtr(&seh)))
return ADE_RETURN_NIL;
return ade_set_args(L, "b", gamesnd_game_sound_try_load(seh->idx));
}
sound_h::sound_h() : entryh(), sig(sound_handle::invalid()) {}
sound_h::sound_h(gamesnd_id n_gs_idx, sound_handle n_sig) : entryh(n_gs_idx), sig(n_sig) {}
sound_handle sound_h::getSignature() const
{
// We can have both regular and raw file sounds here, so the sound itself
// can be valid even if the soundentry is not.
if (!isValid())
return sound_handle::invalid();
return sig;
}
bool sound_h::isValid() const
{
// We can have both regular and raw file sounds here, so the sound itself
// can be valid even if the soundentry is not.
if (!sig.isValid() || ds_get_channel(sig) < 0)
return false;
return true;
}
bool sound_h::isValidWithEntry() const
{
// Check if the sound itself *and* its soundentry are both valid
return entryh.isValid() && isValid();
}
//**********HANDLE: Sound
ADE_OBJ(l_Sound, sound_h, "sound", "sound instance handle");
ADE_VIRTVAR(Pitch, l_Sound, "number", "Pitch of sound, from 100 to 100000", "number", "Pitch, or 0 if handle is invalid")
{
sound_h *sh;
int newpitch = 100;
if(!ade_get_args(L, "o|i", l_Sound.GetPtr(&sh), &newpitch))
return ade_set_error(L, "f", 0.0f);
if (!sh->isValid())
return ade_set_error(L, "f", 0.0f);
if(ADE_SETTING_VAR)
{
if(newpitch < 100)
newpitch = 100;
if(newpitch > 100000)
newpitch = 100000;
snd_set_pitch(sh->sig, log10f(i2fl(newpitch)) - 2.0f);
}
return ade_set_args(L, "f", (float) pow(10.0, snd_get_pitch(sh->sig) + 2.0));
}
ADE_FUNC(getRemainingTime, l_Sound, NULL, "The remaining time of this sound handle", "number", "Remaining time, or -1 on error")
{
sound_h *sh;
if(!ade_get_args(L, "o", l_Sound.GetPtr(&sh)))
return ade_set_error(L, "f", -1.0f);
if (!sh->isValid())
return ade_set_error(L, "f", -1.0f);
int remaining = snd_time_remaining(sh->getSignature());
return ade_set_args(L, "f", i2fl(remaining) / 1000.0f);
}
ADE_FUNC(setVolume,
l_Sound,
"number, [boolean voice = false]",
"Sets the volume of this sound instance. Set voice to true to use the voice channel multiplier, or false to use the "
"effects channel multiplier",
"boolean",
"true if succeeded, false otherwise")
{
sound_h *sh;
float newVol = -1.0f;
bool voice = false;
if (!ade_get_args(L, "of|b", l_Sound.GetPtr(&sh), &newVol, &voice))
return ADE_RETURN_FALSE;
if (!sh->isValid())
return ADE_RETURN_FALSE;
CAP(newVol, 0.0f, 1.0f);
snd_set_volume(sh->getSignature(), newVol, voice);
return ADE_RETURN_TRUE;
}
ADE_FUNC(setPanning, l_Sound, "number", "Sets the panning of this sound. Argument ranges from -1.0 for left to 1.0 for right", "boolean", "true if succeeded, false otherwise")
{
sound_h *sh;
float newPan = -1.0f;
if(!ade_get_args(L, "of", l_Sound.GetPtr(&sh), &newPan))
return ADE_RETURN_FALSE;
if (!sh->isValid())
return ADE_RETURN_FALSE;
CAP(newPan, -1.0f, 1.0f);
snd_set_pan(sh->getSignature(), newPan);
return ADE_RETURN_TRUE;
}
ADE_FUNC(setPosition, l_Sound, "number value, boolean percent = true",
"Sets the absolute position of the sound. If boolean argument is true then the value is given as a percentage.<br>"
"This operation fails if there is no backing soundentry!",
"boolean", "true if successful, false otherwise")
{
sound_h *sh;
float val = -1.0f;
bool percent = true;
if(!ade_get_args(L, "of|b", l_Sound.GetPtr(&sh), &val, &percent))
return ADE_RETURN_FALSE;
if (!sh->isValidWithEntry())
return ADE_RETURN_FALSE;
if (val <= 0.0f)
return ADE_RETURN_FALSE;
snd_set_pos(sh->getSignature(), val, percent);
return ADE_RETURN_TRUE;
}
ADE_FUNC(rewind, l_Sound, "number", "Rewinds the sound by the given number of seconds<br>"
"This operation fails if there is no backing soundentry!", "boolean", "true if succeeded, false otherwise")
{
sound_h *sh;
float val = -1.0f;
if(!ade_get_args(L, "of", l_Sound.GetPtr(&sh), &val))
return ADE_RETURN_FALSE;
if (!sh->isValidWithEntry())
return ADE_RETURN_FALSE;
if (val <= 0.0f)
return ADE_RETURN_FALSE;
snd_rewind(sh->getSignature(), val);
return ADE_RETURN_TRUE;
}
ADE_FUNC(skip, l_Sound, "number", "Skips the given number of seconds of the sound<br>"
"This operation fails if there is no backing soundentry!", "boolean", "true if succeeded, false otherwise")
{
sound_h *sh;
float val = -1.0f;
if(!ade_get_args(L, "of", l_Sound.GetPtr(&sh), &val))
return ADE_RETURN_FALSE;
if (!sh->isValidWithEntry())
return ADE_RETURN_FALSE;
if (val <= 0.0f)
return ADE_RETURN_FALSE;
snd_ffwd(sh->getSignature(), val);
return ADE_RETURN_TRUE;
}
ADE_FUNC(isPlaying, l_Sound, NULL, "Checks if this handle is currently playing", "boolean", "true if playing, false if otherwise")
{
sound_h *sh;
if(!ade_get_args(L, "o", l_Sound.GetPtr(&sh)))
return ade_set_error(L, "b", false);
if (!sh->isValid())
return ade_set_error(L, "b", false);
return ade_set_args(L, "b", snd_is_playing(sh->getSignature()) == 1);
}
ADE_FUNC(stop, l_Sound, NULL, "Stops the sound of this handle", "boolean", "true if succeeded, false otherwise")
{
sound_h *sh;
if(!ade_get_args(L, "o", l_Sound.GetPtr(&sh)))
return ade_set_error(L, "b", false);
if (!sh->isValid())
return ade_set_error(L, "b", false);
snd_stop(sh->getSignature());
return ADE_RETURN_TRUE;
}
ADE_FUNC(pause, l_Sound, NULL, "Pauses the sound of this handle", "boolean", "true if succeeded, false otherwise")
{
sound_h *sh;
if (!ade_get_args(L, "o", l_Sound.GetPtr(&sh)))
return ade_set_error(L, "b", false);
if (!sh->isValid())
return ade_set_error(L, "b", false);
if (snd_is_paused(sh->getSignature()))
return ade_set_error(L, "b", false);
snd_pause(sh->getSignature());
return ADE_RETURN_TRUE;
}
ADE_FUNC(resume, l_Sound, NULL, "Resumes the sound of this handle", "boolean", "true if succeeded, false otherwise")
{
sound_h *sh;
if (!ade_get_args(L, "o", l_Sound.GetPtr(&sh)))
return ade_set_error(L, "b", false);
if (!sh->isValid())
return ade_set_error(L, "b", false);
if (!snd_is_paused(sh->getSignature()))
return ade_set_error(L, "b", false);
snd_resume(sh->getSignature());
return ADE_RETURN_TRUE;
}
ADE_FUNC(isValid, l_Sound, nullptr,
"Detects whether this sound, as well as its associated sound entry, are both valid.<br>"
"<b>Warning:</b> A sound can be usable without a sound entry! This function will not return true for sounds started by a "
"directly loaded sound file. Use isSoundValid() in that case instead.",
"boolean", "true if sound and entry are both valid, false if not, nil if a syntax/type error occurs")
{
sound_h *sh;
if(!ade_get_args(L, "o", l_Sound.GetPtr(&sh)))
return ADE_RETURN_NIL;
return ade_set_args(L, "b", sh->isValidWithEntry());
}
ADE_FUNC(isSoundValid, l_Sound, NULL, "Checks if the sound is valid without regard for whether the entry is valid. Should be used for non soundentry sounds.",
"boolean", "true if valid, false if handle is invalid, nil if a syntax/type error occurs")
{
sound_h *sh;
if(!ade_get_args(L, "o", l_Sound.GetPtr(&sh)))
return ADE_RETURN_NIL;
return ade_set_args(L, "b", sh->isValid());
}
ADE_OBJ_DERIV(l_Sound3D, sound_h, "sound3D", "3D sound instance handle", l_Sound);
ADE_FUNC(updatePosition, l_Sound3D,
"vector Position, [number radius = 0.0]",
"Updates the given 3D sound with the specified position and an optional range value.<br>"
"This operation fails if there is no backing soundentry!",
"boolean", "true if succeeded, false otherwise")
{
sound_h *sh;
vec3d *newPos = NULL;
float radius = 0.0f;
if(!ade_get_args(L, "oo|f", l_Sound.GetPtr(&sh), l_Vector.GetPtr(&newPos), &radius))
return ade_set_error(L, "b", false);
if (!sh->isValidWithEntry() || newPos == NULL)
return ade_set_error(L, "b", false);
snd_update_3d_pos(sh->getSignature(), sh->entryh.Get(), newPos, radius);
return ADE_RETURN_TRUE;
}
//**********HANDLE: Soundfile
soundfile_h::soundfile_h(sound_load_id id) : idx(id) {}
ADE_OBJ(l_Soundfile, soundfile_h, "soundfile", "Handle to a sound file");
ADE_VIRTVAR(Duration, l_Soundfile, "number", "The duration of the sound file, in seconds", "number", "The duration or -1 on error")
{
soundfile_h* handle = nullptr;
if (!ade_get_args(L, "o", l_Soundfile.GetPtr(&handle)))
return ade_set_error(L, "f", -1.0f);
if (!handle->idx.isValid())
return ade_set_error(L, "f", -1.0f);
int duration = snd_get_duration(handle->idx);
return ade_set_args(L, "f", i2fl(duration) / 1000.0f);
}
ADE_VIRTVAR(Filename, l_Soundfile, "string", "The filename of the file", "string", "The file name or empty string on error")
{
soundfile_h* handle = nullptr;
if (!ade_get_args(L, "o", l_Soundfile.GetPtr(&handle)))
return ade_set_error(L, "s", "");
if (!handle->idx.isValid())
return ade_set_error(L, "s", "");
const char* filename = snd_get_filename(handle->idx);
return ade_set_args(L, "s", filename);
}
ADE_FUNC(play,
l_Soundfile,
"[number volume = 1.0, number panning = 0.0, boolean voice = true]",
"Plays the sound. If voice is true then uses the Voice channel volume, else uses the Effects channel volume.",
"sound",
"A sound handle or invalid handle on error")
{
soundfile_h *handle = nullptr;
float volume = 1.0f;
float panning = 0.0f;
bool voice = true;
if (!ade_get_args(L, "o|ffb", l_Soundfile.GetPtr(&handle), &volume, &panning, &voice))
return ade_set_error(L, "o", l_Sound.Set(sound_h()));
if (!handle->idx.isValid())
return ade_set_error(L, "o", l_Sound.Set(sound_h()));
if (volume < 0.0f) {
LuaError(L, "Invalid volume value of %f specified!", volume);
return ade_set_error(L, "o", l_Sound.Set(sound_h()));
}
auto snd_handle = snd_play_raw(handle->idx, panning, volume, 0, voice);
return ade_set_args(L, "o", l_Sound.Set(sound_h(gamesnd_id(), snd_handle)));
}
ADE_FUNC(unload, l_Soundfile, nullptr,
"Unloads the audio data loaded for this sound file. This invalidates the handle on which this is called!",
"boolean", "true if successful, false otherwise")
{
soundfile_h* handle = nullptr;
if (!ade_get_args(L, "o", l_Soundfile.GetPtr(&handle)))
return ade_set_error(L, "b", false);
if (!handle->idx.isValid())
return ade_set_error(L, "b", false);
auto result = snd_unload(handle->idx);
if (result != 0) {
// Invalidate this handle so that the script cannot do something bad with it
handle->idx = sound_load_id::invalid();
}
return ade_set_args(L, "b", result != 0);
}
ADE_FUNC(isValid, l_Soundfile, NULL, "Checks if the soundfile handle is valid", "boolean", "true if valid, false otherwise")
{
soundfile_h* handle = nullptr;
if (!ade_get_args(L, "o", l_Soundfile.GetPtr(&handle)))
return ADE_RETURN_FALSE;
if (handle == nullptr) {
return ADE_RETURN_FALSE;
}
return ade_set_args(L, "b", handle->idx.isValid());
}
}
}
|