private void PeekUpdateTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { try { synchronizationContext.Post(s => { int PeekCount = Device.AudioMeterInformation.PeakValues.Count; _Master.Peek = Device.AudioMeterInformation.MasterPeakValue; for (int iPeek = 0; iPeek < PeekCount; ++iPeek) { _Channels[iPeek].Peek = Device.AudioMeterInformation.PeakValues[iPeek]; } } , null); OnPropertyChanged("Master"); OnPropertyChanged("Channels"); } catch (Exception ex) { Misc.LogStatics.Debug("{0}", ex); _HasException = true; } finally { if (!_HasException) { PeekUpdateTimer.Start(); } } }
public AudioDeviceViewModel(NAudio.CoreAudioApi.MMDevice audioDevice) { synchronizationContext = SynchronizationContext.Current; Device = audioDevice; if (Device.State == NAudio.CoreAudioApi.DeviceState.Active && Device.DataFlow == NAudio.CoreAudioApi.DataFlow.Render) { float MaxDecibels = Device.AudioEndpointVolume.VolumeRange.MaxDecibels; float MinDecibels = Device.AudioEndpointVolume.VolumeRange.MinDecibels; Misc.LogStatics.Debug("Device:{0} MasterLevel={1} MasterScalar={2} MaxDecibels={3} MinDecibels={4}", Device.FriendlyName, Device.AudioEndpointVolume.MasterVolumeLevel, Device.AudioEndpointVolume.MasterVolumeLevelScalar, MaxDecibels, MinDecibels); try { int ChannelCount = Math.Max(Device.AudioEndpointVolume.Channels.Count, Device.AudioMeterInformation.PeakValues.Count); _Master.Volume = Math.Round(Device.AudioEndpointVolume.MasterVolumeLevelScalar * 100.0); _Master.Peek = Device.AudioMeterInformation.MasterPeakValue; _Channels = new ChannelViewModel[ChannelCount]; for (int iChannel = 0; iChannel < ChannelCount; ++iChannel) { double ChVol = 0.0; double ChPeek = 0.0; if (iChannel < Device.AudioEndpointVolume.Channels.Count) { NAudio.CoreAudioApi.AudioEndpointVolumeChannel Channel = Device.AudioEndpointVolume.Channels[iChannel]; ChVol = Math.Round(Channel.VolumeLevelScalar * 100.0); Misc.LogStatics.Debug("\tCH:{0} Level={1} Scalar={2}", iChannel, Channel.VolumeLevel, Channel.VolumeLevelScalar); } if (iChannel < Device.AudioEndpointVolume.Channels.Count) { ChPeek = Device.AudioMeterInformation.PeakValues[iChannel]; } _Channels[iChannel] = new ChannelViewModel(ChVol, ChPeek); } } catch (Exception ex) { Misc.LogStatics.Debug("{0}", ex); } try { Device.AudioEndpointVolume.OnVolumeNotification += AudioEndpointVolume_OnVolumeNotification; PeekUpdateTimer.Elapsed += PeekUpdateTimer_Elapsed; PeekUpdateTimer.Start(); } catch (Exception ex) { Misc.LogStatics.Debug("{0}", ex); } } }