示例#1
0
        public void InterruptInput()
        {
            bool          passed = false, aborted = false;
            var           evt       = new AlsaSequencerEvent();
            AlsaSequencer seq       = null;
            int           appPort   = -1;
            var           waitStart = new ManualResetEvent(false);

            using (seq = new AlsaSequencer(AlsaIOType.Input, AlsaIOMode.None)) {
                appPort = seq.CreateSimplePort("alsa-sharp-test-input", AlsaPortCapabilities.Write | AlsaPortCapabilities.NoExport, AlsaPortType.Application | AlsaPortType.MidiGeneric);
                var cinfo = new AlsaClientInfo {
                    Client = -1
                };
                int client = -1;
                while (seq.QueryNextClient(cinfo))
                {
                    if (cinfo.Name.Contains("Midi Through"))
                    {
                        client = cinfo.Client;
                    }
                }
                if (client < 0)
                {
                    Console.Error.WriteLine("Midi Through not found. Not testable.");
                    return;                     // not testable
                }
                seq.ConnectFrom(appPort, client, 0);
                try {
                    seq.ResetPoolInput();
                    var task = Task.Run(() => {
                        waitStart.Set();
                        try {
                            seq.Input(evt, appPort);
                            passed = true;
                        } catch (Exception ex) {
                            passed  = false;
                            aborted = true;
                            Console.Error.WriteLine("Input threw an error: " + ex);
                        }
                    });
                    waitStart.WaitOne();
                    // it will cause ALSA error as it's waiting for input in blocking mode.
                    seq.DisconnectFrom(appPort, client, 0);
                    task.Wait(50);
                    Assert.IsFalse(passed, "input should keep listening");
                    seq.DeleteSimplePort(appPort);
                    task.Wait(50);
                    seq.Dispose();
                    seq = null;
                    task.Wait(50);
                } finally {
                    if (seq != null)
                    {
                        seq.Dispose();
                    }
                }
                // It doesn't even run exception part...
                Assert.IsFalse(aborted, "We expect it not to abort...");
            }
        }
示例#2
0
        public void Listening()
        {
            using (var seq = new AlsaSequencer(AlsaIOType.Input, AlsaIOMode.NonBlocking)) {
                var cinfo = new AlsaClientInfo {
                    Client = -1
                };
                int lastClient = -1;
                while (seq.QueryNextClient(cinfo))
                {
                    if (cinfo.Name.Contains("Keystation") || cinfo.Name.Contains("Seaboard"))
                    {
                        lastClient = cinfo.Client;
                    }
                }
                if (lastClient < 0)
                {
                    Console.Error.WriteLine("Supported devices not found. Not testable.");
                    return;                     // not testable
                }
                Console.Error.WriteLine("Press any MIDI key to continue...");

                int targetPort = 0;

                int appPort = seq.CreateSimplePort("alsa-sharp-test-input", AlsaPortCapabilities.Write | AlsaPortCapabilities.NoExport, AlsaPortType.Application | AlsaPortType.MidiGeneric);
                try {
                    seq.ConnectFrom(appPort, lastClient, targetPort);
                    var     data = new byte [3];
                    byte [] cbData = null;
                    var     wait = new ManualResetEvent(false);
                    int     cbStart = -1, cbLen = -1;
                    seq.StartListening(appPort, data, (_, start, len) => {
                        cbData  = _;
                        cbStart = start;
                        cbLen   = len;
                        wait.Set();
                    }, 60000);
                    wait.WaitOne(60000);
                    seq.StopListening();
                    Assert.IsNotNull(cbData, "received data");
                    Assert.AreEqual(0, cbStart, "received start");
                    Assert.AreEqual(3, cbLen, "received size");
                    seq.DisconnectFrom(appPort, lastClient, targetPort);
                } finally {
                    seq.DeleteSimplePort(appPort);
                }
            }
        }
示例#3
0
        public void Receive()
        {
            using (var seq = new AlsaSequencer(AlsaIOType.Input, AlsaIOMode.None)) {
                var cinfo = new AlsaClientInfo {
                    Client = -1
                };
                int lastClient = -1;
                while (seq.QueryNextClient(cinfo))
                {
                    if (cinfo.Name.Contains("Keystation"))
                    {
                        lastClient = cinfo.Client;
                    }
                }
                if (lastClient < 0)
                {
                    Console.Error.WriteLine("Keystation not found. Not testable.");
                    return;                     // not testable
                }
                Console.Error.WriteLine("Press any key on Keystation to continue...");

                int targetPort = 0;

                int appPort = seq.CreateSimplePort("alsa-sharp-test-input", AlsaPortCapabilities.Write | AlsaPortCapabilities.NoExport, AlsaPortType.Application | AlsaPortType.MidiGeneric);
                try {
                    seq.ConnectFrom(appPort, lastClient, targetPort);
                    var data     = new byte [3];
                    var received = seq.Receive(appPort, data, 0, 3);
                    Assert.AreEqual(3, received, "received size");
                    Assert.AreEqual(0x90, data [0], "received status");
                    seq.DisconnectFrom(appPort, lastClient, targetPort);
                } finally {
                    seq.DeleteSimplePort(appPort);
                }
            }
        }
示例#4
0
        public void InputToObserveSystemAnnoucements()
        {
            bool          passed  = false;
            var           evt     = new AlsaSequencerEvent();
            AlsaSequencer inseq   = null;
            int           appPort = -1;
            Task          task    = new TaskFactory().StartNew(() => {
                using (inseq = new AlsaSequencer(AlsaIOType.Input, AlsaIOMode.None)) {
                    appPort = inseq.CreateSimplePort("alsa-sharp-test-input", AlsaPortCapabilities.Write | AlsaPortCapabilities.NoExport, AlsaPortType.Application | AlsaPortType.MidiGeneric);
                    inseq.ConnectFrom(appPort, AlsaSequencer.ClientSystem, AlsaPortInfo.PortSystemAnnouncement);
                    try {
                        inseq.ResetPoolInput();
                        // ClientStart, PortStart, PortSubscribed, PortUnsubscribed, PortExit
                        inseq.Input(evt, appPort);
                        Assert.AreEqual(AlsaSequencerEventType.ClientStart, evt.EventType, "evt1");
                        inseq.Input(evt, appPort);
                        Assert.AreEqual(AlsaSequencerEventType.PortStart, evt.EventType, "evt2");
                        inseq.Input(evt, appPort);
                        Assert.AreEqual(AlsaSequencerEventType.PortSubscribed, evt.EventType, "evt3");
                        passed = true;
                    } finally {
                        inseq.DisconnectFrom(appPort, AlsaSequencer.ClientSystem, AlsaPortInfo.PortSystemAnnouncement);
                        appPort = -1;
                    }
                }
            });

            Thread.Sleep(50);              // give some time for announcement client to start.

            // create another port, which is a dummy and just subscribes to notify the system to raise an announcement event.
            var cinfo = new AlsaClientInfo {
                Client = -1
            };
            int lastClient = -1;
            var outseq     = new AlsaSequencer(AlsaIOType.Input, AlsaIOMode.NonBlocking);

            while (outseq.QueryNextClient(cinfo))
            {
                if (cinfo.Name.Contains("Midi Through"))
                {
                    lastClient = cinfo.Client;
                }
            }
            if (lastClient < 0)
            {
                Console.Error.WriteLine("Midi Through not found. Not testable.");
                return;                 // not testable
            }
            int targetPort = 0;

            int testPort = outseq.CreateSimplePort("alsa-sharp-test-output", AlsaPortCapabilities.Write | AlsaPortCapabilities.NoExport, AlsaPortType.Application | AlsaPortType.MidiGeneric);

            try {
                outseq.ConnectTo(testPort, lastClient, targetPort);
                outseq.DisconnectTo(testPort, lastClient, targetPort);
                Thread.Sleep(50);                  // give some time for announcement client to finish.
                Assert.IsTrue(passed, "failed to receive an announcement");
            } finally {
                outseq.DeleteSimplePort(testPort);
            }
        }