Last active
January 20, 2019 12:45
-
-
Save unitycoder/4f9bbd875844437ebaf3 to your computer and use it in GitHub Desktop.
Play Midi Note with midi-dot-net.dll in Unity
This file contains hidden or 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
// 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