private void playAudio() { byte[] original_sound = this.stream.ToArray(); byte[] accelerated_sound = new byte[original_sound.Length / 2]; for (int i = 0; i < original_sound.Length; i = i + 4) { int first_lvl = BitConverter.ToInt16(original_sound, i); int second_lvl = BitConverter.ToInt16(original_sound, i); byte[] average = BitConverter.GetBytes((short)((first_lvl + second_lvl) / 2)); average.CopyTo(accelerated_sound, i / 2); } SoundEffect sound = new SoundEffect(accelerated_sound, microphone.SampleRate, AudioChannels.Mono); sound.Play(); this.durationTalkPiglet = (int)SoundEffect.GetSampleDuration(accelerated_sound.Length, microphone.SampleRate, AudioChannels.Mono).TotalMilliseconds; this.timerPigletTalk.Interval = new TimeSpan(0, 0, 0, 0, 100); this.timerPigletTalk.Start(); this.timerPigletListening.Interval = SoundEffect.GetSampleDuration(accelerated_sound.Length, microphone.SampleRate, AudioChannels.Mono).Add(TimeSpan.FromMilliseconds(1000)); this.timerPigletListening.Start(); this.ImagePigletTalk.Source = new BitmapImage(new Uri("/Images/fonem/fonem_" + Convert.ToString(this.rnd.Next(1, 6)) + ".png", UriKind.Relative)); }
public void GetSampleDuration() { // Test sizeInBytes range. Assert.Throws <ArgumentException>(() => SoundEffect.GetSampleDuration(-1, 8000, AudioChannels.Mono)); Assert.DoesNotThrow(() => SoundEffect.GetSampleDuration(1, 8000, AudioChannels.Mono)); Assert.DoesNotThrow(() => SoundEffect.GetSampleDuration(2, 8000, AudioChannels.Mono)); Assert.DoesNotThrow(() => SoundEffect.GetSampleDuration(3, 8000, AudioChannels.Mono)); // Test sampleRate range. Assert.Throws <ArgumentOutOfRangeException>(() => SoundEffect.GetSampleDuration(2, -1, AudioChannels.Mono)); Assert.Throws <ArgumentOutOfRangeException>(() => SoundEffect.GetSampleDuration(2, 0, AudioChannels.Mono)); Assert.Throws <ArgumentOutOfRangeException>(() => SoundEffect.GetSampleDuration(2, 8000 - 1, AudioChannels.Mono)); Assert.DoesNotThrow(() => SoundEffect.GetSampleDuration(2, 8000, AudioChannels.Mono)); Assert.DoesNotThrow(() => SoundEffect.GetSampleDuration(2, 48000, AudioChannels.Mono)); Assert.Throws <ArgumentOutOfRangeException>(() => SoundEffect.GetSampleDuration(2, 48000 + 1, AudioChannels.Mono)); Assert.Throws <ArgumentOutOfRangeException>(() => SoundEffect.GetSampleDuration(2, int.MaxValue, AudioChannels.Mono)); // Test channel range. Assert.Throws <ArgumentOutOfRangeException>(() => SoundEffect.GetSampleDuration(2, 8000, (AudioChannels)(-1))); Assert.Throws <ArgumentOutOfRangeException>(() => SoundEffect.GetSampleDuration(2, 8000, (AudioChannels)0)); Assert.DoesNotThrow(() => SoundEffect.GetSampleDuration(2, 8000, AudioChannels.Mono)); Assert.DoesNotThrow(() => SoundEffect.GetSampleDuration(2, 8000, AudioChannels.Stereo)); Assert.Throws <ArgumentOutOfRangeException>(() => SoundEffect.GetSampleDuration(2, 8000, (AudioChannels)3)); Assert.Throws <ArgumentOutOfRangeException>(() => SoundEffect.GetSampleDuration(2, 8000, (AudioChannels)int.MaxValue)); // Test for zero duration. Assert.AreEqual(TimeSpan.Zero, SoundEffect.GetSampleDuration(0, 8000, AudioChannels.Mono)); Assert.AreEqual(TimeSpan.Zero, SoundEffect.GetSampleDuration(0, 48000, AudioChannels.Mono)); Assert.AreEqual(TimeSpan.Zero, SoundEffect.GetSampleDuration(0, 8000, AudioChannels.Stereo)); Assert.AreEqual(TimeSpan.Zero, SoundEffect.GetSampleDuration(0, 48000, AudioChannels.Stereo)); // Test for one second. Assert.AreEqual(TimeSpan.FromSeconds(1), SoundEffect.GetSampleDuration(16000, 8000, AudioChannels.Mono)); Assert.AreEqual(TimeSpan.FromSeconds(1), SoundEffect.GetSampleDuration(96000, 48000, AudioChannels.Mono)); Assert.AreEqual(TimeSpan.FromSeconds(1), SoundEffect.GetSampleDuration(32000, 8000, AudioChannels.Stereo)); Assert.AreEqual(TimeSpan.FromSeconds(1), SoundEffect.GetSampleDuration(192000, 48000, AudioChannels.Stereo)); }