private static ISoundOut GetDirectSoundOut(ISoundOutDevice device) { var devices = DirectSoundDeviceEnumerator.EnumerateDevices(); if (devices.Count == 0) { throw new NoDeviceFoundException(); } DirectSoundDevice directSoundDevice = null; if (device.Id == WindowsDefaultId) { var defaultAudioId = GetDefaultDevice().FriendlyName; directSoundDevice = devices.FirstOrDefault(x => x.Description == defaultAudioId); } if (directSoundDevice == null) { directSoundDevice = devices.FirstOrDefault(x => x.Module == device.Id) ?? devices.First(); } return(new DirectSoundOut { Device = directSoundDevice.Guid }); }
private void OptionsForm_Load(object sender, EventArgs e) { var devices = DirectSoundDeviceEnumerator.EnumerateDevices(); deviceList = new List <Guid>(); foreach (var device in devices) { deviceList.Add(device.Guid); cmbMic.Items.Add(device.Description); cmbOut.Items.Add(device.Description); } //for (int deviceId = 0; deviceId < WaveOut.DeviceCount; deviceId++) //{ // var capabilities = WaveOut.GetCapabilities(deviceId); // cmbMic.Items.Add(capabilities.ProductName); // cmbOut.Items.Add(capabilities.ProductName); //} if (deviceList.Contains(MicIndex)) { cmbMic.SelectedIndex = deviceList.IndexOf(MicIndex); } if (deviceList.Contains(OutIndex)) { cmbOut.SelectedIndex = deviceList.IndexOf(OutIndex); } }
private List <AudioDevice> EnumerateDevices() { List <AudioDevice> devicestemp = new List <AudioDevice>(); List <AudioDevice> devices = new List <AudioDevice>(); devicestemp = ListDevices(); var Masterdevices = DirectSoundDeviceEnumerator.EnumerateDevices(); for (int i = 0; i < Masterdevices.Count; i++) { if (Masterdevices[i].Guid != new Guid("00000000-0000-0000-0000-000000000000")) { for (int k = 0; k < devicestemp.Capacity; k++) { if (Masterdevices[i].Description == devicestemp[k].Name) { devices.Add(new AudioDevice(devicestemp[k].Name, devicestemp[k].Id)); break; } } } } return(devices); }
private static void AddDirectSoundDevices(ICollection <Device> devices) { foreach (var directSoundDevice in DirectSoundDeviceEnumerator.EnumerateDevices()) { devices.Add(new Device(directSoundDevice.Guid.ToString(), directSoundDevice.Description, DeviceTypeEnum.DirectSound)); } }
public void EnumerateDirectSoundDeviceTest() { var devices = DirectSoundDeviceEnumerator.EnumerateDevices(); foreach (var device in devices) { Debug.WriteLine(device.ToString()); } }
public List <string> GetDirectSoundOutDeviceName() { List <DirectSoundDevice> list = new List <DirectSoundDevice>(); foreach (var dev in DirectSoundDeviceEnumerator.EnumerateDevices()) { list.Add(dev); } return(list.Select(x => "DirectSoundOut - " + x.Description).ToList()); }
private static ISoundOutDevice GetDirectSoundOutDeviceById(string id) { var device = DirectSoundDeviceEnumerator.EnumerateDevices() .FirstOrDefault(x => x.Module == id); return(device == null ? null : new SoundOutDevice(device.Description, device.Module, SoundOutType.DirectSound)); }
public List <string> GetAudioEndpoints() { List <string> ep = new List <string>(); IReadOnlyCollection <DirectSoundDevice> list = DirectSoundDeviceEnumerator.EnumerateDevices(); foreach (DirectSoundDevice d in list) { ep.Add(d.Description); } return(ep); }
private void LoadSoundOutModes() { SoundOutModes.Clear(); using (var enumerator = new MMDeviceEnumerator()) { MMDevice defaultDevice; try { defaultDevice = enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia); } catch (CoreAudioAPIException) { defaultDevice = null; } if (WasapiOut.IsSupportedOnCurrentPlatform) { var wasApiMode = new SoundOutMode("WASAPI", SoundOutType.WasApi, GetWasApiSoundOutDeviceById, GetWasApiSoundOut, new SoundOutDevice("Windows Default", WindowsDefaultId, SoundOutType.WasApi)); using (var devices = enumerator.EnumAudioEndpoints(DataFlow.Render, DeviceState.Active)) { foreach (var device in devices.Select(x => new SoundOutDevice(x.FriendlyName, x.DeviceID, SoundOutType.WasApi, defaultDevice != null && defaultDevice.DeviceID == x.DeviceID))) { wasApiMode.Devices.Add(device); } } UpdateWindowsDefault(wasApiMode); SoundOutModes.Add(wasApiMode); } var directSoundMode = new SoundOutMode("DirectSound", SoundOutType.DirectSound, GetDirectSoundOutDeviceById, GetDirectSoundOut, new SoundOutDevice("Windows Default", WindowsDefaultId, SoundOutType.DirectSound)); foreach (var device in DirectSoundDeviceEnumerator.EnumerateDevices().Select(x => new SoundOutDevice(x.Description, x.Module, SoundOutType.DirectSound, defaultDevice != null && x.Description == defaultDevice.FriendlyName))) { directSoundMode.Devices.Add(device); } UpdateWindowsDefault(directSoundMode); SoundOutModes.Add(directSoundMode); CheckCurrentState(); } }
public string GetAudioEndpoint() { Guid guid = ((DirectSoundOut)aout).Device; System.Collections.ObjectModel.ReadOnlyCollection <DirectSoundDevice> list = DirectSoundDeviceEnumerator.EnumerateDevices(); DirectSoundDevice dsd = list.First(x => x.Guid == guid); return(dsd.Description); }
public bool SetAudioEndpoint(string dev, bool usedefault = false) { #if true System.Collections.ObjectModel.ReadOnlyCollection <DirectSoundDevice> list = DirectSoundDeviceEnumerator.EnumerateDevices(); DirectSoundDevice dsd = null; if (dev != null) // active selection { dsd = list.FirstOrDefault(x => x.Description.Equals(dev)); // find if (dsd == null && !usedefault) // if not found, and don't use the default (used by constructor) { return(false); } } DirectSoundOut dso = new DirectSoundOut(100, System.Threading.ThreadPriority.Highest); // seems good quality at 200 ms latency if (dsd != null) { dso.Device = dsd.Guid; } #else MMDevice def = MMDeviceEnumerator.DefaultAudioEndpoint(DataFlow.Render, Role.Console); ISoundOut dso = new WasapiOut() { Latency = 100, Device = def }; //BAD breakup #endif if (aout != null) // clean up last { aout.Stopped -= Output_Stopped; aout.Stop(); aout.Dispose(); } aout = dso; aout.Stopped += Output_Stopped; return(true); }
public bool SetAudioEndpoint(string dev, bool usedefault = false) { System.Collections.ObjectModel.ReadOnlyCollection <DirectSoundDevice> list = DirectSoundDeviceEnumerator.EnumerateDevices(); DirectSoundDevice dsd = null; if (dev != null) // active selection { dsd = list.FirstOrDefault(x => x.Description.Equals(dev)); // find if (dsd == null && !usedefault) // if not found, and don't use the default (used by constructor) { return(false); } } DirectSoundOut dso = new DirectSoundOut(200, System.Threading.ThreadPriority.Highest); // seems good quality at 200 ms latency if (dso == null) // if no DSO, fail.. { return(false); } if (dsd != null) { dso.Device = dsd.Guid; } else { DirectSoundDevice def = DirectSoundDevice.DefaultDevice; dso.Device = def.Guid; // use default GUID } NullWaveSource nullw = new NullWaveSource(10); try { dso.Initialize(nullw); // check it takes it.. may not if no sound devices there.. dso.Stop(); nullw.Dispose(); } catch { nullw.Dispose(); dso.Dispose(); return(false); } if (aout != null) // clean up last { aout.Stopped -= Output_Stopped; aout.Stop(); aout.Dispose(); } aout = dso; aout.Stopped += Output_Stopped; return(true); }
private void RefreshSoundOutRepresenter() { var result = new ObservableCollection <SoundOutRepresenter>(); using (var enumerator = new MMDeviceEnumerator()) { MMDevice standarddevice; try { standarddevice = enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia); } catch (CoreAudioAPIException) { standarddevice = null; } if (WasapiOut.IsSupportedOnCurrentPlatform) { var wasApiItem = new SoundOutRepresenter(deviceId => { using (var mmdeviceEnumerator = new MMDeviceEnumerator()) { var device = mmdeviceEnumerator.EnumAudioEndpoints(DataFlow.Render, DeviceState.Active) .FirstOrDefault(x => x.DeviceID == deviceId); return(device == null ? null : new AudioDevice(device.DeviceID, device.FriendlyName)); } }) { Name = "WASAPI", SoundOutMode = SoundOutMode.WASAPI }; using (var devices = enumerator.EnumAudioEndpoints(DataFlow.Render, DeviceState.Active)) { foreach ( var device in devices.Select( device => new AudioDevice(device.DeviceID, device.FriendlyName, standarddevice != null && standarddevice.DeviceID == device.DeviceID))) { wasApiItem.AudioDevices.Add(device); } } CheckDefaultAudioDevice(wasApiItem); result.Add(wasApiItem); } var directSoundDevices = DirectSoundDeviceEnumerator.EnumerateDevices(); var directSoundItem = new SoundOutRepresenter(deviceId => { var device = directSoundDevices .FirstOrDefault(x => x.Guid.ToString() == deviceId); return(device == null ? null : new AudioDevice(device.Guid.ToString(), device.Description)); }) { Name = "DirectSound", SoundOutMode = SoundOutMode.DirectSound }; foreach ( var device in directSoundDevices.Select( x => new AudioDevice(x.Guid.ToString(), x.Description, standarddevice != null && x.Description == standarddevice.FriendlyName))) { directSoundItem.AudioDevices.Add(device); } CheckDefaultAudioDevice(directSoundItem); result.Add(directSoundItem); } SoundOutList = result; }
public bool SetAudioEndpoint(string dev, bool usedefault = false) { System.Collections.ObjectModel.ReadOnlyCollection <DirectSoundDevice> list = DirectSoundDeviceEnumerator.EnumerateDevices(); DirectSoundDevice dsd = null; if (dev != null) // active selection { dsd = list.FirstOrDefault(x => x.Description.Equals(dev)); // find if (dsd == null && !usedefault) // if not found, and don't use the default (used by constructor) { return(false); } } DirectSoundOut dso = new DirectSoundOut(200); // seems good quality at 200 ms latency if (dsd != null) { dso.Device = dsd.Guid; } if (aout != null) // clean up last { aout.Stopped -= Output_Stopped; aout.Stop(); aout.Dispose(); } aout = dso; aout.Stopped += Output_Stopped; return(true); }
//public PlaybackStoppedDele PlaybackStopped { get; set; } //public PlaybackContiuneDele PlaybackContiune { set; get; } #endregion Properties #region Methods private void InitializePlayback(int Volume = 50, string openMethods = "waveout", string device = "扬声器") { MMDevice mMDevice; device = device.Trim(); openMethods = openMethods.Trim(); if (openMethods.IndexOf("WaveOut") != -1) { IEnumerable <WaveOutDevice> dives = WaveOutDevice.EnumerateDevices(); IEnumerable <WaveOutDevice> divselect = dives.Where(x => x.Name.IndexOf(device) != -1); WaveOutDevice div = null; if (divselect.Count() == 0) { div = dives.FirstOrDefault(); } else if (divselect.Count() == 1) { div = divselect.FirstOrDefault(); } else { Debug.Print("*****输入异常"); div = divselect.FirstOrDefault(); } if (div == null) { throw new NotSupportedException("not exist directsound device"); } _soundOut = new WaveOut() { Device = div, Latency = 100 }; //300延时有个运算溢出,怀疑是其他异常造成的 } else if (openMethods.IndexOf("WasApiOut") != -1) { var enumerator = new MMDeviceEnumerator(); IEnumerable <MMDevice> mMDevices = MMDeviceEnumerator.EnumerateDevices(DataFlow.Render).Where(x => x.DeviceState == DeviceState.Active); IEnumerable <MMDevice> dives = enumerator.EnumAudioEndpoints(DataFlow.All, DeviceState.All).Where(x => x.DeviceState == DeviceState.Active); mMDevices = mMDevices.Join(dives, x => x.FriendlyName, x => x.FriendlyName, (x, y) => x).ToArray(); mMDevice = mMDevices.Where(x => x.FriendlyName.IndexOf(device) != -1).FirstOrDefault(x => x.DeviceState == DeviceState.Active); _soundOut = new WasapiOut() { Device = mMDevice, Latency = 200 }; } else { IEnumerable <DirectSoundDevice> dives = DirectSoundDeviceEnumerator.EnumerateDevices(); var divselect = dives.Where(x => x.Description.IndexOf(device) != -1); DirectSoundDevice div = null; if (divselect.Count() == 0) { div = dives.FirstOrDefault(); } else if (divselect.Count() == 1) { div = divselect.FirstOrDefault(); } else { //Debug.Print("*****输入异常*****"); div = divselect.FirstOrDefault(); } if (div == null) { throw new NotSupportedException("not exist directsound device"); } _soundOut = new DirectSoundOut() { Device = div.Guid, Latency = 100 }; } if (_filePath.LastIndexOf(".mp3") != -1)//流异步读取,此api异步读取flac流在频繁pos时有死锁bug { Stream fs = File.OpenRead(_filePath); _waveSource = new CSCore.Codecs.MP3.Mp3MediafoundationDecoder(fs); } else if (_filePath.LastIndexOf(".flac") != -1) { Stream fs = File.OpenRead(_filePath); _waveSource = new CSCore.Codecs.FLAC.FlacFile(fs, CSCore.Codecs.FLAC.FlacPreScanMode.Default); // _waveSource = new CSCore.Codecs.FLAC.FlacFile(_filePath); } else { _waveSource = CodecFactory.Instance.GetCodec(_filePath); } _soundOut.Initialize(_waveSource); _soundOut.Volume = Volume / 100f; //_soundOut.Stopped += _soundOut_Stopped; _total = _waveSource.GetLength(); }
public ISoundOut GetNewSoundSource() { var settings = AnyListenSettings.Instance.Config; MMDevice defaultDevice; using (var enumerator = new MMDeviceEnumerator()) { try { defaultDevice = enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia); } catch (CoreAudioAPIException) { defaultDevice = null; } } if (settings.SoundOutMode == SoundOutMode.DirectSound) { var devices = DirectSoundDeviceEnumerator.EnumerateDevices(); if (devices.Count == 0) { settings.SoundOutMode = SoundOutMode.WASAPI; return(GetNewSoundSource()); } DirectSoundDevice device; if (settings.SoundOutDeviceID == DefaultDevicePlaceholder) { device = defaultDevice != null ? devices.FirstOrDefault(x => x.Description == defaultDevice.FriendlyName) ?? devices.First() : devices.First(); } else { device = devices.FirstOrDefault(x => x.Guid.ToString() == settings.SoundOutDeviceID); if (device == null) { settings.SoundOutDeviceID = DefaultDevicePlaceholder; return(GetNewSoundSource()); } } _currentDeviceId = device.Guid.ToString(); return(new DirectSoundOut { Device = device.Guid, Latency = settings.Latency }); } using (var enumerator = new MMDeviceEnumerator()) { using (var devices = enumerator.EnumAudioEndpoints(DataFlow.Render, DeviceState.Active)) { if (defaultDevice == null || devices.Count == 0) { settings.SoundOutMode = SoundOutMode.DirectSound; return(GetNewSoundSource()); } MMDevice device; if (settings.SoundOutDeviceID == DefaultDevicePlaceholder) { device = defaultDevice; } else { device = devices.FirstOrDefault(x => x.DeviceID == settings.SoundOutDeviceID); if (device == null) { settings.SoundOutDeviceID = DefaultDevicePlaceholder; return(GetNewSoundSource()); } } _currentDeviceId = device.DeviceID; return(new WasapiOut { Device = device, Latency = settings.Latency }); } } }
public override void ProcessCommand(byte[] parameter, IConnectionInfo connectionInfo) { switch ((AudioCommunication)parameter[0]) { case AudioCommunication.PlayAudio: if (CoreHelper.RunningOnXP) { connectionInfo.CommandResponse(this, new[] { (byte)AudioCommunication.ResponseNotSupported }); return; } var serializer = new Serializer(typeof(PlayAudioInformation)); var playInformation = serializer.Deserialize <PlayAudioInformation>(parameter, 1); ISoundOut soundOut; if (WasapiOut.IsSupportedOnCurrentPlatform) { using (var deviceEnumerator = new MMDeviceEnumerator()) { var device = deviceEnumerator.EnumAudioEndpoints(DataFlow.Render, DeviceState.Active) .FirstOrDefault(x => x.DeviceID == playInformation.SoundOutId); if (device == null) { return; } soundOut = new WasapiOut { Device = device }; } } else { var devices = DirectSoundDeviceEnumerator.EnumerateDevices(); var guid = new Guid(playInformation.SoundOutId); var device = devices.FirstOrDefault(x => x.Guid == guid); if (device == null) { return; } soundOut = new DirectSoundOut { Device = guid }; } var memoryStream = new MemoryStream(playInformation.AudioData); IWaveSource waveSource = new DmoMp3Decoder(memoryStream); soundOut.Initialize(waveSource); soundOut.Volume = playInformation.Volume; soundOut.Play(); soundOut.Stopped += (sender, args) => { new Thread(() => { soundOut.Dispose(); waveSource.Dispose(); memoryStream.Dispose(); }).Start(); }; connectionInfo.CommandResponse(this, new[] { (byte)AudioCommunication.ResponseAudioIsPlaying }); break; case AudioCommunication.GetDevices: List <SoundOutDevice> result; if (WasapiOut.IsSupportedOnCurrentPlatform) { using (var deviceEnumerator = new MMDeviceEnumerator()) { var defaultDevice = deviceEnumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia); result = new List <SoundOutDevice>( deviceEnumerator.EnumAudioEndpoints(DataFlow.Render, DeviceState.Active) .Select(enumAudioEndpoint => new SoundOutDevice { Id = enumAudioEndpoint.DeviceID, IsDefault = enumAudioEndpoint.DeviceID == defaultDevice.DeviceID, Name = enumAudioEndpoint.FriendlyName })); } } else { var devices = DirectSoundDeviceEnumerator.EnumerateDevices(); result = devices.Select( x => new SoundOutDevice { Id = x.Guid.ToString("N"), Name = x.Description, IsDefault = false }).ToList(); } var package = new List <byte> { (byte)AudioCommunication.ResponseDevices }; package.AddRange(Encoding.UTF8.GetBytes(new JavaScriptSerializer().Serialize(result))); connectionInfo.CommandResponse(this, package.ToArray()); break; default: throw new ArgumentOutOfRangeException(nameof(parameter), parameter, null); } }