我正在尝试编写一个程序来检测一个人从电子鼓组的midi输出中击中鼓的次数。我正在使用NAudio来处理这个问题。当我尝试选择midi输入时,我被告知存在“调用midiInOpen”的“内存分配错误”,我已经环顾四周,似乎无法找到任何可以解释我的问题的资源。 我的midi设备肯定被认可,目前我正在使用Yamaha piaggero NP-11键盘进行midi输入。
实际错误在第25行,我正在添加设备:
midiIn = new MidiIn(0);
这是我正在使用的代码:
class Program
{
public MidiIn midiIn;
static void Main(string[] args)
{
Program program = new Program();
program.MainRun();
}
void MainRun()
{
Console.WriteLine(GetMidiInDevices()[0]); // This is just to check that a midi device is actually plugged in.
midiIn = new MidiIn(0); // <-- THIS IS WHERE I GET THE ERROR
midiIn.MessageReceived += new EventHandler<MidiInMessageEventArgs>(midiIn_MessageReceived);
}
public string[] GetMidiInDevices()
{
string[] returnDevices = new string[MidiIn.NumberOfDevices];
for (int device = 0; device < returnDevices.Length; device++)
returnDevices[device] = MidiIn.DeviceInfo(device).ProductName;
return returnDevices;
}
public void midiIn_MessageReceived(object sender, MidiInMessageEventArgs e)
{
if (e.MidiEvent != null && e.MidiEvent.CommandCode == MidiCommandCode.AutoSensing)
return;
if (e.MidiEvent.CommandCode == MidiCommandCode.NoteOn)
{
// As the Command Code is a NoteOn then we need
// to cast the MidiEvent to the NoteOnEvent
NoteOnEvent ne;
ne = (NoteOnEvent)e.MidiEvent;
}
Console.WriteLine("Got input!");
}
}