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

时间:2016-02-17 14:48:00

标签: c# win-universal-app midi windows-10-universal

我正在尝试在Windows Universal Application中使用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());
        }
    }
}

1 个答案:

答案 0 :(得分:2)

我遇到了同样的问题。但我注意到通用应用程序样本的MIDI应用程序工作。我发现了.csproj文件的不同之处,特别是对于通用Windows应用程序的#34; Microsoft通用MIDI DLS的引用#34;。添加此引用后,我能够将输出发送到Wavetable Synth。

Windows开发人员中心MIDI article

中有说明
  

使用内置的Windows General MIDI合成器

     

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

     

在您的应用项目中加入通用MIDI Synth SDK扩展程序

     
      
  1. 解决方案资源管理器中,在您的项目下,右键单击参考并选择添加参考
  2.   
  3. 展开通用Windows 节点。
  4.   
  5. 选择扩展程序
  6.   
  7. 从扩展程序列表中,选择适用于通用Windows应用程序的Microsoft通用MIDI DLS
  8.   
  9. 注意如果扩展程序有多个版本,请务必选择与您的应用目标相匹配的版本。您可以在项目属性的“应用程序”选项卡上查看应用所针对的SDK版本。
  10.