public Form1() { InitializeComponent(); String[] Negras = { "37", "39", "_", "42", "44", "46", "_", "49", "51", "_", "54", "56", "58", "_", "61", "63", "_", "66", "68", "70", "_", "73", "75", "_", "78", "80", "82", "_", }; String[] blancas = { "36", "38", "40", "41", "43", "45", "47", "48", "50", "52", "53", "55", "57", "59", "60", "62", "64", "65", "67", "69", "71", "72", "74", "76", "77", "79", "81", "83" }; String[] total = new string[blancas.Length + Negras.Length]; string[] negrasReturn = teclado.CrearTeclasNegras(Negras, panel1); string[] blancasReturn = teclado.CrearTeclasBlancas(blancas, panel1); string[] notasTotal = negrasReturn.Concat(blancasReturn).ToArray(); try { TextBox.CheckForIllegalCrossThreadCalls = false; Globals.inputDevice = InputDevice.GetByName("CASIO USB-MIDI"); Globals.inputDevice.EventReceived += OnEventReceived; Globals.inputDevice.StartEventsListening(); } catch { Console.WriteLine("uwu"); } }
public static int Connect(string name, KeyController _keyController) { wetMidiKeyboard = InputDevice.GetByName(name); { try { wetMidiKeyboard.EventReceived += MidiKeyboard_EventReceived; wetMidiKeyboard.StartEventsListening(); cts = new CancellationTokenSource(); kc = _keyController; kc.UpdateKeyMap(); Task.Run(() => { //var keyPlayLists = mtk.ArrangeKeyPlays(mtk.Index); NoteProcess(cts.Token); }, cts.Token); } catch (Exception e) { MessageBox.Show($"连接错误 \r\n {e.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } return(0); }
public MidiConnection() { inputDevice = InputDevice.GetByName("X-TOUCH MINI"); inputDevice.EventReceived += OnEventReceived; inputDevice.StartEventsListening(); outputDevice = OutputDevice.GetByName("X-TOUCH MINI"); }
public void DisableEnableInputDevice() { using (var outputDevice = OutputDevice.GetByName(MidiDevicesNames.DeviceA)) using (var inputDevice = InputDevice.GetByName(MidiDevicesNames.DeviceA)) { Assert.IsTrue(inputDevice.IsEnabled, "Device is not enabled initially."); var receivedEventsCount = 0; inputDevice.StartEventsListening(); inputDevice.EventReceived += (_, __) => receivedEventsCount++; outputDevice.SendEvent(new NoteOnEvent()); var eventReceived = SpinWait.SpinUntil(() => receivedEventsCount == 1, SendReceiveUtilities.MaximumEventSendReceiveDelay); Assert.IsTrue(eventReceived, "Event is not received."); inputDevice.IsEnabled = false; Assert.IsFalse(inputDevice.IsEnabled, "Device is enabled after disabling."); outputDevice.SendEvent(new NoteOnEvent()); eventReceived = SpinWait.SpinUntil(() => receivedEventsCount > 1, TimeSpan.FromSeconds(5)); Assert.IsFalse(eventReceived, "Event is received after device disabled."); inputDevice.IsEnabled = true; Assert.IsTrue(inputDevice.IsEnabled, "Device is disabled after enabling."); outputDevice.SendEvent(new NoteOnEvent()); eventReceived = SpinWait.SpinUntil(() => receivedEventsCount > 1, SendReceiveUtilities.MaximumEventSendReceiveDelay); Assert.IsTrue(eventReceived, "Event is not received after enabling again."); } }
public FaderPort() { _inputDevice = InputDevice.GetByName("FaderPort"); _outputDevice = OutputDevice.GetByName("FaderPort"); if (_inputDevice == null) { throw new InvalidOperationException("Unable to connect to FaderPort input device."); } if (_outputDevice == null) { throw new InvalidOperationException("Unable to connect to FaderPort output device."); } _inputDevice.EventReceived += OnInputEvent; _inputDevice.StartEventsListening(); _outputDevice.PrepareForEventsSending(); var nativeEvent = new NoteOnEvent((SevenBitNumber)0, (SevenBitNumber)0x64) { Channel = (FourBitNumber)1 }; Console.WriteLine("Switching FaderPort to native mode."); _outputDevice.SendEvent(nativeEvent); ClearAllLights(); _cts = new CancellationTokenSource(); _blinkerThread = new Thread(BlinkerMain); _blinkerThread.Start(); }
/// <summary> /// /// </summary> public static void SetInputMidiDeviceA(string deviceName) { if (inputDeviceA != null && inputDeviceA.Name.Equals(deviceName)) { return; } if (inputDeviceB != null && inputDeviceB.Name.Equals(deviceName)) { return; } if (inputDeviceA != null) { try { inputDeviceA.StopEventsListening(); } catch { } finally { inputDeviceA.Dispose(); } } Settings.Default.MidiIF = deviceName; inputDeviceA = InputDevice.GetByName(deviceName); if (inputDeviceA != null) { inputDeviceA.EventReceived += midiEventReceivedA; inputDeviceA.StartEventsListening(); } }
public void SendEvent_MidiEvent() { var midiEvent = new NoteOnEvent((SevenBitNumber)45, (SevenBitNumber)89) { Channel = (FourBitNumber)6 }; using (var outputDevice = OutputDevice.GetByName(MidiDevicesNames.DeviceA)) { MidiEvent eventSent = null; outputDevice.EventSent += (_, e) => eventSent = e.Event; using (var inputDevice = InputDevice.GetByName(MidiDevicesNames.DeviceA)) { MidiEvent eventReceived = null; inputDevice.EventReceived += (_, e) => eventReceived = e.Event; inputDevice.StartEventsListening(); outputDevice.PrepareForEventsSending(); outputDevice.SendEvent(new NoteOnEvent((SevenBitNumber)45, (SevenBitNumber)89) { Channel = (FourBitNumber)6 }); var timeout = TimeSpan.FromMilliseconds(15); var isEventSentReceived = SpinWait.SpinUntil(() => eventSent != null && eventReceived != null, timeout); Assert.IsTrue(isEventSentReceived, "Event either not sent ot not received."); MidiAsserts.AreEventsEqual(midiEvent, eventSent, false, "Sent event is invalid."); MidiAsserts.AreEventsEqual(eventSent, eventReceived, false, "Received event is invalid."); } } }
public static void CheckEventsReceiving(IReadOnlyList <EventToSend> eventsToSend) { var receivedEvents = new List <ReceivedEvent>(); var sentEvents = new List <SentEvent>(); var stopwatch = new Stopwatch(); using (var outputDevice = OutputDevice.GetByName(DeviceToTestOnName)) { WarmUpDevice(outputDevice); outputDevice.EventSent += (_, e) => sentEvents.Add(new SentEvent(e.Event, stopwatch.Elapsed)); using (var inputDevice = InputDevice.GetByName(DeviceToTestOnName)) { inputDevice.EventReceived += (_, e) => receivedEvents.Add(new ReceivedEvent(e.Event, stopwatch.Elapsed)); inputDevice.StartEventsListening(); stopwatch.Start(); SendEvents(eventsToSend, outputDevice); stopwatch.Stop(); var timeout = TimeSpan.FromTicks(eventsToSend.Sum(e => e.Delay.Ticks)) + MaximumEventSendReceiveDelay; var areEventsReceived = SpinWait.SpinUntil(() => receivedEvents.Count == eventsToSend.Count, timeout); Assert.IsTrue(areEventsReceived, $"Events are not received for timeout {timeout}."); inputDevice.StopEventsListening(); } } CompareSentReceivedEvents(eventsToSend, sentEvents, receivedEvents); }
static void Redirect() { var input = "Sentive Forwarder"; _port = new TeVirtualMIDI(input); Thread thread = new Thread(RedirectorRun); thread.Start(); _outputDevice = OutputDevice.GetAll().First(t => t.Name.Contains("POP")); _outputDevice.SendEvent(new NormalSysExEvent(PDevice.BuildPianoVerifyPacket().AddTimestamp())); _outputDevice.SendEvent(new NormalSysExEvent(PDevice.BuildQueryPacket().AddTimestamp())); var inputDevice = InputDevice.GetByName(input); inputDevice.EventReceived += InputDeviceOnEventReceived; inputDevice.StartEventsListening(); //_outputDevice.EventSent += OnEventSent; DevicesConnector connector = new DevicesConnector(inputDevice, _outputDevice); connector.Connect(); System.Console.WriteLine("Running..."); System.Console.ReadLine(); inputDevice.StopEventsListening(); connector.Disconnect(); connector.Dispose(); inputDevice.Dispose(); _outputDevice.Dispose(); thread.Abort(); _port.shutdown(); System.Console.WriteLine("Stopped!"); System.Console.ReadLine(); }
public void StartRecording_DeviceNotListeningEvents() { using (var inputDevice = InputDevice.GetByName(SendReceiveUtilities.DeviceToTestOnName)) using (var recording = new Recording(TempoMap.Default, inputDevice)) { Assert.Throws <InvalidOperationException>(() => recording.Start(), "Recording started on device which is not listening events."); } }
public void Setup() { _outputDevice = OutputDevice.GetByName("MIDI A"); _outputDevice.EventSent += (_, e) => _sent = true; _inputDevice = InputDevice.GetByName("MIDI A"); _inputDevice.EventReceived += (_, e) => _received = true; _inputDevice.StartEventsListening(); }
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()); }
public void InputDeviceIsReleasedByDispose() { for (var i = 0; i < 10; i++) { var inputDevice = InputDevice.GetByName(MidiDevicesNames.DeviceA); Assert.DoesNotThrow(() => inputDevice.StartEventsListening()); inputDevice.Dispose(); } }
public void GlobalSetup() { _event = GetMidiEvent(); _outputDevice = OutputDevice.GetByName(MidiDevicesNames.DeviceA); _inputDevice = InputDevice.GetByName(MidiDevicesNames.DeviceA); _inputDevice.EventReceived += OnEventReceived; _inputDevice.StartEventsListening(); }
public void Setup() { _outputDevice = OutputDevice.GetByName(MidiDevicesNames.DeviceA); _outputDevice.PrepareForEventsSending(); _outputDevice.EventSent += (_, e) => _sent++; _inputDevice = InputDevice.GetByName(MidiDevicesNames.DeviceA); _inputDevice.EventReceived += (_, e) => _received++; _inputDevice.StartEventsListening(); }
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."); } } } }
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 InputDeviceIsInUse() { using (var inputDevice1 = InputDevice.GetByName(MidiDevicesNames.DeviceA)) { inputDevice1.StartEventsListening(); using (var inputDevice2 = InputDevice.GetByName(MidiDevicesNames.DeviceA)) { Assert.Throws <MidiDeviceException>(() => inputDevice2.StartEventsListening()); } } }
public void CheckInputDeviceId(string deviceName) { var device = InputDevice.GetByName(deviceName); Assert.IsNotNull(device, $"Unable to get device '{deviceName}' by its name."); var deviceId = device.Id; device = InputDevice.GetById(deviceId); Assert.IsNotNull(device, $"Unable to get device '{deviceName}' by its ID."); Assert.AreEqual(deviceName, device.Name, "Device retrieved by ID is not the same as retrieved by its name."); }
public void InputDeviceIsReleasedByFinalizer() { for (var i = 0; i < 10; i++) { { var inputDevice = InputDevice.GetByName(MidiDevicesNames.DeviceA); Assert.DoesNotThrow(() => inputDevice.StartEventsListening()); } GC.Collect(); GC.WaitForPendingFinalizers(); } }
private void MidiEventListener_DoWork(object sender, DoWorkEventArgs e) { if ("Midi events from network".Equals(inputDeviceName, System.StringComparison.InvariantCulture)) { try { NetworkComms.AppendGlobalIncomingPacketHandler <byte[]>("midiEvent", OnNetworkEvent); Connection.StartListening(ConnectionType.TCP, new System.Net.IPEndPoint(System.Net.IPAddress.Any, Int32.Parse(MidiForwarder.Properties.Settings.Default.receivePort))); listening = true; while (listeningMidiEvents) { System.Threading.Thread.Sleep(250); } NetworkComms.RemoveGlobalIncomingPacketHandler <byte[]>("midiEvent", OnNetworkEvent); } catch (CommsException ce) { // set connection to red MessageBox.Show("CommsException occurs : " + ce.Message); } finally { try { NetworkComms.Shutdown(); } catch (CommsException ce) { Console.WriteLine("CommsException occurs : " + ce.ToString()); } finally { listening = false; } } } else if (inputDeviceName != null) { using (var inputDevice = InputDevice.GetByName(inputDeviceName)) { inputDevice.EventReceived += OnEventReceived; inputDevice.StartEventsListening(); while (listeningMidiEvents) { System.Threading.Thread.Sleep(250); } inputDevice.StopEventsListening(); inputDevice.EventReceived -= OnEventReceived; } } }
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); }
private void InputCombo_SelectedIndexChanged(object sender, EventArgs e) { if (inputDevice != null) { inputDevice.StopEventsListening(); inputDevice.Dispose(); } inputDevice = InputDevice.GetByName(InputCombo.SelectedItem.ToString()); inputDevice.EventReceived += new EventHandler <MidiEventReceivedEventArgs>(Form1.OnMidiRecieved); if (!inputDevice.IsListeningForEvents) { inputDevice.StartEventsListening(); } }
public MainWindowVM() { SynthData = new SynthData(); Synth = new Synth(SynthData); Pitch = 60; Synth.Play(); MIDIDeviceNames = InputDevice .GetAll() .Select(inputDevice => inputDevice.Name) .ToArray(); Synth.InputDevice = InputDevice.GetByName("Axiom A.I.R. Mini32"); }
public void SendEvent(MidiEvent midiEvent) { using (var outputDevice = OutputDevice.GetByName(MidiDevicesNames.DeviceA)) { MidiEvent eventSent = null; outputDevice.EventSent += (_, e) => eventSent = e.Event; string errorOnSend = null; outputDevice.ErrorOccurred += (_, e) => errorOnSend = e.Exception.Message; using (var inputDevice = InputDevice.GetByName(MidiDevicesNames.DeviceA)) { MidiEvent eventReceived = null; inputDevice.EventReceived += (_, e) => eventReceived = e.Event; string errorOnReceive = null; inputDevice.ErrorOccurred += (_, e) => errorOnReceive = e.Exception.Message; inputDevice.StartEventsListening(); outputDevice.PrepareForEventsSending(); outputDevice.SendEvent(midiEvent); var timeout = TimeSpan.FromMilliseconds(15); var isEventSentReceived = SpinWait.SpinUntil(() => eventSent != null && eventReceived != null, timeout); if (!isEventSentReceived) { if (errorOnSend != null) { Assert.Fail($"Failed to send event: {errorOnSend}"); } else if (errorOnReceive != null) { Assert.Fail($"Failed to receive event: {errorOnReceive}"); } else { Assert.Fail("Event either not sent ot not received."); } } MidiAsserts.AreEventsEqual(midiEvent, eventSent, false, "Sent event is invalid."); MidiAsserts.AreEventsEqual(eventSent, eventReceived, false, "Received event is invalid."); } } }
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."); }
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}."); } } } }
public async Task RunAsync() { while (true) { using (var outputDevice = OutputDevice.GetByName("Yamaha Portatone-1")) { outputDevice.EventSent += OnEventSent; using (var inputDevice = InputDevice.GetByName("Yamaha Portatone-1")) { inputDevice.EventReceived += OnEventReceived; inputDevice.StartEventsListening(); outputDevice.SendEvent(new NoteOnEvent()); outputDevice.SendEvent(new NoteOffEvent()); } } await Task.Delay(0); } }
public bool SetInputDevice(string deviceName) { if (SelectedInputDevice != null) { if (SelectedInputDevice.IsListeningForEvents) { SelectedInputDevice.StopEventsListening(); SelectedInputDevice.EventReceived -= OnEventReceived; } } SelectedInputDevice = InputDevice.GetByName(deviceName); if (SelectedInputDevice == null) { return(false); } SelectedInputDevice.EventReceived += OnEventReceived; SelectedInputDevice.StartEventsListening(); return(true); }
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()); } }