Example #1
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);
        }