Beispiel #1
0
        public void CheckFileEventsReceiving()
        {
            var filesToTest = TestFilesProvider.GetValidFiles(
                f => f.GetTrackChunks().Count() == 1,
                f => (TimeSpan)f.GetDuration <MetricTimeSpan>() < TimeSpan.FromSeconds(30))
                              .Take(5)
                              .ToArray();

            for (var i = 0; i < filesToTest.Length; i++)
            {
                var file     = filesToTest[i];
                var tempoMap = file.GetTempoMap();

                var eventsToSend = new List <EventToSend>();
                var currentTime  = TimeSpan.Zero;

                foreach (var timedEvent in file.GetTimedEvents().Where(e => !(e.Event is MetaEvent)))
                {
                    var time        = (TimeSpan)timedEvent.TimeAs <MetricTimeSpan>(tempoMap);
                    var eventToSend = new EventToSend(timedEvent.Event, time - currentTime);
                    currentTime = time;
                    eventsToSend.Add(eventToSend);
                }

                SendReceiveUtilities.CheckEventsReceiving(eventsToSend);
            }
        }
        private void CheckEventCallback(
            ICollection <EventToSend> eventsToSend,
            ICollection <ReceivedEvent> expectedReceivedEvents,
            TimeSpan changeCallbackAfter,
            EventCallback eventCallback,
            EventCallback secondEventCallback)
        {
            var playbackContext = new PlaybackContext();

            var receivedEvents = playbackContext.ReceivedEvents;
            var sentEvents     = playbackContext.SentEvents;
            var stopwatch      = playbackContext.Stopwatch;
            var tempoMap       = playbackContext.TempoMap;

            var eventsForPlayback = GetEventsForPlayback(eventsToSend, tempoMap);

            var notesStarted  = new List <Note>();
            var notesFinished = new List <Note>();

            using (var outputDevice = OutputDevice.GetByName(SendReceiveUtilities.DeviceToTestOnName))
            {
                SendReceiveUtilities.WarmUpDevice(outputDevice);
                outputDevice.EventSent += (_, e) => sentEvents.Add(new SentEvent(e.Event, stopwatch.Elapsed));

                using (var playback = new Playback(eventsForPlayback, tempoMap, outputDevice))
                {
                    playback.NotesPlaybackStarted  += (_, e) => notesStarted.AddRange(e.Notes);
                    playback.NotesPlaybackFinished += (_, e) => notesFinished.AddRange(e.Notes);

                    playback.EventCallback = eventCallback;

                    using (var inputDevice = InputDevice.GetByName(SendReceiveUtilities.DeviceToTestOnName))
                    {
                        inputDevice.EventReceived += (_, e) =>
                        {
                            lock (playbackContext.ReceivedEventsLockObject)
                            {
                                receivedEvents.Add(new ReceivedEvent(e.Event, stopwatch.Elapsed));
                            }
                        };

                        inputDevice.StartEventsListening();
                        stopwatch.Start();
                        playback.Start();

                        SpinWait.SpinUntil(() => stopwatch.Elapsed >= changeCallbackAfter);
                        playback.EventCallback = secondEventCallback;

                        var timeout         = TimeSpan.FromTicks(eventsToSend.Sum(e => e.Delay.Ticks)) + SendReceiveUtilities.MaximumEventSendReceiveDelay;
                        var playbackStopped = SpinWait.SpinUntil(() => !playback.IsRunning, timeout);
                        Assert.IsTrue(playbackStopped, "Playback is running after completed.");
                    }
                }
            }

            CompareReceivedEvents(receivedEvents, expectedReceivedEvents.ToList());
        }
Beispiel #3
0
        public void CheckEventsReceiving_AllEventTypes_ExceptSysEx()
        {
            var events = TypesProvider.GetAllEventTypes()
                         .Where(t => !typeof(MetaEvent).IsAssignableFrom(t) && !typeof(SysExEvent).IsAssignableFrom(t))
                         .Select(t => (MidiEvent)Activator.CreateInstance(t))
                         .ToArray();

            CollectionAssert.IsNotEmpty(events, "Events collection is empty.");
            SendReceiveUtilities.CheckEventsReceiving(events.Select(e => new EventToSend(e, TimeSpan.Zero)).ToArray());
        }
Beispiel #4
0
        private void CheckTrackControlValueWithOutputDevice(
            ICollection <EventToSend> eventsToSend,
            ICollection <EventToSend> eventsWillBeSent,
            TimeSpan moveFrom,
            TimeSpan moveTo)
        {
            var playbackContext = new PlaybackContext();

            var receivedEvents = playbackContext.ReceivedEvents;
            var sentEvents     = playbackContext.SentEvents;
            var stopwatch      = playbackContext.Stopwatch;
            var tempoMap       = playbackContext.TempoMap;

            var eventsForPlayback = GetEventsForPlayback(eventsToSend, tempoMap);
            var notes             = eventsForPlayback.GetNotes().ToArray();

            using (var outputDevice = OutputDevice.GetByName(SendReceiveUtilities.DeviceToTestOnName))
            {
                SendReceiveUtilities.WarmUpDevice(outputDevice);
                outputDevice.EventSent += (_, e) => sentEvents.Add(new SentEvent(e.Event, stopwatch.Elapsed));

                using (var playback = new Playback(eventsForPlayback, tempoMap, outputDevice))
                {
                    playback.TrackControlValue = true;

                    using (var inputDevice = InputDevice.GetByName(SendReceiveUtilities.DeviceToTestOnName))
                    {
                        inputDevice.EventReceived += (_, e) =>
                        {
                            lock (playbackContext.ReceivedEventsLockObject)
                            {
                                receivedEvents.Add(new ReceivedEvent(e.Event, stopwatch.Elapsed));
                            }
                        };
                        inputDevice.StartEventsListening();
                        stopwatch.Start();
                        playback.Start();

                        SpinWait.SpinUntil(() => stopwatch.Elapsed >= moveFrom);
                        playback.MoveToTime((MetricTimeSpan)moveTo);

                        var timeout           = TimeSpan.FromTicks(eventsWillBeSent.Sum(e => e.Delay.Ticks)) + SendReceiveUtilities.MaximumEventSendReceiveDelay;
                        var areEventsReceived = SpinWait.SpinUntil(() => receivedEvents.Count == eventsWillBeSent.Count, timeout);
                        Assert.IsTrue(areEventsReceived, $"Events are not received for timeout {timeout}.");

                        stopwatch.Stop();

                        var playbackStopped = SpinWait.SpinUntil(() => !playback.IsRunning, SendReceiveUtilities.MaximumEventSendReceiveDelay);
                        Assert.IsTrue(playbackStopped, "Playback is running after completed.");
                    }
                }
            }

            CompareSentReceivedEvents(sentEvents, receivedEvents, eventsWillBeSent.ToList());
        }
        public void SaveRecordingToFile()
        {
            var tempoMap = TempoMap.Default;

            var eventsToSend = new[]
            {
                new EventToSend(new NoteOnEvent(), TimeSpan.Zero),
                new EventToSend(new NoteOffEvent(), TimeSpan.FromMilliseconds(500)),
                new EventToSend(new ProgramChangeEvent((SevenBitNumber)40), TimeSpan.FromSeconds(5)),
                new EventToSend(new ActiveSensingEvent(), TimeSpan.FromMilliseconds(100)),
                new EventToSend(new ProgramChangeEvent((SevenBitNumber)50), TimeSpan.FromMilliseconds(500)),
            };

            using (var outputDevice = OutputDevice.GetByName(SendReceiveUtilities.DeviceToTestOnName))
            {
                SendReceiveUtilities.WarmUpDevice(outputDevice);

                using (var inputDevice = InputDevice.GetByName(SendReceiveUtilities.DeviceToTestOnName))
                {
                    var receivedEventsNumber = 0;

                    inputDevice.StartEventsListening();
                    inputDevice.EventReceived += (_, __) => receivedEventsNumber++;

                    using (var recording = new Recording(tempoMap, inputDevice))
                    {
                        var sendingThread = new Thread(() =>
                        {
                            SendReceiveUtilities.SendEvents(eventsToSend, outputDevice);
                        });

                        recording.Start();
                        sendingThread.Start();

                        SpinWait.SpinUntil(() => !sendingThread.IsAlive && receivedEventsNumber == eventsToSend.Length);
                        recording.Stop();

                        var midiFile    = recording.ToFile();
                        var timedEvents = midiFile.GetTimedEvents();

                        var expectedEvents = new[]
                        {
                            new TimedEvent(new NoteOnEvent(), TimeConverter.ConvertFrom((MetricTimeSpan)TimeSpan.Zero, tempoMap)),
                            new TimedEvent(new NoteOffEvent(), TimeConverter.ConvertFrom((MetricTimeSpan)TimeSpan.FromMilliseconds(500), tempoMap)),
                            new TimedEvent(new ProgramChangeEvent((SevenBitNumber)40), TimeConverter.ConvertFrom((MetricTimeSpan)TimeSpan.FromSeconds(5.5), tempoMap)),
                            new TimedEvent(new ProgramChangeEvent((SevenBitNumber)50), TimeConverter.ConvertFrom((MetricTimeSpan)TimeSpan.FromSeconds(6.1), tempoMap))
                        };

                        Assert.IsTrue(
                            TimedEventEquality.AreEqual(expectedEvents, timedEvents, false, 10),
                            "Timed events saved incorrectly.");
                    }
                }
            }
        }
Beispiel #6
0
        private static void CheckEventsReceiving(IReadOnlyList <EventToSend> eventsToSend)
        {
            var receivedEventsB = new List <ReceivedEvent>();
            var receivedEventsC = new List <ReceivedEvent>();
            var sentEvents      = new List <SentEvent>();
            var stopwatch       = new Stopwatch();

            using (var outputA = OutputDevice.GetByName(MidiDevicesNames.DeviceA))
            {
                SendReceiveUtilities.WarmUpDevice(outputA);
                outputA.EventSent += (_, e) => sentEvents.Add(new SentEvent(e.Event, stopwatch.Elapsed));

                using (var inputB = InputDevice.GetByName(MidiDevicesNames.DeviceB))
                    using (var inputC = InputDevice.GetByName(MidiDevicesNames.DeviceC))
                    {
                        inputB.EventReceived += (_, e) => receivedEventsB.Add(new ReceivedEvent(e.Event, stopwatch.Elapsed));
                        inputB.StartEventsListening();

                        inputC.EventReceived += (_, e) => receivedEventsC.Add(new ReceivedEvent(e.Event, stopwatch.Elapsed));
                        inputC.StartEventsListening();

                        using (var inputA = InputDevice.GetByName(MidiDevicesNames.DeviceA))
                        {
                            inputA.StartEventsListening();

                            using (var outputB = OutputDevice.GetByName(MidiDevicesNames.DeviceB))
                                using (var outputC = OutputDevice.GetByName(MidiDevicesNames.DeviceC))
                                {
                                    var devicesConnector = inputA.Connect(outputB, outputC);
                                    Assert.IsTrue(devicesConnector.AreDevicesConnected, "Devices aren't connected.");

                                    stopwatch.Start();
                                    SendReceiveUtilities.SendEvents(eventsToSend, outputA);
                                    stopwatch.Stop();

                                    var timeout           = TimeSpan.FromTicks(eventsToSend.Sum(e => e.Delay.Ticks)) + SendReceiveUtilities.MaximumEventSendReceiveDelay;
                                    var areEventsReceived = SpinWait.SpinUntil(
                                        () => receivedEventsB.Count == eventsToSend.Count && receivedEventsC.Count == eventsToSend.Count,
                                        timeout);
                                    Assert.IsTrue(areEventsReceived, $"Events are not received for timeout {timeout}.");

                                    devicesConnector.Disconnect();
                                    Assert.IsFalse(devicesConnector.AreDevicesConnected, "Devices aren't disconnected.");
                                }
                        }
                    }
            }

            SendReceiveUtilities.CompareSentReceivedEvents(eventsToSend, sentEvents, receivedEventsB, MaximumEventSendReceiveDelay);
            SendReceiveUtilities.CompareSentReceivedEvents(eventsToSend, sentEvents, receivedEventsC, MaximumEventSendReceiveDelay);
        }
Beispiel #7
0
 public void CheckEventsReceiving()
 {
     SendReceiveUtilities.CheckEventsReceiving(new[]
     {
         new EventToSend(new NoteOnEvent((SevenBitNumber)100, (SevenBitNumber)20)
         {
             Channel = (FourBitNumber)5
         }, TimeSpan.Zero),
         new EventToSend(new NormalSysExEvent(new byte[] { 1, 2, 3, 0xF7 }), TimeSpan.FromSeconds(1)),
         new EventToSend(new NoteOffEvent((SevenBitNumber)100, (SevenBitNumber)10)
         {
             Channel = (FourBitNumber)5
         }, TimeSpan.FromSeconds(2)),
         new EventToSend(new NormalSysExEvent(new byte[] { 4, 5, 6, 0xF7 }), TimeSpan.FromSeconds(2)),
         new EventToSend(new SongSelectEvent((SevenBitNumber)20), TimeSpan.Zero),
         new EventToSend(new TuneRequestEvent(), TimeSpan.FromMilliseconds(200)),
     });
 }
Beispiel #8
0
        public void CheckMidiTimeCodeEventReceiving()
        {
            MidiTimeCode midiTimeCodeReceived = null;

            var eventsToSend = new[]
            {
                new EventToSend(new ProgramChangeEvent((SevenBitNumber)100), TimeSpan.FromMilliseconds(500)),
                new EventToSend(new MidiTimeCodeEvent(MidiTimeCodeComponent.FramesLsb, (FourBitNumber)1), TimeSpan.FromSeconds(1)),
                new EventToSend(new ProgramChangeEvent((SevenBitNumber)70), TimeSpan.FromMilliseconds(500)),
                new EventToSend(new MidiTimeCodeEvent(MidiTimeCodeComponent.FramesMsb, (FourBitNumber)1), TimeSpan.FromSeconds(2)),
                new EventToSend(new MidiTimeCodeEvent(MidiTimeCodeComponent.HoursLsb, (FourBitNumber)7), TimeSpan.FromSeconds(1)),
                new EventToSend(new MidiTimeCodeEvent(MidiTimeCodeComponent.HoursMsbAndTimeCodeType, (FourBitNumber)7), TimeSpan.FromSeconds(2)),
                new EventToSend(new ProgramChangeEvent((SevenBitNumber)80), TimeSpan.FromMilliseconds(500)),
                new EventToSend(new MidiTimeCodeEvent(MidiTimeCodeComponent.MinutesLsb, (FourBitNumber)10), TimeSpan.FromSeconds(1)),
                new EventToSend(new ProgramChangeEvent((SevenBitNumber)10), TimeSpan.FromMilliseconds(500)),
                new EventToSend(new ProgramChangeEvent((SevenBitNumber)15), TimeSpan.FromMilliseconds(500)),
                new EventToSend(new MidiTimeCodeEvent(MidiTimeCodeComponent.MinutesMsb, (FourBitNumber)2), TimeSpan.FromSeconds(2)),
                new EventToSend(new MidiTimeCodeEvent(MidiTimeCodeComponent.SecondsLsb, (FourBitNumber)10), TimeSpan.FromSeconds(1)),
                new EventToSend(new ProgramChangeEvent((SevenBitNumber)40), TimeSpan.FromMilliseconds(500)),
                new EventToSend(new MidiTimeCodeEvent(MidiTimeCodeComponent.SecondsMsb, (FourBitNumber)1), TimeSpan.FromSeconds(2))
            };

            using (var outputDevice = OutputDevice.GetByName(MidiDevicesNames.DeviceA))
                using (var inputDevice = InputDevice.GetByName(MidiDevicesNames.DeviceA))
                {
                    inputDevice.MidiTimeCodeReceived += (_, e) => midiTimeCodeReceived = new MidiTimeCode(e.Format, e.Hours, e.Minutes, e.Seconds, e.Frames);
                    inputDevice.StartEventsListening();

                    SendReceiveUtilities.SendEvents(eventsToSend, outputDevice);

                    var timeout = TimeSpan.FromTicks(eventsToSend.Sum(e => e.Delay.Ticks)) + SendReceiveUtilities.MaximumEventSendReceiveDelay;
                    var isMidiTimeCodeReceived = SpinWait.SpinUntil(() => midiTimeCodeReceived != null, timeout);
                    Assert.IsTrue(isMidiTimeCodeReceived, $"MIDI time code received for timeout {timeout}.");

                    inputDevice.StopEventsListening();
                }

            Assert.AreEqual(MidiTimeCodeType.Thirty, midiTimeCodeReceived.Format, "Format is invalid.");
            Assert.AreEqual(23, midiTimeCodeReceived.Hours, "Hours number is invalid.");
            Assert.AreEqual(42, midiTimeCodeReceived.Minutes, "Minutes number is invalid.");
            Assert.AreEqual(26, midiTimeCodeReceived.Seconds, "Seconds number is invalid.");
            Assert.AreEqual(17, midiTimeCodeReceived.Frames, "Frames number is invalid.");
        }
Beispiel #9
0
        public void GetDuration(TimeSpan start, TimeSpan delayFromStart)
        {
            var eventsToSend = new[]
            {
                new EventToSend(new NoteOnEvent(), start),
                new EventToSend(new NoteOffEvent(), delayFromStart)
            };

            var receivedEvents = new List <ReceivedEvent>();
            var stopwatch      = new Stopwatch();

            using (var outputDevice = OutputDevice.GetByName(SendReceiveUtilities.DeviceToTestOnName))
            {
                SendReceiveUtilities.WarmUpDevice(outputDevice);

                using (var inputDevice = InputDevice.GetByName(SendReceiveUtilities.DeviceToTestOnName))
                {
                    inputDevice.StartEventsListening();
                    inputDevice.EventReceived += (_, e) => receivedEvents.Add(new ReceivedEvent(e.Event, stopwatch.Elapsed));

                    using (var recording = new Recording(TempoMap.Default, inputDevice))
                    {
                        recording.Start();
                        stopwatch.Start();
                        SendReceiveUtilities.SendEvents(eventsToSend, outputDevice);

                        var timeout           = start + delayFromStart + SendReceiveUtilities.MaximumEventSendReceiveDelay;
                        var areEventsReceived = SpinWait.SpinUntil(() => receivedEvents.Count == eventsToSend.Length, timeout);
                        Assert.IsTrue(areEventsReceived, $"Events are not received for timeout {timeout}.");

                        recording.Stop();
                        Assert.IsFalse(recording.IsRunning, "Recording is running after stop.");

                        TimeSpan duration = recording.GetDuration <MetricTimeSpan>();
                        Assert.IsTrue(
                            AreTimeSpansEqual(duration, start + delayFromStart),
                            $"Duration is invalid. Actual is {duration}. Expected is {start + delayFromStart}.");
                    }
                }
            }
        }
        private void CheckPlaybackStop(
            ICollection <EventToSend> eventsToSend,
            ICollection <EventToSend> eventsWillBeSent,
            TimeSpan stopAfter,
            TimeSpan stopPeriod,
            PlaybackAction setupPlayback,
            PlaybackAction afterStart,
            PlaybackAction afterStop,
            PlaybackAction afterResume,
            IEnumerable <Tuple <TimeSpan, PlaybackAction> > runningAfterResume = null,
            ICollection <TimeSpan> explicitExpectedTimes = null,
            double speed = 1.0,
            ICollection <ReceivedEvent> expectedReceivedEvents = null,
            ICollection <ReceivedEvent> expectedPlayedEvents   = null)
        {
            var playbackContext = new PlaybackContext();

            var playedEvents   = new List <ReceivedEvent>();
            var receivedEvents = playbackContext.ReceivedEvents;
            var sentEvents     = playbackContext.SentEvents;
            var stopwatch      = playbackContext.Stopwatch;
            var tempoMap       = playbackContext.TempoMap;

            var eventsForPlayback = GetEventsForPlayback(eventsToSend, tempoMap);
            var expectedTimes     = playbackContext.ExpectedTimes;

            if (explicitExpectedTimes != null || expectedReceivedEvents != null || expectedPlayedEvents != null)
            {
                expectedTimes.AddRange(explicitExpectedTimes ?? (expectedReceivedEvents?.Select(e => e.Time) ?? expectedPlayedEvents.Select(e => e.Time)));
            }
            else
            {
                var currentTime = TimeSpan.Zero;

                foreach (var eventWillBeSent in eventsWillBeSent)
                {
                    currentTime += eventWillBeSent.Delay;
                    var scaledCurrentTime = ScaleTimeSpan(currentTime, 1.0 / speed);
                    expectedTimes.Add(currentTime > stopAfter ? scaledCurrentTime + stopPeriod : scaledCurrentTime);
                }
            }

            using (var outputDevice = OutputDevice.GetByName(SendReceiveUtilities.DeviceToTestOnName))
            {
                SendReceiveUtilities.WarmUpDevice(outputDevice);
                outputDevice.EventSent += (_, e) => sentEvents.Add(new SentEvent(e.Event, stopwatch.Elapsed));

                using (var playback = new Playback(eventsForPlayback, tempoMap, outputDevice))
                {
                    playback.Speed = speed;
                    setupPlayback(playbackContext, playback);

                    if (expectedPlayedEvents != null)
                    {
                        playback.EventPlayed += (_, e) => playedEvents.Add(new ReceivedEvent(e.Event, stopwatch.Elapsed));
                    }

                    using (var inputDevice = InputDevice.GetByName(SendReceiveUtilities.DeviceToTestOnName))
                    {
                        inputDevice.EventReceived += (_, e) =>
                        {
                            lock (playbackContext.ReceivedEventsLockObject)
                            {
                                receivedEvents.Add(new ReceivedEvent(e.Event, stopwatch.Elapsed));
                            }
                        };
                        inputDevice.StartEventsListening();
                        stopwatch.Start();
                        playback.Start();

                        afterStart(playbackContext, playback);

                        SpinWait.SpinUntil(() => stopwatch.Elapsed >= stopAfter);
                        playback.Stop();

                        afterStop(playbackContext, playback);

                        Thread.Sleep(stopPeriod);
                        playback.Start();

                        afterResume(playbackContext, playback);

                        if (runningAfterResume != null)
                        {
                            foreach (var check in runningAfterResume)
                            {
                                Thread.Sleep(check.Item1);
                                check.Item2(playbackContext, playback);
                            }
                        }

                        var timeout           = expectedTimes.Last() + SendReceiveUtilities.MaximumEventSendReceiveDelay;
                        var areEventsReceived = SpinWait.SpinUntil(() => receivedEvents.Count == expectedTimes.Count, timeout);
                        Assert.IsTrue(areEventsReceived, $"Events are not received for timeout {timeout}.");

                        stopwatch.Stop();

                        var playbackStopped = SpinWait.SpinUntil(() => !playback.IsRunning, SendReceiveUtilities.MaximumEventSendReceiveDelay);
                        Assert.IsTrue(playbackStopped, "Playback is running after completed.");
                    }
                }
            }

            CompareSentReceivedEvents(sentEvents, receivedEvents, expectedTimes);

            if (expectedReceivedEvents != null)
            {
                CompareReceivedEvents(receivedEvents, expectedReceivedEvents.ToList());
            }

            if (expectedPlayedEvents != null)
            {
                CompareReceivedEvents(playedEvents, expectedPlayedEvents.ToList());
            }
        }
        private void CheckPlayback(
            ICollection <EventToSend> eventsToSend,
            double speed,
            PlaybackAction beforePlaybackStarted,
            PlaybackAction startPlayback,
            PlaybackAction afterPlaybackStarted,
            PlaybackAction waiting,
            PlaybackAction finalChecks,
            CreateTickGeneratorCallback createTickGeneratorCallback = null)
        {
            var playbackContext = new PlaybackContext();

            var receivedEvents = playbackContext.ReceivedEvents;
            var sentEvents     = playbackContext.SentEvents;
            var stopwatch      = playbackContext.Stopwatch;
            var tempoMap       = playbackContext.TempoMap;

            var eventsForPlayback = new List <MidiEvent>();
            var expectedTimes     = playbackContext.ExpectedTimes;
            var currentTime       = TimeSpan.Zero;

            foreach (var eventToSend in eventsToSend.Where(e => !(e.Event is MetaEvent)))
            {
                var midiEvent = eventToSend.Event.Clone();
                midiEvent.DeltaTime = LengthConverter.ConvertFrom((MetricTimeSpan)eventToSend.Delay, (MetricTimeSpan)currentTime, tempoMap);
                currentTime        += eventToSend.Delay;
                eventsForPlayback.Add(midiEvent);
                expectedTimes.Add(TimeSpan.FromTicks(MathUtilities.RoundToLong(currentTime.Ticks / speed)));
            }

            using (var outputDevice = OutputDevice.GetByName(SendReceiveUtilities.DeviceToTestOnName))
            {
                SendReceiveUtilities.WarmUpDevice(outputDevice);
                outputDevice.EventSent += (_, e) => sentEvents.Add(new SentEvent(e.Event, stopwatch.Elapsed));

                var clockSettings = createTickGeneratorCallback != null
                    ? new MidiClockSettings {
                    CreateTickGeneratorCallback = createTickGeneratorCallback
                }
                    : null;

                using (var playback = new Playback(eventsForPlayback, tempoMap, outputDevice, clockSettings))
                {
                    playback.Speed = speed;
                    beforePlaybackStarted(playbackContext, playback);

                    using (var inputDevice = InputDevice.GetByName(SendReceiveUtilities.DeviceToTestOnName))
                    {
                        inputDevice.EventReceived += (_, e) =>
                        {
                            lock (playbackContext.ReceivedEventsLockObject)
                            {
                                receivedEvents.Add(new ReceivedEvent(e.Event, stopwatch.Elapsed));
                            }
                        };
                        inputDevice.StartEventsListening();
                        stopwatch.Start();

                        startPlayback(playbackContext, playback);
                        afterPlaybackStarted(playbackContext, playback);
                        waiting(playbackContext, playback);

                        stopwatch.Stop();

                        finalChecks(playbackContext, playback);
                    }
                }
            }

            CompareSentReceivedEvents(sentEvents.Take(expectedTimes.Count).ToList(), receivedEvents.Take(expectedTimes.Count).ToList(), expectedTimes);
        }
Beispiel #12
0
        public void PlayRecordedData()
        {
            var tempoMap = TempoMap.Default;

            var eventsToSend = new[]
            {
                new EventToSend(new NoteOnEvent(), TimeSpan.Zero),
                new EventToSend(new NoteOffEvent(), TimeSpan.FromMilliseconds(500)),
                new EventToSend(new ProgramChangeEvent((SevenBitNumber)40), TimeSpan.FromSeconds(5))
            };

            MidiFile recordedFile = null;

            using (var outputDevice = OutputDevice.GetByName(SendReceiveUtilities.DeviceToTestOnName))
            {
                SendReceiveUtilities.WarmUpDevice(outputDevice);

                using (var inputDevice = InputDevice.GetByName(SendReceiveUtilities.DeviceToTestOnName))
                {
                    var receivedEventsNumber = 0;

                    inputDevice.StartEventsListening();
                    inputDevice.EventReceived += (_, __) => receivedEventsNumber++;

                    using (var recording = new Recording(tempoMap, inputDevice))
                    {
                        var sendingThread = new Thread(() =>
                        {
                            SendReceiveUtilities.SendEvents(eventsToSend, outputDevice);
                        });

                        recording.Start();
                        sendingThread.Start();

                        SpinWait.SpinUntil(() => !sendingThread.IsAlive && receivedEventsNumber == eventsToSend.Length);
                        recording.Stop();

                        recordedFile = recording.ToFile();
                    }
                }
            }

            CheckPlayback(
                eventsToSend,
                1.0,
                beforePlaybackStarted: NoPlaybackAction,
                startPlayback: (context, playback) => playback.Start(),
                afterPlaybackStarted: NoPlaybackAction,
                waiting: (context, playback) =>
            {
                var timeout           = context.ExpectedTimes.Last() + SendReceiveUtilities.MaximumEventSendReceiveDelay;
                var areEventsReceived = SpinWait.SpinUntil(() => context.ReceivedEvents.Count == eventsToSend.Length, timeout);
                Assert.IsTrue(areEventsReceived, $"Events are not received for timeout {timeout}.");
            },
                finalChecks: (context, playback) =>
            {
                var playbackStopped = SpinWait.SpinUntil(() => !playback.IsRunning, SendReceiveUtilities.MaximumEventSendReceiveDelay);
                Assert.IsTrue(playbackStopped, "Playback is running after completed.");
            },
                createPlayback: (outputDevice, clockSettings) => recordedFile.GetPlayback(outputDevice, clockSettings));
        }
Beispiel #13
0
        public void CheckRecording()
        {
            var tempoMap = TempoMap.Default;

            var stopAfter  = TimeSpan.FromSeconds(1);
            var stopPeriod = TimeSpan.FromSeconds(2);

            var eventsToSend = new[]
            {
                new EventToSend(new NoteOnEvent(), TimeSpan.Zero),
                new EventToSend(new NoteOffEvent(), TimeSpan.FromMilliseconds(500)),
                new EventToSend(new TimingClockEvent(), TimeSpan.FromSeconds(5))
            };

            var sentEvents     = new List <SentEvent>();
            var receivedEvents = new List <ReceivedEvent>();
            var stopwatch      = new Stopwatch();

            var expectedTimes         = new List <TimeSpan>();
            var expectedRecordedTimes = new List <TimeSpan>();
            var currentTime           = TimeSpan.Zero;

            foreach (var eventToSend in eventsToSend)
            {
                currentTime += eventToSend.Delay;
                expectedTimes.Add(currentTime);
                expectedRecordedTimes.Add(currentTime > stopAfter ? currentTime - stopPeriod : currentTime);
            }

            using (var outputDevice = OutputDevice.GetByName(SendReceiveUtilities.DeviceToTestOnName))
            {
                SendReceiveUtilities.WarmUpDevice(outputDevice);
                outputDevice.EventSent += (_, e) => sentEvents.Add(new SentEvent(e.Event, stopwatch.Elapsed));

                using (var inputDevice = InputDevice.GetByName(SendReceiveUtilities.DeviceToTestOnName))
                {
                    inputDevice.StartEventsListening();
                    inputDevice.EventReceived += (_, e) => receivedEvents.Add(new ReceivedEvent(e.Event, stopwatch.Elapsed));

                    using (var recording = new Recording(tempoMap, inputDevice))
                    {
                        var sendingThread = new Thread(() =>
                        {
                            SendReceiveUtilities.SendEvents(eventsToSend, outputDevice);
                        });

                        stopwatch.Start();
                        recording.Start();
                        sendingThread.Start();
                        Thread.Sleep(stopAfter);

                        recording.Stop();
                        Thread.Sleep(stopPeriod);

                        recording.Start();

                        var timeout           = expectedTimes.Last() + SendReceiveUtilities.MaximumEventSendReceiveDelay;
                        var areEventsReceived = SpinWait.SpinUntil(() => receivedEvents.Count == expectedTimes.Count, timeout);
                        Assert.IsTrue(areEventsReceived, $"Events are not received for timeout {timeout}.");

                        CompareSentReceivedEvents(sentEvents, receivedEvents, expectedTimes);

                        var recordedEvents = recording.GetEvents();
                        CheckRecordedEvents(recordedEvents, expectedRecordedTimes, tempoMap);
                    }
                }
            }
        }