OSDN Git Service

#39490 環境によっては同じ名前のWASAPIデバイスが複数定義されている場合に対応。 実際に利用可能なWASAPIデバイスのみ利用する。
[dtxmania/dtxmania.git] / FDK / コード / 03.サウンド / CSoundDeviceWASAPI.cs
index 179f92a..4ad9cff 100644 (file)
@@ -104,6 +104,13 @@ namespace FDK
                                }
                        }
                }
+
+               public string strDefaultSoundDeviceBusType {
+                       get;
+                       protected set;
+               }
+
+               
                // メソッド
 
                /// <summary>
@@ -184,22 +191,30 @@ namespace FDK
                        //(デバッグ用)
                        Trace.TraceInformation("サウンドデバイス一覧:");
                        int a;
-                       string strDefaultSoundDeviceName = "";
+                       string strDefaultSoundDeviceName = null;
                        BASS_DEVICEINFO[] bassDevInfos = Bass.BASS_GetDeviceInfos();
                        for (a = 0; a < bassDevInfos.GetLength(0); a++)
                        {
                                {
-                                       Trace.TraceInformation("Sound Device #{0}: {1}: IsDefault={2}, isEnabled={3}, flags={4}, driver={5}",
+                                       Trace.TraceInformation("Sound Device #{0}: {1}: IsDefault={2}, isEnabled={3}, flags={4}, id={5}",
                                                a,
                                                bassDevInfos[a].name,
                                                bassDevInfos[a].IsDefault,
                                                bassDevInfos[a].IsEnabled,
                                                bassDevInfos[a].flags,
-                                               bassDevInfos[a].driver
+                                               bassDevInfos[a].id
                                        );
                                        if (bassDevInfos[a].IsDefault)
                                        {
+                                               // これはOS標準のdefault device。後でWASAPIのdefault deviceと比較する。
                                                strDefaultSoundDeviceName = bassDevInfos[a].name;
+                                               
+                                               // 以下はOS標準 default deviceのbus type (PNPIDの頭の文字列)。上位側で使用する。
+                                               string[] s = bassDevInfos[a].id.ToString().ToUpper().Split(new char[] { '#' });
+                                               if (s != null && s[0] != null)
+                                               {
+                                                       strDefaultSoundDeviceBusType = s[0];
+                                               }
                                        }
                                }
                        }
@@ -219,11 +234,15 @@ namespace FDK
                        BASS_WASAPI_DEVICEINFO deviceInfo;
                        for ( int n = 0; ( deviceInfo = BassWasapi.BASS_WASAPI_GetDeviceInfo( n ) ) != null; n++ )
                        {
-                               // BASS_DEVICEINFOとBASS_WASAPI_DEVICEINFOで、IsDefaultとなっているデバイスが異なる場合がある。
+                               // #37940 2018.2.15: BASS_DEVICEINFOとBASS_WASAPI_DEVICEINFOで、IsDefaultとなっているデバイスが異なる場合がある。
                                // (WASAPIでIsDefaultとなっているデバイスが正しくない場合がある)
                                // そのため、BASS_DEVICEでIsDefaultとなっているものを探し、それと同じ名前のWASAPIデバイスを使用する。
+                               // #39490 2019.8.19: 更に、環境によっては同じ名前のWASAPIデバイスが複数定義されている場合があるため、
+                               // 実際に利用可能なWASAPIデバイスのみに対象を絞り込む。
+                               // (具体的には、defperiod, minperiod, mixchans, mixfreqがすべて0のデバイスは使用不可のため
+                               //  これらが0でないものを選択する)
                                //if ( deviceInfo.IsDefault )
-                               if ( deviceInfo.name == strDefaultSoundDeviceName)
+                               if ( deviceInfo.name == strDefaultSoundDeviceName && deviceInfo.mixfreq > 0 )
                                {
                                        nDevNo = n;
 #region [ 既定の出力デバイスの情報を表示 ]
@@ -322,7 +341,7 @@ Trace.TraceInformation("WASAPI Device #{0}: {1}: IsDefault={2}, defPeriod={3}s,
                                        f希望バッファサイズsec = f更新間隔sec * 2;
                                }
                        }
-
+                       else
                        if (COS.bIsWin10OrLater() && (mode == Eデバイスモード.共有))           // Win10 low latency shared mode support
                        {
                                // バッファ自動設定をユーザーが望む場合は、periodを最小値にする。さもなくば、バッファサイズとしてユーザーが指定した値を、periodとして用いる。
@@ -333,6 +352,10 @@ Trace.TraceInformation("WASAPI Device #{0}: {1}: IsDefault={2}, defPeriod={3}s,
                                else
                                {
                                        f更新間隔sec = n希望バッファサイズms / 1000.0f;
+                                       if (f更新間隔sec < deviceInfo.minperiod)
+                                       {
+                                               f更新間隔sec = deviceInfo.minperiod;
+                                       }
                                }
                                f希望バッファサイズsec = 0.0f;
                        }
@@ -340,7 +363,7 @@ Trace.TraceInformation("WASAPI Device #{0}: {1}: IsDefault={2}, defPeriod={3}s,
                        Trace.TraceInformation("f希望バッファサイズsec=" + f希望バッファサイズsec + ", f更新間隔sec=" + f更新間隔sec + ": Win10 low latency audio 考慮後");
 
                        Trace.TraceInformation("Start Bass_Wasapi_Init(device=" + nDevNo + ", freq=" + n周波数 + ", nchans=" + nチャンネル数 + ", flags=" + flags + "," +
-                               " buffer=" + f希望バッファサイズsec + ", period=" + f更新間隔sec);
+                               " buffer=" + f希望バッファサイズsec + ", period=" + f更新間隔sec + ")" );
                        if (BassWasapi.BASS_WASAPI_Init(nDevNo, n周波数, nチャンネル数, flags, f希望バッファサイズsec, f更新間隔sec, this.tWasapiProc, IntPtr.Zero))
                        {
                                        if ( mode == Eデバイスモード.排他 )
@@ -435,8 +458,10 @@ Trace.TraceInformation("WASAPI Device #{0}: {1}: IsDefault={2}, defPeriod={3}s,
                                //-----------------
                                #endregion
                        }
-
-
+#if TEST_MultiThreadedMixer
+                       //LoadLibraryに失敗する・・・
+                       //BASSThreadedMixerLibraryWrapper.InitBASSThreadedMixerLibrary();
+#endif
 
 
                        // WASAPI出力と同じフォーマットを持つ BASS ミキサーを作成。
@@ -446,11 +471,20 @@ Trace.TraceInformation("WASAPI Device #{0}: {1}: IsDefault={2}, defPeriod={3}s,
                        //      string.Format( "BASS_SetConfig(CONFIG_MIXER_BUFFER) に失敗しました。[{0}", Bass.BASS_ErrorGetCode() ) );
 
                        var info = BassWasapi.BASS_WASAPI_GetInfo();
+#if TEST_MultiThreadedMixer
+                       this.hMixer = BASSThreadedMixerLibraryWrapper.BASS_ThreadedMixer_Create(
+                               info.freq,
+                               info.chans,
+                               (int)(BASSFlag.BASS_MIXER_NONSTOP | BASSFlag.BASS_SAMPLE_FLOAT | BASSFlag.BASS_STREAM_DECODE | BASSFlag.BASS_MIXER_POSEX),
+                               out hMixerThreaded
+                       );
+#else
                        this.hMixer = BassMix.BASS_Mixer_StreamCreate(
                                info.freq,
                                info.chans,
-                               BASSFlag.BASS_MIXER_NONSTOP | BASSFlag.BASS_SAMPLE_FLOAT | BASSFlag.BASS_STREAM_DECODE | BASSFlag.BASS_MIXER_POSEX );   // デコードのみ=発声しない。WASAPIに出力されるだけ。
-                       if ( this.hMixer == 0 )
+                               BASSFlag.BASS_MIXER_NONSTOP | BASSFlag.BASS_SAMPLE_FLOAT | BASSFlag.BASS_STREAM_DECODE | BASSFlag.BASS_MIXER_POSEX);    // デコードのみ=発声しない。WASAPIに出力されるだけ。
+#endif
+                       if (this.hMixer == 0 )
                        {
                                BASSError errcode = Bass.BASS_ErrorGetCode();
                                BassWasapi.BASS_WASAPI_Free();
@@ -462,10 +496,19 @@ Trace.TraceInformation("WASAPI Device #{0}: {1}: IsDefault={2}, defPeriod={3}s,
 
                        for (int i = 0; i <= (int)CSound.EInstType.Unknown; i++)
                        {
+#if TEST_MultiThreadedMixer
+                               this.hMixer_Chips[i] = BASSThreadedMixerLibraryWrapper.BASS_ThreadedMixer_Create(
+                                       info.freq,
+                                       info.chans,
+                                       (int)(BASSFlag.BASS_MIXER_NONSTOP | BASSFlag.BASS_SAMPLE_FLOAT | BASSFlag.BASS_STREAM_DECODE | BASSFlag.BASS_MIXER_POSEX),
+                                       out this.hMixerThreaded_Chips[i]
+                               );    // デコードのみ=発声しない。WASAPIに出力されるだけ。
+#else
                                this.hMixer_Chips[ i ] = BassMix.BASS_Mixer_StreamCreate(
                                        info.freq,
                                        info.chans,
                                        BASSFlag.BASS_MIXER_NONSTOP | BASSFlag.BASS_SAMPLE_FLOAT | BASSFlag.BASS_STREAM_DECODE | BASSFlag.BASS_MIXER_POSEX);    // デコードのみ=発声しない。WASAPIに出力されるだけ。
+#endif
                                if (this.hMixer_Chips[ i ] == 0)
                                {
                                        BASSError errcode = Bass.BASS_ErrorGetCode();
@@ -476,10 +519,14 @@ Trace.TraceInformation("WASAPI Device #{0}: {1}: IsDefault={2}, defPeriod={3}s,
                                }
 
                                // Mixerのボリューム設定
-                               Bass.BASS_ChannelSetAttribute(this.hMixer_Chips[ i ], BASSAttribute.BASS_ATTRIB_VOL, CSound管理.nMixerVolume[ i ] / 100.0f );
-//Trace.TraceInformation("Vol{0}: {1}", i, CSound管理.nMixerVolume[i]);
+                               Bass.BASS_ChannelSetAttribute(this.hMixer_Chips[i], BASSAttribute.BASS_ATTRIB_VOL, CSound管理.nMixerVolume[i] / 100.0f);
+                               //Trace.TraceInformation("Vol{0}: {1}", i, CSound管理.nMixerVolume[i]);
 
+#if TEST_MultiThreadedMixer
+                               bool b1 = BASSThreadedMixerLibraryWrapper.BASS_ThreadedMixer_AddSource(this.hMixerThreaded, this.hMixer_Chips[i], IntPtr.Zero);
+#else
                                bool b1 = BassMix.BASS_Mixer_StreamAddChannel(this.hMixer, this.hMixer_Chips[i], BASSFlag.BASS_DEFAULT);
+#endif
                                if (!b1)
                                {
                                        BASSError errcode = Bass.BASS_ErrorGetCode();
@@ -535,7 +582,11 @@ Trace.TraceInformation("WASAPI Device #{0}: {1}: IsDefault={2}, defPeriod={3}s,
                                switch (strRecordFileType.ToUpper())
                                {
                                        case "WAV":
-                                               encoder = new EncoderWAV(this.hMixer_DeviceOut);
+                                               {
+                                                       var e = new EncoderWAV(this.hMixer_DeviceOut);
+                                                       //e.WAV_EncoderType = BASSChannelType.BASS_CTYPE_STREAM_WAV_PCM;
+                                                       encoder = e;
+                                               }
                                                break;
                                        case "OGG":
                                                {
@@ -576,20 +627,20 @@ Trace.TraceInformation("WASAPI Device #{0}: {1}: IsDefault={2}, defPeriod={3}s,
                        BassWasapi.BASS_WASAPI_Start();
                }
 
-               #region [録音開始]
+#region [録音開始]
                public bool tStartRecording()
                {
                        return encoder.Pause(false);
                }
-               #endregion
-               #region [録音終了]
+#endregion
+#region [録音終了]
                public bool tStopRecording()
                {
                        return encoder.Stop(true);
                }
-               #endregion
+#endregion
 
-               #region [ tサウンドを作成する() ]
+#region [ tサウンドを作成する() ]
                public CSound tサウンドを作成する(string strファイル名)
                {
                        return tサウンドを作成する( strファイル名, CSound.EInstType.Unknown );
@@ -597,7 +648,11 @@ Trace.TraceInformation("WASAPI Device #{0}: {1}: IsDefault={2}, defPeriod={3}s,
                public CSound tサウンドを作成する( string strファイル名, CSound.EInstType eInstType )
                {
                        var sound = new CSound();
-                       int hmixer = hMixer_Chips[ (int)eInstType ];
+#if TEST_MultiThreadedMixer
+                       int hmixer = (int)hMixerThreaded_Chips[ (int)eInstType ];
+#else
+                       int hmixer = hMixer_Chips[(int)eInstType];
+#endif
                        sound.tWASAPIサウンドを作成する( strファイル名, hmixer, this.e出力デバイス, eInstType );
                        return sound;
                }
@@ -608,23 +663,35 @@ Trace.TraceInformation("WASAPI Device #{0}: {1}: IsDefault={2}, defPeriod={3}s,
                public CSound tサウンドを作成する( byte[] byArrWAVファイルイメージ, CSound.EInstType eInstType )
                {
                        var sound = new CSound();
+#if TEST_MultiThreadedMixer
+                       int hmixer = (int)hMixerThreaded_Chips[(int)eInstType];
+#else
                        int hmixer = hMixer_Chips[(int)eInstType];
+#endif
                        sound.tWASAPIサウンドを作成する( byArrWAVファイルイメージ, hmixer, this.e出力デバイス, eInstType );
                        return sound;
                }
                public void tサウンドを作成する( string strファイル名, ref CSound sound, CSound.EInstType eInstType )
                {
+#if TEST_MultiThreadedMixer
+                       int hmixer = (int)hMixerThreaded_Chips[(int)eInstType];
+#else
                        int hmixer = hMixer_Chips[(int)eInstType];
+#endif
                        sound.tWASAPIサウンドを作成する( strファイル名, hmixer, this.e出力デバイス, eInstType );
                }
                public void tサウンドを作成する( byte[] byArrWAVファイルイメージ, ref CSound sound, CSound.EInstType eInstType)
                {
+#if TEST_MultiThreadedMixer
+                       int hmixer = (int)hMixerThreaded_Chips[(int)eInstType];
+#else
                        int hmixer = hMixer_Chips[(int)eInstType];
+#endif
                        sound.tWASAPIサウンドを作成する( byArrWAVファイルイメージ, hmixer, this.e出力デバイス, eInstType );
                }
-               #endregion
+#endregion
 
-               #region [ Dispose-Finallizeパターン実装 ]
+#region [ Dispose-Finallizeパターン実装 ]
                //-----------------
                public void Dispose()
                {
@@ -673,6 +740,9 @@ Trace.TraceInformation("WASAPI Device #{0}: {1}: IsDefault={2}, defPeriod={3}s,
                                        }
                                }
                        }
+#if TEST_MultiThreadedMixer
+                       //BASSThreadedMixerLibraryWrapper.FreeBASSThreadedMixerLibrary();               
+#endif
 
                        if ( !this.bIsBASSFree )
                        {
@@ -690,13 +760,17 @@ Trace.TraceInformation("WASAPI Device #{0}: {1}: IsDefault={2}, defPeriod={3}s,
                        this.Dispose( false );
                }
                //-----------------
-               #endregion
+#endregion
 
                protected int hMixer = 0;
                protected int hMixer_DeviceOut = 0;
                protected int hMixer_Record = 0;
                protected int[] hMixer_Chips = new int[(int)CSound.EInstType.Unknown + 1];  //DTX2WAV対応 BGM, SE, Drums...を別々のmixerに入れて、個別に音量変更できるようにする
 
+#if TEST_MultiThreadedMixer
+               protected IntPtr hMixerThreaded = IntPtr.Zero;
+               protected IntPtr[] hMixerThreaded_Chips = new IntPtr[(int)CSound.EInstType.Unknown + 1];
+#endif
                protected BaseEncoder encoder;
                protected int stream;
                protected WASAPIPROC tWasapiProc = null;
@@ -705,7 +779,8 @@ Trace.TraceInformation("WASAPI Device #{0}: {1}: IsDefault={2}, defPeriod={3}s,
                {
                        // BASSミキサからの出力データをそのまま WASAPI buffer へ丸投げ。
 
-                       int num = Bass.BASS_ChannelGetData( this.hMixer_DeviceOut, buffer, length );            // num = 実際に転送した長さ
+                       int num = Bass.BASS_ChannelGetData( this.hMixer_DeviceOut, buffer, length );        // num = 実際に転送した長さ
+                       //int num = BassMix.BASS_Mixer_ChannelGetData(this.hMixer_DeviceOut, buffer, length);      // これだと動作がめちゃくちゃ重くなる
                        if ( num == -1 ) num = 0;