public static RealtimeHostConfig CreateConfig(RealtimeHostConfig config = null)
        {
            if (!PortAudio.Pa_IsInitialized)
                PortAudio.Pa_Initialize();

            var editor = new RealtimeHostConfigEditor();

            if (config != null)
            {
                editor.InputDeviceID = config.InputDeviceID;
                editor.OutputDeviceID = config.OutputDeviceID;
                editor.Samplerate = config.Samplerate;
                editor.Latency = (int)(config.InputLatencyMs * config.Samplerate);
                editor.InputChannels = config.NumberOfInputs;
                editor.OutputChannels = config.NumberOfOutputs;
            }

            editor.ShowDialog();

            if (editor.OK == false)
                return null;
            else
            {
                var conf = new RealtimeHostConfig();
                conf.InputDeviceID = editor.InputDeviceID;
                conf.OutputDeviceID = editor.OutputDeviceID;
                conf.NumberOfInputs = editor.InputChannels;
                conf.NumberOfOutputs = editor.OutputChannels;
                conf.Samplerate = editor.Samplerate;
                conf.InputLatencyMs = editor.Latency / (double)editor.Samplerate;
                conf.OutputLatencyMs = editor.Latency / (double)editor.Samplerate;
                return conf;
            }
        }
Example #2
0
        public static RealtimeHostConfig CreateConfig(RealtimeHostConfig config = null)
        {
            if (!PortAudio.Pa_IsInitialized)
            {
                PortAudio.Pa_Initialize();
            }

            Application.EnableVisualStyles();
            var editor = new RealtimeHostConfigEditor();

            if (config != null)
            {
                var apiName = config.APIName;

                var devices            = GetSelectedDevices(apiName, config.InputDeviceID, config.OutputDeviceID, config.InputDeviceName, config.OutputDeviceName);
                var realInputDeviceId  = devices.Item1;
                var realOutputDeviceId = devices.Item2;

                var inDevice  = PortAudio.Pa_GetDeviceInfo(realInputDeviceId);
                var outDevice = PortAudio.Pa_GetDeviceInfo(realOutputDeviceId);

                var numInputs = (config.NumberOfInputs < inDevice.maxInputChannels)
                                        ? config.NumberOfInputs
                                        : inDevice.maxInputChannels;

                var numOutputs = (config.NumberOfOutputs < inDevice.maxOutputChannels)
                                        ? config.NumberOfOutputs
                                        : outDevice.maxOutputChannels;

                editor.InputDeviceID  = realInputDeviceId;
                editor.OutputDeviceID = realOutputDeviceId;
                editor.Samplerate     = config.Samplerate;
                editor.Latency        = (int)(config.InputLatencyMs * config.Samplerate);
                editor.InputChannels  = numInputs;
                editor.OutputChannels = numOutputs;
            }

            editor.ShowDialog();

            if (editor.OK == false)
            {
                return(null);
            }
            else
            {
                var conf = new RealtimeHostConfig();
                conf.InputDeviceID   = editor.InputDeviceID;
                conf.OutputDeviceID  = editor.OutputDeviceID;
                conf.NumberOfInputs  = editor.InputChannels;
                conf.NumberOfOutputs = editor.OutputChannels;
                conf.Samplerate      = editor.Samplerate;
                conf.InputLatencyMs  = editor.Latency / (double)editor.Samplerate;
                conf.OutputLatencyMs = editor.Latency / (double)editor.Samplerate;

                var supported = PortAudio.Pa_IsFormatSupported(ref conf.inputParameters, ref conf.outputParameters, conf.Samplerate);
                if (supported == PortAudio.PaError.paInvalidSampleRate)
                {
                    throw new InvalidFormatException("The samplerate you have selected is not supported by the device");
                }
                if (supported == PortAudio.PaError.paInvalidChannelCount)
                {
                    throw new InvalidFormatException("The number of inputs or outputs you have selected is not supported by the device");
                }
                if (supported != PortAudio.PaError.paNoError)
                {
                    throw new InvalidFormatException("The configuration you have selected is not supported by the device");
                }

                return(conf);
            }
        }