我又寫道擴展方法這是不是100%準確但八九不離十:
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));
}
背景
我也倒黴,找到 '真實'數學背後,所以我寫了這樣一個小函數:
private void DurationTest(string sfxAssetName)
{
for (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
有了這些數據,我用多項式趨勢線建立了一個圖表,並在excel中顯示公式,這是您在GetEstimatedDuration
中看到的計算結果。 然後我提出我的對比DurationTest(註釋以上線路) - 它輸出這樣的:
// 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
我希望這是對你有所幫助,隨意使用的方法爲自己的需要和comparisions。
哇,我沒想到這會是那麼複雜。我假設了一個簡單的方程,但不是一個多項式擬合;-)我會盡快檢查! –
嗯,那只是我擺弄出來的解決方案:) - 也許別人可以啓發我們 – Bashn