Пример #1
0
        private static void PlayCore(
            string file,
            float volume = 1.0f,
            WavePlayerTypes playerType = WavePlayerTypes.WASAPI,
            string deviceID            = null)
        {
            if (!File.Exists(file))
            {
                return;
            }

            if (string.IsNullOrWhiteSpace(deviceID))
            {
                return;
            }

            if (deviceID == PlayDevice.DiscordDeviceID)
            {
                Task.Run(() => DiscordClientModel.Instance.Play(file));
                return;
            }

            WavePlayer.Instance.Play(
                file,
                volume,
                Settings.Default.Player,
                deviceID);
        }
Пример #2
0
        /// <summary>
        /// 再生デバイスを列挙する
        /// </summary>
        /// <returns>再生デバイスのリスト</returns>
        public static List <PlayDevice> EnumlateDevices(
            WavePlayerTypes playerType)
        {
            var list = default(List <PlayDevice>);

            switch (playerType)
            {
            case WavePlayerTypes.WaveOut:
                list = EnumlateDevicesByWaveOut();
                break;

            case WavePlayerTypes.DirectSound:
                list = EnumlateDevicesByDirectSoundOut();
                break;

            case WavePlayerTypes.WASAPI:
                list = EnumlateDevicesByWasapiOut();
                break;

            case WavePlayerTypes.ASIO:
                list = EnumlateDevicesByAsioOut();
                break;
            }

            if (list != null)
            {
                list.Add(PlayDevice.DiscordPlugin);
            }

            return(list);
        }
Пример #3
0
        public void Play(
            string file,
            float volume = 1.0f,
            WavePlayerTypes playerType = WavePlayerTypes.WASAPI,
            string deviceID            = null)
        {
            if (!File.Exists(file))
            {
                return;
            }

            var audio = new AudioFileReader(file)
            {
                Volume = volume
            };

            var player = this.CreatePlayer(playerType, deviceID);

            player.Init(audio);
            player.PlaybackStopped += (x, y) =>
            {
                audio.Dispose();
                player.Dispose();
                audio  = null;
                player = null;
            };

            player.Play();
        }
Пример #4
0
 public static string ToDisplay(
     this WavePlayerTypes v) => new[]
 {
     "WaveOut",
     "DirectSound",
     "WASAPI",
     "WASAPI (Buffered)"
 }[(int)v];
Пример #5
0
        /// <summary>
        /// 再生する
        /// </summary>
        /// <param name="file">対象のサウンドファイル</param>
        /// <param name="volume">再生ボリューム</param>
        /// <param name="playerType">再生方式</param>
        /// <param name="deviceID">再生デバイス</param>
        /// <param name="sync">同期再生か?</param>
        public void Play(
            string file,
            float volume = 1.0f,
            WavePlayerTypes playerType = WavePlayerTypes.WASAPI,
            string deviceID            = null,
            bool sync = false)
        {
#if DEBUG
            var sw = System.Diagnostics.Stopwatch.StartNew();
#endif
            try
            {
                if (!File.Exists(file))
                {
                    return;
                }

                if (playerType == WavePlayerTypes.WASAPIBuffered)
                {
                    BufferedWavePlayer.Instance.Play(file, volume, deviceID, sync);

                    if (DisposeTimer.Enabled)
                    {
                        DisposeTimer.Stop();
                    }

                    DisposeSoundOuts();
                    return;
                }

                var audio = new AudioFileReader(file)
                {
                    Volume = volume
                };

                var player = this.CreatePlayer(playerType, deviceID);
                player.Init(audio);

                player.PlaybackStopped += (x, y)
                                          => DisposeQueue.Enqueue(x as IWavePlayer);

                player.Play();

                if (!DisposeTimer.Enabled)
                {
                    DisposeTimer.Start();
                }

                BufferedWavePlayer.Instance.Dispose();
            }
            finally
            {
#if DEBUG
                sw.Stop();
                AppLog.DefaultLogger.Info($"play wave duration_ticks={sw.ElapsedTicks:N0}, duration_ms={sw.ElapsedMilliseconds:N0} type={playerType}");
#endif
            }
        }
Пример #6
0
        /// <summary>
        /// プレイヤーを生成する
        /// </summary>
        /// <param name="playerType">プレイヤーの種類</param>
        /// <param name="deviceID">再生デバイス</param>
        /// <returns>
        /// プレイヤー</returns>
        public IWavePlayer CreatePlayer(
            WavePlayerTypes playerType = WavePlayerTypes.WASAPI,
            string deviceID            = null)
        {
            var deviceEnumrator = new MMDeviceEnumerator();

            var player = default(IWavePlayer);

            switch (playerType)
            {
            case WavePlayerTypes.WaveOut:
                player = deviceID == null ?
                         new WaveOut() :
                         new WaveOut()
                {
                    DeviceNumber   = int.Parse(deviceID),
                    DesiredLatency = PlaybackLatency,
                };
                break;

            case WavePlayerTypes.DirectSound:
                player = deviceID == null ?
                         new DirectSoundOut() :
                         new DirectSoundOut(Guid.Parse(deviceID), PlaybackLatency);
                break;

            case WavePlayerTypes.WASAPI:
                var device = deviceID switch
                {
                    PlayDevice.DefaultDeviceID => deviceEnumrator
                    .GetDefaultAudioEndpoint(DataFlow.Render, Role.Console),
                    PlayDevice.DefaultComDeviceID => deviceEnumrator
                    .GetDefaultAudioEndpoint(DataFlow.Render, Role.Communications),
                    _ => deviceEnumrator
                    .EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active)
                    .FirstOrDefault(x => x.ID == deviceID)
                };

                player = device == null ?
                         new WasapiOut() :
                         new WasapiOut(
                    device,
                    AudioClientShareMode.Shared,
                    false,
                    PlaybackLatency);
                break;
            }

            return(player);
        }
    }
Пример #7
0
        private static void PlayCore(
            string file,
            float volume = 1.0f,
            WavePlayerTypes playerType = WavePlayerTypes.WASAPI,
            string deviceID            = null,
            bool isSync = false)
        {
            if (!File.Exists(file))
            {
                return;
            }

            if (string.IsNullOrWhiteSpace(deviceID))
            {
                return;
            }

            if (deviceID == PlayDevice.DiscordDeviceID)
            {
                Task.Run(() => DiscordClientModel.Model.Play(file));
                return;
            }

            var key       = $"{deviceID}-{file}";
            var timestamp = DateTime.MinValue;

            if (LastPlayTimestamp.ContainsKey(key))
            {
                timestamp = LastPlayTimestamp[key];
            }

            if ((DateTime.Now - timestamp).TotalSeconds
                <= Settings.Default.GlobalSoundInterval)
            {
                return;
            }

            LastPlayTimestamp[key] = DateTime.Now;

            isSync |= Settings.Default.IsSyncPlayback;

            WavePlayer.Instance.Play(
                file,
                volume,
                Settings.Default.Player,
                deviceID,
                isSync);
        }
Пример #8
0
        /// <summary>
        /// プレイヤーを生成する
        /// </summary>
        /// <param name="playerType">プレイヤーの種類</param>
        /// <param name="deviceID">再生デバイス</param>
        /// <returns>
        /// プレイヤー</returns>
        public IWavePlayer CreatePlayer(
            WavePlayerTypes playerType = WavePlayerTypes.WASAPI,
            string deviceID            = null)
        {
            var player = default(IWavePlayer);

            switch (playerType)
            {
            case WavePlayerTypes.WaveOut:
                player = deviceID == null ?
                         new WaveOut() :
                         new WaveOut()
                {
                    DeviceNumber   = int.Parse(deviceID),
                    DesiredLatency = PlayerLatencyWaveOut,
                };
                break;

            case WavePlayerTypes.DirectSound:
                player = deviceID == null ?
                         new DirectSoundOut() :
                         new DirectSoundOut(
                    Guid.Parse(deviceID),
                    PlayerLatencyDirectSoundOut);
                break;

            case WavePlayerTypes.WASAPI:
                player = deviceID == null ?
                         new WasapiOut() :
                         new WasapiOut(
                    deviceEnumrator.GetDevice(deviceID),
                    AudioClientShareMode.Shared,
                    false,
                    PlayerLatencyWasapiOut);
                break;

            case WavePlayerTypes.ASIO:
                player = deviceID == null ?
                         new AsioOut() :
                         new AsioOut(deviceID);
                break;
            }

            return(player);
        }
Пример #9
0
        /// <summary>
        /// 再生デバイスを列挙する
        /// </summary>
        /// <returns>再生デバイスのリスト</returns>
        public static List <PlayDevice> EnumerateDevices(
            WavePlayerTypes playerType)
        {
            if (currentPlayerType == playerType)
            {
                return(DeviceList);
            }

            var list = default(List <PlayDevice>);

            switch (playerType)
            {
            case WavePlayerTypes.WaveOut:
                list = EnumerateDevicesByWaveOut();
                break;

            case WavePlayerTypes.DirectSound:
                list = EnumerateDevicesByDirectSoundOut();
                break;

            case WavePlayerTypes.WASAPI:
                list = EnumerateDevicesByWasapiOut();
                break;

            case WavePlayerTypes.WASAPIBuffered:
                list = EnumerateDevicesByWasapiOut();
                break;
            }

            if (list != null)
            {
                if (!list.Any(x => x.ID == PlayDevice.DiscordPlugin.ID))
                {
                    list.Add(PlayDevice.DiscordPlugin);
                }
            }

            DeviceList.Clear();
            DeviceList.AddRange(list);
            currentPlayerType = playerType;

            return(DeviceList);
        }
Пример #10
0
        private static void PlayCore(
            string file,
            float volume = 1.0f,
            WavePlayerTypes playerType = WavePlayerTypes.WASAPI,
            string deviceID            = null)
        {
            if (!File.Exists(file))
            {
                return;
            }

            if (string.IsNullOrWhiteSpace(deviceID))
            {
                return;
            }

            if (deviceID == PlayDevice.DiscordDeviceID)
            {
                Task.Run(() => DiscordClientModel.Instance.Play(file));
                return;
            }

            var playParameter = $"{deviceID}-{file}";

            if (lastPlayParameter == playParameter &&
                (DateTime.Now - lastPlayTimestamp).TotalSeconds
                <= Settings.Default.GlobalSoundInterval)
            {
                return;
            }

            lastPlayParameter = playParameter;
            lastPlayTimestamp = DateTime.Now;

            WavePlayer.Instance.Play(
                file,
                volume,
                Settings.Default.Player,
                deviceID);
        }
Пример #11
0
 public static IEnumerable <WavePlayerTypes> GetAvailablePlayers(
     this WavePlayerTypes t) =>
 Enum.GetValues(typeof(WavePlayerTypes))
 .Cast <WavePlayerTypes>()
 .Where(x =>
        x != WavePlayerTypes.WaveOut);