2009-04-18 19:00:33 +02:00
|
|
|
/* Copyright (C) 2009 Wildfire Games.
|
|
|
|
* This file is part of 0 A.D.
|
|
|
|
*
|
|
|
|
* 0 A.D. 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 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 0 A.D. 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 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2004-11-15 19:23:39 +01:00
|
|
|
#include "precompiled.h"
|
|
|
|
#include "JSI_Sound.h"
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "maths/Vector3D.h"
|
2004-11-15 19:23:39 +01:00
|
|
|
|
2006-04-10 22:12:25 +02:00
|
|
|
#include "lib/res/sound/snd_mgr.h"
|
2004-11-15 19:23:39 +01:00
|
|
|
#include "lib/res/h_mgr.h" // h_filename
|
|
|
|
|
|
|
|
|
2009-11-03 22:46:35 +01:00
|
|
|
JSI_Sound::JSI_Sound(const VfsPath& pathname)
|
2004-11-15 19:23:39 +01:00
|
|
|
{
|
2009-11-03 22:46:35 +01:00
|
|
|
m_Handle = snd_open(pathname);
|
2005-07-15 19:58:27 +02:00
|
|
|
|
|
|
|
// special-case to avoid throwing exceptions if quickstart has
|
|
|
|
// disabled sound: set a flag queried by Construct; the object will
|
|
|
|
// then immediately be freed.
|
2007-01-18 17:24:37 +01:00
|
|
|
if (m_Handle == ERR::AGAIN)
|
2005-07-15 19:58:27 +02:00
|
|
|
{
|
|
|
|
m_SoundDisabled = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_SoundDisabled = false;
|
|
|
|
|
2005-12-07 04:44:17 +01:00
|
|
|
// if open failed, raise an exception - it's the only way to
|
|
|
|
// report errors, since we're in the ctor and don't want to move
|
|
|
|
// the open call elsewhere (by requiring an explicit open() call).
|
2007-01-18 17:24:37 +01:00
|
|
|
if (m_Handle < 0)
|
2007-01-19 19:06:54 +01:00
|
|
|
throw std::exception(); // caught by JSI_Sound::Construct.
|
2004-11-15 23:30:01 +01:00
|
|
|
|
2008-07-12 12:45:11 +02:00
|
|
|
(void)snd_set_pos(m_Handle, 0,0,0, true);
|
2004-11-15 19:23:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
JSI_Sound::~JSI_Sound()
|
|
|
|
{
|
2008-07-12 12:45:11 +02:00
|
|
|
(void)this->Free(0, 0, 0);
|
2004-11-15 19:23:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-18 17:24:37 +01:00
|
|
|
bool JSI_Sound::SetGain(JSContext* cx, uintN argc, jsval* argv)
|
2004-11-20 22:32:00 +01:00
|
|
|
{
|
2007-01-18 17:24:37 +01:00
|
|
|
if (! m_Handle)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
debug_assert(argc >= 1); // FIXME
|
2004-11-20 22:32:00 +01:00
|
|
|
float gain;
|
2007-01-18 17:24:37 +01:00
|
|
|
if (! ToPrimitive<float>(cx, argv[0], gain))
|
2004-11-20 22:32:00 +01:00
|
|
|
return false;
|
|
|
|
|
2008-07-12 12:45:11 +02:00
|
|
|
(void)snd_set_gain(m_Handle, gain);
|
2004-11-20 22:32:00 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-01-18 17:24:37 +01:00
|
|
|
bool JSI_Sound::SetPitch(JSContext* cx, uintN argc, jsval* argv)
|
2005-03-09 17:17:26 +01:00
|
|
|
{
|
2007-01-18 17:24:37 +01:00
|
|
|
if (! m_Handle)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
debug_assert(argc >= 1); // FIXME
|
2005-03-09 17:17:26 +01:00
|
|
|
float pitch;
|
2007-01-18 17:24:37 +01:00
|
|
|
if (! ToPrimitive<float>(cx, argv[0], pitch))
|
2005-03-09 17:17:26 +01:00
|
|
|
return false;
|
|
|
|
|
2008-07-12 12:45:11 +02:00
|
|
|
(void)snd_set_pitch(m_Handle, pitch);
|
2005-03-09 17:17:26 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-01-18 17:24:37 +01:00
|
|
|
bool JSI_Sound::SetPosition(JSContext* cx, uintN argc, jsval* argv)
|
2004-11-20 22:32:00 +01:00
|
|
|
{
|
2007-01-18 17:24:37 +01:00
|
|
|
if (! m_Handle)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
debug_assert(argc >= 1); // FIXME
|
|
|
|
|
2004-11-20 22:32:00 +01:00
|
|
|
CVector3D pos;
|
|
|
|
// absolute world coords
|
2007-01-18 17:24:37 +01:00
|
|
|
if (ToPrimitive<CVector3D>(cx, argv[0], pos))
|
2008-07-12 12:45:11 +02:00
|
|
|
(void)snd_set_pos(m_Handle, pos[0], pos[1], pos[2]);
|
2004-11-20 22:32:00 +01:00
|
|
|
// relative, 0 offset - right on top of the listener
|
|
|
|
// (we don't need displacement from the listener, e.g. always behind)
|
|
|
|
else
|
2008-07-12 12:45:11 +02:00
|
|
|
(void)snd_set_pos(m_Handle, 0,0,0, true);
|
2004-11-20 22:32:00 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-12-14 00:17:50 +01:00
|
|
|
|
2007-01-18 17:24:37 +01:00
|
|
|
bool JSI_Sound::Fade(JSContext* cx, uintN argc, jsval* argv)
|
2005-12-14 00:17:50 +01:00
|
|
|
{
|
2007-01-18 17:24:37 +01:00
|
|
|
if (! m_Handle)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
debug_assert(argc >= 3); // FIXME
|
2005-12-14 00:17:50 +01:00
|
|
|
float initial_gain, final_gain;
|
|
|
|
float length;
|
2007-01-18 17:24:37 +01:00
|
|
|
if (! (ToPrimitive<float>(cx, argv[0], initial_gain)
|
|
|
|
&& ToPrimitive<float>(cx, argv[1], final_gain)
|
|
|
|
&& ToPrimitive<float>(cx, argv[2], length)))
|
2005-12-14 00:17:50 +01:00
|
|
|
return false;
|
2007-01-18 17:24:37 +01:00
|
|
|
|
2008-07-12 12:45:11 +02:00
|
|
|
(void)snd_fade(m_Handle, initial_gain, final_gain, length, FT_S_CURVE);
|
2005-12-14 00:17:50 +01:00
|
|
|
|
|
|
|
// HACK: snd_fade causes <m_Handle> to be automatically freed when a
|
|
|
|
// fade to 0 has completed. however, we're still holding on to a
|
|
|
|
// reference, which will cause a double-free warning when Free() is
|
|
|
|
// called from the dtor. solution is to neuter our Handle by
|
|
|
|
// setting it to 0 (ok since it'll be freed). this does mean that
|
|
|
|
// no further operations can be carried out during that final fade.
|
|
|
|
m_Handle = 0;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-11-15 19:23:39 +01:00
|
|
|
// start playing the sound (one-shot).
|
|
|
|
// it will automatically be freed when done.
|
2007-01-18 17:24:37 +01:00
|
|
|
bool JSI_Sound::Play(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv))
|
2004-11-15 19:23:39 +01:00
|
|
|
{
|
2007-01-18 17:24:37 +01:00
|
|
|
if (! m_Handle)
|
|
|
|
return false;
|
|
|
|
|
2008-07-12 12:45:11 +02:00
|
|
|
(void)snd_play(m_Handle);
|
2007-01-18 17:24:37 +01:00
|
|
|
// We can't do anything else with this sound now, since it's impossible to
|
|
|
|
// know whether or not it's still valid (since it might have finished playing
|
|
|
|
// already). So set it to 0, so we don't try doing anything (like freeing it)
|
|
|
|
// in the future.
|
|
|
|
m_Handle = 0;
|
2004-11-20 22:32:00 +01:00
|
|
|
return true;
|
2004-11-15 19:23:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// request the sound be played until free() is called. returns immediately.
|
2007-01-18 17:24:37 +01:00
|
|
|
bool JSI_Sound::Loop(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv))
|
2004-11-15 19:23:39 +01:00
|
|
|
{
|
2007-01-18 17:24:37 +01:00
|
|
|
if (! m_Handle)
|
|
|
|
return false;
|
|
|
|
|
2008-07-12 12:45:11 +02:00
|
|
|
(void)snd_set_loop(m_Handle, true);
|
|
|
|
(void)snd_play(m_Handle);
|
2004-11-20 22:32:00 +01:00
|
|
|
return true;
|
2004-11-15 19:23:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// stop sound if currently playing and free resources.
|
|
|
|
// doesn't need to be called unless played via loop() -
|
|
|
|
// sounds are freed automatically when done playing.
|
2007-01-18 17:24:37 +01:00
|
|
|
bool JSI_Sound::Free(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv))
|
2004-11-15 19:23:39 +01:00
|
|
|
{
|
2007-01-18 17:24:37 +01:00
|
|
|
if (! m_Handle)
|
|
|
|
return false;
|
|
|
|
|
2008-07-12 12:45:11 +02:00
|
|
|
(void)snd_free(m_Handle); // resets it to 0
|
2004-11-20 22:32:00 +01:00
|
|
|
return true;
|
2004-11-15 19:23:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Script-bound functions
|
|
|
|
|
|
|
|
|
|
|
|
void JSI_Sound::ScriptingInit()
|
|
|
|
{
|
2007-05-29 21:01:21 +02:00
|
|
|
AddMethod<CStr, &JSI_Sound::ToString>("toString", 0);
|
2007-01-18 17:24:37 +01:00
|
|
|
AddMethod<bool, &JSI_Sound::Play>("play", 0);
|
|
|
|
AddMethod<bool, &JSI_Sound::Loop>("loop", 0);
|
|
|
|
AddMethod<bool, &JSI_Sound::Free>("free", 0);
|
|
|
|
AddMethod<bool, &JSI_Sound::SetGain>("setGain", 0);
|
|
|
|
AddMethod<bool, &JSI_Sound::SetPitch>("setPitch", 0);
|
2007-05-11 15:11:25 +02:00
|
|
|
AddMethod<bool, &JSI_Sound::SetPosition>("setPosition", 0);
|
2007-01-18 17:24:37 +01:00
|
|
|
AddMethod<bool, &JSI_Sound::Fade>("fade", 0);
|
|
|
|
|
|
|
|
CJSObject<JSI_Sound>::ScriptingInit("Sound", &JSI_Sound::Construct, 1);
|
2004-11-15 19:23:39 +01:00
|
|
|
}
|
|
|
|
|
2007-05-29 21:01:21 +02:00
|
|
|
CStr JSI_Sound::ToString(JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv))
|
2004-11-15 19:23:39 +01:00
|
|
|
{
|
2009-11-03 22:46:35 +01:00
|
|
|
return "[object Sound: " + CStr(h_filename(m_Handle).string()) + "]";
|
2004-11-15 19:23:39 +01:00
|
|
|
}
|
2004-11-15 23:30:01 +01:00
|
|
|
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
JSBool JSI_Sound::Construct(JSContext* cx, JSObject* UNUSED(obj), uintN argc, jsval* argv, jsval* rval)
|
2004-11-15 23:30:01 +01:00
|
|
|
{
|
2007-01-18 17:24:37 +01:00
|
|
|
debug_assert(argc >= 1); // FIXME
|
2004-11-15 23:30:01 +01:00
|
|
|
CStrW filename;
|
2007-01-18 17:24:37 +01:00
|
|
|
if (! ToPrimitive<CStrW>(cx, argv[0], filename))
|
|
|
|
return JS_FALSE;
|
2004-11-15 23:30:01 +01:00
|
|
|
|
2005-05-09 19:10:52 +02:00
|
|
|
try
|
|
|
|
{
|
2007-01-18 17:24:37 +01:00
|
|
|
JSI_Sound* newObject = new JSI_Sound(filename);
|
2005-07-15 19:58:27 +02:00
|
|
|
|
|
|
|
// somewhat of a hack to avoid throwing exceptions if
|
|
|
|
// sound was disabled due to quickstart. see JSI_Sound().
|
2007-01-18 17:24:37 +01:00
|
|
|
if (newObject->m_SoundDisabled)
|
2005-07-15 19:58:27 +02:00
|
|
|
{
|
|
|
|
delete newObject;
|
|
|
|
*rval = JSVAL_NULL;
|
2007-01-18 17:24:37 +01:00
|
|
|
return JS_TRUE;
|
2005-07-15 19:58:27 +02:00
|
|
|
}
|
|
|
|
|
2005-05-09 19:10:52 +02:00
|
|
|
newObject->m_EngineOwned = false;
|
2007-01-18 17:24:37 +01:00
|
|
|
*rval = OBJECT_TO_JSVAL(newObject->GetScript());
|
2005-05-09 19:10:52 +02:00
|
|
|
}
|
2007-01-18 17:24:37 +01:00
|
|
|
catch (std::exception)
|
2005-05-09 19:10:52 +02:00
|
|
|
{
|
|
|
|
// failed, but this can happen easily enough that we don't want to
|
|
|
|
// return JS_FALSE (since that stops the script).
|
|
|
|
*rval = JSVAL_NULL;
|
|
|
|
}
|
2004-11-15 23:30:01 +01:00
|
|
|
|
2007-01-18 17:24:37 +01:00
|
|
|
return JS_TRUE;
|
2004-11-24 00:56:10 +01:00
|
|
|
}
|