public void GetSampleDuration() { var monoInstance = new DynamicSoundEffectInstance(8000, AudioChannels.Mono); var stereoInstance = new DynamicSoundEffectInstance(24000, AudioChannels.Stereo); // Zero length Assert.AreEqual(0, (int)monoInstance.GetSampleDuration(0).TotalMilliseconds / 10); Assert.AreEqual(0, (int)stereoInstance.GetSampleDuration(0).TotalMilliseconds / 10); // Nonzero length Assert.AreNotEqual(0, monoInstance.GetSampleDuration(16000).Ticks); Assert.AreNotEqual(0, stereoInstance.GetSampleDuration(96000).Ticks); // Length not aligned with format Assert.AreEqual(1, (int)stereoInstance.GetSampleDuration(97).TotalMilliseconds); // Negative length Assert.Throws <ArgumentException>(() => { monoInstance.GetSampleDuration(-1); }); // Disposed monoInstance.Dispose(); Assert.Throws <ObjectDisposedException>(() => { monoInstance.GetSampleDuration(0); }); stereoInstance.Dispose(); }
public void GetSampleDuration() { var monoInstance = new DynamicSoundEffectInstance(8000, AudioChannels.Mono); var stereoInstance = new DynamicSoundEffectInstance(24000, AudioChannels.Stereo); // Zero length Assert.AreEqual(TimeSpan.Zero, monoInstance.GetSampleDuration(0)); Assert.AreEqual(TimeSpan.Zero, stereoInstance.GetSampleDuration(0)); // Nonzero length Assert.AreEqual(TimeSpan.FromSeconds(1), monoInstance.GetSampleDuration(16000)); Assert.AreEqual(TimeSpan.FromSeconds(1), stereoInstance.GetSampleDuration(96000)); // Length not aligned with format Assert.AreEqual(TimeSpan.FromMilliseconds(1), stereoInstance.GetSampleDuration(97)); // Negative length Assert.Throws <ArgumentException>(() => { monoInstance.GetSampleDuration(-1); }); // Disposed monoInstance.Dispose(); Assert.Throws <ObjectDisposedException>(() => { monoInstance.GetSampleDuration(0); }); stereoInstance.Dispose(); }
/// <summary> /// Plays the effect. /// </summary> /// <param name="asEffect">Set to false for music, true for sound effects.</param> public void Play(bool asEffect, AudioEffects effect = AudioEffects.None, float volume = 1.0f, bool spamCheck = false) { double now = UltimaGame.TotalMS; CullExpiredEffects(now); if (spamCheck && (LastPlayed + MinimumDelay > DateTime.Now)) { return; } m_ThisInstance = GetNewInstance(asEffect); if (m_ThisInstance == null) { Dispose(); return; } switch (effect) { case AudioEffects.PitchVariation: float pitch = Utility.RandomValue(-5, 5) * .025f; m_ThisInstance.Pitch = pitch; break; } BeforePlay(); LastPlayed = DateTime.Now; byte[] buffer = GetBuffer(); if (buffer != null && buffer.Length > 0) { m_ThisInstance.BufferNeeded += OnBufferNeeded; m_ThisInstance.SubmitBuffer(buffer); m_ThisInstance.Volume = volume; m_ThisInstance.Play(); List <Tuple <DynamicSoundEffectInstance, double> > list = (asEffect) ? m_EffectInstances : m_MusicInstances; double ms = m_ThisInstance.GetSampleDuration(buffer.Length).TotalMilliseconds; list.Add(new Tuple <DynamicSoundEffectInstance, double>(m_ThisInstance, now + ms)); } }
public void Play() { float now = (float)UltimaEngine.TotalMS; // Check to see if any existing instances of this sound effect have stopped playing. If // they have, remove the reference to them so the garbage collector can collect them. for (int i = 0; i < m_instances.Count; i++) { if (m_instances[i].Item2 < now) { m_instances.RemoveAt(i); i--; } } DynamicSoundEffectInstance instance = new DynamicSoundEffectInstance(22050, AudioChannels.Mono); instance.BufferNeeded += new EventHandler <EventArgs>(instance_BufferNeeded); instance.SubmitBuffer(m_waveBuffer); instance.Play(); m_instances.Add(new Tuple <DynamicSoundEffectInstance, float>(instance, now + (instance.GetSampleDuration(m_waveBuffer.Length).Milliseconds))); }
/// <summary> /// Plays the effect. /// </summary> /// <param name="asEffect">Set to false for music, true for sound effects.</param> public void Play(bool asEffect = true) { double now = UltimaGame.TotalMS; CullExpiredEffects(now); m_ThisInstance = GetNewInstance(asEffect); if (m_ThisInstance == null) { this.Dispose(); return; } BeforePlay(); byte[] buffer = GetBuffer(); if (buffer != null && buffer.Length > 0) { m_ThisInstance.BufferNeeded += new EventHandler <EventArgs>(OnBufferNeeded); m_ThisInstance.SubmitBuffer(buffer); m_ThisInstance.Play(); List <Tuple <DynamicSoundEffectInstance, double> > list = (asEffect) ? m_EffectInstances : m_MusicInstances; list.Add(new Tuple <DynamicSoundEffectInstance, double>(m_ThisInstance, now + (m_ThisInstance.GetSampleDuration(buffer.Length).Milliseconds))); } }