OSDN Git Service

排他モードと共有モードの両方の実相を完了。
[strokestylet/CsWin10Desktop3.git] / StrokeStyleT / ステージ / 演奏 / 演奏ステージ.cs
index a3fbdd7..ca4871a 100644 (file)
@@ -1,4 +1,5 @@
 using System;
+using System.Collections.Concurrent;
 using System.Collections.Generic;
 using System.Diagnostics;
 using System.Linq;
@@ -11,8 +12,9 @@ namespace SST.ステージ.演奏
        /// 入力:
        ///  StrokeStyleT.演奏スコア(ビュアーモードなら null でも可)
        ///  StrokeStyleT.ユーザ管理.現在選択されているユーザ(譜面スクロール速度の倍率)
-       ///  StrokeStyleT.ビュアーモードである
        ///  StrokeStyleT.Wasapiデバイス.AudioClock
+       ///  StrokeStyleT.ビュアーモードである
+       ///  StrokeStyleT.最後に取得したビュアーメッセージ
        ///  
        /// 出力:
        ///  (A) クリアまたは失敗した場合
@@ -25,7 +27,7 @@ namespace SST.ステージ.演奏
        /// </remarks>
        class 演奏ステージ : ステージ
        {
-               public Dictionary<ヒット判定種別, int> ヒットした回数 { get; } = new Dictionary<ヒット判定種別, int>();
+               public ConcurrentDictionary<ヒット判定種別, int> ヒットした回数 { get; } = new ConcurrentDictionary<ヒット判定種別, int>();
                public enum フェーズ
                {
                        初期状態,
@@ -34,7 +36,7 @@ namespace SST.ステージ.演奏
                        キャンセル,
                        ビュアーメッセージ待機中,
                }
-               public フェーズ 現在のフェーズ { get; protected set; } = フェーズ.初期状態;
+               public FDK.同期.RWLock<フェーズ> 現在のフェーズ { get; } = new FDK.同期.RWLock<フェーズ>( フェーズ.初期状態 );
 
                public 演奏ステージ()
                {
@@ -62,21 +64,32 @@ namespace SST.ステージ.演奏
                        this.スクロール譜面.ヒット判定数加算 = ( hitType ) => {
                                this.ヒットした回数[ hitType ]++;
                        };
-                       this.スクロール譜面.背景動画再生開始 = () => {
-                               this.背景動画開始済み = true;
+                       this.スクロール譜面.背景動画の長さsec = () => {
+                               return ( null != this.BGM ) ? this.BGM.長さsec : 0.0;
+                       };
+                       this.スクロール譜面.背景動画再生開始 = ( 開始位置sec ) => {
+                               this.背景動画.再生を開始する( 開始位置sec );
+                               this.背景動画開始済み.Value = true;
+                               if( null != this.BGM )
+                               {
+                                       this.BGM.位置sec = 開始位置sec;
+                                       this.BGM?.Play();
+                               }
+                               this.BGM再生開始済み = true;
                        };
                        this.スクロール譜面.チップヒット = ( chip ) => {
                                this.回転羽.発火する( chip.チップ種別 );
-                               this.ドラムサウンド.発声する( chip.チップ種別, chip.音量 * 0.25f );
+                               if( this.Autoチップのドラム音を再生する )
+                                       this.ドラムサウンド.発声する( chip.チップ種別, chip.音量 * 0.25f );
                        };
                        this.スクロール譜面.ステージクリア = () => {
-                               this.現在のフェーズ = ( StrokeStyleT.ビュアーモードである ) ? フェーズ.ビュアーメッセージ待機中 : フェーズ.クリアor失敗;
+                               this.現在のフェーズ.Value = フェーズ.クリアor失敗;
                        };
                        this.スクロール譜面.リアルタイム演奏時刻sec = () => {
                                return this.現在の演奏時刻secを返す();
                        };
                        this.スクロール譜面.譜面スクロール速度の倍率 = () => {
-                               return this.現在進行描画中の譜面スクロール速度の倍率;
+                               return this.現在進行描画中の譜面スクロール速度の倍率.Value;
                        };
                        this.スクロール譜面.FPSをカウント = () => {
                                this.FPS.FPSをカウントする();
@@ -91,12 +104,10 @@ namespace SST.ステージ.演奏
                        if( ( null != StrokeStyleT.演奏スコア ) && ( StrokeStyleT.演奏スコア.背景動画ファイル名.Nullでも空でもない() ) )
                        {
                                // 動画を子リストに追加。
-                               this.子リスト.Add( this.背景動画 = new 動画( StrokeStyleT.演奏スコア.背景動画ファイル名 ) );
+                               this.子リスト.Add( this.背景動画 = new 動画( StrokeStyleT.演奏スコア.背景動画ファイル名, StrokeStyleT.Config.動画デコーダのキューサイズ ) );
 
-                               // 動画から BGM を作成してミキサーに追加。
-                               this.BGM = new FDK.メディア.サウンド.WASAPI排他.Sound();
-                               this.BGM.ファイルから作成する( StrokeStyleT.演奏スコア.背景動画ファイル名 );
-                               StrokeStyleT.Wasapiデバイス.サウンドをミキサーに追加する( this.BGM ); // 作成に失敗した Sound を追加しても鳴らないだけなので、ノーチェックで大丈夫。
+                               // 動画から BGM を作成。
+                               this.BGM = StrokeStyleT.サウンドデバイス.CreateSound( StrokeStyleT.演奏スコア.背景動画ファイル名 );
                        }
                        else
                        {
@@ -107,16 +118,27 @@ namespace SST.ステージ.演奏
                        #endregion
 
                        this.演奏開始時刻sec = 0.0;
-                       this.現在進行描画中の譜面スクロール速度の倍率 = StrokeStyleT.ユーザ管理.現在選択されているユーザ.譜面スクロール速度の倍率;
+                       this.現在進行描画中の譜面スクロール速度の倍率.Value = StrokeStyleT.ユーザ管理.現在選択されているユーザ.譜面スクロール速度の倍率;
                        this.BGM再生開始済み = false;
-                       this.背景動画開始済み = false;
+                       this.背景動画開始済み.Value = false;
                        this.レーンフレーム.左端位置dpx = 400f;
                        this.レーンフレーム.高さdpx = dr.設計画面サイズdpx.Height;
                        this.ヒットした回数.Clear();
                        foreach( var 判定 in typeof( ヒット判定種別 ).GetEnumValues() )
                                this.ヒットした回数[ (ヒット判定種別) 判定 ] = 0;
 
-                       this.現在のフェーズ = ( StrokeStyleT.ビュアーモードである ) ? フェーズ.ビュアーメッセージ待機中 : フェーズ.演奏中;
+                       // 最初のフェーズを設定する。
+                       if( StrokeStyleT.ビュアーモードである )
+                       {
+                               // 演奏スコアが設定済みなら演奏開始。それ以外ならメッセージ待機へ。
+                               this.現在のフェーズ.Value = ( null != StrokeStyleT.演奏スコア ) ? フェーズ.演奏中 : フェーズ.ビュアーメッセージ待機中;
+                       }
+                       else
+                       {
+                               // 演奏開始。
+                               this.現在のフェーズ.Value = フェーズ.演奏中;
+                       }
+
                        this.活性化した直後である = true;
                }
                protected override void On非活性化( デバイスリソース dr )
@@ -138,7 +160,23 @@ namespace SST.ステージ.演奏
                        {
                                this.活性化した直後である = false;
                                this.FPS = new FDK.カウンタ.FPS();
-                               this.演奏開始時刻sec = this.サウンドタイマ.現在のデバイス位置secを取得する( StrokeStyleT.Wasapiデバイス.AudioClock );
+
+                               double 演奏開始位置の先頭からの時間sec = 0.0;
+                               int 演奏開始小節番号 = 0;
+                               var msg = StrokeStyleT.最後に取得したビュアーメッセージ;
+                               if( null != msg )
+                               {
+                                       演奏開始小節番号 = msg.演奏開始小節番号;
+                                       this.Autoチップのドラム音を再生する = msg.ドラムチップ発声;
+                               }
+                               演奏開始位置の先頭からの時間sec = this.スクロール譜面.演奏開始小節番号を設定しその時刻secを返す( 演奏開始小節番号 );
+
+                               long position, qpcPosition, frequency;
+                               StrokeStyleT.サウンドデバイス.GetClock( out position, out qpcPosition, out frequency );
+
+                               this.演奏開始時刻sec = 
+                                       this.サウンドタイマ.現在のデバイス位置secを取得する( position, qpcPosition, frequency )
+                                       - 演奏開始位置の先頭からの時間sec;                // 「+」じゃないので注意!
                        }
                        //----------------
                        #endregion
@@ -147,23 +185,24 @@ namespace SST.ステージ.演奏
 
                        #region " 譜面スクロール速度が変化している場合の追い付き進行。"
                        //----------------
-                       if( this.現在進行描画中の譜面スクロール速度の倍率 < StrokeStyleT.ユーザ管理.現在選択されているユーザ.譜面スクロール速度の倍率 )
+                       double 倍率 = this.現在進行描画中の譜面スクロール速度の倍率.Value;
+                       if( 倍率 < StrokeStyleT.ユーザ管理.現在選択されているユーザ.譜面スクロール速度の倍率 )
                        {
                                // todo: 時間間隔に関係なく進行描画ごとに倍率を変えてしまっているので、あとからVPSに依存しないよう修正すること。
-                               this.現在進行描画中の譜面スクロール速度の倍率 =
-                                       Math.Min( this.現在進行描画中の譜面スクロール速度の倍率 + 0.05, StrokeStyleT.ユーザ管理.現在選択されているユーザ.譜面スクロール速度の倍率 );
+                               this.現在進行描画中の譜面スクロール速度の倍率.Value =
+                                       Math.Min( 倍率 + 0.05, StrokeStyleT.ユーザ管理.現在選択されているユーザ.譜面スクロール速度の倍率 );
                        }
-                       else if( this.現在進行描画中の譜面スクロール速度の倍率 > StrokeStyleT.ユーザ管理.現在選択されているユーザ.譜面スクロール速度の倍率 )
+                       else if( 倍率 > StrokeStyleT.ユーザ管理.現在選択されているユーザ.譜面スクロール速度の倍率 )
                        {
                                // todo: 同上。
-                               this.現在進行描画中の譜面スクロール速度の倍率 =
-                                       Math.Max( this.現在進行描画中の譜面スクロール速度の倍率 - 0.05, StrokeStyleT.ユーザ管理.現在選択されているユーザ.譜面スクロール速度の倍率 );
+                               this.現在進行描画中の譜面スクロール速度の倍率.Value =
+                                       Math.Max( 倍率 - 0.05, StrokeStyleT.ユーザ管理.現在選択されているユーザ.譜面スクロール速度の倍率 );
                        }
                        //----------------
                        #endregion
                        #region " 背景動画とBGMの開始/進行描画を行う。"
                        //----------------
-                       if( this.背景動画開始済み )
+                       if( this.背景動画開始済み.Value )
                        {
                                // 背景動画チップがヒット済みなら、背景動画の進行描画を行う。
                                this.背景動画?.進行描画する( dr, new SharpDX.RectangleF( 0f, 0f, dr.設計画面サイズdpx.Width, dr.設計画面サイズdpx.Height ) );
@@ -174,7 +213,7 @@ namespace SST.ステージ.演奏
                                // 背景動画が再生されているのにBGMがまだ再生されていないなら、再生を開始する。
                                if( false == this.BGM再生開始済み )
                                {
-                                       this.BGM?.再生を開始する();
+                                       this.BGM?.Play();
                                        this.BGM再生開始済み = true;
                                }
                        }
@@ -200,10 +239,10 @@ namespace SST.ステージ.演奏
 
                        // ESC 押下 → キャンセル
                        if( StrokeStyleT.キーボード入力.キーが押された( SharpDX.DirectInput.Key.Escape ) &&
-                               ( false == StrokeStyleT.ビュアーモードである ) )
+                               StrokeStyleT.ビュアーモードではない )  // ビュアーモードでは無効。
                        {
                                this.BGMを解放する();
-                               this.現在のフェーズ = フェーズ.キャンセル;
+                               this.現在のフェーズ.Value = フェーズ.キャンセル;
                        }
                        // 上矢印押下 → 譜面スクロール速度の倍率を +0.5
                        else if( StrokeStyleT.キーボード入力.キーが押された( SharpDX.DirectInput.Key.Up ) )
@@ -218,21 +257,28 @@ namespace SST.ステージ.演奏
                                        Math.Max( StrokeStyleT.ユーザ管理.現在選択されているユーザ.譜面スクロール速度の倍率 - 0.5, 0.5 );    // 最小 0.5
                        }
                }
+               public void 演奏を停止する()
+               {
+                       this.スクロール譜面?.演奏を停止する();
+                       this.背景動画開始済み.Value = false;
+                       this.BGMを解放する();
+               }
                public void BGMを解放する()
                {
                        if( null != this.BGM )  // 背景動画がなければ BGM も null である
                        {
-                               StrokeStyleT.Wasapiデバイス.サウンドをミキサーから削除する( this.BGM );
+                               this.BGM.Stop();
                                FDK.Utilities.解放する( ref this.BGM );
                        }
                }
 
                protected bool 活性化した直後である = false;
-               protected bool 背景動画開始済み = false;
+               protected FDK.同期.RWLock<bool> 背景動画開始済み = new FDK.同期.RWLock<bool>( false );
                protected bool BGM再生開始済み = false;
-               protected double 現在進行描画中の譜面スクロール速度の倍率 = 0.0;
+               protected FDK.同期.RWLock<double> 現在進行描画中の譜面スクロール速度の倍率 = new FDK.同期.RWLock<double>( 0.0 );
                protected double 演奏開始時刻sec = 0.0;
-               protected readonly FDK.メディア.サウンド.WASAPI排他.SoundTimer サウンドタイマ = new FDK.メディア.サウンド.WASAPI排他.SoundTimer();
+               protected bool Autoチップのドラム音を再生する = true;
+               protected readonly FDK.メディア.サウンド.WASAPI.SoundTimer サウンドタイマ = new FDK.メディア.サウンド.WASAPI.SoundTimer();
                protected readonly SST.ステージ.演奏.コンボ コンボ;
                protected readonly SST.ステージ.演奏.レーンフレーム レーンフレーム;
                protected readonly SST.ステージ.演奏.スクロール譜面 スクロール譜面;
@@ -247,7 +293,7 @@ namespace SST.ステージ.演奏
                /// 解放は、演奏ステージクラスの非活性化後に、外部から行われる。
                /// <see cref="SST.ステージ.演奏.演奏ステージ.BGMを解放する"/>
                /// </remarks>
-               protected FDK.メディア.サウンド.WASAPI排他.Sound BGM = null;
+               protected FDK.メディア.サウンド.WASAPI.Sound BGM = null;
                protected FDK.カウンタ.FPS FPS = null;
                /// <summary>
                /// 動的子Activity。背景動画を再生しない場合は null のまま。
@@ -256,7 +302,10 @@ namespace SST.ステージ.演奏
 
                private double 現在の演奏時刻secを返す()
                {
-                       return this.サウンドタイマ.現在のデバイス位置secを取得する( StrokeStyleT.Wasapiデバイス.AudioClock ) - this.演奏開始時刻sec;
+                       long position, qpcPosition, frequency;
+                       StrokeStyleT.サウンドデバイス.GetClock( out position, out qpcPosition, out frequency );
+
+                       return this.サウンドタイマ.現在のデバイス位置secを取得する( position, qpcPosition, frequency ) - this.演奏開始時刻sec;
                }
        }
 }