OSDN Git Service

WASAPIクライアントの起動と終了を追加。
authorくまかみ工房 <kumakamikoubou@gmail.com>
Sun, 18 Sep 2016 09:10:53 +0000 (18:10 +0900)
committerくまかみ工房 <kumakamikoubou@gmail.com>
Sun, 18 Sep 2016 09:10:53 +0000 (18:10 +0900)
FDK24/メディア/サウンド/WASAPI排他/ExclusiveDevice.cs
StrokeStyleT/SSTException.cs [new file with mode: 0644]
StrokeStyleT/StrokeStyleT.cs
StrokeStyleT/StrokeStyleT.csproj

index aa24f88..95e038d 100644 (file)
@@ -8,8 +8,7 @@ namespace FDK.メディア.サウンド.WASAPI排他
                private static int AUDCLNT_E_BUFFER_SIZE_NOT_ALIGNED = unchecked((int) 0x88890019);
 
                // for SoundTimer
-               public CSCore.CoreAudioAPI.AudioClock AudioClock
-                       => this.bs_AudioClock;
+               public CSCore.CoreAudioAPI.AudioClock AudioClock => this.bs_AudioClock;
 
                public ExclusiveDevice()
                {
@@ -191,6 +190,7 @@ namespace FDK.メディア.サウンド.WASAPI排他
                                //SharpDX.MediaFoundation.MediaFactory.CancelWorkItem( this.出力要請イベントキャンセル用キー ); --> コールバックの実行中にキャンセルしてしまうと NullReference例外
                                this.出力終了通知.状態 = 同期.TriStateEvent.状態種別.ON;
                                this.出力終了通知.OFFになるまでブロックする();
+                               FDK.Log.Info( "WASAPI出力処理を終了しました。" );
                        }
                        //-----------------
                        #endregion
@@ -199,11 +199,9 @@ namespace FDK.メディア.サウンド.WASAPI排他
                        {
                                #region " オーディオのレンダリングを停止する。"
                                //-----------------
-                               if( null != this.AudioClient )
-                                       this.AudioClient.Stop();
+                               this.AudioClient?.Stop();
                                //-----------------
                                #endregion
-
                                #region " ミキサー(とサウンドリスト)は現状を維持する。"
                                //-----------------
 
@@ -236,6 +234,8 @@ namespace FDK.メディア.サウンド.WASAPI排他
                                //-----------------
                                #endregion
                        }
+
+                       FDK.Log.Info( "WASAPIクライアントを終了しました。" );
                }
                public void サウンドをミキサーに追加する( Sound sound )
                {
diff --git a/StrokeStyleT/SSTException.cs b/StrokeStyleT/SSTException.cs
new file mode 100644 (file)
index 0000000..8135795
--- /dev/null
@@ -0,0 +1,17 @@
+using System;
+
+namespace SST
+{
+       class SSTException : Exception
+       {
+               public SSTException() : base()
+               {
+               }
+               public SSTException( string msg ) : base( msg )
+               {
+               }
+               public SSTException( string msg, Exception inner ) : base( msg, inner )
+               {
+               }
+       }
+}
index ce26913..9d61857 100644 (file)
@@ -10,6 +10,8 @@ namespace SST
        {
                // グローバルリソース (static) 
                public static SST.フォルダ フォルダ => StrokeStyleT.bs_フォルダ;
+               public static FDK.メディア.サウンド.WASAPI排他.ExclusiveDevice Wasapiデバイス => StrokeStyleT.bs_Wasapiデバイス;
+               public static Random 乱数 => StrokeStyleT.bs_乱数;
 
                // get only static property の初期化。
                static StrokeStyleT()
@@ -29,6 +31,30 @@ namespace SST
                        this.Text = $"StrokeStyle<T> {System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()}";
                        this.ClientSize = new System.Drawing.Size( 640, 480 );  // 初期サイズ
 
+                       #region " System.Stopwatch が高解像度タイマを使わないならエラー。"
+                       //-----------------
+                       if( false == System.Diagnostics.Stopwatch.IsHighResolution )
+                               throw new SSTException( "このシステムは、高解像度タイマをサポートしていません。" );
+                       //-----------------
+                       #endregion
+                       #region " MediaFoundation を起動する。"
+                       //-----------------
+                       SharpDX.MediaFoundation.MediaManager.Startup();
+                       //-----------------
+                       #endregion
+                       #region " Sleep 精度を上げる。"
+                       //-----------------
+                       StrokeStyleT.timeBeginPeriod( 1 );
+                       //-----------------
+                       #endregion
+
+                       #region " WASAPI デバイスを初期化する。"
+                       //----------------
+                       StrokeStyleT.bs_Wasapiデバイス = new FDK.メディア.サウンド.WASAPI排他.ExclusiveDevice();
+                       StrokeStyleT.bs_Wasapiデバイス.初期化する( 7.0f );
+                       //----------------
+                       #endregion
+
                        this.KeyDown += ( target, arg ) => {
                                // Esc → アプリ終了。
                                if( arg.KeyCode == System.Windows.Forms.Keys.Escape )
@@ -38,6 +64,17 @@ namespace SST
                public override void 終了する()
                {
                        Debug.Assert( null == this.デバイスリソース );  // 解放された後である。
+
+                       #region " WASAPIデバイスを解放する。"
+                       //----------------
+                       FDK.Utilities.解放する( ref StrokeStyleT.bs_Wasapiデバイス );
+                       //----------------
+                       #endregion
+                       #region " MediaFoundation を終了する。"
+                       //-----------------
+                       SharpDX.MediaFoundation.MediaManager.Shutdown();
+                       //-----------------
+                       #endregion
                }
                public override void シーンを描画する()
                {
@@ -62,7 +99,19 @@ namespace SST
                #region " バックストア。"
                //----------------
                private static SST.フォルダ bs_フォルダ = null;
+               private static FDK.メディア.サウンド.WASAPI排他.ExclusiveDevice bs_Wasapiデバイス = null;
+               private static readonly Random bs_乱数 = new Random( DateTime.Now.Millisecond );
                //----------------
                #endregion
+
+               #region " Win32 API "
+               //-----------------
+               [System.Runtime.InteropServices.DllImport( "winmm.dll", EntryPoint = "timeBeginPeriod" )]
+               private static extern uint timeBeginPeriod( uint uMilliseconds );
+
+               [System.Runtime.InteropServices.DllImport( "winmm.dll", EntryPoint = "timeEndPeriod" )]
+               private static extern uint timeEndPeriod( uint uMilliseconds );
+               //-----------------
+               #endregion
        }
 }
index cee7d86..51acc1a 100644 (file)
@@ -88,6 +88,7 @@
     <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="SSTException.cs" />
     <Compile Include="StrokeStyleT.cs">
       <SubType>Form</SubType>
     </Compile>