Ejemplo n.º 1
0
 public string GetAudioEndpoint()
 {
     if (aout != null)
     {
         Guid guid = ((DirectSoundOut)aout).Device;
         System.Collections.ObjectModel.ReadOnlyCollection <DirectSoundDevice> list = DirectSoundDeviceEnumerator.EnumerateDevices();
         DirectSoundDevice dsd = list.First(x => x.Guid == guid);
         return(dsd.Description);
     }
     else
     {
         return("");
     }
 }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
        public void CanCreatePlayer()
        {
            var device = new DirectSoundDevice(new DirectSoundDeviceInfo());

            device.Player.Should().Not.Be.Null();
        }
Ejemplo n.º 4
0
        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);
                }
            }

            // seems good quality at 200 ms latency
            // note CS core is crap at samples around 200ms of length.. i've debugged it using audacity and they are truncated
            // need to find a replacement to cscore - not been updated since 2017

            DirectSoundOut dso = new DirectSoundOut(200, System.Threading.ThreadPriority.Highest);

            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);
        }
        //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();
        }