Created
December 23, 2015 23:05
-
-
Save unitycoder/856af22f048f26334cd7 to your computer and use it in GitHub Desktop.
MidiPlayer for Unity (using winmm.dll)
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
// 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"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment