Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Last active January 20, 2019 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unitycoder/4f9bbd875844437ebaf3 to your computer and use it in GitHub Desktop.
Save unitycoder/4f9bbd875844437ebaf3 to your computer and use it in GitHub Desktop.
Play Midi Note with midi-dot-net.dll in Unity
// usage: copy Midi.dll to Assets/Plugins/ folder
// Get it from https://code.google.com/p/midi-dot-net/
using UnityEngine;
using System.Collections;
using Midi; // needs this
public class MidiSound : MonoBehaviour
{
OutputDevice outputDevice;
void Start ()
{
outputDevice = OutputDevice.InstalledDevices[0];
if (outputDevice.IsOpen) outputDevice.Close();
if (!outputDevice.IsOpen) outputDevice.Open();
// play note
outputDevice.SendNoteOn(Channel.Channel1, Pitch.C4, 80); // Middle C, velocity 80
outputDevice.SendPitchBend(Channel.Channel1, 7000); // 8192 is centered, so 7000 is bent down
}
void OnDestroy ()
{
if (outputDevice != null && outputDevice.IsOpen) outputDevice.Close();
}
void OnDisable ()
{
if (outputDevice != null && outputDevice.IsOpen) outputDevice.Close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment