24
2015
Open & Play MIDI Files with winmm.dll in Unity
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())
This file contains 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"); | |
} | |
} |
Related Posts
Leave a comment
Recent posts
- [GreaseMonkey] Unity Forum Fixer
- UnityHub: Make Hub application background Translucent
- Customize SpriteShapeRenderer quality (but has issues)
- Editor tool: Copy selected gameobject’s names into clipboard as rows (for Excel)
- Editor tool: Replace string in selected gameobject’s names
- UnityHub: Enable built-in Login Dialog (no more browser login/logout issues!)
- Use TikTok-TTS in Unity (with WebRequest)
- Create Scene Thumbnail Image using OnSceneSaved & OnPreviewGUI
- webgl+javascript TTS
- Using Moonsharp (LUA) + Unity Webgl
- Using 3D gameobject prefabs with Unity Tilemap + NavMesh Surface
- Custom Unity Hub Project Template Preview Image/Video (using HTML+CSS in package description)
Recent Comments
- Vector3 maths for dummies! on
- UnityHub: Make Hub application background Translucent on
- UnityHub: Make Hub application background Translucent on
- Install Android SDK+JDK+NDK for Unity (without AndroidStudio or Unity Hub) on
- Install Android SDK+JDK+NDK for Unity (without AndroidStudio or Unity Hub) on
- [Asset Store] Point Cloud Viewer & Tools on
- [Asset Store] Point Cloud Viewer & Tools on
- ffmpeg stream raw video into Unity Texture2D on