无法使用NAudio和teVirtualMIDI发送Midi音符

时间:2019-01-21 00:16:57

标签: c# naudio

我正在尝试使用NAudio通过teVirtualMIDI创建的虚拟端口将Midi Note On消息发送到DAW软件。我能够在Ableton Live 10中看到teVirtualMIDI创建的“设备”,但是在Live中接收任何信息均失败。我的声音调好了,我再也没有看到Live的Midi计步器移动。代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using NAudio.Midi;
using TobiasErichsen.teVirtualMIDI;

namespace BDBConsole_1_0
{
    public class BDBMidiClient
    {
        public static ConsoleKeyInfo cKi;
        public static TeVirtualMIDI port;
        static MidiOut midiOut;
        int midiOutIndex;
        //public static List<MidiEvent> noteEvent;

        static void Main(string[] args) 
        {
            CreateMidiPort();
        }


        public static void CreateMidiPort()
        {
            TeVirtualMIDI.logging(TeVirtualMIDI.TE_VM_LOGGING_MISC | TeVirtualMIDI.TE_VM_LOGGING_RX | TeVirtualMIDI.TE_VM_LOGGING_TX);

            string portName = "BDB Client";
            //port = new TeVirtualMIDI(portName, 65535, TeVirtualMIDI.TE_VM_FLAGS_INSTANTIATE_BOTH);
            port = new TeVirtualMIDI(portName);

            Console.WriteLine("New Midi Port Opened...");
            Console.ReadKey();

            EnumerateMidiOutDevices();

            Thread thread = new Thread(new ThreadStart(SendNextMidiOutMessage)); 
            thread.Start();

            //SendNextMidiOutMessage();
        }


        public static void EnumerateMidiOutDevices() 
        {

            int noOutDevices = MidiOut.NumberOfDevices;
            Console.WriteLine("No. of Midi Out Devices..." + noOutDevices);
            Console.ReadKey();

            string deviceOutOne = MidiOut.DeviceInfo(1).ProductName;
            Console.WriteLine("Device One..." + deviceOutOne);
            Console.ReadKey();
        }


        public static void SendNextMidiOutMessage()
        {
            midiOut = new MidiOut(deviceNo: 0);
            //events = new List<MidiEvent>();
            int channel = 1;
            int note = 64;
            var noteOnEvent = new NoteOnEvent(0, channel, note, 127, 1000);

            try
            {
                //while (true) loop here before
                do
                {
                    Console.WriteLine("Send Midi Note...");
                    cKi = Console.ReadKey();

                    midiOut.Send(noteOnEvent.GetAsShortMessage());
                    Console.WriteLine("Note Sent...");
                    cKi = Console.ReadKey();
                } while (cKi.Key != ConsoleKey.Escape);

                port.shutdown();
                Console.WriteLine("Midi Port Closed...");
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                Console.WriteLine("thread aborting: " + ex.Message);
            }
        }
    }
}

任何对此的帮助将不胜感激!

0 个答案:

没有答案