Exemplo n.º 1
0
        public void GetSampleSizeInBytes()
        {
            // Test duration range.
            Assert.Throws <ArgumentOutOfRangeException>(() => SoundEffect.GetSampleSizeInBytes(TimeSpan.MinValue, 8000, AudioChannels.Mono));
            Assert.DoesNotThrow(() => SoundEffect.GetSampleSizeInBytes(TimeSpan.Zero, 8000, AudioChannels.Mono));
            Assert.DoesNotThrow(() => SoundEffect.GetSampleSizeInBytes(TimeSpan.FromMilliseconds(0x7FFFFFF), 8000, AudioChannels.Mono));
            Assert.Throws <ArgumentOutOfRangeException>(() => SoundEffect.GetSampleSizeInBytes(TimeSpan.FromMilliseconds(0x7FFFFFF + 1), 8000, AudioChannels.Mono));
            Assert.Throws <ArgumentOutOfRangeException>(() => SoundEffect.GetSampleSizeInBytes(TimeSpan.MaxValue, 8000, AudioChannels.Mono));

            // Test sampleRate range.
            Assert.Throws <ArgumentOutOfRangeException>(() => SoundEffect.GetSampleSizeInBytes(TimeSpan.Zero, -1, AudioChannels.Mono));
            Assert.Throws <ArgumentOutOfRangeException>(() => SoundEffect.GetSampleSizeInBytes(TimeSpan.Zero, 0, AudioChannels.Mono));
            Assert.Throws <ArgumentOutOfRangeException>(() => SoundEffect.GetSampleSizeInBytes(TimeSpan.Zero, 8000 - 1, AudioChannels.Mono));
            Assert.DoesNotThrow(() => SoundEffect.GetSampleSizeInBytes(TimeSpan.Zero, 8000, AudioChannels.Mono));
            Assert.DoesNotThrow(() => SoundEffect.GetSampleSizeInBytes(TimeSpan.Zero, 48000, AudioChannels.Mono));
            Assert.Throws <ArgumentOutOfRangeException>(() => SoundEffect.GetSampleSizeInBytes(TimeSpan.Zero, 48000 + 1, AudioChannels.Mono));
            Assert.Throws <ArgumentOutOfRangeException>(() => SoundEffect.GetSampleSizeInBytes(TimeSpan.Zero, int.MaxValue, AudioChannels.Mono));

            // Test channel range.
            Assert.Throws <ArgumentOutOfRangeException>(() => SoundEffect.GetSampleSizeInBytes(TimeSpan.Zero, 8000, (AudioChannels)(-1)));
            Assert.Throws <ArgumentOutOfRangeException>(() => SoundEffect.GetSampleSizeInBytes(TimeSpan.Zero, 8000, (AudioChannels)0));
            Assert.DoesNotThrow(() => SoundEffect.GetSampleSizeInBytes(TimeSpan.Zero, 8000, AudioChannels.Mono));
            Assert.DoesNotThrow(() => SoundEffect.GetSampleSizeInBytes(TimeSpan.Zero, 8000, AudioChannels.Stereo));
            Assert.Throws <ArgumentOutOfRangeException>(() => SoundEffect.GetSampleSizeInBytes(TimeSpan.Zero, 8000, (AudioChannels)3));
            Assert.Throws <ArgumentOutOfRangeException>(() => SoundEffect.GetSampleSizeInBytes(TimeSpan.Zero, 8000, (AudioChannels)int.MaxValue));

            // Test for zero duration.
            Assert.AreEqual(0, SoundEffect.GetSampleSizeInBytes(TimeSpan.Zero, 8000, AudioChannels.Mono));
            Assert.AreEqual(0, SoundEffect.GetSampleSizeInBytes(TimeSpan.Zero, 48000, AudioChannels.Mono));
            Assert.AreEqual(0, SoundEffect.GetSampleSizeInBytes(TimeSpan.Zero, 8000, AudioChannels.Stereo));
            Assert.AreEqual(0, SoundEffect.GetSampleSizeInBytes(TimeSpan.Zero, 48000, AudioChannels.Stereo));

            // Test for one second.
            Assert.AreEqual(16000, SoundEffect.GetSampleSizeInBytes(TimeSpan.FromSeconds(1), 8000, AudioChannels.Mono));
            Assert.AreEqual(96000, SoundEffect.GetSampleSizeInBytes(TimeSpan.FromSeconds(1), 48000, AudioChannels.Mono));
            Assert.AreEqual(32000, SoundEffect.GetSampleSizeInBytes(TimeSpan.FromSeconds(1), 8000, AudioChannels.Stereo));
            Assert.AreEqual(192000, SoundEffect.GetSampleSizeInBytes(TimeSpan.FromSeconds(1), 48000, AudioChannels.Stereo));
        }
Exemplo n.º 2
0
        // Sound function
        public SoundEffectInstance GetSound(string soundName, bool contentLoad)
        {
            SoundEffect returnEffect;

            try
            {
                if (contentLoad)
                {
                    returnEffect = Display2D.C2DEffect._content.Load <SoundEffect>(soundName);
                }
                else
                {
                    returnEffect = SoundEffect.FromStream(new System.IO.FileStream(soundName, System.IO.FileMode.Open));
                }
            }
            catch
            {
                byte[] buffer = new byte[SoundEffect.GetSampleSizeInBytes(TimeSpan.FromSeconds(1), 48000, AudioChannels.Stereo)];
                returnEffect = new SoundEffect(buffer, 48000, AudioChannels.Stereo);
            }

            return(returnEffect.CreateInstance());
        }