/// <summary> /// sets the volume when a value is passed by the GUIs trackbar /// </summary> /// <param name="percentage">the volume value passed</param> public void SetVolume(int percentage) { int waveInDeviceNumber = 0; var mixerLine = new NAudio.Mixer.MixerLine((IntPtr)waveInDeviceNumber, 0, NAudio.Mixer.MixerFlags.WaveIn); foreach (var control in mixerLine.Controls) { if (control.ControlType == NAudio.Mixer.MixerControlType.Volume) { NAudio.Mixer.UnsignedMixerControl volumeControl = control as NAudio.Mixer.UnsignedMixerControl; volumeControl.Percent = (double)percentage; break; } } }
/// <summary> /// gets the volume level to show it on the trackbar /// </summary> /// <returns></returns> public int GetVolume() { int waveInDeviceNumber = 0; var mixerLine = new NAudio.Mixer.MixerLine((IntPtr)waveInDeviceNumber, 0, NAudio.Mixer.MixerFlags.WaveIn); foreach (var control in mixerLine.Controls) { if (control.ControlType == NAudio.Mixer.MixerControlType.Volume) { NAudio.Mixer.UnsignedMixerControl volumeControl = control as NAudio.Mixer.UnsignedMixerControl; return((int)volumeControl.Percent); } } return(-1); }
// ctor, constructor /// <exception cref="InitFailure">When initialization fails in a way that can not be continued from.</exception> public MicManager() { System.Diagnostics.Debug.Assert(Taskmaster.IsMainThread(), "Requires main thread"); // Target = Maximum; // superfluous; CLEANUP var stats = Taskmaster.Config.Load(statfile); // there should be easier way for this, right? Corrections = (stats.Config.Contains("Statistics") && stats.Config["Statistics"].Contains("Corrections")) ? stats.Config["Statistics"]["Corrections"].IntValue : 0; // DEVICES // find control interface // FIXME: Deal with multiple recording devices. var waveInDeviceNumber = IntPtr.Zero; // 0 is default or first? NAudio.Mixer.MixerLine mixerLine = null; try { mixerLine = new NAudio.Mixer.MixerLine(waveInDeviceNumber, 0, NAudio.Mixer.MixerFlags.WaveIn); } catch (NAudio.MmException ex) { Log.Fatal("<Microphone> Default device not found."); throw new InitFailure("Failed to get default microphone device.", ex); } Control = (NAudio.Mixer.UnsignedMixerControl)mixerLine.Controls.FirstOrDefault( (control) => control.ControlType == NAudio.Mixer.MixerControlType.Volume ); if (Control == null) { Log.Error("<Microphone> No volume control acquired!"); throw new InitFailure("Mic monitor control not acquired."); } _volume = Control.Percent; // get default communications device var mm_enum = new NAudio.CoreAudioApi.MMDeviceEnumerator(); m_dev = mm_enum?.GetDefaultAudioEndpoint(NAudio.CoreAudioApi.DataFlow.Capture, NAudio.CoreAudioApi.Role.Communications); if (m_dev != null) { m_dev.AudioEndpointVolume.OnVolumeNotification += VolumeChangedHandler; } mm_enum = null; // unnecessary? if (m_dev == null) { Log.Error("<Microphone> No communications device found!"); throw new InitFailure("No communications device found"); } var mvol = "Microphone volume"; var corecfg = Taskmaster.Config.Load(Taskmaster.coreconfig); var save = false || !corecfg.Config["Media"].Contains(mvol); var defaultvol = corecfg.Config["Media"].GetSetDefault(mvol, 100d).DoubleValue; if (save) { corecfg.MarkDirty(); } var fname = "Microphone.Devices.ini"; var vname = "Volume"; var devcfg = Taskmaster.Config.Load(fname); var guid = (m_dev.ID.Split('}'))[1].Substring(2); var devname = m_dev.DeviceFriendlyName; var unset = !(devcfg.Config[guid].Contains(vname)); var devvol = devcfg.Config[guid].GetSetDefault(vname, defaultvol).DoubleValue; devcfg.Config[guid]["Name"].StringValue = devname; if (unset) { devcfg.Save(); } Target = devvol.Constrain(0, 100); Log.Information("<Microphone> Default device: {Device} (volume: {TargetVolume:N1}%)", m_dev.FriendlyName, Target); Volume = Target; Log.Information("<Microphone> Component loaded."); }