OSDN Git Service

SSTFEditor から Viewer モードの StrokeStyleT を使って再生する機能を実装。ただし、まだ曲の先頭からの再生のみ。
[strokestylet/CsWin10Desktop3.git] / StrokeStyleT / StrokeStyleT.cs
index 952ef12..607fe88 100644 (file)
@@ -2,52 +2,47 @@
 using System.Collections.Concurrent;
 using System.Collections.Generic;
 using System.Diagnostics;
-using System.IO;
-using System.IO.Pipes;
 using System.Linq;
+using System.ServiceModel;
 using System.Windows.Forms;
 using FDK;
 
 namespace SST
 {
-       [System.ServiceModel.ServiceBehavior(InstanceContextMode = System.ServiceModel.InstanceContextMode.Single)]
+       [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]     // サービスインターフェースをシングルスレッドで呼び出す。
        class StrokeStyleT : SST.IStrokeStyleTService
        {
                // グローバルリソース (static) 
 
                public static SST.フォルダ フォルダ
-                       => ( StrokeStyleT.bs_フォルダ );
-
-               public static FDK.入力.Keyboard キーボード入力
-                       => ( StrokeStyleT.bs_キーボード入力 );
-
-               public static FDK.入力.MidiIn MIDI入力
-                       => ( StrokeStyleT.bs_MIDI入力 );
-
-               public static void すべての入力デバイスをポーリングする()
                {
-                       // hack: 追加の入力デバイスができたら、ここにポーリングコードを追加すること。
-                       StrokeStyleT.キーボード入力?.ポーリングする();
-                       StrokeStyleT.MIDI入力?.ポーリングする();
+                       get { return StrokeStyleT.bs_フォルダ; }
                }
-
                public static FDK.メディア.サウンド.WASAPI排他.ExclusiveDevice Wasapiデバイス
-                       => ( StrokeStyleT.bs_Wasapiデバイス );
-
+               {
+                       get { return StrokeStyleT.bs_Wasapiデバイス; }
+               }
                public static System.Random 乱数
-                       => ( StrokeStyleT.bs_乱数 );
-
+               {
+                       get { return StrokeStyleT.bs_乱数; }
+               }
                public static SST.ユーザ.ユーザ管理 ユーザ管理
-                       => ( StrokeStyleT.bs_ユーザ管理 );
-
+               {
+                       get { return StrokeStyleT.bs_ユーザ管理; }
+               }
                public static SST.曲.曲ツリー管理 曲ツリー管理
-                       => ( StrokeStyleT.bs_曲ツリー管理 );
-
-               public static SSTFormat.スコア 演奏スコア { get; set; } = null;
-
+               {
+                       get { return StrokeStyleT.bs_曲ツリー管理; }
+               }
+               public static SSTFormat.スコア 演奏スコア
+               {
+                       get;
+                       set;
+               } = null;
                public static SST.設定.Config Config
-                       => ( StrokeStyleT.bs_Config );
-
+               {
+                       get { return StrokeStyleT.bs_Config; }
+               }
                public static bool ビュアーモードである
                {
                        get;
@@ -58,9 +53,21 @@ namespace SST
                        get { return !StrokeStyleT.ビュアーモードである; }
                        set { StrokeStyleT.ビュアーモードである = !value; }
                }
+               public static FDK.入力.Keyboard キーボード入力
+               {
+                       get { return StrokeStyleT.bs_キーボード入力; }
+               }
+               public static FDK.入力.MidiIn MIDI入力
+               {
+                       get { return StrokeStyleT.bs_MIDI入力; }
+               }
 
-               public static ConcurrentQueue<SST.ステージ.演奏.ビュアーメッセージ> ビュアーメッセージキュー
-                       => ( StrokeStyleT.bs_ビュアーメッセージキュー );
+               public static void すべての入力デバイスをポーリングする()
+               {
+                       // hack: 追加の入力デバイスができたら、ここにポーリングコードを追加すること。
+                       StrokeStyleT.キーボード入力?.ポーリングする();
+                       StrokeStyleT.MIDI入力?.ポーリングする();
+               }
 
                static StrokeStyleT()
                {
@@ -72,7 +79,21 @@ namespace SST
                }
                public StrokeStyleT()
                {
+                       #region " ビュアーモードかどうかを確認する。"
+                       //----------------
+                       foreach( var arg in Environment.GetCommandLineArgs() )
+                       {
+                               if( ( "-v" == arg.ToLower() ) || ( "-viewer" == arg.ToLower() ) )
+                               {
+                                       StrokeStyleT.ビュアーモードである = true;
+                                       break;
+                               }
+                       }
+                       //----------------
+                       #endregion
+
                        this.State = ApplicationState.起動;
+
                        this.MainForm = new SharpDX.Windows.RenderForm();
                        this.MainForm.AllowUserResizing = false;        // ユーザはフォームサイズを変更できない。
                        this.MainForm.UserResized += フォームサイズが変更された;
@@ -81,6 +102,7 @@ namespace SST
                public void Run()
                {
                        Debug.Assert( null != this.MainForm );
+
                        SharpDX.Windows.RenderLoop.Run( this.MainForm, () => {
 
                                // アプリケーションの状態に応じて処理分岐。
@@ -105,20 +127,38 @@ namespace SST
                        } );
                }
 
-               #region " サービスインターフェースの実装 "
+               #region " WCF サービスインターフェースの実装 "
                //----------------
+               // ・このサービスインターフェースは、シングルスレッド(GUIスレッド)で同期実行される。(StrokeStyleT クラスの ServiceBehavior属性を参照。)
+               // ・このサービスホストはシングルトンであり、すべてのクライアントセッションは同一(単一)のサービスインスタンスへ接続される。(Program.Main() を参照。)
+
                public void ViewerPlay( string path, int startPart = 0, bool drumsSound = true )
                {
-                       // todo: 演奏を開始する。
+                       if( StrokeStyleT.ビュアーモードではない )
+                               return;
+
+                       this.ビュアーメッセージキュー.Enqueue( new ViewerMessage() {
+                               種別 = ViewerMessageType.演奏開始,
+                               曲ファイルパス = path,
+                               演奏開始小節番号 = startPart,
+                               ドラムチップ発声 = drumsSound,
+                       } );
                }
                public void ViewerStop()
                {
-                       // todo: 演奏を停止する。
+                       if( StrokeStyleT.ビュアーモードではない )
+                               return;
+
+                       this.ビュアーメッセージキュー.Enqueue( new ViewerMessage() {
+                               種別 = ViewerMessageType.演奏停止,
+                       } );
                }
                public float GetSoundDelay()
                {
-                       // todo: 発声遅延[ms]を返す。
-                       return 0f;
+                       if( StrokeStyleT.ビュアーモードではない )
+                               return 0f;
+
+                       return ( null != StrokeStyleT.Wasapiデバイス ) ? StrokeStyleT.Wasapiデバイス.遅延ms : 0f;
                }
                //----------------
                #endregion
@@ -127,7 +167,7 @@ namespace SST
                protected ApplicationState State;
                protected SharpDX.Windows.RenderForm MainForm = null;
                protected SharpDX.Size2F 設計画面サイズdpx = SharpDX.Size2F.Empty;
-
+               protected ConcurrentQueue<ViewerMessage> ビュアーメッセージキュー = new ConcurrentQueue<ViewerMessage>();
                protected FDK.メディア.デバイスリソース デバイスリソース = null;
                protected SST.ステージ.ステージ 最初のダミーステージ = null;
                protected SST.ステージ.起動.起動ステージ 起動ステージ = null;
@@ -143,6 +183,8 @@ namespace SST
                {
                        FDK.Log.現在のスレッドに名前をつける( "Render" );
                        FDK.Log.BeginInfo( $"{FDK.Utilities.現在のメソッド名}" );
+
+                       // 開始条件チェック。
                        Debug.Assert( null == this.デバイスリソース, "デバイスリソースの作成前であること。" );
 
                        StrokeStyleT.bs_ユーザ管理 = new ユーザ.ユーザ管理();
@@ -167,7 +209,6 @@ namespace SST
 
                        #region " コンフィグ を初期化する。"
                        //----------------
-                       FDK.Log.Info( "コンフィグを初期化します。" );
                        StrokeStyleT.bs_Config = new 設定.Config();
                        StrokeStyleT.bs_Config.ConfigXmlを読み込む();
                        //----------------
@@ -190,7 +231,6 @@ namespace SST
 
                        #region " デバイスリソースを作成する。"
                        //----------------
-                       FDK.Log.BeginInfo( "デバイスリソースを作成します。" );
                        FDK.Log.Info( $"設計画面サイズ: {this.設計画面サイズdpx}" );
                        FDK.Log.Info( $"物理画面サイズ: {this.MainForm.ClientSize}" );
                        this.デバイスリソース = new FDK.メディア.デバイスリソース( this.設計画面サイズdpx );
@@ -217,7 +257,7 @@ namespace SST
                        #endregion
                        #region " ユーザを初期化する。"
                        //-----------------
-                       FDK.Log.Info( "ユーザ情報を初期化します。" );
+                       // Users.xml を読み込む。
                        StrokeStyleT.ユーザ管理.UsersXmlを読み込む();
 
                        // ユーザ別の初期化。
@@ -233,13 +273,11 @@ namespace SST
                        #endregion
                        #region " キーボード入力 を初期化する。"
                        //-----------------
-                       FDK.Log.Info( "キーボード入力デバイスを初期化します。" );
                        StrokeStyleT.bs_キーボード入力 = new FDK.入力.Keyboard( this.MainForm.Handle );
                        //-----------------
                        #endregion
                        #region " MIDI入力 を初期化する。"
                        //-----------------
-                       FDK.Log.Info( "MIDI入力デバイスを初期化します。" );
                        StrokeStyleT.bs_MIDI入力 = new FDK.入力.MidiIn();
                        //-----------------
                        #endregion
@@ -247,7 +285,7 @@ namespace SST
                        FDK.Log.Info( "最初のダミーステージを開始します。" );
                        this.現在のステージ = this.最初のダミーステージ;
 
-                       //#warning 全画面モード切替えを KeyDown で仮実装。
+                       //#warning 全画面モード切替えを仮実装。
                        this.MainForm.KeyDown += ( target, arg ) => {
 
                                // F11 → 画面モードの切り替え
@@ -258,7 +296,9 @@ namespace SST
                                }
                        };
 
-                       Debug.Assert( SharpDX.Size2F.Empty != this.設計画面サイズdpx, "初期化メソッド内で設計画面サイズを設定してあること。" );
+                       // 終了条件チェック。
+                       Debug.Assert( SharpDX.Size2F.Empty != this.設計画面サイズdpx, "設計画面サイズが設定されていません。" );
+
                        FDK.Log.EndInfo( $"{FDK.Utilities.現在のメソッド名}" );
                }
                protected void 終了する()
@@ -374,8 +414,9 @@ namespace SST
                        if( StrokeStyleT.Config.垂直帰線待ちを行う )
                        {
                                // We recommend that you use Flush when the CPU waits for an arbitrary amount of time
-                               // ( such as when you call the Sleep function). 
-                               // https://msdn.microsoft.com/ja-jp/library/windows/desktop/ff476425(v=vs.85).aspx
+                               // (such as when you call the Sleep function). 
+                               // → https://msdn.microsoft.com/ja-jp/library/windows/desktop/ff476425(v=vs.85).aspx
+
                                var d3dDevice = (SharpDX.Direct3D11.Device) null;
                                using( var d3dLock = new FDK.同期.AutoD3DDeviceLock( this.デバイスリソース.DXGIDeviceManager, out d3dDevice ) )
                                using( d3dDevice )
@@ -390,6 +431,7 @@ namespace SST
                                SharpDX.DXGI.PresentFlags.None );
                        //----------------
                        #endregion
+
                        #region " ステージの状態をチェックし、必要あれば遷移またはアプリを終了する。"
                        //----------------
                        if( null != this.現在のステージ )
@@ -397,16 +439,24 @@ namespace SST
                                switch( this.現在のステージ.GetType().Name )
                                {
                                        case nameof( ステージ.ステージ ):
-                                               #region " ã\83\93ã\83¥ã\82¢ã\83¼ã\83¢ã\83¼ã\83\89 â\86\92 AutoPlayerã\81§ã\83­ã\82°ã\82¤ã\83³ã\81\97ã\81¦æ\9b²èª­è¾¼ステージへ。"
+                                               #region " ã\83\93ã\83¥ã\82¢ã\83¼ã\83¢ã\83¼ã\83\89 â\86\92 AutoPlayerã\81§ã\83­ã\82°ã\82¤ã\83³ã\81\97ã\81¦æ¼\94å¥\8fステージへ。"
                                                //----------------
                                                if( StrokeStyleT.ビュアーモードである )
                                                {
+                                                       // ビュアー用ユーザでログインする。
                                                        FDK.Log.Info( "ビュアーモード: AutoPlayer ユーザでログインします。" );
                                                        this.ログインする( Properties.Resources.AUTOPLAYER );
 
-                                                       this.曲読込ステージ.読込曲のファイルパスを取得する = () => ( null );  // 今は null 。あとでメッセージキューを見る。
-                                                       this.曲読込ステージ.活性化する( this.デバイスリソース );
-                                                       this.現在のステージ = this.曲読込ステージ;
+                                                       // 曲読込ステージ向けの初期設定。
+                                                       StrokeStyleT.曲ツリー管理.現在の管理対象ツリー = null;
+                                                       StrokeStyleT.曲ツリー管理.現在選択されているノード = null;
+                                                       
+                                                       // 演奏ステージ向けの初期設定。
+                                                       StrokeStyleT.演奏スコア = null;
+
+                                                       // 演奏ステージへ。
+                                                       this.演奏ステージ.活性化する( this.デバイスリソース );
+                                                       this.現在のステージ = this.演奏ステージ;
                                                }
                                                //----------------
                                                #endregion
@@ -529,45 +579,113 @@ namespace SST
                                                break;
 
                                        case nameof( ステージ.演奏.演奏ステージ ):
-                                               #region " 演奏終了 → 結果ステージへ。"
-                                               //--------------------
-                                               if( this.演奏ステージ.現在のフェーズ.Value == ステージ.演奏.演奏ステージ.フェーズ.クリアor失敗 )
-                                               {
-                                                       this.現在のステージ.非活性化する( this.デバイスリソース );
-                                                       this.現在のステージ = this.結果ステージ;
-                                                       this.現在のステージ.活性化する( this.デバイスリソース );
-                                               }
-                                               //--------------------
-                                               #endregion
-                                               #region " キャンセル → 選曲ステージへ。"
-                                               //--------------------
-                                               if( this.演奏ステージ.現在のフェーズ.Value == ステージ.演奏.演奏ステージ.フェーズ.キャンセル )
+                                               if( StrokeStyleT.ビュアーモードである )
                                                {
-                                                       this.現在のステージ.非活性化する( this.デバイスリソース );
-                                                       this.現在のステージ = this.選曲ステージ;
-                                                       this.現在のステージ.活性化する( this.デバイスリソース );
+                                                       // (A) ビュアーモード
+
+                                                       #region " ビュアーメッセージがあれば処理する。"
+                                                       //----------------
+                                                       var msg = this.ビュアーメッセージを取得する();
+
+                                                       if( null != msg )
+                                                       {
+                                                               #region " (A) 演奏開始 "
+                                                               //----------------
+                                                               if( msg.種別 == ViewerMessageType.演奏開始 && (
+                                                                       this.演奏ステージ.現在のフェーズ.Value == ステージ.演奏.演奏ステージ.フェーズ.ビュアーメッセージ待機中 ||
+                                                                       this.演奏ステージ.現在のフェーズ.Value == ステージ.演奏.演奏ステージ.フェーズ.演奏中 )
+                                                                       )
+                                                               {
+                                                                       // MusicNode を作成する。
+                                                                       var node = (SST.曲.MusicNode) null;
+                                                                       try
+                                                                       {
+                                                                               node = new 曲.MusicNode( msg.曲ファイルパス );
+                                                                       }
+                                                                       catch
+                                                                       {
+                                                                               FDK.Log.ERROR( $"MusicNode の作成に失敗しました。" );
+                                                                               node = null;
+                                                                       }
+
+                                                                       // 作成に成功したときのみ次へ進む。
+                                                                       if( null != node )
+                                                                       {
+                                                                               // 演奏を停止する。
+                                                                               this.演奏ステージ.非活性化する( this.デバイスリソース );
+                                                                               this.演奏ステージ.BGMを解放する();
+
+                                                                               // 現在選択されているノードとして登録する。
+                                                                               StrokeStyleT.曲ツリー管理.現在選択されているノード = node;
+
+                                                                               // todo: 演奏開始小節番号を保存し、反映する。
+
+                                                                               // 曲読込ステージへ。
+                                                                               this.現在のステージ = this.曲読込ステージ;
+                                                                               this.曲読込ステージ.活性化する( this.デバイスリソース );
+                                                                       }
+                                                               }
+                                                               //----------------
+                                                               #endregion
+                                                               #region " (B) 演奏停止 "
+                                                               //----------------
+                                                               else if( msg.種別 == ViewerMessageType.演奏停止 && (
+                                                                       this.演奏ステージ.現在のフェーズ.Value == ステージ.演奏.演奏ステージ.フェーズ.ビュアーメッセージ待機中 ||
+                                                                       this.演奏ステージ.現在のフェーズ.Value == ステージ.演奏.演奏ステージ.フェーズ.演奏中 )
+                                                                       )
+                                                               {
+                                                                       this.演奏ステージ.演奏を停止する();
+                                                                       this.演奏ステージ.現在のフェーズ.Value = ステージ.演奏.演奏ステージ.フェーズ.ビュアーメッセージ待機中;
+                                                               }
+                                                               //----------------
+                                                               #endregion
+                                                               #region " その他 "
+                                                               //----------------
+                                                               else
+                                                               {
+                                                                       // その他のメッセージは無視。
+                                                               }
+                                                               //----------------
+                                                               #endregion
+                                                       }
+                                                       //----------------
+                                                       #endregion
+                                                       #region " クリア/キャンセル → メッセージ待機へ。"
+                                                       //----------------
+                                                       if( this.演奏ステージ.現在のフェーズ.Value == ステージ.演奏.演奏ステージ.フェーズ.クリアor失敗 ||
+                                                               this.演奏ステージ.現在のフェーズ.Value == ステージ.演奏.演奏ステージ.フェーズ.キャンセル )
+                                                       {
+                                                               // フェーズのみ変更。BGM は止めない。
+                                                               this.演奏ステージ.現在のフェーズ.Value = ステージ.演奏.演奏ステージ.フェーズ.ビュアーメッセージ待機中;
+                                                       }
+                                                       //----------------
+                                                       #endregion
                                                }
-                                               //--------------------
-                                               #endregion
-                                               #region " ビュアーメッセージ受信 → 曲読込ステージへ。"
-                                               //--------------------
-                                               if( this.演奏ステージ.現在のフェーズ.Value == ステージ.演奏.演奏ステージ.フェーズ.ビュアーメッセージ待機中 )
+                                               else
                                                {
-                                                       var msg = (SST.ステージ.演奏.ビュアーメッセージ) null;
-                                                       if( ( 0 < StrokeStyleT.ビュアーメッセージキュー.Count ) &&
-                                                               ( StrokeStyleT.ビュアーメッセージキュー.TryDequeue( out msg ) ) )
-                                                       {
-                                                               FDK.Log.Info( "ビュアーメッセージを受信しました。" );
-                                                               this.曲読込ステージ.読込曲のファイルパスを取得する = () => { return msg.曲ファイルパス; };
+                                                       // (B) 通常モード
 
+                                                       #region " 演奏終了 → 結果ステージへ。"
+                                                       //--------------------
+                                                       if( this.演奏ステージ.現在のフェーズ.Value == ステージ.演奏.演奏ステージ.フェーズ.クリアor失敗 )
+                                                       {
                                                                this.現在のステージ.非活性化する( this.デバイスリソース );
-                                                               this.演奏ステージ.BGMを解放する();
-                                                               this.現在のステージ = this.曲読込ステージ;
+                                                               this.現在のステージ = this.結果ステージ;
                                                                this.現在のステージ.活性化する( this.デバイスリソース );
                                                        }
+                                                       //--------------------
+                                                       #endregion
+                                                       #region " キャンセル → 選曲ステージへ。"
+                                                       //--------------------
+                                                       if( this.演奏ステージ.現在のフェーズ.Value == ステージ.演奏.演奏ステージ.フェーズ.キャンセル )
+                                                       {
+                                                               this.現在のステージ.非活性化する( this.デバイスリソース );
+                                                               this.現在のステージ = this.選曲ステージ;
+                                                               this.現在のステージ.活性化する( this.デバイスリソース );
+                                                       }
+                                                       //--------------------
+                                                       #endregion
                                                }
-                                               //--------------------
-                                               #endregion
                                                break;
 
                                        case nameof( ステージ.結果.結果ステージ ):
@@ -671,6 +789,20 @@ namespace SST
                        StrokeStyleT.ユーザ管理.ユーザを選択する( ユーザ名 );
                        FDK.Log.Info( $"ユーザが選択されました。[{StrokeStyleT.ユーザ管理.現在選択されているユーザ.名前}]" );
                }
+               /// <returns>メッセージがない場合は null を返す。</returns>
+               private ViewerMessage ビュアーメッセージを取得する()
+               {
+                       var msg = (ViewerMessage) null;
+
+                       if( StrokeStyleT.ビュアーモードである &&
+                               ( 0 < this.ビュアーメッセージキュー.Count ) &&
+                               this.ビュアーメッセージキュー.TryDequeue( out msg ) )
+                       {
+                               return msg;
+                       }
+
+                       return null;
+               }
 
                #region " バックストア。"
                //----------------
@@ -682,7 +814,6 @@ namespace SST
                private static SST.ユーザ.ユーザ管理 bs_ユーザ管理 = null;
                private static SST.曲.曲ツリー管理 bs_曲ツリー管理 = null;
                private static SST.設定.Config bs_Config = null;
-               private static readonly ConcurrentQueue<SST.ステージ.演奏.ビュアーメッセージ> bs_ビュアーメッセージキュー = new ConcurrentQueue<ステージ.演奏.ビュアーメッセージ>();
                //----------------
                #endregion
        }