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..."); } }
public void EnumerateClientsAndPortsPrimitive() { using (var seq = new AlsaSequencer(AlsaIOType.Output, AlsaIOMode.NonBlocking)) { var cli = new AlsaClientInfo(); cli.Client = -1; while (seq.QueryNextClient(cli)) { TextWriter.Null.WriteLine("Client:" + cli.Client); foreach (var cpi in cli.GetType().GetProperties()) { TextWriter.Null.WriteLine($" [{cpi}]\t{cpi.GetValue (cli)}"); } var port = new AlsaPortInfo(); port.Client = cli.Client; port.Port = -1; while (seq.QueryNextPort(port)) { TextWriter.Null.WriteLine(" Port:" + port.Id); foreach (var ppi in port.GetType().GetProperties()) { TextWriter.Null.WriteLine($" [{ppi}]\t{ppi.GetValue (port)}"); } } } } }
public AlsaMidiAccess() { input = new AlsaSequencer(AlsaIOType.Duplex, AlsaIOMode.NonBlocking); input_client_id = input.CurrentClientId; output = new AlsaSequencer(AlsaIOType.Output, AlsaIOMode.NonBlocking); output_client_id = output.CurrentClientId; system_announcement = new AlsaSequencer(AlsaIOType.Input, AlsaIOMode.NonBlocking); }
public void SetClientName() { using (var seq = new AlsaSequencer(AlsaIOType.Input, AlsaIOMode.NonBlocking, "default")) { Assert.AreEqual("default", seq.Name, "#1"); seq.SetClientName("overwritten sequencer name"); Assert.AreEqual("default", seq.Name, "#2"); Assert.AreEqual("overwritten sequencer name", seq.GetClient(seq.CurrentClientId).Name, "#3"); } }
public static void Stop() { if (!isRunning) { return; } sequencer.Dispose(); sequencer = null; isRunning = false; }
public void GetPort() { using (var seq = new AlsaSequencer(AlsaIOType.Input, AlsaIOMode.NonBlocking)) { var port = seq.GetPort(AlsaSequencer.ClientSystem, AlsaPortInfo.PortSystemAnnouncement); foreach (var ppi in port.GetType().GetProperties()) { TextWriter.Null.WriteLine($" [{ppi}]\t{ppi.GetValue (port)}"); } } }
public void GetClient() { using (var seq = new AlsaSequencer(AlsaIOType.Input, AlsaIOMode.NonBlocking)) { var cli = seq.GetClient(AlsaSequencer.ClientSystem); foreach (var cpi in cli.GetType().GetProperties()) { TextWriter.Null.WriteLine($" [{cpi}]\t{cpi.GetValue (cli)}"); } } }
// app generates messages --> [RETURNED PORT] --> [output device port] AlsaPortInfo CreateOutputConnectedPort(AlsaSequencer seq, AlsaPortInfo pinfo, string portName = "alsa-sharp output") { var portId = seq.CreateSimplePort(portName, output_connected_cap, midi_port_type); var sub = new AlsaPortSubscription(); sub.Sender.Client = (byte)seq.CurrentClientId; sub.Sender.Port = (byte)portId; sub.Destination.Client = (byte)pinfo.Client; sub.Destination.Port = (byte)pinfo.Port; seq.SubscribePort(sub); return(seq.GetPort(sub.Sender.Client, sub.Sender.Port)); }
public Task <IMidiInput> OpenInputAsync(string portId) { var sourcePort = (AlsaMidiPortDetails)Inputs.FirstOrDefault(p => p.Id == portId); if (sourcePort == null) { throw new ArgumentException($"Port '{portId}' does not exist."); } var seq = new AlsaSequencer(AlsaIOType.Input, AlsaIOMode.NonBlocking); var appPort = CreateInputConnectedPort(seq, sourcePort.PortInfo); return(Task.FromResult <IMidiInput> (new AlsaMidiInput(seq, new AlsaMidiPortDetails(appPort), sourcePort))); }
public AlsaMidiInput(AlsaSequencer seq, AlsaMidiPortDetails appPort, AlsaMidiPortDetails sourcePort) { this.seq = seq; this.port = appPort; byte [] buffer = new byte [0x200]; seq.StartListening(port.PortInfo.Port, buffer, (buf, start, len) => { var args = new MidiReceivedEventArgs() { Data = buf, Start = start, Length = len, Timestamp = 0 }; MessageReceived(this, args); }); }
public override IMidiInput CreateVirtualOutputReceiver(PortCreatorContext context) { var seq = new AlsaSequencer(AlsaIOType.Duplex, AlsaIOMode.NonBlocking); var portNumber = seq.CreateSimplePort( context.PortName ?? "managed-midi virtual out", AlsaMidiAccess.virtual_output_connected_cap, AlsaMidiAccess.midi_port_type); seq.SetClientName(context.ApplicationName ?? "managed-midi output port creator"); var port = seq.GetPort(seq.CurrentClientId, portNumber); var details = new AlsaMidiPortDetails(port); return(new SimpleVirtualMidiInput(details, () => seq.DeleteSimplePort(portNumber))); }
public void SystemInfo() { using (var seq = new AlsaSequencer(AlsaIOType.Output, AlsaIOMode.NonBlocking)) { using (var sys = new AlsaSystemInfo()) { sys.SetContextSequencer(seq); Assert.AreEqual(1, sys.CurrentQueueCount, "cur_q"); Assert.IsTrue(0 < sys.CurrentClientCount, "cur_ch"); Assert.IsTrue(0 < sys.PortCount, "port"); Assert.IsTrue(0 < sys.ChannelCount, "ch"); Assert.IsTrue(sys.CurrentQueueCount < sys.MaxQueueCount, "max_q"); Assert.IsTrue(sys.CurrentClientCount < sys.MaxClientCount, "max_cli"); } } }
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); } } }
public void Send() { using (var seq = new AlsaSequencer(AlsaIOType.Output, AlsaIOMode.NonBlocking)) { var cinfo = new AlsaClientInfo { Client = -1 }; int lastClient = -1; while (seq.QueryNextClient(cinfo)) { if (cinfo.Name.Contains("TiMidity")) { lastClient = cinfo.Client; } } if (lastClient < 0) { Console.Error.WriteLine("TiMidity not found. Not testable."); return; // not testable } int targetPort = 3; try { seq.GetPort(lastClient, targetPort); } catch { Console.Error.WriteLine("TiMidity port #3 not available. Not testable."); return; // not testable } int appPort = seq.CreateSimplePort("alsa-sharp-test-output", AlsaPortCapabilities.Write | AlsaPortCapabilities.NoExport, AlsaPortType.Application | AlsaPortType.MidiGeneric); try { seq.ConnectTo(appPort, lastClient, targetPort); var setup = new byte [] { 0xC0, 0x48, 0xB0, 7, 110, 0xB0, 11, 127 }; var keyon = new byte [] { 0x90, 0x40, 0x70 }; var keyoff = new byte [] { 0x80, 0x40, 0x70 }; seq.Send(appPort, setup, 0, setup.Length); seq.Send(appPort, keyon, 0, keyon.Length); System.Threading.Thread.Sleep(100); seq.Send(appPort, keyoff, 0, keyoff.Length); System.Threading.Thread.Sleep(100); seq.DisconnectTo(appPort, lastClient, targetPort); } finally { seq.DeleteSimplePort(appPort); } } }
public override IMidiOutput CreateVirtualInputSender(PortCreatorContext context) { var seq = new AlsaSequencer(AlsaIOType.Duplex, AlsaIOMode.NonBlocking); var portNumber = seq.CreateSimplePort( context.PortName ?? "managed-midi virtual in", AlsaMidiAccess.virtual_input_connected_cap, AlsaMidiAccess.midi_port_type); seq.SetClientName(context.ApplicationName ?? "managed-midi input port creator"); var port = seq.GetPort(seq.CurrentClientId, portNumber); var details = new AlsaMidiPortDetails(port); SendDelegate send = (buffer, start, length, timestamp) => seq.Send(portNumber, buffer, start, length); return(new SimpleVirtualMidiOutput(details, () => seq.DeleteSimplePort(portNumber)) { OnSend = send }); }
public void SubscribeUnsubscribePort() { using (var seq = new AlsaSequencer(AlsaIOType.Input, AlsaIOMode.NonBlocking)) { var subs = new AlsaPortSubscription(); subs.Sender.Client = AlsaSequencer.ClientSystem; subs.Sender.Port = AlsaPortInfo.PortSystemAnnouncement; subs.Destination.Client = (byte)seq.CurrentClientId; subs.Destination.Port = (byte)seq.CreateSimplePort("test in port", AlsaPortCapabilities.SubsRead | AlsaPortCapabilities.Read, AlsaPortType.MidiGeneric | AlsaPortType.Application); try { seq.SubscribePort(subs); foreach (var ppi in subs.GetType().GetProperties()) { TextWriter.Null.WriteLine($" [{ppi}]\t{ppi.GetValue (subs)}"); } seq.UnsubscribePort(subs); } finally { seq.DeleteSimplePort(subs.Destination.Port); } } }
IEnumerable <AlsaPortInfo> EnumerateMatchingPorts(AlsaSequencer seq, AlsaPortCapabilities cap) { var cinfo = new AlsaClientInfo { Client = -1 }; while (seq.QueryNextClient(cinfo)) { var pinfo = new AlsaPortInfo { Client = cinfo.Client, Port = -1 }; while (seq.QueryNextPort(pinfo)) { if ((pinfo.PortType & midi_port_type) != 0 && (pinfo.Capabilities & cap) == cap) { yield return(pinfo.Clone()); } } } }
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); } } }
public static void StartSequencer(AlsaIOType type, AlsaIOMode mode, string drivername = "default") { sequencer = new AlsaSequencer(type, mode, drivername); isRunning = true; }
public AlsaMidiOutput(AlsaSequencer seq, AlsaMidiPortDetails port) { this.seq = seq; this.port = port; }
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); } }
public void SetClientNameInvalid() { Assert.Throws(typeof(AlsaException), () => { var seq = new AlsaSequencer(AlsaIOType.Input, AlsaIOMode.NonBlocking, "this_is_an_invalid_config_name_and_should_throw_exception"); }, "should throw ALSA exception"); }
public AlsaMidiOutput(AlsaSequencer seq, AlsaMidiPortDetails appPort, AlsaMidiPortDetails targetPort) { this.seq = seq; this.port = appPort; }
public AlsaMidiAccess() { ExtensionManager = new AlsaMidiAccessExtensionManager(this); system_watcher = new AlsaSequencer(AlsaIOType.Duplex, AlsaIOMode.NonBlocking); }
public AlsaMidiAccess() { system_watcher = new AlsaSequencer(AlsaIOType.Duplex, AlsaIOMode.NonBlocking); }