internal int GetCount()
        {
            int result;

            Marshal.ThrowExceptionForHR(_AudioMeterInformation.GetMeteringChannelCount(out result));
            return(result);
        }
Ejemplo n.º 2
0
        public static float[] ReadPeakValues(IAudioMeterInformation meter)
        {
            var ret = new float[2];

            try
            {
                uint chanCount = meter.GetMeteringChannelCount();
                if (chanCount > 0)
                {
                    var arrayPtr = Marshal.AllocHGlobal((int)chanCount * 4); // 4 bytes in float
                    if (meter.GetChannelsPeakValues(chanCount, arrayPtr) == HRESULT.S_OK)
                    {
                        var values = new float[chanCount];
                        Marshal.Copy(arrayPtr, values, 0, (int)chanCount);

                        if (chanCount == 1)
                        {
                            ret[0] = values[0];
                            ret[1] = values[0];
                        }
                        else
                        {
                            ret[0] = values[0];
                            ret[1] = values[1];
                        }
                    }
                }
            }
            catch (Exception ex) when(ex.Is(HRESULT.AUDCLNT_E_DEVICE_INVALIDATED))
            {
                // Expected in some cases.
            }
            return(ret);
        }
Ejemplo n.º 3
0
        // ******************************** Audio Devices - Audio Input Peak Meter - Open / Close / GetValues

        #region  Audio Devices - Audio Input Peak Meter - Open / Close / GetValues

        internal bool InputMeter_Open(AudioInputDevice device, bool change)
        {
            // tested for null at event subscription

            if (!pm_HasInputMeter || change)
            {
                IMMDeviceEnumerator deviceEnumerator = null;
                IMMDevice           levelDevice      = null;

                if (pm_HasInputMeter)
                {
                    InputMeter_Close();
                }

                try
                {
                    deviceEnumerator = (IMMDeviceEnumerator) new MMDeviceEnumerator();
                    deviceEnumerator.GetDevice(device._id, out levelDevice);

                    if (levelDevice != null)
                    {
                        levelDevice.Activate(ref IID_IAudioMeterInformation, 3, IntPtr.Zero, out object levelDeviceInfo);
                        pm_InputMeterInfo = (IAudioMeterInformation)levelDeviceInfo;

                        pm_InputMeterInfo.GetMeteringChannelCount(out pm_InputMeterChannelCount);
                        if (pm_InputMeterChannelCount > MAX_AUDIO_CHANNELS)
                        {
                            pm_InputMeterChannelCount = MAX_AUDIO_CHANNELS;
                        }

                        if (pm_InputMeterValues == null)
                        {
                            pm_InputMeterValues     = new float[MAX_AUDIO_CHANNELS];
                            pm_InputMeterValuesStop = new float[MAX_AUDIO_CHANNELS];
                            for (int i = 0; i < MAX_AUDIO_CHANNELS; i++)
                            {
                                pm_InputMeterValuesStop[i] = STOP_VALUE;
                            }
                        }
                        pm_HasInputMeter = true;

                        //StartSystemDevicesChangedHandlerCheck();
                    }
                }
                catch
                { /* ignore */
                }

                if (levelDevice != null)
                {
                    Marshal.ReleaseComObject(levelDevice);
                }
                if (deviceEnumerator != null)
                {
                    Marshal.ReleaseComObject(deviceEnumerator);
                }
            }
            return(pm_HasInputMeter);
        }
Ejemplo n.º 4
0
        // ******************************** Player Peak Meter - Open / Close / GetValues

        #region Player Peak Meter - Open / Close / GetValues

        internal bool PeakMeter_Open(AudioDevice device, bool change)
        {
            if (!pm_HasPeakMeter || change)
            {
                IMMDeviceEnumerator deviceEnumerator = null;
                IMMDevice           levelDevice      = null;
                object levelDeviceInfo;

                if (pm_HasPeakMeter)
                {
                    PeakMeter_Close();
                }

                try
                {
                    deviceEnumerator = (IMMDeviceEnumerator) new MMDeviceEnumerator();
                    if (device == null)
                    {
                        deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia, out levelDevice);
                    }
                    else
                    {
                        deviceEnumerator.GetDevice(device._id, out levelDevice);
                    }

                    if (levelDevice != null)
                    {
                        levelDevice.Activate(ref IID_IAudioMeterInformation, 3, IntPtr.Zero, out levelDeviceInfo);
                        pm_PeakMeterInfo = (IAudioMeterInformation)levelDeviceInfo;

                        pm_PeakMeterInfo.GetMeteringChannelCount(out pm_PeakMeterChannelCount);
                        if (pm_PeakMeterChannelCount > MAX_AUDIO_CHANNELS)
                        {
                            pm_PeakMeterChannelCount = MAX_AUDIO_CHANNELS;
                        }

                        if (pm_PeakMeterValues == null)
                        {
                            pm_PeakMeterValues     = new float[MAX_AUDIO_CHANNELS];
                            pm_PeakMeterValuesStop = new float[MAX_AUDIO_CHANNELS];
                            for (int i = 0; i < MAX_AUDIO_CHANNELS; i++)
                            {
                                pm_PeakMeterValuesStop[i] = STOP_VALUE;
                            }
                        }
                        pm_HasPeakMeter = true;

                        StartSystemDevicesChangedHandlerCheck();
                    }
                }
                catch
                { /* ignore */
                }

                if (levelDevice != null)
                {
                    Marshal.ReleaseComObject(levelDevice);
                }
                if (deviceEnumerator != null)
                {
                    Marshal.ReleaseComObject(deviceEnumerator);
                }
            }
            return(pm_HasPeakMeter);
        }