C# Windows MIDI流媒体和SysEx

C# Windows MIDI流媒体和SysEx,c#,windows,winapi,midi,multimedia,C#,Windows,Winapi,Midi,Multimedia,我使用的是C#中古老的Windows多媒体API(WinMM.dll中的midiXyz函数) 在以非流模式()打开Midi输出设备/端口后,使用()发送SysEx可以正常工作 在以流模式()打开Midi输出设备/端口后,使用midiOutLongMsg发送SysEx不起作用 相反,midiOutLongMsg失败,出现错误MMSYSERR\u不受支持(=8)。 错误文本为:“不支持此功能。请使用“功能”功能确定驱动程序支持哪些功能和消息。” 然而,根据MSDN,()还应该与流句柄一起工作。 还声

我使用的是C#中古老的Windows多媒体API(WinMM.dll中的midiXyz函数)

在以非流模式()打开Midi输出设备/端口后,使用()发送SysEx可以正常工作

在以流模式()打开Midi输出设备/端口后,使用
midiOutLongMsg
发送SysEx不起作用

相反,
midiOutLongMsg
失败,出现错误
MMSYSERR\u不受支持(=8)。
错误文本为:“不支持此功能。请使用“功能”功能确定驱动程序支持哪些功能和消息。”

然而,根据MSDN,()还应该与流句柄一起工作。 还声明,SysEx和streaming可以一起使用()

通过使用()midristreamout将缓冲的SysEx消息排入队列来发送消息,效果很好。 但是,我也需要/希望使用
midiOutLongMsg
直接发送SysEx

我已经检查了各种开源Midi库(托管和非托管),几个Midi驱动程序源,甚至WINE的WinMM.dll源,但是找不到任何提示我做错了什么

为了在尽可能小的代码中重现我的问题,我删除了所有回调、取消准备、清理和发布的内容,并在一个函数中压缩了几个类。 以下代码打开第一个Midi设备/端口,并尝试发送“GM Mode On”SysEx消息:

更新2014年1月12日:请参见下面的代码版本3

public static class MidiTest { // version 1 - x86/32 bit only

  public static void Test () {
    int moID = 0; // midi out device/port ID
    int moHdl; // midi out device/port handle
#if !true
    // SysEx via midiOutLongMsg works
    Chk (WinMM.midiOutOpen (out moHdl, moID, null, 0, 0)); // open midi out in non-stream mode
#else
    // SysEx via midiOutLongMsg fails
    Chk (WinMM.midiStreamOpen (out moHdl, ref moID, 1, null, 0, 0)); // open midi out in stream mode
#endif
    byte [] sx = { 0xF0, 0x7E, 0x7F, 0x09, 0x01, 0xF7 }; // GM On sysex
    int shdr = Marshal.SizeOf (typeof (MidiHdr)); // hdr size
    var mhdr = new MidiHdr (); // allocate managed hdr
    mhdr.bufferLength = mhdr.bytesRecorded = sx.Length; // length of message bytes
    mhdr.data = Marshal.AllocHGlobal (mhdr.bufferLength); // allocate native message bytes
    Marshal.Copy (sx, 0, mhdr.data, mhdr.bufferLength); // copy message bytes from managed to native memory
    IntPtr nhdr = Marshal.AllocHGlobal (shdr); // allocate native hdr
    Marshal.StructureToPtr (mhdr, nhdr, false); // copy managed hdr to native hdr
    Chk (WinMM.midiOutPrepareHeader (moHdl, nhdr, shdr)); // prepare native hdr
    Chk (WinMM.midiOutLongMsg (moHdl, nhdr, shdr)); // send native message bytes
  } // Test

  static void Chk (int f) {
    if (0 == f) return;
    var sb = new StringBuilder (256); // MAXERRORLENGTH
    var s = 0 == WMM.midiOutGetErrorText (f, sb, sb.Capacity) ? sb.ToString () : String.Format ("MIDI Error {0}.", f);
    System.Diagnostics.Trace.WriteLine (s);
  }

  [StructLayout (LayoutKind.Sequential)]
  internal struct MidiHdr { // sending long MIDI messages requires a header
    public IntPtr data; // native pointer to message bytes, allocated on native heap
    public int bufferLength; // length of buffer 'data'
    public int bytesRecorded; // actual amount of data in buffer 'data'
    public int user; // custom user data
    public int flags; // information flags about buffer
    public IntPtr next; // reserved
    public int reserved; // reserved
    public int offset; // buffer offset on callback
    [MarshalAs (UnmanagedType.ByValArray, SizeConst = 4)]
    public int[] reservedArray; // reserved
  } // struct MidiHdr

  internal sealed class WinMM { // native MIDI calls from WinMM.dll
    public delegate void CB (int hdl, int msg, int inst, int p1, int p2); // callback
    [DllImport ("winmm.dll")] public static extern int midiStreamOpen (out int hdl, ref int devID, int reserved, CB proc, int inst, int flags);
    [DllImport ("winmm.dll")] public static extern int midiOutOpen (out int hdl, int devID, CB proc, int inst, int flags);
    [DllImport ("winmm.dll")] public static extern int midiOutPrepareHeader (int hdl, IntPtr pHdr, int sHdr);
    [DllImport ("winmm.dll")] public static extern int midiOutLongMsg (int hdl, IntPtr pHdr, int sHdr);
    [DllImport ("winmm.dll")] public static extern int midiOutGetErrorText (int err, StringBuilder msg, int sMsg);
  } // class WinMM

} // class MidiTest
问题1:当Midi设备/端口以流模式打开时(
midiStreamOpen
),是否可以通过
midiOutLongMsg
发送SysEx

问题2:如果是,你知道我遗漏了什么吗

问题3:我在流媒体模式下没有找到很多使用MIDI的源。所以,如果您知道一些在流模式下使用MIDI输出的开源库,请给我一个链接,以便我可以比较

谢谢

更新(2013年10月19日):

嗨,CL

谢谢你的回答。是的,可能是微软的某个人在维护WinMM.dll时弄错了什么,但我认为我丢失某些东西的可能性更高,因为

a) 有一本旧手册“Windows NT DDK-多媒体驱动程序”(可用),它比当前的MSDN页面更详细地描述了WinMM内容。第56页显示了一个图表,其中WinMM.dll是应用程序和MIDI/音频驱动程序之间的中间层。WinMM的主要工作是将MIDI数据从应用程序传递到一个MIDI/音频驱动程序。当MIDI驱动程序是外部键盘/合成器/音调发生器的端口驱动程序时,WinMM无法对MIDI数据进行太多更改。第94页描述了驱动程序消息MODM_LONGDATA及其参数——它与midiOutLongMsg的参数几乎相同。这限制了在WinMM.dll中搞乱某些东西的机会

b) WinMM.dll中的代码路径,从调用midiOutLongMsg到使用MODM_LONGDATA调用驱动程序,在midiOutOpen之后可以正常工作,但在midiStreamOpen之后不能正常工作。结果代码是MMSYSERR_NOTSUPPORTED,这告诉我在WinMM.dll中的代码路径的开头有一些健全性检查,比如

if (whatever_weird_condition) return MMSYSERR_NOTSUPPORTED;
无论什么奇怪的情况都很可能是我本该做的事,但还没有做

c) 如果MIDI驱动程序本身不支持流式输出(可选),WinMM将从缓冲/排队输出(MIDI流式输出)转换为更简单的非流式驱动程序调用(非可选)。我的机器上的8个MIDI驱动程序都不支持流媒体本身,都依赖WinMM来实现。流式短消息和长消息都可以正常工作

d) 我的测试代码在Windows 7和Windows XP上的行为完全相同。如果微软把事情搞砸了,这个错误一定是很久以前(XP之前)造成的。我要么是第一个找到它的人(几年后),要么是所有人都把它保密起来 难以收集的

e) 我的测试代码与我机器上的所有8个midi驱动程序的行为完全相同。这告诉我,这很可能不是驾驶员的问题


f) 多年的调试经验告诉我,如果某个东西不能正常工作,那么问题很可能出在我这边的屏幕上-P

几乎没有人使用MIDI流

如今,
midi
*功能不再由硬件供应商的驱动程序实现,而是由微软的MM WDM兼容驱动程序实现。
这一细节似乎在重写过程中被忽略了。

更新:很抱歉没有早点回来。我工作忙得不可开交。是的,你是对的,它现在失败了。也许那天晚上我睡得太晚了,但我不明白它是怎么工作的,甚至不知道它是否工作。我还需要在流模式下发送sysex,但我的应用程序还没有在非流模式下发送(midiOutOpen)。我会继续看,看看是否能找到解决办法。您必须使用sysex主卷还是可以使用CC:7音量控制?当然,这对sysex没有帮助,但是短消息可以在流模式下通过。哦,感谢您的更新,我还获得了在x86或x64(AnyCPU)中编译和运行的代码

原始信息:我不知道您是否仍然感兴趣,但我认为下面的代码可能会回答您的问题。我将您的旧代码放在注释
//以前的代码
下,将新代码放在注释
//新代码

此外,对于x86,标头的大小不应包括数据。我知道x86的大小是0x40,但我仍在试图找出编写此代码的最佳方法,因此如果您有任何想法,请告诉我

我只是在另一个应用程序中发现了这一点,所以我还没有把它全部充实起来,但我运行了这段代码,它似乎对您有用。我喜欢这个旧dll中的流模式。它是
using System;

using System.Collections.Generic;

using System.Text;

using System.Runtime.InteropServices;

using System.Diagnostics;

public static class MidiTest
{

    public static void Test() 
    {
        int moID = 0; // midi out device/port ID

        //PREVIOUS CODE
        //int moHdl; // midi out device/port handle
        //NEW CODE
        IntPtr moHdl = IntPtr.Zero;

#if !true
    // SysEx via midiOutLongMsg works
    Chk (WinMM.midiOutOpen (out moHdl, moID, null, 0, 0)); // open midi out in non-stream mode
#else
        // SysEx via midiOutLongMsg fails
        //PREVIOUS CODE
        //Chk(WinMM.midiStreamOpen(out moHdl, ref moID, 1, null, 0, 0)); // open midi out in stream mode
        //NEW CODE
        IntPtr instance = IntPtr.Zero;
        Chk(WinMM.midiStreamOpen(out moHdl, ref moID, 1, null, instance, 0)); // open midi out in stream mode

#endif
        byte[] sx = { 0xF0, 0x7E, 0x7F, 0x09, 0x01, 0xF7 }; // GM On sysex

        //PREVIOUS CODE
        //int shdr = Marshal.SizeOf(typeof(MidiHdr)); // hdr size
        //NEW CODE
        int shdr = 0x40; // hdr size

        var mhdr = new MidiHdr(); // allocate managed hdr
        mhdr.bufferLength = mhdr.bytesRecorded = sx.Length; // length of message bytes
        mhdr.data = Marshal.AllocHGlobal(mhdr.bufferLength); // allocate native message bytes
        Marshal.Copy(sx, 0, mhdr.data, mhdr.bufferLength); // copy message bytes from managed to native memory
        IntPtr nhdr = Marshal.AllocHGlobal(shdr); // allocate native hdr
        Marshal.StructureToPtr(mhdr, nhdr, false); // copy managed hdr to native hdr
        Chk(WinMM.midiOutPrepareHeader(moHdl, nhdr, shdr)); // prepare native hdr
        Chk(WinMM.midiOutLongMsg(moHdl, nhdr, shdr)); // send native message bytes
    } // Test

    static void Chk(int f)
    {
        if (0 == f) return;
        var sb = new StringBuilder(256); // MAXERRORLENGTH
        var s = 0 == WinMM.midiOutGetErrorText(f, sb, sb.Capacity) ? sb.ToString() : String.Format("MIDI Error {0}.", f);
        System.Diagnostics.Trace.WriteLine(s);
    }

    [StructLayout(LayoutKind.Sequential)]
    internal struct MidiHdr
    { // sending long MIDI messages requires a header
        public IntPtr data; // native pointer to message bytes, allocated on native heap
        public int bufferLength; // length of buffer 'data'
        public int bytesRecorded; // actual amount of data in buffer 'data'
        public int user; // custom user data
        public int flags; // information flags about buffer
        public IntPtr next; // reserved
        public int reserved; // reserved
        public int offset; // buffer offset on callback
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
        public int[] reservedArray; // reserved
    } // struct MidiHdr

    internal sealed class WinMM
    { // native MIDI calls from WinMM.dll
        public delegate void CB(int hdl, int msg, int inst, int p1, int p2); // callback

        //PREVIOUS CODE
        //[DllImport("winmm.dll")]
        //public static extern int midiStreamOpen(out int hdl, ref int devID, int reserved, CB proc, int inst, int flags);
        //[DllImport("winmm.dll")]
        //public static extern int midiOutOpen(out int hdl, int devID, CB proc, int inst, int flags);
        //[DllImport("winmm.dll")]
        //public static extern int midiOutPrepareHeader(int hdl, IntPtr pHdr, int sHdr);
        //[DllImport("winmm.dll")]
        //public static extern int midiOutLongMsg(int hdl, IntPtr pHdr, int sHdr);
        //[DllImport("winmm.dll")]
        //public static extern int midiOutGetErrorText(int err, StringBuilder msg, int sMsg);

        //NEW CODE
        #region winmm declarations
        [DllImport("winmm.dll")]
        public static extern int midiOutPrepareHeader(IntPtr handle,
            IntPtr headerPtr, int sizeOfMidiHeader);
        [DllImport("winmm.dll")]
        public static extern int midiOutUnprepareHeader(IntPtr handle,
            IntPtr headerPtr, int sizeOfMidiHeader);
        [DllImport("winmm.dll")]
        public static extern int midiOutOpen(out IntPtr handle, int deviceID,
            CB proc, IntPtr instance, int flags);
        [DllImport("winmm.dll")]
        public static extern int midiOutGetErrorText(int errCode,
            StringBuilder message, int sizeOfMessage);
        [DllImport("winmm.dll")]
        public static extern int midiOutClose(IntPtr handle);
        [DllImport("winmm.dll")]
        public static extern int midiStreamOpen(out IntPtr handle, ref int deviceID, int reserved,
            CB proc, IntPtr instance, uint flag);
        [DllImport("winmm.dll")]
        public static extern int midiStreamClose(IntPtr handle);
        [DllImport("winmm.dll")]
        public static extern int midiStreamOut(IntPtr handle, IntPtr headerPtr, int sizeOfMidiHeader);
        [DllImport("winmm.dll")]
        public static extern int midiOutLongMsg(IntPtr handle,
            IntPtr headerPtr, int sizeOfMidiHeader);
        #endregion

    } // class WinMM

} // class MidiTest
public static class MidiTest { // version 3 - x68/x64 32/64-bit compatible

  public static void Test () {
    int moID = 0; // midi out device/port ID
    IntPtr moHdl = IntPtr.Zero;

#if !true
    // SysEx via midiOutLongMsg works
    Chk (WinMM.midiOutOpen (out moHdl, moID, null, 0, 0)); // open midi out in non-stream mode
#else
    // SysEx via midiOutLongMsg fails
    IntPtr instance = IntPtr.Zero;
    Chk (WinMM.midiStreamOpen (out moHdl, ref moID, 1, null, instance, 0)); // open midi out in stream mode
#endif
    byte[] sx = { 0xF0, 0x7E, 0x7F, 0x09, 0x01, 0xF7 }; // GM On sysex

    int shdr = Marshal.SizeOf(typeof(MidiHdr)); // hdr size
    IntPtr x = Marshal.OffsetOf (typeof (MidiHdr), "data");   // ptr; size: 4/8, offset: 0
    x = Marshal.OffsetOf (typeof (MidiHdr), "bufferLength");  // int; size: 4  , offset: 4/8
    x = Marshal.OffsetOf (typeof (MidiHdr), "bytesRecorded"); // int; size: 4  , offset: 8/12
    x = Marshal.OffsetOf (typeof (MidiHdr), "user");          // ptr; size: 4/8, offset: 12/16
    x = Marshal.OffsetOf (typeof (MidiHdr), "flags");         // int; size: 4  , offset: 16/24; followed by 4 byte padding
    x = Marshal.OffsetOf (typeof (MidiHdr), "next");          // ptr; size: 4/8, offset: 20/32
    x = Marshal.OffsetOf (typeof (MidiHdr), "reserved");      // ptr; size: 4/8, offset: 24/40
    x = Marshal.OffsetOf (typeof (MidiHdr), "offset");        // int; size: 4  , offset: 28/48; followed by 4 byte padding
    x = Marshal.OffsetOf (typeof (MidiHdr), "reservedArray"); // ptr; size: 4/8 x 8 = 32/64, offset: 32/56
    // total size: 64/120
    var mhdr = new MidiHdr (); // allocate managed hdr
    mhdr.bufferLength = mhdr.bytesRecorded = sx.Length; // length of message bytes
    mhdr.data = Marshal.AllocHGlobal (mhdr.bufferLength); // allocate native message bytes
    Marshal.Copy (sx, 0, mhdr.data, mhdr.bufferLength); // copy message bytes from managed to native memory
    IntPtr nhdr = Marshal.AllocHGlobal (shdr); // allocate native hdr
    Marshal.StructureToPtr (mhdr, nhdr, false); // copy managed hdr to native hdr
    Chk (WinMM.midiOutPrepareHeader (moHdl, nhdr, shdr)); // prepare native hdr
    int r = WinMM.midiOutLongMsg (moHdl, nhdr, shdr); // send native message bytes
    Chk (r); // send native message bytes
  } // Test

  static void Chk (int f) {
    if (0 == f) return;
    var sb = new StringBuilder (256); // MAXERRORLENGTH
    var s = 0 == WinMM.midiOutGetErrorText (f, sb, sb.Capacity) ? sb.ToString () : String.Format ("MIDI Error {0}.", f);
    System.Diagnostics.Trace.WriteLine (s);
  }

  [StructLayout (LayoutKind.Sequential)]
  internal struct MidiHdr { // sending long MIDI messages requires a header
    public IntPtr data; // native pointer to message bytes, allocated on native heap
    public int bufferLength; // length of buffer 'data'
    public int bytesRecorded; // actual amount of data in buffer 'data'
    public IntPtr user; // custom user data
    public int flags; // information flags about buffer
    public IntPtr next; // reserved
    public IntPtr reserved; // reserved
    public int offset; // buffer offset on callback
    [MarshalAs (UnmanagedType.ByValArray, SizeConst = 8)]
    public IntPtr[] reservedArray; // reserved
  } // struct MidiHdr

  internal sealed class WinMM { // native MIDI calls from WinMM.dll
    public delegate void CB (IntPtr hdl, int msg, IntPtr inst, int p1, int p2); // callback
    [DllImport ("winmm.dll")] public static extern int midiStreamOpen (out IntPtr hdl, ref int devID, int reserved, CB proc, IntPtr inst, uint flags);
    [DllImport ("winmm.dll")] public static extern int midiOutOpen (out IntPtr hdl, int devID, CB proc, IntPtr instance, int flags);
    [DllImport ("winmm.dll")] public static extern int midiOutPrepareHeader (IntPtr hdl, IntPtr pHdr, int sHdr);
    [DllImport ("winmm.dll")] public static extern int midiOutLongMsg (IntPtr hdl, IntPtr pHdr, int sHdr);
    [DllImport ("winmm.dll")] public static extern int midiOutGetErrorText (int err, StringBuilder msg, int sMsg);
  } // class WinMM

} // class MidiTest
//MidiEvent - r, r, r, r, s, s, s, s, e, e, e,   e,   d,   d,   d, d, d,   d, p, p
byte[] sx = { 9, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 128, 240, 126, 127, 9, 1, 247, 0, 0 }; // GM On sysex
Chk(WinMM.midiStreamOut(moHdl, nhdr, shdr));
int r = WinMM.midiOutLongMsg(moHdl, nhdr, shdr);
Chk(r); // send native message bytes
Chk(WinMM.midiStreamRestart(moHdl));
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include <MMSystem.h>

#pragma comment ( lib, "winmm.lib" )

static void Chk (UINT r) {
  static _TCHAR errmsg[256];
  if (!r) return;
  UINT rr = midiOutGetErrorText (r, errmsg, sizeof (errmsg));
  printf ("MIDI Error %d: '%S'\n", r, errmsg);
}

int _tmain (int argc, _TCHAR* argv[]) {
  UINT moID = 0; // first midi out device/port ID
  DWORD_PTR inst = NULL; // no instance
  DWORD_PTR clbk = NULL; // no callback
  DWORD flgs = CALLBACK_NULL; // flags, no callback
#if 0
  // SysEx via midiOutLongMsg works
  HMIDIOUT hmo = 0; // midi out device/port handle
  Chk (midiOutOpen (&hmo, moID, clbk, inst, flgs)); // open midi out in non-stream mode
#else
  // SysEx via midiOutLongMsg fails
  HMIDISTRM hms = 0; // midi out device/port handle
  Chk (midiStreamOpen (&hms, &moID, 1, clbk, inst, flgs)); // open midi out in stream mode
  HMIDIOUT hmo = (HMIDIOUT) hms;
#endif
  Chk (midiOutShortMsg (hmo, 0x00404090)); // note on
  Sleep (200); // ms
  Chk (midiOutShortMsg (hmo, 0x00004090)); // note off
  static unsigned char sx [] = { 0xF0, 0x7E, 0x7F, 0x09, 0x01, 0xF7 }; // GM On sysex
  static MIDIHDR mhdr; // midi header describes long msg
  UINT shdr = sizeof (mhdr);
  memset (&mhdr, 0, shdr); // clear header
  mhdr.lpData = (LPSTR) sx; // point to sysex
  mhdr.dwBufferLength = mhdr.dwBytesRecorded = sizeof (sx); // length of message bytes
  Chk (midiOutPrepareHeader (hmo, &mhdr, shdr)); // prepare hdr
  UINT r = midiOutLongMsg (hmo, &mhdr, shdr); // send message bytes
  Chk (r);
  // unprepare header, close etc. omitted ...
  return 0;
}