c# 如何使用 Windows.Devices.Midi 将 MIDI 消息发送到内置的 Microsoft GS Wa

本文关键字:内置 Microsoft Wa GS 消息 Windows 何使用 Devices Midi MIDI | 更新日期: 2023-09-27 18:35:06

我正在尝试在Windows通用应用程序中使用C#将MIDI消息发送到Windows 10上内置Microsoft GS Wavetable Synth。但是,检索 IMidiOutPort 对象 ( outputPort = await MidiOutPort.FromIdAsync(outputDevice.Id); ) 的代码始终返回 null。如何将 MIDI 消息发送到内置合成器?

在MidiInPort中使用类似的代码可以与外部MIDI键盘完美配合。不幸的是,键盘没有 MIDI,否则我会尝试发送到它以尝试缩小可能性。

附带说明一下,创建 MidiSynthesizer (MidiSynthesizer synth = await MidiSynthesizer.CreateAsync();) 的代码也返回 null。

using System;
using Windows.Devices.Enumeration;
using Windows.Devices.Midi;
using Windows.UI.Xaml.Controls;
namespace HelloMidi
{
    public sealed partial class MainPage : Page
    {
        IMidiOutPort outputPort;
        DeviceInformation outputDevice;
        DeviceInformationCollection outputDevices;
        public MainPage()
        {
            this.InitializeComponent();
            Connect();
        }
        public async void Connect()
        {
            outputDevices = await DeviceInformation.FindAllAsync(MidiOutPort.GetDeviceSelector());
            outputList.DisplayMemberPath = "Name";
            outputList.ItemsSource = outputDevices;
            outputList.SelectionMode = SelectionMode.Single;
            outputList.SelectionChanged += OutputList_SelectionChanged;
        }
        private async void OutputList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (outputPort != null)
                outputPort.Dispose();
            if (e.AddedItems.Count > 0)
                outputDevice = (DeviceInformation)e.AddedItems[0];
            if (outputDevice != null)
                outputPort = await MidiOutPort.FromIdAsync(outputDevice.Id);
            if (outputPort != null)
                outputPort.SendMessage(new MidiStartMessage());
        }
    }
}

c# 如何使用 Windows.Devices.Midi 将 MIDI 消息发送到内置的 Microsoft GS Wa

我遇到了同样的问题。 但我注意到通用应用程序示例中的 MIDI 应用程序有效。 我发现.csproj文件有所不同,特别是提到了"通用Windows应用程序的Microsoft通用MIDI DLS"。 添加此引用后,我能够将输出发送到波表合成器。

Windows Dev Center MIDI 文章中有说明

使用内置的 Windows General MIDI 合成器

使用上述技术枚举输出 MIDI 设备时,应用将发现名为"Microsoft GS 波表合成器"的 MIDI 设备。这是一个内置的通用 MIDI 合成器,您可以从您的应用程序播放。但是,尝试创建到此设备的 MIDI 输出端口将失败,除非您在项目中包含了内建合成器的 SDK 扩展。

在应用项目中包括通用 MIDI 合成器 SDK 扩展

  1. "解决方案资源管理器"中的项目下,右键单击"引用",然后选择"添加引用..."
  2. 展开"通用窗口"节点。
  3. 选择"扩展"。
  4. 从扩展名列表中,选择"Microsoft通用 Windows 应用的通用 MIDI DLS"。
  5. 注意 如果扩展有多个版本,请务必选择与应用目标匹配的版本。可以在项目"属性"的"应用程序"选项卡上查看应用的目标 SDK 版本。