所以我正在尝试使用 midiOutShortMsg() 在 C# 中播放单个音符。问题是没有声音播放。我想出的播放音符的一种方法是将 midiOutShortMsg() 放入从 i=0 到 10000 的 for 循环中。但我不相信 API 应该是这样工作的。
在项目的后期,我想将 MIDI 实现到 Kinect 项目中,并且让 for 循环延迟 Kinect 的实时反馈。所以for循环方法是不行的。
下面是我用来播放音符的代码,如果您注释掉 for 循环,则不会播放任何声音。任何帮助,将不胜感激。
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();
}
}
}