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 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"); | |
| } | |
| } |
Related Posts
Leave a comment
Recent posts
- Testing Unity AI Beta
- Ways to Support UnityCoder Development
- Using UI Slider to Create 5-Star Rating Element
- Game Music Library For Unity (editor plugin)
- Fontastic : Easily Test Fonts in Unity Editor!
- GeoTiff Importer & Terrain Generator for Unity
- Create Baked DropShadow for UI images
- .JP2 Ortho Image Converter to PNG/JPG/TIFF
- Convert LAS/LAZ/PLY pointclouds to GLTF (GLB) Point Meshes (standalone converter)
- Detect SRP (URP or HDRP) with Assembly Definition Version Defines
- [LudumDare57] Theme: Depths
- MotionVector Effect: Object “disappears” when paused
Recent Comments
- on [Asset Store] PolygonCollider2D Optimizer
- on Trajectory Test Scene 2.0
- on Vector3 maths for dummies!
- on UnityHub 3.6.0: Remove Version Control & Cloud Dashboard columns
- on Using RenderDoc with Unity (graphics debugger)
- on UI Scroll View automatic Content height
- on [Asset Store] Point Cloud Viewer & Tools
- on [Asset Store] Point Cloud Viewer & Tools
Coin:
CUgDSbRqFcAumDSAcdKDvuXsw26VdkJe8C8WGUQHBAGS
An article by











