Esempio n. 1
0
        public void TrackCanBePaused()
        {
            // Arrange
            const int fullCount = 1000;
            const int halfCount = fullCount / 2;

            var mixer     = new Mixer();
            var soundData = GetRandomFloats(fullCount);
            var sound     = new TestSampleSource(soundData);

            var track = mixer.AddTrack(sound);

            track.Play();

            var buffer = new float[fullCount];

            // Act
            mixer.Read(buffer, 0, halfCount);
            track.Pause();
            mixer.Read(buffer, halfCount, halfCount);

            // Assert
            Assert.That(track.IsPlaying, Is.False);
            var expectedBuffer = soundData.ToArray();

            Array.Clear(expectedBuffer, halfCount, halfCount);
            Assert.That(buffer, Is.EqualTo(expectedBuffer));
        }
Esempio n. 2
0
        public void TrackCanBeStoppedAndPlayedFromTheBeginning()
        {
            // Arrange
            const int fullCount    = 1000;
            const int halfCount    = fullCount / 2;
            const int quarterCount = fullCount / 4;

            var mixer     = new Mixer();
            var soundData = GetRandomFloats(fullCount);
            var sound     = new TestSampleSource(soundData);

            var track = mixer.AddTrack(sound);

            track.Play();

            var buffer = new float[fullCount];

            // Act
            mixer.Read(buffer, 0, halfCount);
            track.Stop();
            mixer.Read(buffer, halfCount, quarterCount);
            track.Play();
            mixer.Read(buffer, halfCount + quarterCount, quarterCount);

            // Assert
            Assert.That(track.IsPlaying, Is.True);
            var expectedBuffer = soundData.ToArray();

            Array.Copy(soundData, 0, expectedBuffer, halfCount + quarterCount, quarterCount);
            Array.Clear(expectedBuffer, halfCount, quarterCount);
            Assert.That(buffer, Is.EqualTo(expectedBuffer));
        }
Esempio n. 3
0
        public void Stopped_EventShouldBeRaised_WhenTrackIsStopped()
        {
            // Arrange
            var sampleSource = Substitute.For <ISampleSource>();

            sampleSource.WaveFormat.Returns(new WaveFormat(44100, 32, 2, AudioEncoding.IeeeFloat));
            var track = _mixer.AddTrack(sampleSource);

            IPlayback playback    = new Playback(_mixer, track);
            object?   eventSender = null;

            playback.Stopped += (sender, args) => eventSender = sender;

            // Act
            track.Stop();

            // Assert
            Assert.That(eventSender, Is.EqualTo(playback));
        }
Esempio n. 4
0
        public void Read_ShouldMixTwoSoundsByAddition()
        {
            // Arrange
            var mixer  = new Mixer();
            var sound1 = new TestSampleSource(new[] { 1f, 2f, 3f });
            var sound2 = new TestSampleSource(new[] { 10f, 20f, 30f });
            var track1 = mixer.AddTrack(sound1);
            var track2 = mixer.AddTrack(sound2);

            track1.Play();
            track2.Play();

            var buffer = new float[3];

            // Act
            mixer.Read(buffer, 0, buffer.Length);

            // Assert
            Assert.That(buffer, Is.EqualTo(new[] { 11f, 22f, 33f }));
        }
Esempio n. 5
0
        public void AddTrack_ShouldThrowException_WhenMixerDisposed()
        {
            // Arrange
            var mixer = new Mixer();
            var sound = Substitute.For <ISampleSource>();

            mixer.Dispose();

            // Act
            // Assert
            Assert.That(() => mixer.AddTrack(sound), Throws.TypeOf <ObjectDisposedException>());
        }
Esempio n. 6
0
        public void AddTrack_ShouldThrowNothing_WhenSampleSourceMatchesMixerWaveFormat()
        {
            // Arrange
            var mixer = new Mixer();
            var sound = Substitute.For <ISampleSource>();

            sound.WaveFormat.Returns(new WaveFormat(44100, 32, 2, AudioEncoding.IeeeFloat));

            // Act
            // Assert
            Assert.That(() => mixer.AddTrack(sound), Throws.Nothing);
        }
Esempio n. 7
0
        public void AddTrack_ShouldThrowException_WhenSampleSourceUsesNotMatchingWaveFormat()
        {
            // Arrange
            var mixer = new Mixer();
            var sound = Substitute.For <ISampleSource>();

            sound.WaveFormat.Returns(new WaveFormat(44100, 32, 2, AudioEncoding.WAVE_FORMAT_FLAC));

            // Act
            // Assert
            Assert.That(() => mixer.AddTrack(sound), Throws.ArgumentException.With.Message.Contain("wave format"));
        }
Esempio n. 8
0
        public void AddTrack_ShouldThrowException_WhenSampleSourceUsesOtherSampleRateThan44100(int sampleRate)
        {
            // Arrange
            var mixer = new Mixer();
            var sound = Substitute.For <ISampleSource>();

            sound.WaveFormat.Returns(new WaveFormat(sampleRate, 32, 2, AudioEncoding.IeeeFloat));

            // Act
            // Assert
            Assert.That(() => mixer.AddTrack(sound), Throws.ArgumentException.With.Message.Contain("sample rate"));
        }
Esempio n. 9
0
        public void AddTrack_ShouldThrowException_WhenSampleSourceDoesNotHaveTwoChannels(int channels)
        {
            // Arrange
            var mixer = new Mixer();
            var sound = Substitute.For <ISampleSource>();

            sound.WaveFormat.Returns(new WaveFormat(44100, 32, channels, AudioEncoding.IeeeFloat));

            // Act
            // Assert
            Assert.That(() => mixer.AddTrack(sound), Throws.ArgumentException.With.Message.Contain("channels"));
        }
Esempio n. 10
0
        public void Dispose_ShouldDisposeAllAddedSampleSources()
        {
            // Arrange
            var mixer     = new Mixer();
            var soundData = GetRandomFloats();
            var sound1    = new TestSampleSource(soundData);
            var sound2    = new TestSampleSource(soundData);

            mixer.AddTrack(sound1);
            mixer.AddTrack(sound2);

            // Assume
            Assume.That(sound1.IsDisposed, Is.False);
            Assume.That(sound2.IsDisposed, Is.False);

            // Act
            mixer.Dispose();

            // Assert
            Assert.That(sound1.IsDisposed, Is.True);
            Assert.That(sound2.IsDisposed, Is.True);
        }
Esempio n. 11
0
        public void RemoveTrack_ShouldDisposeSampleSource()
        {
            // Arrange
            var mixer     = new Mixer();
            var soundData = GetRandomFloats();
            var sound     = new TestSampleSource(soundData);

            var track = mixer.AddTrack(sound);

            // Act
            mixer.RemoveTrack(track);

            // Assert
            Assert.That(sound.IsDisposed, Is.True);
        }
Esempio n. 12
0
        public void RemoveTrack_ShouldThrowException_WhenMixerDisposed()
        {
            // Arrange
            var mixer     = new Mixer();
            var soundData = GetRandomFloats();
            var sound     = new TestSampleSource(soundData);

            var track = mixer.AddTrack(sound);

            mixer.Dispose();

            // Act
            // Assert
            Assert.That(() => mixer.RemoveTrack(track), Throws.TypeOf <ObjectDisposedException>());
        }
Esempio n. 13
0
        public void Read_ShouldFillBufferWithZeroes_WhenTrackAddedButNotPlayed()
        {
            // Arrange
            var mixer     = new Mixer();
            var soundData = GetRandomFloats();
            var sound     = new TestSampleSource(soundData);

            mixer.AddTrack(sound);

            var buffer = new float[soundData.Length];

            // Act
            mixer.Read(buffer, 0, buffer.Length);

            // Assert
            Assert.That(buffer, Is.EqualTo(new float[buffer.Length]));
        }
Esempio n. 14
0
        public void Read_ShouldNotDisposeSampleSource_WhenThereIsMoreToReadFromIt()
        {
            // Arrange
            var mixer     = new Mixer();
            var soundData = GetRandomFloats();
            var sound     = new TestSampleSource(soundData);
            var track     = mixer.AddTrack(sound);

            track.Play();

            var buffer = new float[soundData.Length];

            // Act
            mixer.Read(buffer, 0, buffer.Length);

            // Assert
            Assert.That(sound.IsDisposed, Is.False);
        }
Esempio n. 15
0
        public void Read_ShouldFillBufferWithSoundDataFromTheOffset()
        {
            // Arrange
            var mixer     = new Mixer();
            var soundData = GetRandomFloats();
            var sound     = new TestSampleSource(soundData);
            var track     = mixer.AddTrack(sound);

            track.Play();

            var buffer = new float[soundData.Length];

            // Act
            mixer.Read(buffer, 123, 456);

            // Assert
            Assert.That(buffer.Take(123), Is.EqualTo(Enumerable.Repeat(0f, 123)));
            Assert.That(buffer.Skip(123).Take(456), Is.EqualTo(soundData.Take(456)));
            Assert.That(buffer.Skip(123).Skip(456), Is.EqualTo(Enumerable.Repeat(0f, buffer.Length - (123 + 456))));
        }
Esempio n. 16
0
        public void Read_ShouldStopTrack_WhenItHasCompleted()
        {
            // Arrange
            var mixer     = new Mixer();
            var soundData = GetRandomFloats();
            var sound     = new TestSampleSource(soundData);

            sound.Position = sound.Length;
            var track = mixer.AddTrack(sound);

            track.Play();

            var buffer = new float[soundData.Length];

            // Act
            mixer.Read(buffer, 0, buffer.Length);

            // Assert
            Assert.That(track.IsPlaying, Is.False);
            Assert.That(sound.Position, Is.Zero);
        }
Esempio n. 17
0
        public void TrackNotifiesWithEventWhenItIsDisposed()
        {
            // Arrange
            var mixer     = new Mixer();
            var soundData = GetRandomFloats();
            var sound     = new TestSampleSource(soundData);

            var track = mixer.AddTrack(sound);

            object?eventSender = null;

            track.Disposed += (sender, args) => { eventSender = sender; };

            // Assume
            Assume.That(eventSender, Is.Null);

            // Act
            mixer.RemoveTrack(track);

            // Assert
            Assert.That(eventSender, Is.EqualTo(track));
        }