Пример #1
0
        public void ChangeAudioDevice(string deviceName = null)
        {
            if (CurrentDeviceName == deviceName && ApplicationDevice != null)
            {
                return;
            }
            var playbackDevices = DirectSound.GetDevices();
            // Use default device.
            Guid driverGuid = Guid.Empty;

            foreach (var device in playbackDevices)
            {
                // Pick specific device for the plaback.
                if (string.Compare(device.Description, deviceName, true) == 0)
                {
                    driverGuid = device.DriverGuid;
                }
            }
            if (ApplicationDevice != null)
            {
                ApplicationDevice.Dispose();
                ApplicationDevice = null;
            }
            // Create and set the sound device.
            ApplicationDevice = new DirectSound(driverGuid);
            SpeakerConfiguration speakerSet;
            SpeakerGeometry      geometry;

            ApplicationDevice.GetSpeakerConfiguration(out speakerSet, out geometry);
            ApplicationDevice.SetCooperativeLevel(_Handle, CooperativeLevel.Normal);
            CurrentDeviceName = deviceName;
        }
Пример #2
0
        public DirectSoundSoundOutput(Sound sound, IntPtr mainWindowHandle)
        {
            _sound = sound;

            var deviceInfo = DirectSound.GetDevices().FirstOrDefault(d => d.Description == Global.Config.SoundDevice);

            _device = deviceInfo != null ? new DirectSound(deviceInfo.DriverGuid) : new DirectSound();
            _device.SetCooperativeLevel(mainWindowHandle, CooperativeLevel.Priority);
        }
Пример #3
0
        public DXWavePlayer(int device, int BufferByteSize, DataRequestDelegate fillProc)
        {
            if (BufferByteSize < 1000)
            {
                throw new ArgumentOutOfRangeException("BufferByteSize", "minimal size of buffer is 1000 bytes");
            }

            _buffersize  = BufferByteSize;
            _requestproc = fillProc;
            var devices = DirectSound.GetDevices();

            if (device <= 0 || device >= devices.Count)
            {
                device = 0;
            }

            _outputDevice = new DirectSound(devices[device].DriverGuid);


            System.Windows.Interop.WindowInteropHelper wh = new System.Windows.Interop.WindowInteropHelper(Application.Current.MainWindow);
            _outputDevice.SetCooperativeLevel(wh.Handle, CooperativeLevel.Priority);

            _buffDescription             = new SoundBufferDescription();
            _buffDescription.Flags       = BufferFlags.ControlPositionNotify | BufferFlags.ControlFrequency | BufferFlags.ControlEffects | BufferFlags.GlobalFocus | BufferFlags.GetCurrentPosition2;
            _buffDescription.BufferBytes = BufferByteSize * InternalBufferSizeMultiplier;

            WaveFormat format = new WaveFormat(16000, 16, 1);

            _buffDescription.Format = format;

            _soundBuffer  = new SecondarySoundBuffer(_outputDevice, _buffDescription);
            _synchronizer = new AutoResetEvent(false);

            NotificationPosition[] nots = new NotificationPosition[InternalBufferSizeMultiplier];

            NotificationPosition not;
            int bytepos = 800;

            for (int i = 0; i < InternalBufferSizeMultiplier; i++)
            {
                not            = new NotificationPosition();
                not.Offset     = bytepos;
                not.WaitHandle = _synchronizer;
                nots[i]        = not;
                bytepos       += BufferByteSize;
            }

            _soundBuffer.SetNotificationPositions(nots);


            _waitThread = new Thread(new ThreadStart(DataRequestThread))
            {
                Name = "MyWavePlayer.DataRequestThread"
            };
            _waitThread.Start();
        }
        public DirectSoundSoundOutput(IHostAudioManager sound, IntPtr mainWindowHandle, string soundDevice)
        {
            _sound        = sound;
            _retryCounter = 5;

            var deviceInfo = DirectSound.GetDevices().FirstOrDefault(d => d.Description == soundDevice);

            _device = deviceInfo != null ? new DirectSound(deviceInfo.DriverGuid) : new DirectSound();
            _device.SetCooperativeLevel(mainWindowHandle, CooperativeLevel.Priority);
        }
Пример #5
0
        /// <summary>
        ///   Returns an enumerator that iterates through the device collection.
        /// </summary>
        ///
        /// <returns>
        ///   A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that
        ///   can be used to iterate through the collection.
        /// </returns>
        ///
        public IEnumerator <AudioDeviceInfo> GetEnumerator()
        {
            DeviceCollection devices = (Category == AudioDeviceCategory.Capture) ?
                                       DirectSoundCapture.GetDevices() :
                                       DirectSound.GetDevices();

            foreach (DeviceInformation info in devices)
            {
                yield return(new AudioDeviceInfo(info));
            }
        }
Пример #6
0
        public static IEnumerable <string> DeviceNames()
        {
            var ret = new List <string>();
            var dc  = DirectSound.GetDevices();

            foreach (var dev in dc)
            {
                ret.Add(dev.Description);
            }
            return(ret);
        }
Пример #7
0
        public static DirectSound Create()
        {
            var dc = DirectSound.GetDevices();

            foreach (var dev in dc)
            {
                if (dev.Description == Global.Config.SoundDevice)
                {
                    return(new DirectSound(dev.DriverGuid));
                }
            }
            return(new DirectSound());
        }
Пример #8
0
        public static DeviceInfo[] GetDevices()
        {
            ArrayList        infos = new ArrayList();
            DeviceCollection coll  = DirectSound.GetDevices();

            for (int i = 0; i < coll.Count; i++)
            {
                DeviceInfo info = new DeviceInfo();
                info.Name = coll[i].Description;
                info.Guid = coll[i].DriverGuid;
                infos.Add(info);
            }

            return((DeviceInfo[])infos.ToArray(typeof(DeviceInfo)));
        }
Пример #9
0
        public override void Load()
        {
            Guid currentDeviceId = SettingsManager.Load <LibRetroSettings>().AudioDeviceId;

            _devices = DirectSound.GetDevices();
            _items   = new List <IResourceString>(_devices.Count);
            for (int i = 0; i < _devices.Count; i++)
            {
                DeviceInformation device = _devices[i];
                _items.Add(LocalizationHelper.CreateStaticString(device.Description));
                if (device.DriverGuid == currentDeviceId)
                {
                    Selected = i;
                }
            }
        }
Пример #10
0
        public static IEnumerable <AudioDeviceBase> GetWindowsAudioDevices()
        {
            var audioDevices = new List <AudioDeviceBase>();

            try
            {
                Dictionary <Guid, MMDevice> systemMMDeviceCollection = new Dictionary <Guid, MMDevice>();
                using (var deviceEnumerator = new MMDeviceEnumerator())
                {
                    foreach (var device in deviceEnumerator.EnumerateAudioEndPoints(DataFlow.All, DeviceState.Active))
                    {
                        try
                        {
                            var deviceIdString = device.Properties[WindowsDevicePropertyKeys.PKEY_AudioEndpoint_GUID.ToNAudioPropertyKey()]?.Value.ToString();

                            Regex guidRegex = new Regex(@"{(?<Guid>.{8}-.{4}-.{4}-.{4}-.{12})}");
                            deviceIdString = guidRegex.Match(deviceIdString).Groups["Guid"].Value;
                            Guid deviceId = Guid.Parse(deviceIdString);

                            systemMMDeviceCollection[deviceId] = device;
                        }
                        catch { }
                    }
                }

                DirectSound.GetDevices().ForEach(audioDevice =>
                {
                    var mmDeviceInstance = systemMMDeviceCollection.ContainsKey(audioDevice.DriverGuid) ? systemMMDeviceCollection[audioDevice.DriverGuid] : null;

                    audioDevices.Add(new AudioDeviceBase(audioDevice.DriverGuid)
                    {
                        FriendlyName          = audioDevice.Description,
                        MMDeviceInstance      = mmDeviceInstance,
                        AudioMeterInformation = mmDeviceInstance?.AudioMeterInformation
                    });
                });
            }
            catch (Exception ex)
            {
                ApplicationLogger.Log(ex.Message, ex.StackTrace);
            }

            return(audioDevices);
        }
Пример #11
0
 public static IEnumerable <string> GetDeviceNames()
 {
     return(DirectSound.GetDevices().Select(d => d.Description).ToList());
 }