Shinsuke
Shinsuke

Reputation: 61

midioutshortmsg no sound

So I'm trying to play single notes in C# using midiOutShortMsg(). The problem is that no sound is played. The one way I've figured out to play the note is by putting the midiOutShortMsg() in a for loop from i=0 to 10000. But I don't believe that's how the API is supposed to work.

Later in the project I want to implement the MIDI into a Kinect project, and having the for loop delays the real-time feedback of the Kinect. So the for loop method is a no-go.

Below is the code I am using to play notes, if you comment out the for loop then no sound will play. Any help would be appreciated.

using System;
using System.Runtime.InteropServices;
using System.Text;

namespace MIDITest
{

    [StructLayout(LayoutKind.Sequential)]
    public struct MidiOutCaps
    {
        public UInt16 wMid;
        public UInt16 wPid;
        public UInt32 vDriverVersion;

        [MarshalAs(UnmanagedType.ByValTStr,
           SizeConst = 32)]
        public String szPname;

        public UInt16 wTechnology;
        public UInt16 wVoices;
        public UInt16 wNotes;
        public UInt16 wChannelMask;
        public UInt32 dwSupport;
    }

class Program
    {
        // MCI INterface
        [DllImport("winmm.dll")]
        private static extern long mciSendString(string command,
           StringBuilder returnValue, int returnLength,
           IntPtr winHandle);

        // Midi API
        [DllImport("winmm.dll")]
        private static extern int midiOutGetNumDevs();

        [DllImport("winmm.dll")]
        private static extern int midiOutGetDevCaps(Int32 uDeviceID,
           ref MidiOutCaps lpMidiOutCaps, 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 void Main()
        {
            int handle = 0;

            var numDevs = midiOutGetNumDevs();
            Console.WriteLine("You have {0} midi output devices", numDevs);
            MidiOutCaps myCaps = new MidiOutCaps();
            var res = midiOutGetDevCaps(0, ref myCaps,
                   (UInt32)Marshal.SizeOf(myCaps));

            res = midiOutOpen(ref handle, 0, null, 0, 0);

            byte[] data = new byte[4];

            data[0] = 0x90;
            data[1] = 50;
            data[2] = 111;
            uint msg = BitConverter.ToUInt32(data, 0);

            for (int i = 0; i < 10000; i++)
            {
                // both hard coding the message and creating it with byte doesn't work
                //res = midiOutShortMsg(handle, 0x007F4A90);
                res = midiOutShortMsg(handle, (int)msg);
            }
            res = midiOutClose(handle);
            Console.ReadLine();
        }
    }
}

Upvotes: 5

Views: 980

Answers (1)

Bassie
Bassie

Reputation: 10390

This is because midiOutShortMsg does not stop the execution of the code, meaning that midiOutClose gets called before the note has had time to play.

One way to overcome this would be to add a Sleep:

res = midiOutShortMsg(handle, (int)msg);

if (res == 0) // Check success code
{
    System.Threading.Thread.Sleep(length);
}

res = midiOutClose(handle);

Where length is the amount of time in ms it takes for the note to finish playing.

However this is almost certainly not recommended.

Upvotes: 2

Related Questions