Exemplo n.º 1
0
        public async Task Manual([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 ManualEventInput();
            var format = new AudioFormat(sampleRate, 1, 32);

            player.Initialize(dev, format);

            var source     = new CancellationTokenSource();
            var runnerTask = player.Run(input, source.Token);

            var tone = new IonianTone()
            {
                Scale  = IonianScale.A,
                Octave = 4,
                Sharp  = false,
            };

            bool   exit = false;
            string line;

            while (!exit)
            {
                Console.Write(" > ");
                line = Console.ReadLine();
                switch (line)
                {
                case "":
                    break;

                case "attack":
                    input.NextEvent = new ToneKeyEvent(tone, false);
                    break;

                case "release":
                    input.NextEvent = new ToneKeyEvent(tone, true);
                    break;

                case "filter":
                    input.NextEvent = new ToneFilterEvent();
                    break;

                case "wave":
                    input.NextEvent = new ToneWaveEvent();
                    break;

                case "1":
                    input.NextEvent = new ToneThumbEvent(ThumbType.CutoffLow, ThumbDirection.CounterClockwise);
                    break;

                case "2":
                    input.NextEvent = new ToneThumbEvent(ThumbType.CutoffLow, ThumbDirection.Clockwise);
                    break;

                case "3":
                    input.NextEvent = new ToneThumbEvent(ThumbType.CutoffHigh, ThumbDirection.CounterClockwise);
                    break;

                case "4":
                    input.NextEvent = new ToneThumbEvent(ThumbType.CutoffHigh, ThumbDirection.Clockwise);
                    break;

                case "5":
                    input.NextEvent = new ToneThumbEvent(ThumbType.Sustain, ThumbDirection.CounterClockwise);
                    break;

                case "6":
                    input.NextEvent = new ToneThumbEvent(ThumbType.Sustain, ThumbDirection.Clockwise);
                    break;

                case "exit":
                    source.Cancel();
                    await runnerTask;
                    exit = true;
                    break;

                default:
                    Console.WriteLine($"Unknown command");
                    break;
                }
            }
        }
Exemplo n.º 2
0
        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();
            }
        }