Dec
24
2015

Open & Play MIDI Files with winmm.dll in Unity

load_play_midi_file_in_unity3d

Yay – Playing Midi files in unity!

Notes:
– Works on PC only
– If you make a build, midi files wont be included automatically (unless they are in StreamingAssets folder, havent tested it though..)

References:
– Midi code from http://www.codeguru.com/columns/dotnet/making-music-with-midi-and-c.html
– MidiOutCaps c# struct from http://svn.tapr.org/repos_sdr_windows/PowerSDR/trunk/Source/Console/midi.cs
– Get midi files from internets, like: http://www.vgmusic.com/music/computer/commodore/commodore/

Source: (attach to gameobject in scene, set proper midi path inside PlayMidi())


// Midi Player – unitycoder.com
// MidiOutCaps struct from http://svn.tapr.org/repos_sdr_windows/PowerSDR/trunk/Source/Console/midi.cs
// Midi code from http://www.codeguru.com/columns/dotnet/making-music-with-midi-and-c.html
// USAGE: set proper path+filename inside PlayMidi(), attach this scrip to gameobject in scene and hit play!
// Get midi files from internets, like: http://www.vgmusic.com/music/computer/commodore/commodore/
using UnityEngine;
using System.Runtime.InteropServices;
using System.Text;
public class MidiPlayer : MonoBehaviour
{
static string res;
public const int MAXPNAMELEN = 32;
public struct MidiOutCaps
{
public short wMid;
public short wPid;
public int vDriverVersion;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAXPNAMELEN)]
public string szPname;
public short wTechnology;
public short wVoices;
public short wNotes;
public short wChannelMask;
public int dwSupport;
}
// MCI INterface
[DllImport("winmm.dll")]
private static extern long mciSendString(string command, StringBuilder returnValue, int returnLength, System.IntPtr winHandle);
// Midi API
[DllImport("winmm.dll")]
private static extern int midiOutGetNumDevs();
[DllImport("winmm.dll")]
private static extern int midiOutGetDevCaps(System.Int32 uDeviceID, ref MidiOutCaps lpMidiOutCaps, System.UInt32 cbMidiOutCaps);
[DllImport("winmm.dll")]
private static extern int midiOutOpen(ref int handle, int deviceID, MidiCallBack proc, int instance, int flags);
[DllImport("winmm.dll")]
private static extern int midiOutShortMsg(int handle, int message);
[DllImport("winmm.dll")]
private static extern int midiOutClose(int handle);
private delegate void MidiCallBack(int handle, int msg, int instance, int param1, int param2);
static string Mci(string command)
{
StringBuilder reply = new StringBuilder(256);
mciSendString(command, reply, 256, System.IntPtr.Zero);
return reply.ToString();
}
void Start()
{
var numDevs = midiOutGetNumDevs();
MidiOutCaps myCaps = new MidiOutCaps();
var res = midiOutGetDevCaps(0, ref myCaps, (System.UInt32)Marshal.SizeOf(myCaps));
PlayMidi();
}
static void PlayMidi()
{
res = System.String.Empty;
// set path to midi file here
string filename = Application.dataPath + "/Mids/" + "commando.mid";
Debug.Log("Loading midi:" + filename);
res = Mci("open \"" + filename + "\" alias music");
res = Mci("play music");
}
void OnDestroy()
{
res = Mci("close music");
}
void OnDisable()
{
res = Mci("close music");
}
}

view raw

MidiPlayer.cs

hosted with ❤ by GitHub


Leave a comment

Connect

Twitter View LinkedIn profile Youtube Youtube Join Discord Twitch Instagram

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.