Example #1
0
        public static async Task StartAsync()
        {
            waveStream.Seek(44, SeekOrigin.Begin); // Header
            _output = await InitializeOutput(new AudioFormat(48000, 2, 16));

            RunBufferLoop(_output.Format);

            _output.Start();
        }
Example #2
0
        public void Devices()
        {
            var output = new SoundioOutput();

            for (var i = 0; i < output.Devices.Count; i++)
            {
                Console.WriteLine($"[{i}] {output.Devices[i].Name}");
            }
            output.Dispose();
        }
Example #3
0
        private void InitSynthesizer(SoundIOSharp.SoundIODevice device, AudioFormat format)
        {
            if (Output == null)
            {
                Output = new SoundioOutput();
            }

            Output.DesiredLatency = TimeSpan.FromMilliseconds(100);
            Output.Initialize(device, format);
            Output.Buffer += OutputOnBuffer;

            Synthesizer = new Synthesizer(format.SampleRate, 50);
        }
Example #4
0
        public void Execute(string[] args)
        {
            Console.WriteLine();
            var format = SoundioHelper.InitializeFormat(new AudioFormat(48000, 2));

            Console.WriteLine();

            if (!Input.InitializeInputDevice(format, args))
            {
                Input.InitializeInputFromConsole(format);
            }
            Console.WriteLine($"Input : {Input.Device.Name}, latency {Input.SoftwareLatency.TotalMilliseconds}ms");
            Console.WriteLine();

            if (!args.Contains("--no-out"))
            {
                Output = new SoundioOutput();
                if (!Output.InitializeOutputDevice(format, args))
                {
                    Output.InitializeOutputFromConsole(format);
                }
                Console.WriteLine($"Output : {Output.Device.Name}, latency {Output.SoftwareLatency.TotalMilliseconds}ms");
                Console.WriteLine();
            }

            Input.Filled += InputOnFilled;
            Input.Start();
            Output?.Start();

            Shell.Run("Passthrough", (arguments) =>
            {
                if (arguments.Length == 0)
                {
                    return;
                }

                switch (arguments[0])
                {
                case "export":
                    ExportConfig(arguments);
                    break;

                default:
                    Console.WriteLine("Unknown command");
                    break;
                }
            });
        }
        public static bool InitializeOutputDevice(this SoundioOutput output, AudioFormat format, string[] args)
        {
            var index = args.GetInt("-o", "--output");

            if (index == null || index.Value >= output.Devices.Count || index.Value < 0)
            {
                return(false);
            }

            var latencyMs = args.GetDouble("--output-latency");

            if (latencyMs != null)
            {
                output.DesiredLatency = TimeSpan.FromMilliseconds(latencyMs.Value);
            }

            output.Initialize(output.Devices[index.Value], format);
            return(true);
        }
Example #6
0
        public void Initialize(SoundIODevice device)
        {
            if (device is null)
            {
                throw new ArgumentNullException(nameof(device));
            }

            if (IsInitialized)
            {
                throw new InvalidOperationException("Output device was already initialized");
            }

            IsInitialized = false;
            var output = new SoundioOutput();

            output.Initialize(device, SharedFormat);
            Output        = output;
            IsInitialized = true;
        }
Example #7
0
        private static async Task <SoundioOutput> InitializeOutput(AudioFormat format)
        {
            var output = new SoundioOutput(SoundIOBackend.Alsa);

            output.Underflow         += OutputOnUnderflow;
            output.UnderflowTimedOut += OutputOnUnderflowTimedOut;

            for (int i = 0; i < output.Devices.Count; i++)
            {
                Console.WriteLine($"[{i}] {output.Devices[i].Name}");
            }
            var deviceIndex = ReadInt("Output device index : ");

            output.SetDevice(output.Devices[deviceIndex], format);
            output.Initialize();

            Console.WriteLine($"{output.Format.SampleRate} {output.Format.Channels}");

            return(output);
        }
        public static void InitializeOutputFromConsole(this SoundioOutput output, AudioFormat format)
        {
            Console.WriteLine("Output devices");
            for (var i = 0; output.Devices.Count > i; i++)
            {
                Console.WriteLine($"[{i}] {output.Devices[i].Name}");
            }

            var deviceIndex = ConsoleHelper.ReadInt("Output device index > ", output.Devices.Count - 1);
            var inputDevice = output.Devices[deviceIndex];

            for (var i = 0; output.Devices.Count > i; i++)
            {
                if (i != deviceIndex)
                {
                    output.Devices[i].RemoveReference();
                }
            }

            output.Initialize(inputDevice, format);
        }
        public static async Task StartAsync()
        {
            input.Filled += InputOnFilled;
            Console.WriteLine("Input devices");
            for (var i = 0; input.Devices.Count > i; i++)
            {
                Console.WriteLine($"[{i}] {input.Devices[i].Name}");
            }

            var deviceIndex = ReadInt("Input device index : ");
            var inputDevice = input.Devices[deviceIndex];

            for (var i = 0; input.Devices.Count > i; i++)
            {
                if (i != deviceIndex)
                {
                    input.Devices[i].RemoveReference();
                }
            }

            AudioFormat format = new AudioFormat(48000, 2, 16);

            input.SetDevice(inputDevice);
            await input.InitializeAsync(format);

            Console.WriteLine($"{input.Format.SampleRate} {input.Format.Channels}");

            _output = await InitializeOutput(format);

            _ringBuffer = new RingBuffer <byte>((uint)(format.SampleRate * format.Channels * 30));

            await input.StartAsync();

            _output.Start();

            Console.WriteLine("Input latency : " + input.SoftwareLatency.TotalMilliseconds + " ms");
            Console.WriteLine("Output latency : " + _output.SoftwareLatency.TotalMilliseconds + " ms");
        }
Example #10
0
        public static async Task StartAsync()
        {
            input.Filled += InputOnFilled;
            Console.WriteLine("Input devices");
            for (var i = 0; input.Devices.Count > i; i++)
            {
                Console.WriteLine($"[{i}] {input.Devices[i].Name}");
            }

            var deviceIndex = ReadInt("Input device index : ");
            var inputDevice = input.Devices[deviceIndex];

            for (var i = 0; input.Devices.Count > i; i++)
            {
                if (i != deviceIndex)
                {
                    input.Devices[i].RemoveReference();
                }
            }

            AudioFormat format = new AudioFormat(48000, 2, 16);

            input.SetDevice(inputDevice);
            await input.InitializeAsync(format);

            Console.WriteLine($"{input.Format.SampleRate} {input.Format.Channels}");

            _output = await InitializeOutput(format);

            await input.StartAsync();

//            Console.WriteLine("Buffering started");
//            Console.WriteLine("Press any key to start playback...");
//            Console.ReadKey();
            _output.Start();
        }
Example #11
0
        static void Main(string[] args)
        {
            var output = new SoundioOutput();

            for (var i = 0; i < output.Devices.Count; i++)
            {
                Console.WriteLine($"[{i}] {output.Devices[i].Name}");
            }

            Console.Write("Device Index > ");
            var outputIndex = int.Parse(Console.ReadLine());

            Console.Write("Latency(ms) > ");
            var intervalMs = Double.Parse(Console.ReadLine());

            output.DesiredLatency = TimeSpan.FromMilliseconds(intervalMs);
            output.Initialize(output.Devices[outputIndex], new AudioFormat(48000, 1, 16));
            Output = output;
            Console.WriteLine("Output device initialized");
            Console.WriteLine($"Actual Latency : {output.ActualLatency.TotalMilliseconds}ms");

            Source = new PeriodicFunctionsSource(output.Format.SampleRate)
            {
                Function = FunctionType.Sin,
                Volume   = 1,
            };

            var midi = new MidiPlayer(output.Format.SampleRate, new Synthesizer(output.Format.SampleRate));

            midi.OscillatorConfigs.Add(new Oscillator()
            {
                Function = KSynthesizer.Sources.FunctionType.Sin, Volume = 0.3f
            });
            midi.Open(MidiFile.Read("sample.mid"));

            Source.SetFrequency(Frequency);
            RecordFilter = new RecordFilter(midi);

            Output.Buffer += OutputOnBuffer;
            Output.Play();
            Console.WriteLine("Player started");

            bool exit = false;

            while (!exit)
            {
                var key = Console.ReadKey();
                Console.WriteLine();
                switch (key.Key)
                {
                case ConsoleKey.P:
                    Playing = !Playing;
                    Console.WriteLine(Playing ? "Resumed" : "Paused");
                    break;

                case ConsoleKey.R:
                    if (RecordFilter.IsRecording)
                    {
                        RecordFilter.StopRecording();
                    }
                    else
                    {
                        var now  = DateTime.Now;
                        var name = $"{now.Ticks}.wav";
                        RecordFilter.StartRecording(new FileStream(name, FileMode.Create, FileAccess.Write), true, false);
                        Console.WriteLine("Output : " + name);
                    }
                    Console.WriteLine(RecordFilter.IsRecording ? "Recording Started" : "Recording Stopped");
                    break;

                case ConsoleKey.UpArrow:
                    Frequency += 100;
                    Source.SetFrequency(Frequency);
                    Console.WriteLine($"Frequency : {Frequency}Hz");
                    break;

                case ConsoleKey.DownArrow:
                    Frequency -= 100;
                    if (Frequency <= 300)
                    {
                        Frequency = 300;
                    }
                    Source.SetFrequency(Frequency);
                    Console.WriteLine($"Frequency : {Frequency}Hz");
                    break;

                case ConsoleKey.Escape:
                    exit = true;
                    break;

                default:
                    Console.WriteLine("Unknown Key");
                    break;
                }
            }

            output.Dispose();
            Console.WriteLine("Exited");
        }
Example #12
0
        private bool InitAudio(string[] args, bool noShell)
        {
            var         path = args.GetString("--audio");
            AudioConfig audio;

            try
            {
                using var sr = new StreamReader(path);
                audio        = AudioConfig.Deserialize(sr.ReadToEnd());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("Failed to open the audio configuration file");
                Console.ResetColor();
                return(false);
            }

            Input = new SoundioInput();
            SoundIODevice inputDevice = null;

            foreach (var device in Input.Devices)
            {
                if (device.Name == audio.InputDevice)
                {
                    inputDevice = device;
                    break;
                }
            }

            if (inputDevice != null)
            {
                Input.Initialize(inputDevice, audio.Format);
                SayInitialized($"Input {inputDevice.Name}, latency {Input.SoftwareLatency.TotalMilliseconds}ms");
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine($"Input device was not found ({audio.InputDevice})");
                Console.ResetColor();
                return(false);
            }

            if (audio.OutputDevice != null)
            {
                SoundIODevice outputDevice = null;
                Output = new SoundioOutput();
                foreach (var device in Output.Devices)
                {
                    if (device.Name == audio.OutputDevice)
                    {
                        outputDevice = device;
                        break;
                    }
                }

                if (outputDevice != null)
                {
                    Output.Initialize(outputDevice, audio.Format);
                    SayInitialized($"Output {outputDevice.Name}, latency {Output.SoftwareLatency.TotalMilliseconds}ms");
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine($"Output device was not found ({audio.OutputDevice})");
                    Console.ResetColor();
                    if (noShell)
                    {
                        return(false);
                    }
                    else
                    {
                        return(ConsoleHelper.ReadYesNo("Proceed anyway? [yes/no] > "));
                    }
                }
            }

            return(true);
        }