A game framework written with osu! in mind.
at master 106 lines 3.2 kB view raw
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. 2// See the LICENCE file in the repository root for full licence text. 3 4using NUnit.Framework; 5using osu.Framework.Allocation; 6using osu.Framework.Audio; 7using osu.Framework.Audio.Sample; 8using osu.Framework.Testing; 9 10namespace osu.Framework.Tests.Visual.Audio 11{ 12 public class TestSceneSampleLooping : FrameworkTestScene 13 { 14 private Sample sample; 15 private SampleChannel channel; 16 17 [Resolved] 18 private AudioManager audioManager { get; set; } 19 20 [SetUpSteps] 21 public void SetUpSteps() 22 { 23 AddUntilStep("audio device ready", () => audioManager.IsLoaded); 24 AddStep("create looping sample", () => 25 { 26 sample?.Dispose(); 27 sample = audioManager.Samples.Get("tone.wav"); 28 }); 29 } 30 31 [Test] 32 public void TestDisableLoopingFlag() 33 { 34 playAndCheckSample(); 35 36 AddStep("disable looping", () => channel.Looping = false); 37 AddUntilStep("ensure stops", () => !channel.Playing); 38 } 39 40 [Test] 41 public void TestZeroFrequency() 42 { 43 playAndCheckSample(); 44 45 AddStep("set frequency to 0", () => channel.Frequency.Value = 0); 46 AddWaitStep("wait for audio thread", 3); 47 AddAssert("is still playing", () => channel.Playing); 48 } 49 50 [Test] 51 public void TestZeroFrequencyOnStart() 52 { 53 AddStep("set frequency to 0", () => sample.Frequency.Value = 0); 54 playAndCheckSample(); 55 56 AddStep("set frequency to 1", () => channel.Frequency.Value = 1); 57 AddWaitStep("wait for audio thread", 3); 58 AddAssert("is still playing", () => channel.Playing); 59 } 60 61 [Test] 62 public void TestZeroFrequencyAfterStop() 63 { 64 stopAndCheckSample(); 65 66 AddStep("set frequency to 0", () => channel.Frequency.Value = 0); 67 AddWaitStep("wait for audio thread", 3); 68 AddAssert("still stopped", () => !channel.Playing); 69 } 70 71 [TearDownSteps] 72 public void TearDownSteps() 73 { 74 stopAndCheckSample(); 75 } 76 77 private void playAndCheckSample() 78 { 79 AddStep("play sample", () => 80 { 81 channel = sample.GetChannel(); 82 channel.Looping = true; 83 84 // reduce volume of the tone due to how loud it normally is. 85 channel.Volume.Value = 0.05; 86 channel.Play(); 87 }); 88 89 // ensures that it is in fact looping given that the loaded sample length is very short. 90 AddWaitStep("wait", 10); 91 AddAssert("is playing", () => channel.Playing); 92 } 93 94 private void stopAndCheckSample() 95 { 96 AddStep("stop playing", () => channel?.Stop()); 97 AddUntilStep("stopped", () => channel?.Playing != true); 98 } 99 100 protected override void Dispose(bool isDisposing) 101 { 102 sample?.Dispose(); 103 base.Dispose(isDisposing); 104 } 105 } 106}