Dec
23
2015

Play Midi Sounds with midi-dot-net dll

play_midi_note_with_unity

Instructions:
– Download midi-dot-net from https://code.google.com/p/midi-dot-net/ (from downloads section, midi-dot-net_1.1.0.zip)
– Unzip and copy “Midi.dll” into your unity project folder, Assets/Plugins/
– Create c# script and use the source below for testing
– Attach the script to gameobject in scene and hit play, should hear 1 note sound
– See the midi-dot-net examples & API for more info

Note:
– This library doesn’t support loading MIDI files!
– (probably works in Windows only)

Source:


// 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();
}
}

view raw

MidiSound.cs

hosted with ❤ by GitHub


Leave a comment

Connect

Twitter View LinkedIn profile Youtube Youtube Join Discord Twitch

UnityLauncherPro

Get UnityLauncherPRO and work faster with Unity Projects!
*free unity hub alternative

@unitycoder_com

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.