public void OnFrameCompletedWorksWithMultiplePulsesAndOverflow(int[] tacts, float[] samples)
        {
            // --- Arrange
            var spectrum     = new SpectrumBeepTestMachine();
            var beeperDevice = new BeeperDevice();

            beeperDevice.OnAttachedToVm(spectrum);
            var initialBit = false;

            // --- Act
            foreach (var tact in tacts)
            {
                spectrum.SetCurrentCpuTact(tact);
                beeperDevice.ProcessEarBitValue(false, initialBit);
                initialBit = !initialBit;
            }
            spectrum.SetCurrentCpuTact(69888 + 11);
            beeperDevice.OnFrameCompleted();

            // --- Assert
            beeperDevice.LastEarBit.ShouldBe(!initialBit);
            beeperDevice.FrameCount.ShouldBe(0);
            beeperDevice.Overflow.ShouldBe(11);
            beeperDevice.LastSampleTact.ShouldBe(beeperDevice.HostVm.AudioConfiguration.TactsPerSample * 699);
            for (var i = 0; i < samples.Length; i++)
            {
                samples[i].ShouldBe(beeperDevice.AudioSamples[i]);
            }
            var remainingSample = beeperDevice.LastEarBit ? 1.0f : 0.0f;

            for (var i = samples.Length; i < 699; i++)
            {
                beeperDevice.AudioSamples[i].ShouldBe(remainingSample);
            }
        }
        public void OnNewFrameCreatesOverflowSample(int overflow, int[] tacts, float?sample)
        {
            // --- Arrange
            var spectrum     = new SpectrumBeepTestMachine();
            var beeperDevice = new BeeperDevice();

            beeperDevice.OnAttachedToVm(spectrum);
            var initialBit = false;

            // --- Act
            foreach (var tact in tacts)
            {
                spectrum.SetCurrentCpuTact(tact);
                beeperDevice.ProcessEarBitValue(false, initialBit);
                initialBit = !initialBit;
            }
            spectrum.SetCurrentCpuTact(69888 + overflow);
            beeperDevice.OnFrameCompleted();
            var overflowBefore = beeperDevice.Overflow;

            beeperDevice.OnNewFrame();

            // --- Assert
            beeperDevice.SamplesIndex.ShouldBe(sample == null ? 0 : 1);
            overflowBefore.ShouldBe(overflow);
            beeperDevice.Overflow.ShouldBe(0);
            if (sample.HasValue)
            {
                beeperDevice.AudioSamples[0].ShouldBe(sample.Value);
            }
        }
        public void OnFrameCompletedWorksWithLowPulseAfterTheFirsttact()
        {
            // --- Arrange
            var spectrum     = new SpectrumBeepTestMachine();
            var beeperDevice = new BeeperDevice();

            beeperDevice.OnAttachedToVm(spectrum);

            // --- Act
            spectrum.SetCurrentFrameTact(100);
            beeperDevice.ProcessEarBitValue(false, false);
            spectrum.SetCurrentFrameTact(spectrum.FrameTacts);
            beeperDevice.OnFrameCompleted();

            // --- Assert
            beeperDevice.FrameCount.ShouldBe(0);
            beeperDevice.Pulses.Count.ShouldBe(2);
            beeperDevice.LastEarBit.ShouldBeFalse();
            beeperDevice.LastPulseTact.ShouldBe(100);
            var pulse1 = beeperDevice.Pulses[0];

            pulse1.EarBit.ShouldBeTrue();
            pulse1.Lenght.ShouldBe(100);
            var pulse2 = beeperDevice.Pulses[1];

            pulse2.EarBit.ShouldBeFalse();
            pulse2.Lenght.ShouldBe(spectrum.FrameTacts - 100);
        }
        public void SampleLengthIsCalculatedProperly(int frames, int[] lenghts)
        {
            // --- Arrange
            var spectrum     = new SpectrumBeepTestMachine();
            var beeperDevice = new BeeperDevice();

            beeperDevice.OnAttachedToVm(spectrum);

            // --- Act/Assert
            for (var i = 1; i <= frames; i++)
            {
                spectrum.SetCurrentCpuTact(69888 * i);
                beeperDevice.OnFrameCompleted();
                beeperDevice.AudioSamples.Length.ShouldBe(lenghts[i - 1]);
                beeperDevice.OnNewFrame();
            }
        }
        public void OnFrameCompletedWorksWithNoPulse()
        {
            // --- Arrange
            var spectrum     = new SpectrumBeepTestMachine();
            var beeperDevice = new BeeperDevice();

            beeperDevice.OnAttachedToVm(spectrum);

            // --- Act
            beeperDevice.OnFrameCompleted();

            // --- Assert
            beeperDevice.FrameCount.ShouldBe(0);
            beeperDevice.Pulses.Count.ShouldBe(1);
            beeperDevice.LastEarBit.ShouldBeTrue();
            beeperDevice.LastPulseTact.ShouldBe(0);
        }
        public void OnNewFrameKeepsLastEarBitValue()
        {
            // --- Arrange
            var spectrum     = new SpectrumBeepTestMachine();
            var beeperDevice = new BeeperDevice();

            beeperDevice.OnAttachedToVm(spectrum);

            // --- Act
            spectrum.SetCurrentFrameTact(100);
            beeperDevice.ProcessEarBitValue(false, false);
            spectrum.SetCurrentFrameTact(spectrum.FrameTacts);
            beeperDevice.OnFrameCompleted();
            beeperDevice.OnNewFrame();

            // --- Assert
            beeperDevice.FrameCount.ShouldBe(1);
            beeperDevice.Pulses.Count.ShouldBe(0);
            beeperDevice.LastEarBit.ShouldBeFalse();
            beeperDevice.LastPulseTact.ShouldBe(0);
        }
        public void OnFrameCompletedWorksWithNoPulseEndOverflow()
        {
            // --- Arrange
            var spectrum     = new SpectrumBeepTestMachine();
            var beeperDevice = new BeeperDevice();

            beeperDevice.OnAttachedToVm(spectrum);

            // --- Act
            spectrum.SetCurrentCpuTact(69888 + 11);
            beeperDevice.OnFrameCompleted();

            // --- Assert
            beeperDevice.LastEarBit.ShouldBeTrue();
            beeperDevice.FrameCount.ShouldBe(0);
            beeperDevice.Overflow.ShouldBe(11);
            beeperDevice.AudioSamples.Length.ShouldBe(699);

            foreach (var sample in beeperDevice.AudioSamples)
            {
                sample.ShouldBe(1.0f);
            }
        }