1
0
forked from 0ad/0ad

add pitch shift capability to snd.cpp and JS binding

This was SVN commit r1977.
This commit is contained in:
janwas 2005-03-09 16:17:26 +00:00
parent 3050d4c7fe
commit 11419fc1fe
4 changed files with 48 additions and 1 deletions

View File

@ -1148,7 +1148,8 @@ struct VSrc
// AL source properties (set via snd_set*)
ALfloat pos[3];
ALfloat gain;
ALfloat gain; // [0,1]
ALfloat pitch; // (0,1]
ALboolean loop;
ALboolean relative;
@ -1272,6 +1273,7 @@ static void vsrc_latch(VSrc* vs)
alSourcefv(vs->al_src, AL_POSITION, vs->pos);
alSourcei (vs->al_src, AL_SOURCE_RELATIVE, vs->relative);
alSourcef (vs->al_src, AL_GAIN, vs->gain);
alSourcef (vs->al_src, AL_PITCH, vs->pitch);
alSourcei (vs->al_src, AL_LOOPING, vs->loop);
al_check("vsrc_latch");
}
@ -1468,6 +1470,8 @@ static int VSrc_reload(VSrc* vs, const char* fn, Handle hvs)
// note: vs->gain can legitimately be > 1.0 - don't clamp.
vs->pitch = 1.0f;
vs->hvs = hvs;
// needed so we can snd_free ourselves when done playing.
@ -1562,6 +1566,9 @@ int snd_set_gain(Handle hvs, float gain)
{
H_DEREF(hvs, VSrc, vs);
if(!(0.0f <= gain && gain <= 1.0f))
return -1;
vs->gain = gain;
vsrc_latch(vs);
@ -1569,6 +1576,25 @@ int snd_set_gain(Handle hvs, float gain)
}
// change pitch shift of the sound source.
// 1.0 means no change; each reduction by 50% equals a pitch shift of
// -12 semitones (one octave). zero is invalid.
// may be called at any time; fails with invalid handle return if
// the sound has already been closed (e.g. it never played).
int snd_set_pitch(Handle hvs, float pitch)
{
H_DEREF(hvs, VSrc, vs);
if(!(0.0f < pitch && pitch <= 1.0f))
return -1;
vs->pitch = pitch;
vsrc_latch(vs);
return 0;
}
// enable/disable looping on the sound source.
// used to implement variable-length sounds (e.g. while building).
// may be called at any time; fails with invalid handle return if

View File

@ -159,6 +159,13 @@ extern int snd_set_pos(Handle hs, float x, float y, float z, bool relative = fal
// the sound has already been closed (e.g. it never played).
extern int snd_set_gain(Handle hs, float gain);
// change pitch shift of the sound source.
// 1.0 means no change; each reduction by 50% equals a pitch shift of
// -12 semitones (one octave). zero is invalid.
// may be called at any time; fails with invalid handle return if
// the sound has already been closed (e.g. it never played).
extern int snd_set_pitch(Handle hs, float pitch);
// enable/disable looping on the sound source.
// used to implement variable-length sounds (e.g. while building).
// may be called at any time; fails with invalid handle return if

View File

@ -31,6 +31,17 @@ bool JSI_Sound::SetGain( JSContext* cx, uintN argc, jsval* argv )
return true;
}
bool JSI_Sound::SetPitch( JSContext* cx, uintN argc, jsval* argv )
{
assert( argc >= 1 );
float pitch;
if( !ToPrimitive<float>( cx, argv[0], pitch) )
return false;
snd_set_pitch(m_Handle, pitch);
return true;
}
bool JSI_Sound::SetPosition( JSContext* cx, uintN argc, jsval* argv )
{
assert( argc >= 1 );
@ -82,6 +93,7 @@ void JSI_Sound::ScriptingInit()
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 );
AddMethod<bool, &JSI_Sound::SetPosition>( "setPosition", 0 );
CJSObject<JSI_Sound>::ScriptingInit( "Sound", &JSI_Sound::Construct, 1 );

View File

@ -53,6 +53,8 @@ public:
bool SetGain( JSContext* cx, uintN argc, jsval* argv );
bool SetPitch( JSContext* cx, uintN argc, jsval* argv );
bool SetPosition( JSContext* cx, uintN argc, jsval* argv );
static JSBool Construct( JSContext* cx, JSObject* obj, unsigned int argc, jsval* argv, jsval* rval );