-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finish up MIDI input device support for now
- Loading branch information
Showing
3 changed files
with
108 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 78 additions & 1 deletion
79
ProjectObsidian/Components/Devices/MIDI_PitchWheel_Value.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |