1
0
Fork 0
mirror of https://github.com/AquariaOSE/Aquaria.git synced 2024-12-25 06:05:45 +00:00

Fixed positional audio & pitch shift.

Apparently this was half implemented but not fully finished.
Mono samples work fine, for stereo, L/R panning does not work (due to OpenAL's limitation)
Also fixed: entity sounds should not play if a cutscene is currently skipped.
This commit is contained in:
fgenesis 2011-11-26 21:00:24 +01:00
parent ff195c6b16
commit ac2091fb69
8 changed files with 91 additions and 105 deletions

View file

@ -2178,53 +2178,45 @@ void DSQ::playMenuSelectSfx()
core->sound->playSfx("MenuSelect"); core->sound->playSfx("MenuSelect");
} }
void DSQ::playPositionalSfx(const std::string &sfx, const Vector &position, float f, float fadeOut) PlaySfx DSQ::calcPositionalSfx(const Vector &position, float maxdist)
{ {
if (f == 0) PlaySfx sfx;
f = 1; sfx.vol = 0;
if (dsq->game && dsq->game->avatar) if (dsq->game && dsq->game->avatar)
{ {
Vector diff = position - dsq->game->avatar->position; Vector diff = position - dsq->game->avatar->position;
if (diff.isLength2DIn(1024))
// TODO: this might be cooler if finetuned for different aspect ratios.
// This value is suitable enough for widescreen in default zoom, at least -- FG
if (maxdist <= 0)
maxdist = 1024;
float dist = diff.getLength2D();
if (dist < maxdist)
{ {
sfx.vol = 1.0f - (dist / maxdist);
//sound->playSfx(sfx); sfx.pan = (diff.x / maxdist) * 2.0f;
if (sfx.pan < -1)
int dist = diff.getLength2D(); sfx.pan = -1;
// HACK: grr if (sfx.pan > 1)
int vol = 1.0f - int((dist*1.0f) / 2000.0f); sfx.pan = 1;
//int vol = 1;
//int vol = 255;
int pan = (diff.x)/1024.0f;
if (pan < -1)
pan = -1;
if (pan > 1)
pan = 1;
/*
std::ostringstream os;
os << "vol: " << vol << " pan: " << pan;
debugLog(os.str());
*/
void *c = sound->playSfx(sfx, vol, pan, f);
if (fadeOut != 0)
{
sound->fadeSfx(c, SFT_OUT, fadeOut);
}
/*
if (c && fadeOut != 0)
{
BASS_ChannelSlideAttributes(c,-1,-2,-101,fadeOut*1000);
}
*/
} }
} }
return sfx;
}
void DSQ::playPositionalSfx(const std::string &name, const Vector &position, float f, float fadeOut)
{
PlaySfx sfx = calcPositionalSfx(position);
sfx.freq = f;
sfx.name = name;
void *c = sound->playSfx(sfx);
if (fadeOut != 0)
{
sound->fadeSfx(c, SFT_OUT, fadeOut);
}
} }
void DSQ::shutdown() void DSQ::shutdown()

View file

@ -1429,7 +1429,8 @@ public:
bool voiceOversEnabled; bool voiceOversEnabled;
int recentSaveSlot; int recentSaveSlot;
void playPositionalSfx(const std::string &name, const Vector &position, float freq=1.0, float fadeOut=0); PlaySfx calcPositionalSfx(const Vector &position, float maxdist = 0);
void playPositionalSfx(const std::string &name, const Vector &position, float freq=1.0f, float fadeOut=0);
void playMenuSelectSfx(); void playMenuSelectSfx();

View file

@ -2447,15 +2447,9 @@ void Entity::songNoteDone(int note, float len)
{ {
} }
void Entity::soundFreq(const std::string &sound, float freq, float fadeOut)
{
dsq->playPositionalSfx(sound, position, freq, fadeOut);
}
void Entity::sound(const std::string &sound, float freq, float fadeOut) void Entity::sound(const std::string &sound, float freq, float fadeOut)
{ {
//core->sound->playPositionalSfx2D(sound, position, freq, fadeOut); dsq->playPositionalSfx(sound, position, freq, fadeOut);
dsq->playPositionalSfx(sound, position, 1, fadeOut);
} }
Vector Entity::getEnergyShotTargetPosition() Vector Entity::getEnergyShotTargetPosition()

View file

@ -273,7 +273,6 @@ public:
void popBubble(); void popBubble();
void sound(const std::string &sound, float freq=1, float fadeOut=0); void sound(const std::string &sound, float freq=1, float fadeOut=0);
void soundFreq(const std::string &sound, float freq=1, float fadeOut=0);
void freeze(float time); void freeze(float time);

View file

@ -1415,26 +1415,38 @@ luaFunc(createShot)
luaReturnPtr(s); luaReturnPtr(s);
} }
// deprecated, use entity_playSfx
luaFunc(entity_sound) luaFunc(entity_sound)
{ {
Entity *e = entity(L); Entity *e = entity(L);
if (e) if (e && !dsq->isSkippingCutscene())
{ {
e->sound(lua_tostring(L, 2), lua_tonumber(L, 3), lua_tonumber(L, 4)); e->sound(lua_tostring(L, 2), lua_tonumber(L, 3), lua_tonumber(L, 4));
} }
luaReturnNum(0); luaReturnNum(0);
} }
luaFunc(entity_playSfx)
luaFunc(entity_soundFreq)
{ {
Entity *e = entity(L); Entity *e = entity(L);
if (e) void *h = NULL;
if (e && !dsq->isSkippingCutscene())
{ {
e->soundFreq(lua_tostring(L, 2), lua_tonumber(L, 3), lua_tonumber(L, 4)); PlaySfx sfx = dsq->calcPositionalSfx(e->position, lua_tonumber(L, 7));
sfx.name = lua_tostring(L, 2);
sfx.freq = lua_tonumber(L, 3);
float vol = lua_tonumber(L, 4);
sfx.loops = lua_tonumber(L, 5);
float fadeOut = lua_tonumber(L, 6);
if(vol > 0)
sfx.vol *= vol;
h = core->sound->playSfx(sfx);
if (fadeOut != 0)
{
sound->fadeSfx(h, SFT_OUT, fadeOut);
}
} }
luaReturnNum(0); luaReturnPtr(h);
} }
luaFunc(entity_setSpiritFreeze) luaFunc(entity_setSpiritFreeze)
@ -4491,48 +4503,6 @@ luaFunc(entity_getBoneByName)
luaReturnPtr(b); luaReturnPtr(b);
} }
// ditch entity::sound and use this code instead...
// replace entity sound with this code
luaFunc(entity_playSfx)
{
Entity *e= entity(L);
if (e)
{
std::string sfx = lua_tostring(L, 2);
/*
int f = rand()%200-100;
f += 1000;
*/
dsq->playPositionalSfx(sfx, e->position);
/*
Vector diff = e->position - dsq->game->avatar->position;
if (diff.isLength2DIn(800))
{
int dist = diff.getLength2D();
int vol = 255 - int((dist*255.0f) / 1500.0f);
int pan = (diff.x*100)/800.0f;
if (pan < -100)
pan = -100;
if (pan > 100)
pan = 100;
std::ostringstream os;
os << "vol: " << vol << " pan: " << pan;
debugLog(os.str());
sound->playSfx(sfx, vol, pan, 1000+f);
}
*/
//sound->playSfx(sfx);
}
luaReturnNum(0);
}
luaFunc(bone_getPosition) luaFunc(bone_getPosition)
{ {
Bone *b = bone(L); Bone *b = bone(L);
@ -4996,23 +4966,36 @@ luaFunc(emote)
luaFunc(playSfx) luaFunc(playSfx)
{ {
int freq = lua_tonumber(L, 2); float freq = lua_tonumber(L, 2);
float vol = lua_tonumber(L, 3); float vol = lua_tonumber(L, 3);
int loops = lua_tointeger(L, 4); int loops = lua_tointeger(L, 4);
if (vol == 0) if (vol <= 0)
vol = 1; vol = 1;
PlaySfx sfx; PlaySfx sfx;
if (lua_isnumber(L, 5) && lua_isnumber(L, 6))
{
const Vector pos(lua_tonumber(L, 5), lua_tonumber(L, 6));
sfx = dsq->calcPositionalSfx(pos, lua_tonumber(L, 7));
sfx.vol *= vol;
}
else
{
sfx.vol = vol;
}
sfx.name = getString(L, 1); sfx.name = getString(L, 1);
sfx.vol = vol;
sfx.freq = freq; sfx.freq = freq;
sfx.loops = loops; sfx.loops = loops;
void *handle = NULL; void *handle = NULL;
if (!dsq->isSkippingCutscene()) if (!dsq->isSkippingCutscene())
{
handle = core->sound->playSfx(sfx); handle = core->sound->playSfx(sfx);
}
luaReturnPtr(handle); luaReturnPtr(handle);
} }
@ -7455,7 +7438,6 @@ static const struct {
luaRegister(entity_setActivationType), luaRegister(entity_setActivationType),
luaRegister(entity_setColor), luaRegister(entity_setColor),
{"entity_color", l_entity_setColor}, {"entity_color", l_entity_setColor},
luaRegister(entity_playSfx),
luaRegister(isQuitFlag), luaRegister(isQuitFlag),
luaRegister(isDeveloperKeys), luaRegister(isDeveloperKeys),
@ -7661,7 +7643,7 @@ static const struct {
luaRegister(entity_isFollowingPath), luaRegister(entity_isFollowingPath),
luaRegister(entity_followEntity), luaRegister(entity_followEntity),
luaRegister(entity_sound), luaRegister(entity_sound),
luaRegister(entity_soundFreq), luaRegister(entity_playSfx),
luaRegister(entity_enableMotionBlur), luaRegister(entity_enableMotionBlur),
@ -8179,6 +8161,7 @@ static const struct {
{"entity_incrTargetLeaches", l_avatar_incrLeaches}, {"entity_incrTargetLeaches", l_avatar_incrLeaches},
{"entity_decrTargetLeaches", l_avatar_decrLeaches}, {"entity_decrTargetLeaches", l_avatar_decrLeaches},
{"entity_soundFreq", l_entity_sound},
}; };
//============================================================================================ //============================================================================================

View file

@ -583,6 +583,7 @@ public:
FMOD_RESULT isPlaying(bool *isplaying); FMOD_RESULT isPlaying(bool *isplaying);
FMOD_RESULT setChannelGroup(ChannelGroup *channelgroup); FMOD_RESULT setChannelGroup(ChannelGroup *channelgroup);
FMOD_RESULT stop(); FMOD_RESULT stop();
FMOD_RESULT setPan(const float pan);
void setGroupVolume(const float _volume); void setGroupVolume(const float _volume);
void setSourceName(const ALuint _sid) { sid = _sid; } void setSourceName(const ALuint _sid) { sid = _sid; }
ALuint getSourceName() const { return sid; } ALuint getSourceName() const { return sid; }
@ -807,6 +808,14 @@ FMOD_RESULT OpenALChannel::setPriority(int _priority)
return FMOD_OK; return FMOD_OK;
} }
ALBRIDGE(Channel,setPan,(float volume),(volume))
FMOD_RESULT OpenALChannel::setPan(const float pan)
{
alSource3f(sid, AL_POSITION, pan, 0, 0);
SANITY_CHECK_OPENAL_CALL();
return FMOD_OK;
}
// FMOD::ChannelGroup implementation... // FMOD::ChannelGroup implementation...

View file

@ -145,6 +145,7 @@ namespace FMOD
FMOD_RESULT setPriority(int priority); FMOD_RESULT setPriority(int priority);
FMOD_RESULT stop(); FMOD_RESULT stop();
FMOD_RESULT setPaused(bool paused); FMOD_RESULT setPaused(bool paused);
FMOD_RESULT setPan(float pan);
}; };
class System class System

View file

@ -1147,6 +1147,13 @@ void *SoundManager::playSfx(const PlaySfx &play)
checkError(); checkError();
} }
channel->setPan(play.pan);
float freq = play.freq;
if (freq <= 0)
freq = 1;
channel->setFrequency(freq);
result = channel->setPaused(false); result = channel->setPaused(false);
checkError(); checkError();