Skip to content

Commit

Permalink
Finish up MIDI input device support for now
Browse files Browse the repository at this point in the history
  • Loading branch information
Nytra committed Jul 19, 2024
1 parent def26d3 commit 44627e5
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 25 deletions.
26 changes: 16 additions & 10 deletions ProjectObsidian/Components/Devices/MIDI_CC_Value.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class MIDI_CC_Value : Component

public readonly Sync<bool> AutoMap;

public readonly Sync<int> Channel;

public readonly Sync<int> ControllerNumber;

public readonly Sync<MIDI_CC_Definition?> OverrideDefinition;
Expand Down Expand Up @@ -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))
Expand All @@ -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;
}
}
}
});
Expand Down
28 changes: 14 additions & 14 deletions ProjectObsidian/Components/Devices/MIDI_InputDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
79 changes: 78 additions & 1 deletion ProjectObsidian/Components/Devices/MIDI_PitchWheel_Value.cs
Original file line number Diff line number Diff line change
@@ -1 +1,78 @@

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<MIDI_InputDevice> InputDevice;

public readonly Sync<int> Channel;

public readonly Sync<int> Value;

public readonly Sync<float> 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<MIDI_InputDevice> 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;
}
}
}

0 comments on commit 44627e5

Please sign in to comment.