public async Task Run([Option("s", "Serial port number")] int serial, [Option("d", "Output device index")] int?device = null, [Option("r", "Sample rate")] int sampleRate = 48000) { var player = new SynthesizerPlayer(); var dev = player.Output.DefaultDevice; if (device != null) { dev = player.Output.Devices[device.Value]; } var input = new SerialEventInput(); input.SetPortName(input.SerialPortNames[serial]); var format = new AudioFormat(sampleRate, 1, 32); player.Initialize(dev, format); var source = new CancellationTokenSource(); var runnerTask = player.Run(input, source.Token); bool exit = false; string[] cmd; while (!exit) { Console.Write(" > "); cmd = ParseArguments(Console.ReadLine()); if (cmd.Length == 0) { continue; } switch (cmd[0]) { case "": break; case "exit": source.Cancel(); await runnerTask; exit = true; break; case "record": switch (cmd[1]) { case "start": if (cmd.Length <= 3) { Console.WriteLine("Path was not specified"); continue; } try { player.StartRecording(cmd[1]); } catch (Exception ex) { Console.WriteLine(ex); Console.WriteLine($"Failed to create {cmd[1]} for writing"); } break; case "stop": player.StopRecording(); break; } break; default: Console.WriteLine($"Unknown command"); break; } } input.Dispose(); }
public async Task Midi([Option("m", "Midi file")] string midi, [Option("f", "Function type")] string function = "sin", [Option("s", "Serial port number")] int?serial = null, [Option("d", "Output device index")] int?device = null, double attack = 0.1, double decay = 0.1, double sustain = 0, double release = 0.2) { var player = new SynthesizerPlayer(); var dev = player.Output.DefaultDevice; if (device != null) { dev = player.Output.Devices[device.Value]; } var file = MidiFile.Read(midi); IEventInput input; if (serial != null) { var serialInput = new SerialEventInput(); serialInput.SetPortName(serialInput.SerialPortNames[serial.Value]); input = serialInput; } else { input = new ManualEventInput(); } var format = new AudioFormat(48000, 1, 32); player.SetThumb(ThumbType.Attack, attack); player.SetThumb(ThumbType.Decay, decay); player.SetThumb(ThumbType.Sustain, sustain); player.SetThumb(ThumbType.Release, release); player.InitializeMidi(file, dev, format); var source = new CancellationTokenSource(); var runnerTask = player.Run(input, source.Token); bool exit = false; string line; while (!exit) { Console.Write(" > "); line = Console.ReadLine(); switch (line) { case "": break; case "exit": source.Cancel(); await runnerTask; exit = true; break; default: if (input is ManualEventInput manual) { switch (line) { case "filter": manual.NextEvent = new ToneFilterEvent(); break; case "wave": manual.NextEvent = new ToneWaveEvent(); break; } } Console.WriteLine($"Unknown command"); break; } } if (input is IDisposable disp) { disp.Dispose(); } }