/// <summary> /// Initialize ASIO and starts playing /// If ASIO throws exception, use Wasapi /// </summary> private void InitializeAsio() { try { if (_asioOut != null) _asioOut.Dispose(); _asioOut = new AsioOut(1); _asioOut.Init(_waveProvider); _asioOut.Play(); } catch (Exception e) { InitializeWasapi(); } }
private void Play() { // allow change device if (asioOut != null && (asioOut.DriverName != comboBoxAsioDevice.Text || asioOut.ChannelOffset != GetUserSpecifiedChannelOffset())) { asioOut.Dispose(); asioOut = null; } // create device if necessary if (asioOut == null) { asioOut = new AsioOut(comboBoxAsioDevice.Text); asioOut.ChannelOffset = GetUserSpecifiedChannelOffset(); asioOut.Init(reader); } reader.Position = 0; asioOut.Play(); timer1.Enabled = true; SetButtonStates(); }
/// <summary> /// 再生する /// </summary> /// <param name="deviceID">再生デバイスID</param> /// <param name="waveFile">wavファイル</param> /// <param name="isDelete">再生後に削除する</param> /// <param name="volume">ボリューム</param> public static void Play( string deviceID, string waveFile, bool isDelete, int volume) { var sw = Stopwatch.StartNew(); var volumeAsFloat = ((float)volume / 100f); try { IWavePlayer player = null; IWaveProvider provider = null; switch (TTSYukkuriConfig.Default.Player) { case WavePlayers.WaveOut: player = new WaveOut() { DeviceNumber = int.Parse(deviceID), DesiredLatency = PlayerLatencyWaveOut, }; break; case WavePlayers.DirectSound: player = new DirectSoundOut( Guid.Parse(deviceID), PlayerLatencyDirectSoundOut); break; case WavePlayers.WASAPI: player = new WasapiOut( deviceEnumrator.GetDevice(deviceID), AudioClientShareMode.Shared, false, PlayerLatencyWasapiOut); break; case WavePlayers.ASIO: player = new AsioOut(deviceID); break; } if (player == null) { return; } provider = new AudioFileReader(waveFile) { Volume = volumeAsFloat }; player.Init(provider); player.PlaybackStopped += (s, e) => { player.Dispose(); var file = provider as IDisposable; if (file != null) { file.Dispose(); } if (isDelete) { File.Delete(waveFile); } }; // 再生する player.Play(); } catch (Exception ex) { ActGlobals.oFormActMain.WriteExceptionLog( ex, "サウンドの再生で例外が発生しました。"); } finally { sw.Stop(); Debug.WriteLine( "PlaySound ({0}) -> {1:N0} ticks", TTSYukkuriConfig.Default.Player, sw.ElapsedTicks); } }