audio - How does the playback duration of sound files change when changing pitch in XNA/Monogame? -


using soundeffects class in xna (or alternative implementations of xna monogame or fna), sound effect has playback duration equal length of sound file stored in:

soundeffect.duration 

however, when pitch changed playback (between 1 , -1), changes effective duration of playback (e.g. lower pitch = slower playback) redering value in soundeffect.duration useless.

what mathematical expression calculate duration depending on playback pitch?

what trying smoothly fade out sound effects during e.g. last second of playback. in order decrease volume linearly in last second need know when playback acually ends hence (pitch-modulated, actual) duration.

i worte extension method not 100% accurate pretty close:

public static timespan getestimatedduration(this soundeffect sfx, float pitch) {     return timespan.frommilliseconds(sfx.duration.totalmilliseconds * (0.2514 * math.pow(pitch, 2) - 0.74 * pitch + 1)); } 

background

i unlucky find 'real' maths behind wrote little function this:

private void durationtest(string sfxassetname)     {         (float pitch = -1; pitch <= 1; pitch += 0.5f)         {             var sfx = this.content.load<soundeffect>(sfxassetname);             using (var instance = sfx.createinstance())             {                 instance.pitch = pitch;                 //var estimatedduration = sfx.getestimatedduration(pitch);                 instance.play();                 var sw = system.diagnostics.stopwatch.startnew();                 while (instance.state == soundstate.playing)                 { }                 sw.stop();                 var duration = sw.elapsed;                 system.diagnostics.debug.writeline("sfx={0} | pitch={1:0.00} | duration={2:0.00}secs", sfxassetname, pitch, duration.totalseconds);                 //system.diagnostics.debug.writeline("sfx={0} | pitch={1:0.00} | estimated={2:0.00}secs | actual={3:0.00}secs", sfxassetname, pitch, estimatedduration.totalseconds, duration.totalseconds);             }         }     } // output sfx=bombfuse | pitch=-1.00 | duration=3.89secs sfx=bombfuse | pitch=-0.50 | duration=2.75secs sfx=bombfuse | pitch= 0.00 | duration=1.95secs sfx=bombfuse | pitch= 0.50 | duration=1.38secs sfx=bombfuse | pitch= 1.00 | duration=0.98secs 

with data build chart polynomial trendline , displayed formula in excel calculation see in getestimatedduration. advanced durationtest comparision (commented lines above) - outputs this:

// output bombfuse (short) sfx=bombfuse | pitch=-1.00 | estimated=3.88secs | actual=3.89secs sfx=bombfuse | pitch=-0.50 | estimated=2.79secs | actual=2.76secs sfx=bombfuse | pitch= 0.00 | estimated=1.95secs | actual=1.95secs sfx=bombfuse | pitch= 0.50 | estimated=1.35secs | actual=1.38secs sfx=bombfuse | pitch= 1.00 | estimated=1.00secs | actual=0.98secs // output dreiklang (long) sfx=dreiklang | pitch=-1.00 | estimated=24.67secs | actual=24.77secs sfx=dreiklang | pitch=-0.50 | estimated=17.75secs | actual=17.52secs sfx=dreiklang | pitch= 0.00 | estimated=12.39secs | actual=12.39secs sfx=dreiklang | pitch= 0.50 | estimated= 8.58secs | actual= 8.76secs sfx=dreiklang | pitch= 1.00 | estimated= 6.33secs | actual= 6.20secs 

i hope helpful you, feel free use methods own needs , comparisions.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -