From 44627e5fe679955e09c275cad0b3b88e4352f874 Mon Sep 17 00:00:00 2001 From: Nytra <14206961+Nytra@users.noreply.github.com> Date: Fri, 19 Jul 2024 18:10:22 +0100 Subject: [PATCH] Finish up MIDI input device support for now --- .../Components/Devices/MIDI_CC_Value.cs | 26 +++--- .../Components/Devices/MIDI_InputDevice.cs | 28 +++---- .../Devices/MIDI_PitchWheel_Value.cs | 79 ++++++++++++++++++- 3 files changed, 108 insertions(+), 25 deletions(-) diff --git a/ProjectObsidian/Components/Devices/MIDI_CC_Value.cs b/ProjectObsidian/Components/Devices/MIDI_CC_Value.cs index f55eab0..b4c1ba7 100644 --- a/ProjectObsidian/Components/Devices/MIDI_CC_Value.cs +++ b/ProjectObsidian/Components/Devices/MIDI_CC_Value.cs @@ -18,6 +18,8 @@ public class MIDI_CC_Value : Component public readonly Sync AutoMap; + public readonly Sync Channel; + public readonly Sync ControllerNumber; public readonly Sync OverrideDefinition; @@ -57,6 +59,7 @@ private void OnControl(MIDI_InputDevice device, MIDI_CC_EventData eventData) { AutoMap.Value = false; ControllerNumber.Value = eventData.controller; + Channel.Value = eventData.channel; if (OverrideDefinition.Value.HasValue) { if (Enum.IsDefined(typeof(MIDI_CC_Definition), eventData.controller)) @@ -69,20 +72,23 @@ private void OnControl(MIDI_InputDevice device, MIDI_CC_EventData eventData) } } } - if (OverrideDefinition.Value.HasValue && OverrideDefinition.Value.Value != MIDI_CC_Definition.UNDEFINED) + if (Channel.Value == eventData.channel) { - if (eventData.controller == (int)OverrideDefinition.Value.Value) + if (OverrideDefinition.Value.HasValue && OverrideDefinition.Value.Value != MIDI_CC_Definition.UNDEFINED) { - Value.Value = eventData.value; - NormalizedValue.Value = eventData.value / 127f; + if (eventData.controller == (int)OverrideDefinition.Value.Value) + { + Value.Value = eventData.value; + NormalizedValue.Value = eventData.value / 127f; + } } - } - else - { - if (eventData.controller == ControllerNumber.Value) + else { - Value.Value = eventData.value; - NormalizedValue.Value = eventData.value / 127f; + if (eventData.controller == ControllerNumber.Value) + { + Value.Value = eventData.value; + NormalizedValue.Value = eventData.value / 127f; + } } } }); diff --git a/ProjectObsidian/Components/Devices/MIDI_InputDevice.cs b/ProjectObsidian/Components/Devices/MIDI_InputDevice.cs index 98700ba..424f5fb 100644 --- a/ProjectObsidian/Components/Devices/MIDI_InputDevice.cs +++ b/ProjectObsidian/Components/Devices/MIDI_InputDevice.cs @@ -227,49 +227,49 @@ private void OnMessageReceived(object sender, MidiReceivedEventArgs args) //SysEx events are probably not worth handling case MidiEvent.SysEx1: - //if (DEBUG) UniLog.Log("SysEx1"); + //if (DEBUG) UniLog.Log("UnhandledEvent: SysEx1"); break; case MidiEvent.SysEx2: // Same as EndSysEx - //if (DEBUG) UniLog.Log("SysEx2"); + //if (DEBUG) UniLog.Log("UnhandledEvent: SysEx2"); break; case MidiEvent.Program: - if (DEBUG) UniLog.Log("Program"); + if (DEBUG) UniLog.Log("UnhandledEvent: Program"); break; case MidiEvent.MtcQuarterFrame: - if (DEBUG) UniLog.Log("MtcQuarterFrame"); + if (DEBUG) UniLog.Log("UnhandledEvent: MtcQuarterFrame"); break; case MidiEvent.SongPositionPointer: - if (DEBUG) UniLog.Log("SongPositionPointer"); + if (DEBUG) UniLog.Log("UnhandledEvent: SongPositionPointer"); break; case MidiEvent.SongSelect: - if (DEBUG) UniLog.Log("SongSelect"); + if (DEBUG) UniLog.Log("UnhandledEvent: SongSelect"); break; case MidiEvent.TuneRequest: - if (DEBUG) UniLog.Log("TuneRequest"); + if (DEBUG) UniLog.Log("UnhandledEvent: TuneRequest"); break; case MidiEvent.MidiClock: - if (DEBUG) UniLog.Log("Clock"); + if (DEBUG) UniLog.Log("UnhandledEvent: Clock"); break; case MidiEvent.MidiTick: - if (DEBUG) UniLog.Log("MidiTick"); + if (DEBUG) UniLog.Log("UnhandledEvent: MidiTick"); break; case MidiEvent.MidiStart: - if (DEBUG) UniLog.Log("MidiStart"); + if (DEBUG) UniLog.Log("UnhandledEvent: MidiStart"); break; case MidiEvent.MidiStop: - if (DEBUG) UniLog.Log("MidiStart"); + if (DEBUG) UniLog.Log("UnhandledEvent: MidiStart"); break; case MidiEvent.MidiContinue: - if (DEBUG) UniLog.Log("MidiContinue"); + if (DEBUG) UniLog.Log("UnhandledEvent: MidiContinue"); break; case MidiEvent.ActiveSense: - if (DEBUG) UniLog.Log("ActiveSense"); + if (DEBUG) UniLog.Log("UnhandledEvent: ActiveSense"); break; case MidiEvent.Reset: // Same as Meta - if (DEBUG) UniLog.Log("Reset"); + if (DEBUG) UniLog.Log("UnhandledEvent: Reset"); break; default: break; diff --git a/ProjectObsidian/Components/Devices/MIDI_PitchWheel_Value.cs b/ProjectObsidian/Components/Devices/MIDI_PitchWheel_Value.cs index 5f28270..dc914bd 100644 --- a/ProjectObsidian/Components/Devices/MIDI_PitchWheel_Value.cs +++ b/ProjectObsidian/Components/Devices/MIDI_PitchWheel_Value.cs @@ -1 +1,78 @@ - \ No newline at end of file +using Elements.Core; +using FrooxEngine; +using System; +using System.Linq; +using System.Threading.Tasks; +using System.Collections.Generic; +using Commons.Music.Midi.RtMidi; +using CoreMidi; +using Commons.Music.Midi; +using Obsidian.Elements; +using System.Runtime.Remoting.Contexts; + +namespace Obsidian; + +[Category(new string[] { "Obsidian/Devices" })] +public class MIDI_PitchWheel_Value : Component +{ + public readonly SyncRef InputDevice; + + public readonly Sync Channel; + + public readonly Sync Value; + + public readonly Sync NormalizedValue; + + private MIDI_InputDevice _device; + + protected override void OnStart() + { + base.OnStart(); + InputDevice.OnTargetChange += OnTargetChange; + if (InputDevice.Target != null) + { + _device = InputDevice.Target; + InputDevice.Target.PitchWheel += OnPitchWheel; + } + } + + protected override void OnPrepareDestroy() + { + base.OnPrepareDestroy(); + if (_device != null) + { + _device.PitchWheel -= OnPitchWheel; + _device = null; + } + } + + private void OnPitchWheel(MIDI_InputDevice device, MIDI_PitchWheelEventData eventData) + { + RunSynchronously(() => + { + if (eventData.channel == Channel.Value) + { + Value.Value = eventData.value; + NormalizedValue.Value = eventData.value == 8192 ? 0f : MathX.Remap(eventData.value, 0f, 16383f, -1f, 1f); + } + }); + } + + private void OnTargetChange(SyncRef syncRef) + { + if (syncRef.Target == null && _device != null) + { + _device.PitchWheel -= OnPitchWheel; + _device = null; + } + else if (syncRef.Target != null) + { + if (_device != null) + { + _device.PitchWheel -= OnPitchWheel; + } + _device = syncRef.Target; + _device.PitchWheel += OnPitchWheel; + } + } +} \ No newline at end of file