Unity中的MIDI输出

Nax*_*xin 6 .net c# midi unity-game-engine

我正在尝试构建简单的软件以连接到Unity中的Windows上的MIDI输出设备并发送MIDI数据.

为了避免重新发明轮子,我开始在CodeProject上使用C#Midi Toolkit,它支持.NET 2.0.

我遇到的问题是它在Unity编辑器中工作正常,但 在独立的Windows构建中失败了.

这是基本的连接/播放声音代码:

    // Log devices
    int deviceCount = OutputDevice.DeviceCount;
    for (int i = 0; i < deviceCount; i++)
    {
        Debug.Log(string.Format("Detected MIDI Device with ID {0}:{1}", i, OutputDevice.GetDeviceCapabilities(i).name));
    }
    deviceID = 1;
    Debug.Log(string.Format("Connected to {0}", deviceID));
    // Connect to device
    device = new OutputDevice(deviceID);
    // Play Middle C
    device.Send(new ChannelMessage(ChannelCommand.NoteOn, 0, note, 127));
Run Code Online (Sandbox Code Playgroud)

在独立版本中,我得到以下异常:

OutputDeviceException:指定的设备句柄无效.

我查看了源代码并注意到该库正在使用Win32句柄来获取winmm.dll,我认为这可能与它有关但不确定从何处开始.

谁能提供任何有关如何处理此问题的见解?我可能会看看专门为Unity构建的替代品,但我有兴趣了解为什么这样的东西首先不起作用.

ane*_*tos 1

我不知道是否存在此类问题,但是这个旧代码项目代码使用的 midiOutOpen 函数的 x86 定义(OutputDevice.cs)是

[DllImport("winmm.dll")]
 50          private static extern int midiOutOpen(ref int handle, int deviceID,
 51              MidiOutProc proc, int instance, int flags);
Run Code Online (Sandbox Code Playgroud)

在 Pinvoke 上我可以找到这个定义:

[DllImport("winmm.dll")]
        static extern uint midiOutOpen(out IntPtr lphMidiOut, uint uDeviceID, IntPtr dwCallback, IntPtr dwInstance, uint dwFlags);
Run Code Online (Sandbox Code Playgroud)

也许是平台问题。