Ejemplo n.º 1
0
        public void SetConfig(RealtimeHostConfig config)
        {
            if (config == null)
            {
                throw new ArgumentNullException();
            }

            var originalState = StreamState;

            if (StreamState == StreamState.Started)
            {
                StopStream();
            }
            if (StreamState == StreamState.Stopped)
            {
                CloseStream();
            }

            if (StreamState != StreamState.Closed)
            {
                throw new Exception("Stream could not be closed, unable to change settings!");
            }

            Config = config;

            if (originalState == StreamState.Started || originalState == StreamState.Open)
            {
                OpenStream();
            }

            if (originalState == StreamState.Started)
            {
                StartStream();
            }
        }
Ejemplo n.º 2
0
        public static RealtimeHostConfig Deserialize(string serializedString)
        {
            try
            {
                var dict = serializedString
                           .Split('\n')
                           .Where(x => x.Contains('='))
                           .Select(x => x.Trim())
                           .ToDictionary(x => x.Split('=')[0].Trim(), x => x.Split('=')[1].Trim());

                var apiName          = dict["APIName"];
                var inDeviceId       = Convert.ToInt32(dict["InputDeviceID"]);
                var outDeviceId      = Convert.ToInt32(dict["OutputDeviceID"]);
                var inputDeviceName  = dict["InputDeviceName"];
                var outputDeviceName = dict["OutputDeviceName"];
                var numInputs        = Convert.ToInt32(dict["NumberOfInputs"]);
                var numOutputs       = Convert.ToInt32(dict["NumberOfOutputs"]);

                // fuzzy matching of devices as the deviceIds can change
                // prefers a device that matches on apiName, deviceId and deviceName, but falls back to match only apiName and deviceName if not found

                var devices            = GetSelectedDevices(apiName, inDeviceId, outDeviceId, inputDeviceName, outputDeviceName);
                var realInputDeviceId  = devices.Item1;
                var realOutputDeviceId = devices.Item2;

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

                if (inDevice.maxInputChannels < numInputs)
                {
                    numInputs = inDevice.maxInputChannels;
                }

                if (outDevice.maxOutputChannels < numOutputs)
                {
                    numOutputs = outDevice.maxOutputChannels;
                }

                var conf = new RealtimeHostConfig();
                conf.InputDeviceID   = realInputDeviceId;
                conf.OutputDeviceID  = realOutputDeviceId;
                conf.NumberOfInputs  = numInputs;
                conf.NumberOfOutputs = numOutputs;
                conf.Samplerate      = Convert.ToInt32(dict["Samplerate"]);
                conf.BufferSize      = Convert.ToUInt32(dict["BufferSize"]);
                return(conf);
            }
            catch
            {
                return(null);
            }
        }
Ejemplo n.º 3
0
        public void SetConfig(RealtimeHostConfig config)
        {
            if (config == null)
                throw new ArgumentNullException();

            var originalState = StreamState;

            if (StreamState == StreamState.Started)
                StopStream();
            if (StreamState == StreamState.Stopped)
                CloseStream();

            if (StreamState != StreamState.Closed)
                throw new Exception("Stream could not be closed, unable to change settings!");

            Config = config;

            if (originalState == StreamState.Started || originalState == StreamState.Open)
                OpenStream();

            if (originalState == StreamState.Started)
                StartStream();
        }
Ejemplo n.º 4
0
        public static RealtimeHostConfig Deserialize(string serializedString)
        {
            try
            {
                var dict = serializedString
                    .Split('\n')
                    .Where(x => x.Contains('='))
                    .Select(x => x.Trim())
                    .ToDictionary(x => x.Split('=')[0].Trim(), x => x.Split('=')[1].Trim());

                var conf = new RealtimeHostConfig();
                conf.InputDeviceID = Convert.ToInt32(dict["InputDeviceID"]);
                conf.OutputDeviceID = Convert.ToInt32(dict["OutputDeviceID"]);
                conf.NumberOfInputs = Convert.ToInt32(dict["NumberOfInputs"]);
                conf.NumberOfOutputs = Convert.ToInt32(dict["NumberOfOutputs"]);
                conf.Samplerate = Convert.ToInt32(dict["Samplerate"]);
                conf.BufferSize = Convert.ToUInt32(dict["BufferSize"]);
                return conf;
            }
            catch
            {
                return null;
            }
        }
Ejemplo n.º 5
0
        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;
            }
        }
Ejemplo n.º 6
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);
            }
        }