Ejemplo n.º 1
0
        public WindowsAudio()
        {
            var device = new MMDeviceEnumerator().GetDefaultAudioEndpoint(DataFlow.Render, Role.Console);

            _volume = AudioEndpointVolume.FromDevice(device);
            _meter  = AudioMeterInformation.FromDevice(device);
        }
Ejemplo n.º 2
0
        public static void Main()
        {
            using (MMDeviceEnumerator enumerator = new MMDeviceEnumerator())
                using (MMDevice device = enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Console))
                    using (AudioEndpointVolume endpointVolume = AudioEndpointVolume.FromDevice(device))
                        using (AudioSessionManager2 sessionManager2 = AudioSessionManager2.FromMMDevice(device))
                            using (AudioSessionEnumerator sessionEnumerator = sessionManager2.GetSessionEnumerator())
                            {
                                Console.WriteLine("Default Render Device: " + device.FriendlyName);
                                Console.WriteLine("Master Volume Scalar: " + endpointVolume.GetMasterVolumeLevelScalar());
                                Console.WriteLine("\nGetting audio sessions...");

                                foreach (AudioSessionControl sessionControl in sessionEnumerator)
                                {
                                    PrintSessionName(sessionControl);
                                    Console.WriteLine("\t- State: " + sessionControl.SessionState.ToString());

                                    using (SimpleAudioVolume sessionSimpleVolume = sessionControl.QueryInterface <SimpleAudioVolume>())
                                    {
                                        Console.WriteLine("\t- Volume: " + sessionSimpleVolume.MasterVolume);
                                        sessionSimpleVolume.MasterVolume = 1.0f;
                                    }

                                    sessionControl.Dispose();
                                }
                            }
            Console.WriteLine("\nVolumes reset!");
            Console.ReadLine();
        }
Ejemplo n.º 3
0
 private void OnAudioDeviceChanged(object sender, AudioDeviceChangedEventArgs e)
 {
     if (e.DefaultPlayback != null)
     {
         _endPointVolume = AudioEndpointVolume.FromDevice(e.DefaultPlayback);
     }
 }
Ejemplo n.º 4
0
        public void CreateDevice(EventHandler <DefaultDeviceChangedEventArgs> aDefaultDeviceChangedHandler,
                                 EventHandler <AudioEndpointVolumeCallbackEventArgs> aVolumeChangedHandler)
        {
            try
            {
                //Create device and register default device change notification
                _mMdeviceEnumerator           = new MMDeviceEnumerator();
                iMultiMediaNotificationClient = new MMNotificationClient(_mMdeviceEnumerator);
                iMultiMediaNotificationClient.DefaultDeviceChanged += iDefaultDeviceChangedHandler = aDefaultDeviceChangedHandler;
                var mMdeviceList = _mMdeviceEnumerator.EnumAudioEndpoints(DataFlow.Render, DeviceState.Active);

                if (mMdeviceList != null && mMdeviceList.Count > 0)
                {
                    _mMdevice = _mMdeviceEnumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
                    GUIGraphicsContext.CurrentAudioRendererDevice = _mMdevice.FriendlyName;

                    //Register to get volume modifications
                    if (_mMdevice != null)
                    {
                        iAudioEndpointVolume = AudioEndpointVolume.FromDevice(_mMdevice);
                    }
                    iAudioEndpointVolumeMixerCallback = new CSCore.CoreAudioAPI.AudioEndpointVolumeCallback();
                    iAudioEndpointVolumeMixerCallback.NotifyRecived += iVolumeChangedHandler = aVolumeChangedHandler;
                    iAudioEndpointVolume?.RegisterControlChangeNotify(iAudioEndpointVolumeMixerCallback);
                }

                // For audio session
                Stop();
                //DispatchingTimerStart(); // Disable because the check will be done in IsMuted code
            }
            catch (Exception)
            {
                // When no device available
            }
        }
Ejemplo n.º 5
0
 public void CanCreateAudioEndpointVolume()
 {
     using (var device = Utils.GetDefaultRenderDevice())
         using (var endpointVolume = AudioEndpointVolume.FromDevice(device))
         {
         }
 }
Ejemplo n.º 6
0
 public CsCoreEngine()
 {
     using (var enumerator = new MMDeviceEnumerator())
     {
         device = enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Console);
     }
     endpointVolume = AudioEndpointVolume.FromDevice(device);
 }
Ejemplo n.º 7
0
 public SysVolumeConfigurator() // Объявление
 {
     using (var enumerator = new MMDeviceEnumerator())
     {
         device = enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Console);
     }
     endpointVolume = AudioEndpointVolume.FromDevice(device);
 }
Ejemplo n.º 8
0
 public void CanGetAudioEndpointVolumeChannelCount()
 {
     using (var device = Utils.GetDefaultRenderDevice())
         using (var endpointVolume = AudioEndpointVolume.FromDevice(device))
         {
             Debug.WriteLine(endpointVolume.GetChannelCount());
         }
 }
Ejemplo n.º 9
0
 public void CanGetAudioEndpointVolume()
 {
     using (var device = Utils.GetDefaultRenderDevice())
         using (var endpointVolume = AudioEndpointVolume.FromDevice(device))
         {
             var volume = endpointVolume.GetMasterVolumeLevelScalar();
             Debug.WriteLine("Volume: {0}", volume);
             endpointVolume.SetMasterVolumeLevelScalar(0.5f, Guid.Empty);
         }
 }
Ejemplo n.º 10
0
        public static void SetMicMute(bool muted)
        {
            var endpoint = AudioEndpointVolume.FromDevice(microphone);

            if (endpoint.GetMute() != muted)
            {
                endpoint.SetMute(muted, eventguid);
                Plugin.Log.Info($"Microphone muted: {muted}");
            }
        }
Ejemplo n.º 11
0
 private static AudioEndpointVolume GetDefaultAudioEndpointVolume()
 {
     using (var enumerator = new MMDeviceEnumerator())
     {
         using (var device = enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia))
         {
             return(AudioEndpointVolume.FromDevice(device));
         }
     }
 }
Ejemplo n.º 12
0
 static AudioSessionManager2 DefaultAudioSessionManager(DataFlow dataFlow)
 {
     using (var enumerator = new MMDeviceEnumerator())
     {
         using (var device = enumerator.GetDefaultAudioEndpoint(dataFlow, Role.Multimedia))
         {
             AudioVolume = AudioEndpointVolume.FromDevice(device);
             return(AudioSessionManager2.FromMMDevice(device));
         }
     }
 }
Ejemplo n.º 13
0
        public static DefaultDevice GetPCDataDefaultDevice()
        {
            DefaultDevice       device         = new DefaultDevice();
            MMDevice            Defaultdevice  = GetDefaultDevice();
            AudioEndpointVolume Endpointvolume = AudioEndpointVolume.FromDevice(Defaultdevice);

            device.IsMute = Endpointvolume.GetMute();
            device.Name   = Defaultdevice.FriendlyName;
            device.Volume = (int)(Endpointvolume.MasterVolumeLevelScalar * 100);
            device.Icon   = null;
            return(device);
        }
Ejemplo n.º 14
0
        public AudioDevice(MMDevice device, bool visibleSystemSounds = false)
        {
            _device = device;
            _visibleSystemSounds = visibleSystemSounds;

            _sessionManager = AudioSessionManager2.FromMMDevice(_device);
            _endpointVolume = AudioEndpointVolume.FromDevice(_device);

            _endpointVolume.RegisterControlChangeNotify(_callback);
            _callback.NotifyRecived        += OnEndpointVolumeChanged;
            _sessionManager.SessionCreated += OnSessionCreated;
        }
Ejemplo n.º 15
0
        private static void SetMasterMute(bool isMute, ref RPIData data)
        {
            MMDevice            device         = GetDefaultDevice();
            AudioEndpointVolume Endpointvolume = AudioEndpointVolume.FromDevice(device);

            Endpointvolume.SetMuteNative(isMute, new Guid());
            if (isMute == false)
            {
                for (int i = 0; i < data.Programs.Count; i++)
                {
                    data.Programs[i].IsMute = false;
                }
            }
        }
Ejemplo n.º 16
0
        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
            var button = (RadioButton)sender;

            using (var device = GetDefaultRenderDevice())
                using (var endpointVolume = AudioEndpointVolume.FromDevice(device))
                {
                    var volume = endpointVolume.GetMasterVolumeLevelScalar();
                    Console.WriteLine("Volume is {0}", volume);



                    endpointVolume.SetMasterVolumeLevelScalar(float.Parse(button.Content.ToString()) / 100, Guid.Empty);
                }
        }
Ejemplo n.º 17
0
        public AudioDevice(MMDevice device)
        {
            Device = device;

            _deviceEnumerator = new MMDeviceEnumerator();
            _deviceEnumerator.DefaultDeviceChanged += OnDefaultDeviceChanged;
            _deviceEnumerator.DeviceRemoved        += OnDeviceRemoved;
            _deviceEnumerator.DeviceStateChanged   += OnDeviceStateChanged;

            _endpointVolume = AudioEndpointVolume.FromDevice(Device);
            _endpointVolume.RegisterControlChangeNotify(_callback);
            _callback.NotifyRecived += OnEndpointVolumeChanged;

            UpdateDisplayName();
        }
        public void Initialize(MMDevice captureDevice)
        {
            //BLARG 01.14.2020: Don't need the default when we're given an Audio Enpoint
            //MMDevice captureDevice = MMDeviceEnumerator.DefaultAudioEndpoint(DataFlow.Render, Role.Console);
            WaveFormat deviceFormat = captureDevice.DeviceFormat;

            _audioEndpointVolume = AudioEndpointVolume.FromDevice(captureDevice);

            //DarthAffe 07.02.2018: This is a really stupid workaround to (hopefully) finally fix the surround driver issues
            for (int i = 1; i < 13; i++)
            {
                try { _capture = new WasapiLoopbackCapture(100, new WaveFormat(deviceFormat.SampleRate, deviceFormat.BitsPerSample, i)); } catch { /* We're just trying ... */ }
            }

            if (_capture == null)
            {
                throw new NullReferenceException("Failed to initialize WasapiLoopbackCapture");
            }

            //BLARG: Actually setting the Device
            _capture.Device = captureDevice;
            _capture.Initialize();

            _soundInSource = new SoundInSource(_capture)
            {
                FillWithZeros = false
            };
            _source = _soundInSource.WaveFormat.SampleRate == 44100
                          ? _soundInSource.ToStereo()
                          : _soundInSource.ChangeSampleRate(44100).ToStereo();

            _stream = new SingleBlockNotificationStream(_source.ToSampleSource());
            _stream.SingleBlockRead += StreamOnSingleBlockRead;

            _source = _stream.ToWaveSource();

            byte[] buffer = new byte[_source.WaveFormat.BytesPerSecond / 2];
            _soundInSource.DataAvailable += (s, aEvent) =>
            {
                while ((_source.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ;
                }
            };

            _capture.Start();
        }
Ejemplo n.º 19
0
        public OverlayProfileModel(DeviceManager deviceManager, LuaManager luaManager,
                                   AudioCaptureManager audioCaptureManager) : base(deviceManager, luaManager)
        {
            Settings  = SettingsProvider.Load <OverlayProfileSettings>();
            DataModel = new OverlayProfileDataModel();

            var defaultPlayback = MMDeviceEnumerator.TryGetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);

            if (defaultPlayback != null)
            {
                _endPointVolume = AudioEndpointVolume.FromDevice(defaultPlayback);
            }

            audioCaptureManager.AudioDeviceChanged += OnAudioDeviceChanged;

            Enable();
        }
Ejemplo n.º 20
0
        public static bool IsAudioPlaying(MMDevice device)
        {
            // Value ranges:
            // .3 to .6 is listening to music from an app at high volume.
            // .06 to .07 will be that same music at half the app volume level.
            // ~.022 will be that same music at quarter app volume level.
            // ~.007 will be that same music at 1/8th app volume level.
            // ~.001 will be barely audible.
            //
            // Take the average of the last 5 audio level samples and make sure
            // audio levels are consistently over the threshold
            //
            // Just to make things more complicated, for the user to actually be
            // listening to something, then the master volume should be at a decent
            // level. So at half level (0.6 to 0.7), and a .2 system volume level,
            // we are roughly at the .005 to .001 range of actual hearing levels.
            // rather than do the math, i opted for manually setting those by halving
            // audioVolumeThreshold when master volume < .5

            using (var meter = AudioMeterInformation.FromDevice(device))
            {
                var   masterVolume = AudioEndpointVolume.FromDevice(device).GetMasterVolumeLevelScalar();
                float modifiedAudioVolumeThreshold = audioVolumeThreshold;
                // Look at last 5 samples and average them.
                audioVolumeTracker.Add(meter.PeakValue);
                if (audioVolumeTracker.Count > 5)
                {
                    audioVolumeTracker.RemoveAt(0);
                }
                float audioVolumeAverage = audioVolumeTracker.Average();

                if (masterVolume < .50)
                {
                    modifiedAudioVolumeThreshold = modifiedAudioVolumeThreshold * 1.5f;
                }

                FileLogger.Log("-----------------+ Audio Debug Logs +----------------------------", 2);
                FileLogger.Log("Audio Meter Value  : " + meter.PeakValue, 2);
                FileLogger.Log("Audio Meter Average: " + audioVolumeAverage, 2);
                FileLogger.Log("Master Volume Level: " + masterVolume, 2);
                FileLogger.Log("Modified Threshold: " + modifiedAudioVolumeThreshold, 2);

                return(audioVolumeAverage > modifiedAudioVolumeThreshold);
            }
        }
Ejemplo n.º 21
0
        private void SetupDefaultEndpoint()
        {
            if (_endpointVolume != null)
            {
                _endpointVolume.UnregisterControlChangeNotify(_endpointVolumeCallback);
                _endpointVolume.Dispose();
            }

            try
            {
                var defaultDevice = _deviceEnumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Console);
                _endpointVolume = AudioEndpointVolume.FromDevice(defaultDevice);
                _endpointVolume.RegisterControlChangeNotify(_endpointVolumeCallback);
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
            }
        }
        private void SetPlaybackDevice()
        {
            _playbackDevice = _enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Console);
            if (_playbackDevice == null)
            {
                _logger.Warning("No audio device found with Console role");
                return;
            }
            _audioEndpointVolume = AudioEndpointVolume.FromDevice(_playbackDevice);

            AudioEndpointVolumeCallback audioEndpointVolumeCallback = new AudioEndpointVolumeCallback();

            audioEndpointVolumeCallback.NotifyRecived += (s, e) =>
            {
                UpdateVolumeDataModel();
            };
            _audioEndpointVolume.RegisterControlChangeNotify(audioEndpointVolumeCallback);
            DataModel.DefaultDeviceName = _playbackDevice.FriendlyName;
            _logger.Information(string.Format("Playback device {0} registered to to fill Playback volume data model", _playbackDevice.FriendlyName));
        }
Ejemplo n.º 23
0
        /// <summary>
        /// Creates a new instance of the recorder. Captured data will be forwarded via the supplied data handler, if any.
        /// </summary>
        /// <param name="sampleRate">the target sample rate</param>
        /// <param name="bitsPerSample">the target bits per sample</param>
        /// <param name="handler">captured data handler (optional)</param>
        /// <see cref="DataHandler"/>
        public WasapiRecorder(int sampleRate, int bitsPerSample, DataHandler handler = null)
        {
            _capture = new WasapiLoopbackCapture();
            _capture.Initialize();
            _volume = AudioEndpointVolume.FromDevice(_capture.Device);

            _soundInSource = new SoundInSource(_capture)
            {
                FillWithZeros = false
            };

            //defines the source conversion
            _convertedSource = _soundInSource
                               .ChangeSampleRate(sampleRate)
                               .ToSampleSource()
                               .ToWaveSource(bitsPerSample)
                               .ToStereo();

            WithDataHandler(handler);
        }
Ejemplo n.º 24
0
        private void GetMMD()
        {
            M.D(1000, "GetMMD");
            L.MMD = CSCore.CoreAudioAPI.MMDeviceEnumerator.TryGetDefaultAudioEndpoint(CSCore.CoreAudioAPI.DataFlow.Render, CSCore.CoreAudioAPI.Role.Multimedia);
            if (L.MMD == null)
            {
                M.D(1001, M.Kind.ER, "MMD=null"); return;
            }

            AEV  = AudioEndpointVolume.FromDevice(L.MMD);
            AEVC = new AudioEndpointVolumeCallback();
            AEVC.NotifyRecived += AEV_NotifyRecived;
            AEV.RegisterControlChangeNotify(AEVC);
            L.MSD = new MasterData(AEV.IsMuted, AEV.MasterVolumeLevelScalar);

            AMI = AudioMeterInformation.FromDevice(L.MMD);
            //M.D(1008);
            //TryGetSs();
            M.D(1009);
        }
Ejemplo n.º 25
0
        private void GetMMD()
        {
            DW(1000);
            L.MMD = CSCore.CoreAudioAPI.MMDeviceEnumerator.TryGetDefaultAudioEndpoint(CSCore.CoreAudioAPI.DataFlow.Render, CSCore.CoreAudioAPI.Role.Multimedia);
            if (L.MMD == null)
            {
                return;
            }

            AEV = AudioEndpointVolume.FromDevice(L.MMD);
            var aevc = new AudioEndpointVolumeCallback();

            aevc.NotifyRecived += Mepv_NotifyRecived;
            AEV.RegisterControlChangeNotify(aevc);
            L.MD = new MasterData(AEV.IsMuted, AEV.MasterVolumeLevelScalar);

            AMI = AudioMeterInformation.FromDevice(L.MMD);

            TryGetSs();
        }
Ejemplo n.º 26
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="aDefaultDeviceChangedHandler"></param>
        /// <param name="aVolumeChangedHandler"></param>
        public void Open(EventHandler <DefaultDeviceChangedEventArgs> aDefaultDeviceChangedHandler,
                         EventHandler <AudioEndpointVolumeCallbackEventArgs> aVolumeChangedHandler)
        {
            //Create device and register default device change notification
            iMultiMediaDeviceEnumerator   = new MMDeviceEnumerator();
            iMultiMediaNotificationClient = new MMNotificationClient(iMultiMediaDeviceEnumerator);
            iMultiMediaNotificationClient.DefaultDeviceChanged += iDefaultDeviceChangedHandler = aDefaultDeviceChangedHandler;
            iMultiMediaDevice = iMultiMediaDeviceEnumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
            //Register to get volume modifications
            iAudioEndpointVolume         = AudioEndpointVolume.FromDevice(iMultiMediaDevice);
            iAudioEndpointVolumeCallback = new AudioEndpointVolumeCallback();
            iAudioEndpointVolumeCallback.NotifyRecived += iVolumeChangedHandler = aVolumeChangedHandler;
            iAudioEndpointVolume.RegisterControlChangeNotify(iAudioEndpointVolumeCallback);

            if (iVisualizerCount > 0)
            {
                // We probably got restarted, make sure visualization is running if needed
                StartAudioVisualization();
            }
        }
Ejemplo n.º 27
0
        public void CanCreateAudioEndpointVolumeNotification()
        {
            using (var device = Utils.GetDefaultRenderDevice())
                using (var endpointVolume = AudioEndpointVolume.FromDevice(device))
                {
                    AudioEndpointVolumeCallback callback = new AudioEndpointVolumeCallback();
                    callback.NotifyRecived += (s, e) =>
                    {
                        Debug.WriteLine("Notification1");
                        //Debug.Assert(e.Channels == endpointVolume.ChannelCount);
                    };
                    endpointVolume.RegisterControlChangeNotify(callback);

                    var vol = endpointVolume.GetChannelVolumeLevelScalar(0);
                    endpointVolume.SetChannelVolumeLevelScalar(0, 1f, Guid.Empty);
                    endpointVolume.SetChannelVolumeLevelScalar(0, vol, Guid.Empty);

                    endpointVolume.UnregisterControlChangeNotify(callback);
                    System.Threading.Thread.Sleep(1000);
                }
        }
Ejemplo n.º 28
0
        private IEnumerable <AudioDevice> GetAudioDevices(MMDeviceCollection mmDeviceCollection, string defaultDeviceId,
                                                          AudioEndpointType audioEndpointType)
        {
            lock (_listsLock)
            {
                foreach (var device in mmDeviceCollection)
                {
                    var audioEndpointVolume = AudioEndpointVolume.FromDevice(device);
                    int id;
                    if (_deviceList.Contains(device.DeviceID))
                    {
                        id = _deviceList.IndexOf(device.DeviceID);
                    }
                    else
                    {
                        id = _deviceList.Count;
                        _deviceList.Add(device.DeviceID);
                    }

                    using (audioEndpointVolume)
                        using (device)
                            yield return
                                new AudioDevice
                                {
                                    DeviceId          = id,
                                    Name              = device.FriendlyName,
                                    IsDefault         = device.DeviceID == defaultDeviceId,
                                    CurrentVolume     = audioEndpointVolume.MasterVolumeLevelScalar,
                                    AudioEndpointType = audioEndpointType,
                                    AudioChannels     =
                                        audioEndpointVolume.Channels.Select(
                                            x => new AudioChannel {
                                    ChannelIndex = x.ChannelIndex, Volume = x.VolumeScalar
                                })
                                        .ToList()
                                };
                }
            }
        }
Ejemplo n.º 29
0
        public AudioDevice(MMDevice device)
        {
            Device = device;

            _deviceEnumerator = new MMDeviceEnumerator();
            _deviceEnumerator.DefaultDeviceChanged += OnDefaultDeviceChanged;
            _deviceEnumerator.DeviceStateChanged   += OnDeviceStateChanged;
            _deviceEnumerator.DeviceRemoved        += OnDeviceRemoved;

            _endpointVolume = AudioEndpointVolume.FromDevice(Device);
            _endpointVolume.RegisterControlChangeNotify(_callback);
            _callback.NotifyRecived += OnEndpointVolumeChanged;

            UpdateDisplayName();
            Flow     = Device.DataFlow.ToDeviceFlow();
            DeviceId = Device.DeviceID;
            Id       = DeviceId.GetHashCode();

            // This is kinda silly...
            var defaultDevice = _deviceEnumerator.GetDefaultAudioEndpoint(Device.DataFlow, Role.Console); // Multimedia? Communications?

            IsDefault = defaultDevice.DeviceID == DeviceId;
        }
Ejemplo n.º 30
0
        public void Initialize()
        {
            MMDevice   captureDevice = MMDeviceEnumerator.DefaultAudioEndpoint(DataFlow.Render, Role.Console);
            WaveFormat deviceFormat  = captureDevice.DeviceFormat;

            _audioEndpointVolume = AudioEndpointVolume.FromDevice(captureDevice);

            //DarthAffe 07.02.2018: This is a really stupid workaround to (hopefully) finally fix the surround driver issues
            for (int i = 1; i < 13; i++)
            {
                try
                {
                    _capture = new WasapiLoopbackCapture(100, new WaveFormat(deviceFormat.SampleRate, deviceFormat.BitsPerSample, i));
                }
                catch
                { }
            }

            if (_capture == null)
            {
                throw new NullReferenceException("Failed to initialize WasapiLoopbackCapture");
            }

            _capture.Initialize();
            _soundInSource = new SoundInSource(_capture)
            {
                FillWithZeros = false
            };

            _stream = _soundInSource.WaveFormat.SampleRate == 44100
                ? new SingleBlockNotificationStream(_soundInSource.ToStereo().ToSampleSource())
                : new SingleBlockNotificationStream(_soundInSource.ChangeSampleRate(44100).ToStereo().ToSampleSource());

            _soundInSource.DataAvailable += OnSoundDataAvailable;

            _capture.Start();
        }