public void Initialize()
        {
            Game.InitializeAssetDatabase();

            defaultEngine = new AudioEngine();

            using (var monoStream = AssetManager.FileProvider.OpenStream("EffectBip", VirtualFileMode.Open, VirtualFileAccess.Read))
            {
                monoSoundEffect = SoundEffect.Load(defaultEngine, monoStream);
            }
            using (var stereoStream = AssetManager.FileProvider.OpenStream("EffectStereo", VirtualFileMode.Open, VirtualFileAccess.Read))
            {
                stereoSoundEffect = SoundEffect.Load(defaultEngine, stereoStream);
            }
            using (var contStream = AssetManager.FileProvider.OpenStream("EffectToneA", VirtualFileMode.Open, VirtualFileAccess.Read))
            {
                continousMonoSoundEffect = SoundEffect.Load(defaultEngine, contStream);
            }
            using (var contStream = AssetManager.FileProvider.OpenStream("EffectToneAE", VirtualFileMode.Open, VirtualFileAccess.Read))
            {
                continousStereoSoundEffect = SoundEffect.Load(defaultEngine, contStream);
            }
            using (var monoStream = AssetManager.FileProvider.OpenStream("Effect44100Hz", VirtualFileMode.Open, VirtualFileAccess.Read))
            {
                laugherMono = SoundEffect.Load(defaultEngine, monoStream);
            }

            monoInstance   = monoSoundEffect.CreateInstance();
            stereoInstance = stereoSoundEffect.CreateInstance();
        }
Exemple #2
0
        public void TestGetLeastSignificativeSoundEffect()
        {
            var engine = new AudioEngine();

            //////////////////////////////////////////
            // 1. Test that it returns null by default
            Assert.AreEqual(null, engine.GetLeastSignificativeSoundEffect());

            // create a sound effect
            SoundEffect soundEffect;

            using (var wavStream = AssetManager.FileProvider.OpenStream("EffectBip", VirtualFileMode.Open, VirtualFileAccess.Read))
                soundEffect = SoundEffect.Load(engine, wavStream);

            /////////////////////////////////////////////////////////////////////
            // 2. Test that is returns null when there is no playing sound effect
            Assert.AreEqual(null, engine.GetLeastSignificativeSoundEffect());

            /////////////////////////////////////////////////////////////////////////////////
            // 3. Test that is returns null when there is no not looped sound effect playing
            soundEffect.IsLooped = true;
            soundEffect.Play();
            Assert.AreEqual(null, engine.GetLeastSignificativeSoundEffect());

            ////////////////////////////////////////////////////////////////////////////
            // 4. Test that the sound effect is returned if not playing and not looped
            soundEffect.Stop();
            soundEffect.IsLooped = false;
            soundEffect.Play();
            Assert.AreEqual(soundEffect, engine.GetLeastSignificativeSoundEffect());

            // create another longer sound effect
            SoundEffect longerSoundEffect;

            using (var wavStream = AssetManager.FileProvider.OpenStream("EffectStereo", VirtualFileMode.Open, VirtualFileAccess.Read))
                longerSoundEffect = SoundEffect.Load(engine, wavStream);

            ///////////////////////////////////////////////////////////////////////
            // 5. Test that the longest sound is returned if it is the only playing
            soundEffect.Stop();
            longerSoundEffect.Play();
            Assert.AreEqual(longerSoundEffect, engine.GetLeastSignificativeSoundEffect());

            //////////////////////////////////////////////////////////////
            // 6. Test that the shortest sound is returned if both playing
            longerSoundEffect.Play();
            soundEffect.Play();
            Assert.AreEqual(soundEffect, engine.GetLeastSignificativeSoundEffect());

            //////////////////////////////////////////////////////////////////////
            // 7. Test that the longest sound is returned if the other is looped
            soundEffect.Stop();
            soundEffect.IsLooped = true;
            soundEffect.Play();
            longerSoundEffect.Play();
            Assert.AreEqual(longerSoundEffect, engine.GetLeastSignificativeSoundEffect());

            engine.Dispose();
        }
Exemple #3
0
        public void TestLoad()
        {
            ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
            // 1. Check that SoundEffect.Load throws "ArgumentNullException" when either 'engine' or 'stream' param is null
            // 1.1 Null engine
            validWavStream.Seek(0, SeekOrigin.Begin);
            Assert.Throws <ArgumentNullException>(() => SoundEffect.Load(null, validWavStream), "SoundEffect.Load did not throw 'ArgumentNullException' when called with a null engine.");
            // 1.2 Null stream
            Assert.Throws <ArgumentNullException>(() => SoundEffect.Load(defaultEngine, null), "SoundEffect.Load did not throw 'ArgumentNullException' when called with a null stream.");

            ///////////////////////////////////////////////////////////////////////////////////////////////////////
            // 2. Check that the load function throws "ObjectDisposedException" when the audio engine is disposed.
            var disposedEngine = new AudioEngine();

            disposedEngine.Dispose();
            validWavStream.Seek(0, SeekOrigin.Begin);
            Assert.Throws <ObjectDisposedException>(() => SoundEffect.Load(disposedEngine, validWavStream), "SoundEffect.Load did not throw 'ObjectDisposedException' when called with a displosed audio engine.");

            //////////////////////////////////////////////////////////////////////////////////////////////////////////////
            // 3. Check that the load function throws "InvalidOperationException" when the audio file stream is not valid
            // 3.1 Invalid audio file format.
            var otherFileFormatStream = AssetManager.FileProvider.OpenStream("EffectStereoOgg", VirtualFileMode.Open, VirtualFileAccess.Read);

            Assert.Throws <InvalidOperationException>(() => SoundEffect.Load(defaultEngine, otherFileFormatStream), "SoundEffect.Load did not throw 'InvalidOperationException' when called with an invalid file extension.");
            otherFileFormatStream.Dispose();
            // 3.2 Corrupted Header wav file
            var corruptedWavFormatStream = AssetManager.FileProvider.OpenStream("EffectHeaderCorrupted", VirtualFileMode.Open, VirtualFileAccess.Read);

            Assert.Throws <InvalidOperationException>(() => SoundEffect.Load(defaultEngine, corruptedWavFormatStream), "SoundEffect.Load did not throw 'InvalidOperationException' when called with a corrupted wav stream.");
            corruptedWavFormatStream.Dispose();
            // 3.3 Invalid wav file format (4-channels)
            var invalidChannelWavFormatStream = AssetManager.FileProvider.OpenStream("Effect4Channels", VirtualFileMode.Open, VirtualFileAccess.Read);

            Assert.Throws <InvalidOperationException>(() => SoundEffect.Load(defaultEngine, invalidChannelWavFormatStream), "SoundEffect.Load did not throw 'InvalidOperationException' when called with an invalid 4-channels wav format.");
            invalidChannelWavFormatStream.Dispose();

            //////////////////////////////////////////////////////////////////////////////////////////////////////////////
            // 4. Check that the load function throws "NotSupportedException" when the audio file stream is not supported
            // 4.1 extented wav file format (5-channels)
            var extendedChannelWavFormatStream = AssetManager.FileProvider.OpenStream("EffectSurround5Dot1", VirtualFileMode.Open, VirtualFileAccess.Read);

            Assert.Throws <NotSupportedException>(() => SoundEffect.Load(defaultEngine, extendedChannelWavFormatStream), "SoundEffect.Load did not throw 'NotSupportedException' when called with an extended 5-channels wav format.");
            extendedChannelWavFormatStream.Dispose();
            // 4.2 Invalid wav file format (24bits encoding)
            var invalidEncodingWavFormatStream = AssetManager.FileProvider.OpenStream("Effect24bits", VirtualFileMode.Open, VirtualFileAccess.Read);

            Assert.Throws <NotSupportedException>(() => SoundEffect.Load(defaultEngine, invalidEncodingWavFormatStream), "SoundEffect.Load did not throw 'NotSupportedException' when called with an invalid ieeefloat encoding wav format.");
            invalidEncodingWavFormatStream.Dispose();

            ///////////////////////////////////////////////////////
            // 5. Load a valid wav file stream and try to play it
            validWavStream.Seek(0, SeekOrigin.Begin);
            Assert.DoesNotThrow(() => SoundEffect.Load(defaultEngine, validWavStream), "SoundEffect.Load have thrown an exception when called with an valid wav stream.");
        }
Exemple #4
0
 public T Load <T>(string name) where T : Loadable, new()
 {
     ResourcesReady.Add(name, false);
     if (typeof(T) == typeof(Texture2D))
     {
         var t   = new Texture2D();
         var img = new HTMLImageElement();
         img.OnLoad = (e) =>
         {
             t.Image = img;
             t.Name  = name;
             ResourcesReady[name] = true;
             NotifyIfResourcesLoaded();
         };
         img.Src = RootDirectory + "/" + name + ".png";
         return(t as T);
     }
     else if (typeof(T) == typeof(SoundEffect))
     {
         var t = new SoundEffect();
         t.Load(RootDirectory + "/" + name + ".mp3", () => {
             t.Name = name;
             ResourcesReady[name] = t.Loaded = true;
             NotifyIfResourcesLoaded();
         });
         return(t as T);
     }
     else if (typeof(T) == typeof(Song))
     {
         var t = new Song();
         t.Load(RootDirectory + "/" + name + ".mp3", () => {
             t.Name = name;
             ResourcesReady[name] = t.Loaded = true;
             NotifyIfResourcesLoaded();
         });
         return(t as T);
     }
     else if (typeof(T) == typeof(SpriteFont))
     {
         var t = new SpriteFont();
         t.Name = name;
         ResourcesReady[name] = true;
         return(t as T);
     }
     else
     {
         return(new T());
     }
 }
        public void TestAttachDetachSounds()
        {
            var testInst = new AudioEmitterComponent();

            using (var game = new Game())
            {
                Game.InitializeAssetDatabase();
                using (var audioStream1 = ContentManager.FileProvider.OpenStream("EffectToneA", VirtualFileMode.Open, VirtualFileAccess.Read))
                    using (var audioStream2 = ContentManager.FileProvider.OpenStream("EffectBip", VirtualFileMode.Open, VirtualFileAccess.Read))
                        using (var audioStream3 = ContentManager.FileProvider.OpenStream("EffectStereo", VirtualFileMode.Open, VirtualFileAccess.Read))
                        {
                            var sound1 = SoundEffect.Load(game.Audio.AudioEngine, audioStream1);
                            var sound2 = SoundEffect.Load(game.Audio.AudioEngine, audioStream2);
                            var sound3 = SoundEffect.Load(game.Audio.AudioEngine, audioStream3);

                            // Attach two soundEffect and check that their controller are correctly created.

                            AudioEmitterSoundController soundController1 = null;
                            AudioEmitterSoundController soundController2 = null;
                            Assert.DoesNotThrow(() => testInst.AttachSoundEffect(sound1), "Adding a first soundEffect failed");
                            Assert.DoesNotThrow(() => soundController1 = testInst.SoundEffectToController[sound1], "There are no sound controller for sound1.");
                            Assert.IsNotNull(soundController1, "Sound controller for sound1 is null");

                            Assert.DoesNotThrow(() => testInst.AttachSoundEffect(sound2), "Adding a second soundEffect failed");
                            Assert.DoesNotThrow(() => soundController2 = testInst.SoundEffectToController[sound2], "There are no sound controller for sound1.");
                            Assert.IsNotNull(soundController2, "Sound controller for sound2 is null");

                            // Remove the two soundEffect and check that their controller are correctly erased.

                            Assert.DoesNotThrow(() => testInst.DetachSoundEffect(sound2), "Removing a first soundEffect failed");
                            Assert.IsFalse(testInst.SoundEffectToController.ContainsKey(sound2), "The controller for sound2 is still present in the list.");

                            Assert.DoesNotThrow(() => testInst.DetachSoundEffect(sound1), "Removing a second soundEffect failed");
                            Assert.IsFalse(testInst.SoundEffectToController.ContainsKey(sound1), "The controller for sound1 is still present in the list.");

                            Assert.IsTrue(testInst.SoundEffectToController.Keys.Count == 0, "There are some controller left in the component list.");

                            // Check the exception thrwon by attachSoundEffect.

                            Assert.Throws <ArgumentNullException>(() => testInst.AttachSoundEffect(null), "AttachSoundEffect did not throw ArgumentNullException");
                            Assert.Throws <InvalidOperationException>(() => testInst.AttachSoundEffect(sound3), "AttachSoundEffect did not throw InvalidOperationException.");

                            // Check the exception thrown by detachSoundEffect.

                            Assert.Throws <ArgumentNullException>(() => testInst.DetachSoundEffect(null), "DetachSoundEffect did not throw ArgumentNullException.");
                            Assert.Throws <ArgumentException>(() => testInst.DetachSoundEffect(sound1), "DetachSoundEffect did not throw ArgumentException ");
                        }
            }
        }
        public void Initialize()
        {
            Game.InitializeAssetDatabase();

            engine = AudioEngineFactory.NewAudioEngine();

            using (var stream = ContentManager.FileProvider.OpenStream("EffectBip", VirtualFileMode.Open, VirtualFileAccess.Read))
            {
                oneSound = SoundEffect.Load(engine, stream);
            }
            oneSound.IsLooped = true;

            sayuriPart          = SoundMusic.Load(engine, ContentManager.FileProvider.OpenStream("MusicFishLampMp3", VirtualFileMode.Open, VirtualFileAccess.Read));
            sayuriPart.IsLooped = true;
        }
        public void TestSoundEffectWhenEngineInvalidated()
        {
            // user should unplug the headphone before starting the test
            SoundEffect sound;

            using (var stream = ContentManager.FileProvider.OpenStream("EffectBip", VirtualFileMode.Open, VirtualFileAccess.Read))
                sound = SoundEffect.Load(engine, stream);

            // check the initial status of the engine
            Assert.AreEqual(AudioEngineState.Invalidated, engine.State);

            // check that the play state of the music is never modified when engine is invalid
            Assert.AreEqual(SoundPlayState.Stopped, sound.PlayState);
            Assert.DoesNotThrow(sound.Play);
            Assert.AreEqual(SoundPlayState.Stopped, sound.PlayState);
            Assert.DoesNotThrow(sound.Pause);
            Assert.AreEqual(SoundPlayState.Stopped, sound.PlayState);
            Assert.DoesNotThrow(sound.Stop);
            Assert.AreEqual(SoundPlayState.Stopped, sound.PlayState);

            // check that the source characteristics can be modified
            Assert.DoesNotThrow(() => sound.IsLooped = false);
            Assert.False(sound.IsLooped);
            Assert.DoesNotThrow(() => sound.IsLooped = true);
            Assert.True(sound.IsLooped);

            Assert.DoesNotThrow(() => sound.Volume = 0.5f);
            Assert.AreEqual(0.5f, sound.Volume);
            Assert.DoesNotThrow(() => sound.Volume = 0.67f);
            Assert.AreEqual(0.67f, sound.Volume);

            Assert.DoesNotThrow(() => sound.Pan = 0.5f);
            Assert.AreEqual(0.5f, sound.Pan);
            Assert.DoesNotThrow(() => sound.Pan = 0.67f);
            Assert.AreEqual(0.67f, sound.Pan);

            Assert.DoesNotThrow(() => sound.Apply3D(new AudioListener(), new AudioEmitter()));
            Assert.AreEqual(0f, sound.Pan);

            Assert.DoesNotThrow(sound.Reset3D);

            Assert.DoesNotThrow(sound.ExitLoop);

            Assert.DoesNotThrow(sound.Dispose);
            Assert.IsTrue(sound.IsDisposed);
        }
Exemple #8
0
        public void TestCreateInstance()
        {
            ///////////////////////////////////////////////////////////////////////////////////////////////////////
            // 1. Check that the create instance function throws "ObjectDisposedException" when soundEffect is disposed.
            validWavStream.Seek(0, SeekOrigin.Begin);
            var soundEffect = SoundEffect.Load(defaultEngine, validWavStream);

            soundEffect.Dispose();
            Assert.Throws <ObjectDisposedException>(() => soundEffect.CreateInstance(), "SoundEffect.CreateInstance did not throw 'ObjectDisposedException' when called with a displosed soundEffect.");

            //////////////////////////////////////////////////////////////
            // 2. Load a valid wav file stream and create an new instance
            validWavStream.Seek(0, SeekOrigin.Begin);
            var soundEffect2 = SoundEffect.Load(defaultEngine, validWavStream);

            Assert.DoesNotThrow(() => soundEffect2.CreateInstance(), "SoundEffect.CreateInstance have thrown an exception when called with an soundEffect.");
        }
        public void TestGetController()
        {
            var testInst = new AudioEmitterComponent();

            using (var game = new Game())
            {
                Game.InitializeAssetDatabase();
                using (var audioStream1 = ContentManager.FileProvider.OpenStream("EffectToneA", VirtualFileMode.Open, VirtualFileAccess.Read))
                    using (var audioStream2 = ContentManager.FileProvider.OpenStream("EffectBip", VirtualFileMode.Open, VirtualFileAccess.Read))
                    {
                        var sound1 = SoundEffect.Load(game.Audio.AudioEngine, audioStream1);
                        var sound2 = SoundEffect.Load(game.Audio.AudioEngine, audioStream2);

                        AudioEmitterSoundController soundController1 = null;
                        AudioEmitterSoundController soundController2 = null;

                        // Add two soundEffect and try to get their controller.

                        testInst.AttachSoundEffect(sound1);
                        Assert.DoesNotThrow(() => soundController1 = testInst.GetSoundEffectController(sound1), "Failed to get the controller associated to the soundEffect1.");

                        testInst.AttachSoundEffect(sound2);
                        Assert.DoesNotThrow(() => soundController2 = testInst.GetSoundEffectController(sound2), "Failed to get the controller associated to the soundEffect1.");

                        // Suppress the two SoundEffects and check that the function throws the correct exception (ArgumentException)

                        testInst.DetachSoundEffect(sound1);
                        Assert.Throws <ArgumentException>(() => testInst.GetSoundEffectController(sound1), "GetController did not throw ArgumentException but sound1 was supposed to be detached.");

                        testInst.DetachSoundEffect(sound2);
                        Assert.Throws <ArgumentException>(() => testInst.GetSoundEffectController(sound2), "GetController did not throw ArgumentException but sound2 was supposed to be detached.");

                        // Check the ArgumentNullException
                        Assert.Throws <ArgumentNullException>(() => testInst.GetSoundEffectController(null), "GetController did not throw ArgumentNullException.");
                    }
            }
        }
Exemple #10
0
        public void TestDispose()
        {
            ////////////////////////////////////////////////////////////////////////
            // 1. Check that the SoundEffect dispose status is correct after Loading
            validWavStream.Seek(0, SeekOrigin.Begin);
            var soundEffect = SoundEffect.Load(defaultEngine, validWavStream);

            Assert.False(soundEffect.IsDisposed, "SoundEffect was marked as 'disposed' after being loaded.");

            ////////////////////////////////////////////////////////
            // 2. Check that the SoundEffect.Dispose does not crash
            Assert.DoesNotThrow(soundEffect.Dispose, "SoundEffect.Disposed as crash throwing an exception.");

            //////////////////////////////////////////////////////////////////////////////////
            // 3. Check that the dispose status of SoundEffect is correct after being disposed
            Assert.True(soundEffect.IsDisposed, "SoundEffect was not marked as 'Disposed' after being disposed");

            ///////////////////////////////////////////////////////////////////////////////
            // 4. Check that it does not crash if we make several calls to dispose function
            Assert.Throws <InvalidOperationException>(soundEffect.Dispose, "SoundEffect.Disposed does not have called invalid operation exception when called a second time.");

            //////////////////////////////////////////////////////////////////////////////////////////////////
            // 5. Check that soundEffect.Dispose stops playing and displose all subjacent SoundEffectInstances
            validWavStream.Seek(0, SeekOrigin.Begin);
            var soundEffect2 = SoundEffect.Load(defaultEngine, validWavStream);
            var inst1        = soundEffect2.CreateInstance();
            var inst2        = soundEffect2.CreateInstance();

            inst1.Play();
            Utilities.Sleep(100);
            inst2.Play();
            Utilities.Sleep(100);
            Assert.DoesNotThrow(soundEffect2.Dispose, "SoundEffect.Disposed have crashed throwing an exception when called with several subjacent instances playing.");
            Assert.True(inst1.PlayState == SoundPlayState.Stopped && inst2.PlayState == SoundPlayState.Stopped, "SoundEffect subjacent instances have not been stopped correctly during soundEffect.Dispose");
            Assert.True(inst1.IsDisposed && inst2.IsDisposed, "SoundEffect subjacent instances have not been disposed correctly during soundEffect.Dispose");
        }
Exemple #11
0
        public void TestDefaultInstance()
        {
            validWavStream.Seek(0, SeekOrigin.Begin);
            var soundEffect = SoundEffect.Load(defaultEngine, validWavStream);

            ///////////////////////////
            // 1. Check default values
            Assert.IsFalse(soundEffect.IsLooped, "SoundEffect is looping by default.");
            Assert.AreEqual(SoundPlayState.Stopped, soundEffect.PlayState, "SoundEffect is not stopped by default.");
            Assert.AreEqual(1f, soundEffect.Volume, "SoundEffect.Volume default value is not 1f.");
            Assert.AreEqual(0f, soundEffect.Pan, "SoundEffect.Pan default value is not 0f.");

            /////////////////////////
            // 2. Check IPlayable
            soundEffect.IsLooped = true;
            soundEffect.Play();
            Assert.AreEqual(SoundPlayState.Playing, soundEffect.PlayState, "SoundEffect.PlayState is not Playing.");
            Utilities.Sleep(1000);
            soundEffect.Pause();
            Assert.AreEqual(SoundPlayState.Paused, soundEffect.PlayState, "SoundEffect.PlayState is not Paused.");
            Utilities.Sleep(1000);
            soundEffect.Play();
            Utilities.Sleep(50);
            soundEffect.ExitLoop();
            Utilities.Sleep(1500);
            Assert.AreEqual(SoundPlayState.Stopped, soundEffect.PlayState, "SoundEffect.PlayState is not Stopped after exitLoop.");

            ////////////////////////
            // 3. Volume and stop
            soundEffect.Play();
            Utilities.Sleep(1000);
            soundEffect.Volume = 0.5f;
            Utilities.Sleep(1000);
            soundEffect.Stop();
            Assert.AreEqual(SoundPlayState.Stopped, soundEffect.PlayState, "SoundEffect.PlayState is not Stopped.");
            Utilities.Sleep(1000);
            soundEffect.Volume = 1f;

            /////////////////////
            // 4. Pan
            soundEffect.Pan = -1f;
            soundEffect.Play();
            Utilities.Sleep(1000);
            soundEffect.Pan = 1f;
            Utilities.Sleep(1000);
            soundEffect.Stop();
            Utilities.Sleep(1000);
            soundEffect.Pan = 0;

            ///////////////////////////////////////////////////////////
            // 6. Apply3D (should have no effect and then hear nothing)
            soundEffect.Pan = 1;
            soundEffect.Apply3D(new AudioListener(), new AudioEmitter());
            soundEffect.Play();
            Utilities.Sleep(1000);
            soundEffect.Stop();
            soundEffect.Apply3D(new AudioListener {
                Position = new Vector3(100, 0, 0)
            }, new AudioEmitter());
            soundEffect.Play();
            Utilities.Sleep(1000);
            soundEffect.Stop();
        }
Exemple #12
0
        public void TestDispose()
        {
            var crossDisposedEngine = new AudioEngine();
            var engine = new AudioEngine();

            crossDisposedEngine.Dispose(); // Check there no Dispose problems with sereval cross-disposed instances.

            // Create some SoundEffects
            SoundEffect soundEffect;

            using (var wavStream = AssetManager.FileProvider.OpenStream("EffectBip", VirtualFileMode.Open, VirtualFileAccess.Read))
            {
                soundEffect = SoundEffect.Load(engine, wavStream);
            }
            SoundEffect dispSoundEffect;

            using (var wavStream = AssetManager.FileProvider.OpenStream("EffectBip", VirtualFileMode.Open, VirtualFileAccess.Read))
            {
                dispSoundEffect = SoundEffect.Load(engine, wavStream);
            }
            dispSoundEffect.Dispose();

            var soundEffectInstance = soundEffect.CreateInstance();
            var dispInstance        = soundEffect.CreateInstance();

            dispInstance.Dispose();

            // Create some SoundMusics.
            var soundMusic1 = SoundMusic.Load(engine, AssetManager.FileProvider.OpenStream("MusicBip", VirtualFileMode.Open, VirtualFileAccess.Read));
            var soundMusic2 = SoundMusic.Load(engine, AssetManager.FileProvider.OpenStream("MusicToneA", VirtualFileMode.Open, VirtualFileAccess.Read));

            soundMusic2.Dispose();

            // Create some dynamicSounds.
            var generator = new SoundGenerator();
            var dynSound1 = new DynamicSoundEffectInstance(engine, 44100, AudioChannels.Mono, AudioDataEncoding.PCM_8Bits);
            var dynSound2 = new DynamicSoundEffectInstance(engine, 20000, AudioChannels.Mono, AudioDataEncoding.PCM_8Bits);

            dynSound1.Play();
            dynSound1.SubmitBuffer(generator.Generate(44100, new[] { 1000f }, 1, 120000));
            dynSound2.Dispose();

            // Start playing some
            soundEffectInstance.Play();
            soundMusic1.Play();

            for (int i = 0; i < 10; i++)
            {
                engine.Update();
                Utilities.Sleep(5);
            }

            Assert.DoesNotThrow(engine.Dispose, "AudioEngine crashed during disposal.");
            Assert.IsTrue(soundEffect.IsDisposed, "SoundEffect is not disposed.");
            Assert.Throws <InvalidOperationException>(engine.Dispose, "AudioEngine did not threw invalid operation exception.");
            Assert.AreEqual(SoundPlayState.Stopped, soundEffectInstance.PlayState, "SoundEffectInstance has not been stopped properly.");
            Assert.IsTrue(soundEffectInstance.IsDisposed, "SoundEffectInstance has not been disposed properly.");
            Assert.AreEqual(SoundPlayState.Stopped, soundMusic1.PlayState, "soundMusic1 has not been stopped properly.");
            Assert.IsTrue(soundMusic1.IsDisposed, "soundMusic1 has not been disposed properly.");
            //Assert.AreEqual(SoundPlayState.Stopped, dynSound1.PlayState, "The dynamic sound 1 has not been stopped correctly.");
            //Assert.IsTrue(dynSound1.IsDisposed, "The dynamic sound 1 has not been disposed correctly.");
        }
Exemple #13
0
        public void TestResumeAudio()
        {
            var engine = new AudioEngine();

            // create a sound effect instance
            SoundEffect soundEffect;

            using (var wavStream = AssetManager.FileProvider.OpenStream("EffectBip", VirtualFileMode.Open, VirtualFileAccess.Read))
                soundEffect = SoundEffect.Load(engine, wavStream);
            var wave1Instance = soundEffect.CreateInstance();

            // create a music instance
            var music = SoundMusic.Load(engine, AssetManager.FileProvider.OpenStream("MusicFishLampMp3", VirtualFileMode.Open, VirtualFileAccess.Read));

            // check that resume do not play stopped instances
            engine.PauseAudio();
            engine.ResumeAudio();
            Assert.AreEqual(SoundPlayState.Stopped, music.PlayState);
            Assert.AreEqual(SoundPlayState.Stopped, wave1Instance.PlayState);
            Utilities.Sleep(1000); // listen that nothing comes out

            // check that user paused music does not resume
            wave1Instance.Play();
            wave1Instance.Pause();
            engine.PauseAudio();
            engine.ResumeAudio();
            Assert.AreEqual(SoundPlayState.Paused, wave1Instance.PlayState);
            Utilities.Sleep(1000); // listen that nothing comes out

            // check that sounds paused by PauseAudio are correctly restarted
            wave1Instance.Play();
            music.Play();
            engine.PauseAudio();
            engine.ResumeAudio();
            Assert.AreEqual(SoundPlayState.Playing, wave1Instance.PlayState);
            Assert.AreEqual(SoundPlayState.Playing, music.PlayState);
            TestAudioUtilities.ActiveAudioEngineUpdate(engine, 3000); // listen that the sound comes out
            music.Stop();
            TestAudioUtilities.ActiveAudioEngineUpdate(engine, 100);  // stop the music

            // check that a sound stopped during the pause does not play during the resume
            wave1Instance.Play();
            engine.PauseAudio();
            wave1Instance.Stop();
            engine.ResumeAudio();
            Assert.AreEqual(SoundPlayState.Stopped, wave1Instance.PlayState);
            Utilities.Sleep(1000); // listen that nothing comes out

            // check that a sound played during the pause do not play during the resume
            engine.PauseAudio();
            wave1Instance.Play();
            engine.ResumeAudio();
            Assert.AreEqual(SoundPlayState.Stopped, wave1Instance.PlayState);
            Utilities.Sleep(1000); // listen that nothing comes out

            // check that a two calls to resume do not have side effects (1)
            wave1Instance.Play();
            engine.PauseAudio();
            engine.ResumeAudio();
            Assert.AreEqual(SoundPlayState.Playing, wave1Instance.PlayState);
            Utilities.Sleep(2000); // wait that the sound is finished
            engine.ResumeAudio();
            Assert.AreEqual(SoundPlayState.Stopped, wave1Instance.PlayState);
            Utilities.Sleep(1000); // listen that nothing comes out

            // check that a two calls to resume do not have side effects (2)
            wave1Instance.Play();
            engine.PauseAudio();
            engine.ResumeAudio();
            Assert.AreEqual(SoundPlayState.Playing, wave1Instance.PlayState);
            wave1Instance.Pause();
            engine.ResumeAudio();
            Assert.AreEqual(SoundPlayState.Paused, wave1Instance.PlayState);
            Utilities.Sleep(1000); // listen that nothing comes out

            // check that a several calls to pause/play do not have side effects
            wave1Instance.Play();
            engine.PauseAudio();
            engine.ResumeAudio();
            engine.PauseAudio();
            engine.ResumeAudio();
            Assert.AreEqual(SoundPlayState.Playing, wave1Instance.PlayState);
            Utilities.Sleep(2000); // listen that the sound comes out

            // check that the sound is not played if disposed
            wave1Instance.Play();
            engine.PauseAudio();
            wave1Instance.Dispose();
            engine.ResumeAudio();
            Assert.AreEqual(SoundPlayState.Stopped, wave1Instance.PlayState);
            Utilities.Sleep(1000); // listen that nothing comes out

            music.Dispose();
            soundEffect.Dispose();
        }
Exemple #14
0
        public void TestPauseAudio()
        {
            var engine = new AudioEngine();

            // create a sound effect instance
            SoundEffect soundEffect;

            using (var wavStream = AssetManager.FileProvider.OpenStream("EffectBip", VirtualFileMode.Open, VirtualFileAccess.Read))
                soundEffect = SoundEffect.Load(engine, wavStream);
            var wave1Instance = soundEffect.CreateInstance();

            // create a music instance
            var music = SoundMusic.Load(engine, AssetManager.FileProvider.OpenStream("MusicFishLampMp3", VirtualFileMode.Open, VirtualFileAccess.Read));

            // check state
            engine.PauseAudio();
            Assert.AreEqual(AudioEngineState.Paused, engine.State);

            // check that existing instance can not be played
            wave1Instance.Play();
            Assert.AreEqual(SoundPlayState.Stopped, wave1Instance.PlayState);
            music.Play();
            Assert.AreEqual(SoundPlayState.Stopped, music.PlayState);
            TestAudioUtilities.ActiveAudioEngineUpdate(engine, 1000); // listen that nothing comes out

            // create a new sound effect
            SoundEffect soundEffectStereo;

            using (var wavStream = AssetManager.FileProvider.OpenStream("EffectStereo", VirtualFileMode.Open, VirtualFileAccess.Read))
                soundEffectStereo = SoundEffect.Load(engine, wavStream);

            // check that a new instance can not be played
            soundEffectStereo.Play();
            Assert.AreEqual(SoundPlayState.Stopped, soundEffectStereo.PlayState);
            Utilities.Sleep(1000); // listen that nothing comes out

            // check that a stopped sound stay stopped
            engine.ResumeAudio();
            soundEffectStereo.Stop();
            engine.PauseAudio();
            Assert.AreEqual(SoundPlayState.Stopped, soundEffectStereo.PlayState);

            // check that a paused sound stay paused
            engine.ResumeAudio();
            soundEffectStereo.Play();
            soundEffectStereo.Pause();
            engine.PauseAudio();
            Assert.AreEqual(SoundPlayState.Paused, soundEffectStereo.PlayState);

            // check that a playing sound is paused
            engine.ResumeAudio();
            wave1Instance.Play();
            soundEffectStereo.Play();
            music.Play();
            engine.PauseAudio();
            Assert.AreEqual(SoundPlayState.Paused, wave1Instance.PlayState);
            Assert.AreEqual(SoundPlayState.Paused, soundEffectStereo.PlayState);
            Assert.AreEqual(SoundPlayState.Paused, music.PlayState);
            TestAudioUtilities.ActiveAudioEngineUpdate(engine, 1000); // listen that nothing comes out

            // check that stopping a sound while paused is possible
            engine.PauseAudio();
            soundEffectStereo.Stop();
            Assert.AreEqual(SoundPlayState.Stopped, soundEffectStereo.PlayState);

            // check that disposing a sound while paused is possible
            engine.PauseAudio();
            music.Dispose();
            Assert.IsTrue(music.IsDisposed);

            soundEffect.Dispose();
            soundEffectStereo.Dispose();
        }
Exemple #15
0
        public void TestVolume()
        {
            float vol = 0;
            //////////////////////////////////////////////////////////////////////////////////
            // 1. Check that get and set Volume for an Disposed instance throw the 'ObjectDisposedException'
            var dispInst = SoundMusic.Load(defaultEngine, OpenDataBaseStream("EffectToneA"));

            dispInst.Dispose();
            Assert.Throws <ObjectDisposedException>(() => vol             = dispInst.Volume, "SoundMusic.Volume { get } did not throw the 'ObjectDisposedException' when called from a disposed object.");
            Assert.Throws <ObjectDisposedException>(() => dispInst.Volume = 0, "SoundMusic.Volume { set } did not throw the 'ObjectDisposedException' when called from a disposed object.");

            ///////////////////////////////////////////////////////////
            // 2. Check that Volume set/get do not crash on valid sound
            var volInst = SoundMusic.Load(defaultEngine, OpenDataBaseStream("EffectBip"));

            Assert.DoesNotThrow(() => vol            = volInst.Volume, "SoundMusic.Volume { get } crashed.");
            Assert.DoesNotThrow(() => volInst.Volume = 0.5f, "SoundMusic.Volume { set } crashed.");

            /////////////////////////////////////////////////////
            // 3. Check that Volume value is set to 1 by default
            Assert.AreEqual(1f, vol, "Default volume value is not 1.");

            /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            // 4. Check that modifying the volume works and is correctly clamped (result => sound should go up and back down)
            volInst.IsLooped = true;
            volInst.Play();
            var currentVol = -0.3f;
            var sign       = 1f;

            while (currentVol >= -0.3f)
            {
                Assert.DoesNotThrow(() => volInst.Volume = currentVol, "SoundMusic.Volume { set } crashed.");
                Assert.DoesNotThrow(() => vol            = volInst.Volume, "SoundMusic.Volume { get } crashed.");
                Assert.AreEqual(MathUtil.Clamp(currentVol, 0, 1), vol, "The volume value is not what is supposed to be.");

                ActiveAudioEngineUpdate(10);
                if (currentVol > 1.3)
                {
                    sign = -1;
                }

                currentVol += sign * 0.005f;
            }
            volInst.Stop();
            ActiveAudioEngineUpdate(1000);

            ///////////////////////////////////////////////////////////////////////////////////////////
            // 5. Check that modifying the volume on another instance does not affect current instance
            var volInst2 = SoundMusic.Load(defaultEngine, OpenDataBaseStream("EffectBip"));

            volInst.Play();
            volInst.Volume = 1;
            ActiveAudioEngineUpdate(1500);
            volInst2.Volume = 0.2f;
            ActiveAudioEngineUpdate(1500);
            volInst.Stop();
            ActiveAudioEngineUpdate(1000);

            //////////////////////////////////////////////////////////////////////////////////
            // 6. Check that volume is correctly memorised and updated when changing music
            volInst2.Volume   = 0.2f;
            volInst.Volume    = 1f;
            volInst2.IsLooped = true;
            volInst.Play();
            ActiveAudioEngineUpdate(1500);
            volInst2.Play();
            ActiveAudioEngineUpdate(1500);
            volInst.Play();
            ActiveAudioEngineUpdate(1500);
            volInst2.Stop();

            ///////////////////////////////////////////////////////////////////////////////
            // 7. Check that modifying SoundMusic volume does not modify SoundEffectVolume
            SoundEffect soundEffect;

            using (var contStream = ContentManager.FileProvider.OpenStream("EffectToneA", VirtualFileMode.Open, VirtualFileAccess.Read))
            {
                soundEffect = SoundEffect.Load(defaultEngine, contStream);
            }
            soundEffect.IsLooped = true;
            soundEffect.Play();
            volInst.Play();
            currentVol = -0.3f;
            sign       = 1f;
            while (currentVol >= -0.3f)
            {
                volInst.Volume = currentVol;

                ActiveAudioEngineUpdate(10);
                if (currentVol > 1.3)
                {
                    sign = -1;
                }

                currentVol += sign * 0.005f;
            }
            volInst.Stop();
            soundEffect.Stop();
            ActiveAudioEngineUpdate(1000);
        }