Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Patch to add MIDI output support to keijiro's MidiInput for Unity (https://github.com/keijiro/unity-midi-input)
--- a/UnityMidiReceiver/UnityMidiReceiver.cpp
+++ b/UnityMidiReceiver/UnityMidiReceiver.cpp
@@ -37,6 +37,7 @@ namespace
// MIDI device handle vector.
std::vector<HMIDIIN> handles;
+ std::vector<HMIDIOUT> outDeviceHandles;
// Incoming MIDI message queue.
std::queue<Message> messageQueue;
@@ -86,6 +87,18 @@ namespace
handles.push_back(handle);
}
+
+ auto outDeviceCount = midiOutGetNumDevs();
+ for (auto i = 0U; i < outDeviceCount; i++)
+ {
+ HMIDIOUT handle;
+ DWORD_PTR callback = reinterpret_cast<DWORD_PTR>(MyMidiInProc);
+ // DWORD_PTR instance = reinterpret_cast<DWORD_PTR>(&messageDelegate);
+ MMRESULT result = midiOutOpen(&handle, i, callback, NULL, CALLBACK_FUNCTION);
+ assert(result == MMSYSERR_NOERROR);
+
+ outDeviceHandles.push_back(handle);
+ }
}
}
@@ -146,3 +159,24 @@ extern "C" uint64_t EXPORT_API UnityMIDIReceiver_DequeueIncomingData()
return m.uint64Value;
}
+
+// BOOKMARK
+extern "C" void EXPORT_API UnityMIDIReceiver_Send(uint8_t aStatus, uint8_t aData1, uint8_t aData2)
+{
+ std::string errorString;
+ MMRESULT result;
+
+ // Pack MIDI bytes into double word.
+ DWORD packet;
+ unsigned char *ptr = (unsigned char *)&packet;
+ ptr[0] = aStatus;
+ ptr[1] = aData1;
+ ptr[2] = aData2;
+
+ // Send the message immediately.
+ for (auto& handle : outDeviceHandles)
+ {
+ result = midiOutShortMsg(handle, packet);
+ assert(result == MMSYSERR_NOERROR);
+ }
+}
--- a/Assets/Scripts/MidiInput.cs
+++ b/Assets/Scripts/MidiInput.cs
@@ -70,6 +70,15 @@ public class MidiInput : MonoBehaviour
return 0.0f;
}
}
+
+ public static void SetCC(int channel, int value) {
+ uint status = 0xb0;
+ Send(status, (uint) channel, (uint) value);
+ }
+
+ public static void Send (uint status, uint data1, uint data2) {
+ _Send(status, data1, data2);
+ }
#endregion
#region MIDI message sturcture
@@ -245,6 +254,9 @@ public class MidiInput : MonoBehaviour
[DllImport ("UnityMIDIReceiver", EntryPoint="UnityMIDIReceiver_DequeueIncomingData")]
public static extern ulong DequeueIncomingData ();
+ [DllImport ("UnityMIDIReceiver", EntryPoint="UnityMIDIReceiver_Send")]
+ public static extern void _Send (uint status, uint data1, uint data2);
+
[DllImport ("UnityMIDIReceiver")]
private static extern System.IntPtr UnityMIDIReceiver_GetEndpointName (uint id);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.