From d718f48704114ed3e9df52ac003054d857bee69f Mon Sep 17 00:00:00 2001 From: driAn Date: Thu, 18 Sep 2008 22:46:31 +0000 Subject: [PATCH] smarter sound scheduling algorithm, tweaked priorities git-svn-id: svn://svn.code.sf.net/p/actiongame/code/trunk/ac@2703 e016b1c5-ed68-44aa-a2f7-31cac88cffde --- source/src/clientgame.cpp | 16 +++---- source/src/menus.cpp | 4 +- source/src/rendergl.cpp | 2 + source/src/sound.cpp | 90 +++++++++++++++------------------------ source/src/sound.h | 2 +- source/src/weapon.cpp | 2 +- 6 files changed, 48 insertions(+), 68 deletions(-) diff --git a/source/src/clientgame.cpp b/source/src/clientgame.cpp index 47ae583a..fd8c4e38 100644 --- a/source/src/clientgame.cpp +++ b/source/src/clientgame.cpp @@ -493,7 +493,7 @@ void dodamage(int damage, playerent *pl, playerent *actor, bool gib, bool local) else damageeffect(damage, pl); if(pl->health<=0) { if(local) dokill(pl, actor, gib); } - else if(pl==player1) playsound(S_PAIN6); + else if(pl==player1) playsound(S_PAIN6, SP_HIGH); else playsound(S_PAIN1+rnd(5), pl); } @@ -713,7 +713,7 @@ void flagmsg(int flag, int message, int actor, int flagtime) switch(message) { case FM_PICKUP: - playsound(S_FLAGPICKUP, SP_HIGH); + playsound(S_FLAGPICKUP, SP_HIGHEST); if(firstperson) { conoutf("\f2you got the %sflag", m_ctf ? "enemy " : ""); @@ -725,7 +725,7 @@ void flagmsg(int flag, int message, int actor, int flagtime) case FM_DROP: { const char *droplost = message == FM_LOST ? "lost" : "dropped"; - playsound(S_FLAGDROP, SP_HIGH); + playsound(S_FLAGDROP, SP_HIGHEST); if(firstperson) { conoutf("\f2you %s the flag", droplost); @@ -735,12 +735,12 @@ void flagmsg(int flag, int message, int actor, int flagtime) break; } case FM_RETURN: - playsound(S_FLAGRETURN, SP_HIGH); + playsound(S_FLAGRETURN, SP_HIGHEST); if(firstperson) conoutf("\f2you returned your flag"); else conoutf("\f2%s returned %s flag", colorname(act), teamstr); break; case FM_SCORE: - playsound(S_FLAGSCORE, SP_HIGH); + playsound(S_FLAGSCORE, SP_HIGHEST); if(firstperson) { conoutf("\f2you scored"); @@ -750,7 +750,7 @@ void flagmsg(int flag, int message, int actor, int flagtime) break; case FM_KTFSCORE: { - playsound(S_VOTEPASS, SP_HIGH); // need better ktf sound here + playsound(S_VOTEPASS, SP_HIGHEST); // need better ktf sound here const char *ta = firstperson ? "you have" : colorname(act); const char *tb = firstperson ? "" : " has"; const char *tc = teammate && !firstperson ? "your teammate " : ""; @@ -765,7 +765,7 @@ void flagmsg(int flag, int message, int actor, int flagtime) conoutf("\f2%s failed to score (own team flag not taken)", firstperson ? "you" : colorname(act)); break; case FM_RESET: - playsound(S_FLAGRETURN, SP_HIGH); + playsound(S_FLAGRETURN, SP_HIGHEST); conoutf("the server reset the flag"); if(firstperson) musicfadeout(M_FLAGGRAB); break; @@ -899,7 +899,7 @@ void displayvote(votedisplayinfo *v) DELETEP(curvote); curvote = v; conoutf("%s called a vote: %s", v->owner ? colorname(v->owner) : "", curvote->desc); - playsound(S_CALLVOTE, SP_HIGH); + playsound(S_CALLVOTE, SP_HIGHEST); curvote->localplayervoted = false; } diff --git a/source/src/menus.cpp b/source/src/menus.cpp index 5977000c..caeae599 100644 --- a/source/src/menus.cpp +++ b/source/src/menus.cpp @@ -71,7 +71,7 @@ void menuselect(void *menu, int sel) { m.items[oldsel]->focus(false); m.items[sel]->focus(true); - playsound(S_MENUSELECT, SP_HIGH); + playsound(S_MENUSELECT, SP_HIGHEST); } } } @@ -651,7 +651,7 @@ bool menukey(int code, bool isdown, int unicode) if(code==SDLK_RETURN || code==SDLK_SPACE || code==-1 || code==-2) { m.select(); - playsound(S_MENUENTER, SP_HIGH); + playsound(S_MENUENTER, SP_HIGHEST); return true; } return false; diff --git a/source/src/rendergl.cpp b/source/src/rendergl.cpp index 85282c32..e151469c 100644 --- a/source/src/rendergl.cpp +++ b/source/src/rendergl.cpp @@ -66,11 +66,13 @@ void gl_checkextensions() hasMDA = true; } +#ifndef _DEBUG if(strstr(exts, "GL_EXT_draw_range_elements")) { glDrawRangeElements_ = (PFNGLDRAWRANGEELEMENTSEXTPROC)getprocaddress("glDrawRangeElementsEXT"); hasDRE = true; } +#endif if(strstr(exts, "GL_EXT_stencil_two_side")) { diff --git a/source/src/sound.cpp b/source/src/sound.cpp index 738d61c9..0d2303fc 100644 --- a/source/src/sound.cpp +++ b/source/src/sound.cpp @@ -280,6 +280,9 @@ struct source } }; +VARP(soundschedpriorityscore, 0, 100, 1000); +VARP(soundscheddistancescore, 0, 5, 1000); +VARP(soundschedoldbonus, 0, 100, 1000); // AC sound scheduler, manages available sound sources // under load it uses priority and distance information to reassign its resources @@ -325,16 +328,16 @@ struct sourcescheduler sources.deletecontentsp(); } - // returns a free sound source + // returns a free sound source (channel) // consuming code must call sourcescheduler::releasesource() after use source *newsource(int priority, const vec &o) { source *src = NULL; - + if(sources.length()) { - // get free item + // search unused source loopv(sources) if(!sources[i]->locked) { src = sources[i]; @@ -342,65 +345,47 @@ struct sourcescheduler } } - if(!src) // no channels left :( + if(SP_LOW==priority) return NULL; // low priority sounds can't replace others + + if(!src) // try replacing a used source { - if(SP_LOW == priority) return NULL; // reject low prio sounds - - loopv(sources) // replace stopped or lower prio sound + // score our sound + const float dist = o.iszero() ? 0.0f : camera1->o.dist(o); + const float score = (priority*soundschedpriorityscore) - (dist*soundscheddistancescore); + + // score other sounds + float worstscore = 0.0f; + source *worstsource = NULL; + + loopv(sources) { source *s = sources[i]; - if(SP_LOW == s->priority) + if(s->priority==SP_HIGHEST) continue; // highest priority sounds can't be replaced + + vec otherpos = s->position(); + float otherdist = otherpos.iszero() ? 0.0f : camera1->o.dist(otherpos); + float otherscore = (s->priority*soundschedpriorityscore) - (otherdist*soundscheddistancescore) - soundschedoldbonus; + if(!worstsource || otherscore < worstscore) { - src = s; - if(audiodebug) conoutf("ac sound sched: replaced low prio sound"); - break; + worstsource = s; + worstscore = otherscore; } } - if(!src) // still no channel, replace far away sounds of same priority + // pick worst source and replace it + if(worstsource && score>worstscore) { - // review existing sources and create a score for them based on listener-distance and priority - // take over the source with the highest score if possible - - float dist = o.iszero() ? 0.0f : camera1->o.dist(o); - float score = dist - priority*10.0f; // 1 priority level is worth 10 cubes of distance - - source *farthest = NULL; - float farthestscore = 0.0f; - - // score all sources - loopv(sources) - { - source *l = sources[i]; - if(l->priority <= priority) - { - vec lpos = l->position(); - float ldist = lpos.iszero() ? 0.0f : camera1->o.dist(lpos); - float lscore = ldist - l->priority*10.0f; - if(!farthest || lscore > farthestscore) - { - farthest = l; - farthestscore = lscore; - } - } - } - - // pick winner and replace - if(farthest && farthestscore >= score+5.0f) - { - src = farthest; - if(audiodebug) conoutf("ac sound sched: replaced sound of same prio"); - } + src = worstsource; + if(audiodebug) conoutf("ac sound sched: replaced sound of same prio"); + src->onreassign(); // inform previous owner about the take-over } - - if(src) src->onreassign(); // inform previous owner about the take-over - } + if(!src) { if(audiodebug) conoutf("ac sound sched: sound aborted, no channel takeover possible"); return NULL; - } + } src->reset(); // default settings src->lock(); // exclusive lock @@ -423,13 +408,6 @@ struct sourcescheduler delete src; } } - - void printstats() - { - int lockstats[] = {0, 0}; - loopv(sources) lockstats[sources[i]->locked ? 1 : 0]++; - conoutf("ac sound sched: %d locked\t%d unlocked\t%d total", lockstats[1], lockstats[0], sources.length()); - } }; // scheduler instance @@ -560,7 +538,7 @@ struct oggstream : sourceowner reset(); // grab a source and keep it during the whole lifetime - src = scheduler.newsource(SP_OMGPONIES, camera1->o); + src = scheduler.newsource(SP_HIGHEST, camera1->o); if(src) { if(src->valid) diff --git a/source/src/sound.h b/source/src/sound.h index add61834..dc6d716d 100644 --- a/source/src/sound.h +++ b/source/src/sound.h @@ -61,7 +61,7 @@ enum SP_LOW = 0, SP_NORMAL, SP_HIGH, - SP_OMGPONIES + SP_HIGHEST }; enum diff --git a/source/src/weapon.cpp b/source/src/weapon.cpp index b5f6ae6f..80239503 100644 --- a/source/src/weapon.cpp +++ b/source/src/weapon.cpp @@ -237,7 +237,7 @@ void hit(int damage, playerent *d, playerent *at, const vec &vel, int gun, bool h.dir = ivec(0, 0, 0); d->damageroll(damage); updatedmgindicator(at->o); - playsound(S_PAIN6); + playsound(S_PAIN6, SP_HIGH); } else {