Exemple #1
0
        public DS4Audio(DataFlow audioFlags = DataFlow.Render)
        {
            var audioEnumerator = new MMDeviceEnumeratorComObject() as IMMDeviceEnumerator;
            IMMDeviceCollection audioDevices;

            audioEnumerator.EnumAudioEndpoints(audioFlags, DeviceState.Active, out audioDevices);

            int numAudioDevices;

            Marshal.ThrowExceptionForHR(audioDevices.GetCount(out numAudioDevices));

            for (int deviceNumber = 0; deviceNumber < numAudioDevices; ++deviceNumber)
            {
                IMMDevice audioDevice;
                Marshal.ThrowExceptionForHR(audioDevices.Item(deviceNumber, out audioDevice));
                string deviceName = GetAudioDeviceName(ref audioDevice);

                if (deviceName.Contains("DUALSHOCK®4 USB Wireless Adaptor"))
                {
                    object interfacePointer;
                    Marshal.ThrowExceptionForHR(audioDevice.Activate(ref IID_IAudioEndpointVolume, ClsCtx.ALL, IntPtr.Zero, out interfacePointer));
                    endpointVolume = interfacePointer as IAudioEndpointVolume;
                }

                Marshal.ReleaseComObject(audioDevice);
            }

            Marshal.ReleaseComObject(audioDevices);
            Marshal.ReleaseComObject(audioEnumerator);
        }
Exemple #2
0
        public DS4Audio(DataFlow audioFlags = DataFlow.Render,
                        string searchName   = "Wireless Controller")
        {
            var audioEnumerator = new MMDeviceEnumeratorComObject() as IMMDeviceEnumerator;
            IMMDeviceCollection audioDevices;

            audioEnumerator.EnumAudioEndpoints(audioFlags, DeviceState.Active, out audioDevices);

            int numAudioDevices;

            Marshal.ThrowExceptionForHR(audioDevices.GetCount(out numAudioDevices));

            for (int deviceNumber = 0; deviceNumber < numAudioDevices; ++deviceNumber)
            {
                IMMDevice audioDevice;
                Marshal.ThrowExceptionForHR(audioDevices.Item(deviceNumber, out audioDevice));
                string deviceName = GetAudioDeviceName(ref audioDevice);

                if (deviceName.Contains(searchName))
                {
                    object interfacePointer;
                    Marshal.ThrowExceptionForHR(audioDevice.Activate(ref IID_IAudioEndpointVolume, ClsCtx.ALL, IntPtr.Zero, out interfacePointer));
                    instAudioFlags = audioFlags;
                    endpointVolume = interfacePointer as IAudioEndpointVolume;
                    endpointVolume.RegisterControlChangeNotify(this);
                }

                RefreshVolume();
                Marshal.ReleaseComObject(audioDevice);
            }

            Marshal.ReleaseComObject(audioDevices);
            Marshal.ReleaseComObject(audioEnumerator);
        }
        private static IAudioEndpointVolume Vol()
        {
            IMMDeviceEnumerator enumerator = new MMDeviceEnumeratorComObject() as IMMDeviceEnumerator ?? throw new Exception();

            Marshal.ThrowExceptionForHR(enumerator.GetDefaultAudioEndpoint(/*eRender*/ 0, /*eMultimedia*/ 1, out var dev));
            var epvid = typeof(IAudioEndpointVolume).GUID;

            Marshal.ThrowExceptionForHR(dev.Activate(ref epvid, /*CLSCTX_ALL*/ 23, 0, out var epv));
            return(epv);
        }
Exemple #4
0
        private static IAudioEndpointVolume Vol()
        {
            IMMDeviceEnumerator enumerator = new MMDeviceEnumeratorComObject() as IMMDeviceEnumerator;
            IMMDevice           dev        = null;

            Marshal.ThrowExceptionForHR(enumerator.GetDefaultAudioEndpoint(0, 1, out dev));
            IAudioEndpointVolume epv = null;
            Guid epvid = typeof(IAudioEndpointVolume).GUID;

            Marshal.ThrowExceptionForHR(dev.Activate(ref epvid, 23, 0, out epv));
            return(epv);
        }
        static IAudioEndpointVolume Vol()
        {
            IMMDeviceEnumerator enumerator = new MMDeviceEnumeratorComObject() as IMMDeviceEnumerator;
            IMMDevice           dev        = null;

            Marshal.ThrowExceptionForHR(enumerator.GetDevice(Conf.Instance.DeviceID, out dev));
            IAudioEndpointVolume epv = null;

            System.Guid epvid = typeof(IAudioEndpointVolume).GUID;
            Marshal.ThrowExceptionForHR(dev.Activate(ref epvid, /*CLSCTX_ALL*/ 23, 0, out epv));
            return(epv);
        }
Exemple #6
0
        public static void listDefaultDevice()
        {
            var       enumerator = new MMDeviceEnumeratorComObject() as IMMDeviceEnumerator;
            IMMDevice dev        = null;

            System.Console.Write("GetDefaultAudioEndpoint: ...");
            Marshal.ThrowExceptionForHR(enumerator.GetDefaultAudioEndpoint(0 /*eRender*/, 1 /*eMultimedia*/, out dev));

            listDeviceProperties(dev, new DEVICE_SUMMARY());
            Marshal.ReleaseComObject(dev);

            System.Console.WriteLine();
        }
Exemple #7
0
        public static string GetDefaultAudioDeviceID(DataFlow dataFlow, Role role)
        {
            IMMDeviceEnumerator devenum = new MMDeviceEnumeratorComObject() as IMMDeviceEnumerator;
            IMMDevice           endpoint;

            devenum.GetDefaultAudioEndpoint(dataFlow, role, out endpoint);
            string devID;

            Marshal.ThrowExceptionForHR(endpoint.GetId(out devID));
            Marshal.FinalReleaseComObject(endpoint);
            Marshal.FinalReleaseComObject(devenum);
            return(devID);
        }
Exemple #8
0
        public static void listDevices(int dataFlow, string setMute, System.Collections.Generic.List <DEVICE_SUMMARY> defaultDevices)
        {
            var  enumerator = new MMDeviceEnumeratorComObject() as IMMDeviceEnumerator;
            uint deviceCount = 0, i = 0;

            System.Console.Write("EnumAudioEndpoints: ...");
            IMMDeviceCollection deviceCollection;  enumerator.EnumAudioEndpoints(dataFlow, 1, out deviceCollection);

            // EDataFlow: 0=eRender;1=eCapture;2=eAll | 1=DEVICE_STATE_ACTIVE

            deviceCollection.GetCount(out deviceCount);  System.Console.WriteLine(" deviceCount=" + deviceCount);//return;
            for (i = 0; i < deviceCount; i++)
            {
                System.Console.Write("\n" + i + ":");
                IMMDevice dev = null;  deviceCollection.Item(i, out dev);
                if (!Object.ReferenceEquals(null, dev))
                {
                    IMMEndpoint ep = (IMMEndpoint)dev; // Marshal.QueryInterface shouldn't be necessary, just use cast instead [ http://stackoverflow.com/questions/5077172/how-do-i-use-marshal-queryinterface ]
                    int         dataFlow1;  ep.GetDataFlow(out dataFlow1);
                    System.Console.WriteLine(" GetDataFlow: [" + dataFlow1 + "] " + ((dataFlow1 == 0)?"Playback":"Recording"));

                    DEVICE_SUMMARY defaultDevice = new DEVICE_SUMMARY();
                    foreach (var dev0 in defaultDevices)
                    {
                        if (dev0.dataFlow == dataFlow1)
                        {
                            defaultDevice = dev0;
                            break;
                        }
                    }

                    listDeviceProperties(dev, defaultDevice);
                    if (setMute == "0" || setMute == "1")
                    {
                        IAudioEndpointVolume epv = null;
                        var epvid = typeof(IAudioEndpointVolume).GUID;
                        Marshal.ThrowExceptionForHR(dev.Activate(ref epvid, /*CLSCTX_ALL*/ 23, 0, out epv));
                        epv.SetMute((setMute == "0"), System.Guid.Empty);  System.Console.WriteLine(" SetMute: " + (setMute == "0"));
                    }
                }
                Marshal.ReleaseComObject(dev);
            }
            Marshal.ReleaseComObject(deviceCollection);

            System.Console.WriteLine();
        }
Exemple #9
0
        public static DEVICE_SUMMARY getDefaultDeviceSummary(int dataFlow, int role)
        {
            DEVICE_SUMMARY dev1       = new DEVICE_SUMMARY();  dev1.dataFlow = dataFlow;  dev1.role = role;
            var            enumerator = new MMDeviceEnumeratorComObject() as IMMDeviceEnumerator;
            IMMDevice      dev        = null;  Marshal.ThrowExceptionForHR(enumerator.GetDefaultAudioEndpoint(dataFlow, role, out dev));
            IPropertyStore propertyStore;  dev.OpenPropertyStore(0 /*STGM_READ*/, out propertyStore);
            PROPVARIANT    property;  propertyStore.GetValue(ref PropertyKey.PKEY_Device_FriendlyName, out property);

            dev1.FriendlyName = (string)property.Value;
            Marshal.ReleaseComObject(propertyStore);
            IAudioEndpointVolume epv = null;
            var epvid = typeof(IAudioEndpointVolume).GUID;

            Marshal.ThrowExceptionForHR(dev.Activate(ref epvid, /*CLSCTX_ALL*/ 23, 0, out epv));
            epv.GetMute(out dev1.isMuted);
            Marshal.ReleaseComObject(dev);
            return(dev1);
        }
        private static IMMDevice GetDefaultDevice()
        {
            var enumerator = new MMDeviceEnumeratorComObject() as IMMDeviceEnumerator;

            return(enumerator.GetDefaultAudioEndpoint(/*eRender*/ 0, /*eMultimedia*/ 1));
        }