2 using System.Collections.Generic;
\r
4 using System.Diagnostics;
\r
5 using System.Runtime.InteropServices;
\r
8 using SlimDX.DirectSound;
\r
9 using SlimDX.Multimedia;
\r
11 using Un4seen.BassAsio;
\r
12 using Un4seen.BassWasapi;
\r
13 using Un4seen.Bass.AddOn.Mix;
\r
14 using DirectShowLib;
\r
18 #region [ DTXMania用拡張 ]
\r
19 public class CSound管理 // : CSound
\r
21 //private static ISoundDevice _SoundDevice;
\r
22 //private static ESoundDeviceType _SoundDeviceType = ESoundDeviceType.Unknown;
\r
23 public static ISoundDevice SoundDevice
\r
28 // return _SoundDevice;
\r
32 // _SoundDevice = value;
\r
35 public static ESoundDeviceType SoundDeviceType
\r
40 // return _SoundDeviceType;
\r
44 // _SoundDeviceType = value;
\r
47 public static CSoundTimer rc演奏用タイマ = null;
\r
49 public static IntPtr WindowHandle;
\r
51 public static int nStreams;
\r
53 #region [ WASAPI/ASIO/DirectSound設定値 ]
\r
55 /// <para>WASAPI 排他モード出力における再生遅延[ms](の希望値)。最終的にはこの数値を基にドライバが決定する)。</para>
\r
57 public static int SoundDelayExclusiveWASAPI = 0; // SSTでは、50ms
\r
59 /// <para>WASAPI 共有モード出力における再生遅延[ms]。ユーザが決定する。</para>
\r
61 public static int SoundDelaySharedWASAPI = 100;
\r
63 /// <para>排他WASAPIバッファの更新間隔。出力間隔ではないので注意。</para>
\r
64 /// <para>SoundDelay よりも小さい値であること。(小さすぎる場合はBASSによって自動修正される。)</para>
\r
66 public static int SoundUpdatePeriodExclusiveWASAPI = 6;
\r
68 /// <para>共有WASAPIバッファの更新間隔。出力間隔ではないので注意。</para>
\r
69 /// <para>SoundDelay よりも小さい値であること。(小さすぎる場合はBASSによって自動修正される。)</para>
\r
71 public static int SoundUpdatePeriodSharedWASAPI = 6;
\r
73 ///// <para>ASIO 出力における再生遅延[ms](の希望値)。最終的にはこの数値を基にドライバが決定する)。</para>
\r
75 //public static int SoundDelayASIO = 0; // SSTでは50ms。0にすると、デバイスの設定値をそのまま使う。
\r
77 /// <para>ASIO 出力におけるバッファサイズ。</para>
\r
79 public static int SoundBufferSizeASIO = 0; // 0にすると、デバイスの設定値をそのまま使う。
\r
80 public int GetSoundBufferSizeASIO()
\r
82 return SoundBufferSizeASIO;
\r
84 public void SetSoundBufferSizeASIO(int value)
\r
86 SoundBufferSizeASIO = value;
\r
89 /// <para>DirectSound 出力における再生遅延[ms]。ユーザが決定する。</para>
\r
91 public static int SoundDelayDirectSound = 100;
\r
100 /// <param name="handle"></param>
\r
101 public CSound管理( IntPtr handle, ESoundDeviceType soundDeviceType )
\r
103 WindowHandle = handle;
\r
104 t初期化( soundDeviceType );
\r
106 public void Dispose()
\r
111 public static void t初期化()
\r
113 t初期化( ESoundDeviceType.ExclusiveWASAPI );
\r
116 public static void t初期化( ESoundDeviceType soundDeviceType )
\r
118 SoundDevice = null; // ユーザ依存
\r
119 rc演奏用タイマ = null; // Global.Bass 依存(つまりユーザ依存)
\r
122 ESoundDeviceType[] ESoundDeviceTypes = new ESoundDeviceType[ 4 ]
\r
124 ESoundDeviceType.ExclusiveWASAPI,
\r
125 ESoundDeviceType.ASIO,
\r
126 ESoundDeviceType.DirectSound,
\r
127 ESoundDeviceType.Unknown
\r
131 switch ( soundDeviceType )
\r
133 case ESoundDeviceType.ExclusiveWASAPI:
\r
136 case ESoundDeviceType.ASIO:
\r
139 case ESoundDeviceType.DirectSound:
\r
146 for ( SoundDeviceType = ESoundDeviceTypes[ n初期デバイス ]; ; SoundDeviceType = ESoundDeviceTypes[ ++n初期デバイス ] )
\r
150 t現在のユーザConfigに従ってサウンドデバイスとすべての既存サウンドを再構築する();
\r
153 catch ( Exception e )
\r
155 Trace.TraceInformation( e.Message );
\r
156 if ( ESoundDeviceTypes[ n初期デバイス ] == ESoundDeviceType.Unknown )
\r
158 throw new Exception( string.Format( "サウンドデバイスの初期化に失敗しました。" ) );
\r
163 public static void t終了()
\r
165 C共通.tDisposeする( SoundDevice ); SoundDevice = null;
\r
166 C共通.tDisposeする( ref rc演奏用タイマ ); // Global.Bass を解放した後に解放すること。(Global.Bass で参照されているため)
\r
170 public static void t現在のユーザConfigに従ってサウンドデバイスとすべての既存サウンドを再構築する()
\r
172 #region [ すでにサウンドデバイスと演奏タイマが構築されていれば解放する。]
\r
173 //-----------------
\r
174 if ( SoundDevice != null )
\r
176 // すでに生成済みのサウンドがあれば初期状態に戻す。
\r
178 CSound.tすべてのサウンドを初期状態に戻す();
\r
181 // サウンドデバイスと演奏タイマを解放する。
\r
183 C共通.tDisposeする( SoundDevice ); SoundDevice = null;
\r
184 C共通.tDisposeする( ref rc演奏用タイマ ); // Global.SoundDevice を解放した後に解放すること。(Global.SoundDevice で参照されているため)
\r
186 //-----------------
\r
189 #region [ 新しいサウンドデバイスを構築する。]
\r
190 //-----------------
\r
191 switch ( SoundDeviceType )
\r
193 case ESoundDeviceType.ExclusiveWASAPI:
\r
194 SoundDevice = new CSoundDeviceWASAPI( CSoundDeviceWASAPI.Eデバイスモード.排他, SoundDelayExclusiveWASAPI, SoundUpdatePeriodExclusiveWASAPI );
\r
197 case ESoundDeviceType.SharedWASAPI:
\r
198 SoundDevice = new CSoundDeviceWASAPI( CSoundDeviceWASAPI.Eデバイスモード.共有, SoundDelaySharedWASAPI, SoundUpdatePeriodSharedWASAPI );
\r
201 case ESoundDeviceType.ASIO:
\r
202 SoundDevice = new CSoundDeviceASIO( SoundBufferSizeASIO );
\r
205 case ESoundDeviceType.DirectSound:
\r
206 SoundDevice = new CSoundDeviceDirectSound( WindowHandle, SoundDelayDirectSound );
\r
210 throw new Exception( string.Format( "未対応の SoundDeviceType です。[{0}]", SoundDeviceType.ToString() ) );
\r
212 //-----------------
\r
214 #region [ 新しい演奏タイマを構築する。]
\r
215 //-----------------
\r
216 rc演奏用タイマ = new CSoundTimer( SoundDevice );
\r
217 //-----------------
\r
220 CSound.tすべてのサウンドを再構築する( SoundDevice ); // すでに生成済みのサウンドがあれば作り直す。
\r
222 public CSound tサウンドを生成する( string filename )
\r
224 //Debug.WriteLine( "★★tサウンドを生成する()" + SoundDevice.e出力デバイス + " " + Path.GetFileName( filename ) );
\r
225 if ( SoundDeviceType == ESoundDeviceType.Unknown )
\r
227 throw new Exception( string.Format( "未対応の SoundDeviceType です。[{0}]", SoundDeviceType.ToString() ) );
\r
229 return SoundDevice.tサウンドを作成する( filename );
\r
232 public void t再生中の処理をする()
\r
234 //★★★★★★★★★★★★★★★★★★★★★ダミー★★★★★★★★★★★★★★★★★★
\r
235 // Debug.Write( "再生中の処理をする()" );
\r
238 public void tサウンドを破棄する( CSound csound )
\r
244 public float GetCPUusage()
\r
246 //float f = Bass.BASS_GetCPU();
\r
248 switch ( SoundDeviceType )
\r
250 case ESoundDeviceType.ExclusiveWASAPI:
\r
251 case ESoundDeviceType.SharedWASAPI:
\r
252 f = BassWasapi.BASS_WASAPI_GetCPU();
\r
254 case ESoundDeviceType.ASIO:
\r
255 f = BassAsio.BASS_ASIO_GetCPU();
\r
257 case ESoundDeviceType.DirectSound:
\r
265 //Debug.WriteLine( "cpu=" + f );
\r
269 public string GetCurrentSoundDeviceType()
\r
271 switch ( SoundDeviceType )
\r
273 case ESoundDeviceType.ExclusiveWASAPI:
\r
274 case ESoundDeviceType.SharedWASAPI:
\r
276 case ESoundDeviceType.ASIO:
\r
278 case ESoundDeviceType.DirectSound:
\r
279 return "DirectSound";
\r
288 // CSound は、サウンドデバイスが変更されたときも、インスタンスを再作成することなく、新しいデバイスで作り直せる必要がある。
\r
289 // そのため、デバイスごとに別のクラスに分割するのではなく、1つのクラスに集約するものとする。
\r
291 public class CSound : IDisposable
\r
293 #region [ DTXMania用拡張 ]
\r
294 public int n総演奏時間ms
\r
299 public int nサウンドバッファサイズ // 取りあえず0固定★★★★★★★★★★★★★★★★★★★★
\r
303 public bool bストリーム再生する // 取りあえずfalse固定★★★★★★★★★★★★★★★★★★★★
\r
304 // trueにすると同一チップ音の多重再生で問題が出る(4POLY音源として動かない)
\r
306 get { return false; }
\r
308 public double db周波数倍率;
\r
309 public double db再生速度;
\r
313 private STREAMPROC _myStreamCreate; // make it global, so that the GC can not remove it
\r
316 /// <para>0:最小~100:原音</para>
\r
322 if( this.bBASSサウンドである )
\r
325 if( BassMix.BASS_Mixer_ChannelGetEnvelopePos( this.hBassStream, BASSMIXEnvelope.BASS_MIXER_ENV_VOL, ref f音量 ) == -1 )
\r
327 return (int) ( f音量 * 100 );
\r
329 else if( this.bDirectSoundである )
\r
337 if( this.bBASSサウンドである )
\r
339 float f音量 = Math.Min( Math.Max( value, 0 ), 100 ) / 100.0f; // 0~100 → 0.0~1.0
\r
340 var nodes = new BASS_MIXER_NODE[ 1 ] { new BASS_MIXER_NODE( 0, f音量 ) };
\r
341 BassMix.BASS_Mixer_ChannelSetEnvelope( this.hBassStream, BASSMIXEnvelope.BASS_MIXER_ENV_VOL, nodes );
\r
343 else if( this.bDirectSoundである )
\r
347 if( this._n音量 == 0 )
\r
349 this._n音量db = -10000;
\r
353 this._n音量db = (int) ( ( 20.0 * Math.Log10( ( (double) this._n音量 ) / 100.0 ) ) * 100.0 );
\r
356 this.Buffer.Volume = this._n音量db;
\r
362 /// <para>左:-100~中央:0~100:右。set のみ。</para>
\r
368 if( this.bBASSサウンドである )
\r
371 if( BassMix.BASS_Mixer_ChannelGetEnvelopePos( this.hBassStream, BASSMIXEnvelope.BASS_MIXER_ENV_PAN, ref f位置 ) == -1 )
\r
373 return (int) ( f位置 * 100 );
\r
375 else if( this.bDirectSoundである )
\r
383 if( this.bBASSサウンドである )
\r
385 float f位置 = Math.Min( Math.Max( value, -100 ), 100 ) / 100.0f; // -100~100 → -1.0~1.0
\r
386 var nodes = new BASS_MIXER_NODE[ 1 ] { new BASS_MIXER_NODE( 0, f位置 ) };
\r
387 BassMix.BASS_Mixer_ChannelSetEnvelope( this.hBassStream, BASSMIXEnvelope.BASS_MIXER_ENV_PAN, nodes );
\r
389 else if( this.bDirectSoundである )
\r
391 this._n位置 = Math.Min( Math.Max( -100, value ), 100 ); // -100~100
\r
393 if( this._n位置 == 0 )
\r
397 else if( this._n位置 == -100 )
\r
399 this._n位置db = -10000;
\r
401 else if( this._n位置 == 100 )
\r
403 this._n位置db = 100000;
\r
405 else if( this._n位置 < 0 )
\r
407 this._n位置db = (int) ( ( 20.0 * Math.Log10( ( (double) ( this._n位置 + 100 ) ) / 100.0 ) ) * 100.0 );
\r
411 this._n位置db = (int) ( ( -20.0 * Math.Log10( ( (double) ( 100 - this._n位置 ) ) / 100.0 ) ) * 100.0 );
\r
414 this.Buffer.Pan = this._n位置db;
\r
420 /// <para>DirectSoundのセカンダリバッファ。</para>
\r
422 public SecondarySoundBuffer DirectSoundBuffer
\r
424 get { return this.Buffer; }
\r
428 /// <para>DirectSoundのセカンダリバッファ作成時のフラグ。</para>
\r
430 public BufferFlags DirectSoundBufferFlags
\r
437 /// <para>全インスタンスリスト。</para>
\r
438 /// <para>~を作成する() で追加され、t解放する() or Dispose() で解放される。</para>
\r
440 public static List<CSound> listインスタンス = new List<CSound>();
\r
446 this.db周波数倍率 = 1.0;
\r
448 this.DirectSoundBufferFlags = CSoundDeviceDirectSound.DefaultFlags;
\r
451 public void tASIOサウンドを作成する( string strファイル名, int hMixer )
\r
453 this.tBASSサウンドを作成する( strファイル名, hMixer, BASSFlag.BASS_STREAM_DECODE );
\r
454 this.eデバイス種別 = ESoundDeviceType.ASIO; // 作成後に設定する。(作成に失敗してると例外発出されてここは実行されない)
\r
456 public void tASIOサウンドを作成する( byte[] byArrWAVファイルイメージ, int hMixer )
\r
458 this.tBASSサウンドを作成する( byArrWAVファイルイメージ, hMixer, BASSFlag.BASS_STREAM_DECODE );
\r
459 this.eデバイス種別 = ESoundDeviceType.ASIO; // 作成後に設定する。(作成に失敗してると例外発出されてここは実行されない)
\r
461 public void tWASAPIサウンドを作成する( string strファイル名, int hMixer, ESoundDeviceType eデバイス種別 )
\r
463 this.tBASSサウンドを作成する( strファイル名, hMixer, BASSFlag.BASS_STREAM_DECODE | BASSFlag.BASS_SAMPLE_FLOAT );
\r
464 this.eデバイス種別 = eデバイス種別; // 作成後に設定する。(作成に失敗してると例外発出されてここは実行されない)
\r
466 public void tWASAPIサウンドを作成する( byte[] byArrWAVファイルイメージ, int hMixer, ESoundDeviceType eデバイス種別 )
\r
468 this.tBASSサウンドを作成する( byArrWAVファイルイメージ, hMixer, BASSFlag.BASS_STREAM_DECODE | BASSFlag.BASS_SAMPLE_FLOAT );
\r
469 this.eデバイス種別 = eデバイス種別; // 作成後に設定する。(作成に失敗してると例外発出されてここは実行されない)
\r
471 public void tDirectSoundサウンドを作成する( string strファイル名, DirectSound DirectSound )
\r
473 this.e作成方法 = E作成方法.ファイルから;
\r
474 this.strファイル名 = strファイル名;
\r
476 if ( String.Compare( Path.GetExtension( strファイル名 ), ".xa", true ) == 0 ||
\r
477 String.Compare( Path.GetExtension( strファイル名 ), ".ogg", true ) == 0 ) // caselessで文字列比較
\r
479 tDirectSoundサウンドを作成するXAOGG( strファイル名, DirectSound );
\r
483 // すべてのファイルを DirectShow でデコードすると時間がかかるので、ファイルが WAV かつ PCM フォーマットでない場合のみ DirectShow でデコードする。
\r
485 byte[] byArrWAVファイルイメージ = null;
\r
486 bool bファイルがWAVかつPCMフォーマットである = true;
\r
489 #region [ ファイルがWAVかつPCMフォーマットか否か調べる。]
\r
490 //-----------------
\r
493 using ( var ws = new WaveStream( strファイル名 ) )
\r
495 if ( ws.Format.FormatTag != WaveFormatTag.Pcm )
\r
496 bファイルがWAVかつPCMフォーマットである = false;
\r
501 bファイルがWAVかつPCMフォーマットである = false;
\r
503 //-----------------
\r
506 if ( bファイルがWAVかつPCMフォーマットである )
\r
508 #region [ ファイルを読み込んで byArrWAVファイルイメージへ格納。]
\r
509 //-----------------
\r
510 var fs = File.Open( strファイル名, FileMode.Open, FileAccess.Read );
\r
511 var br = new BinaryReader( fs );
\r
513 byArrWAVファイルイメージ = new byte[ fs.Length ];
\r
514 br.Read( byArrWAVファイルイメージ, 0, (int) fs.Length );
\r
518 //-----------------
\r
523 #region [ DirectShow でデコード変換し、 byArrWAVファイルイメージへ格納。]
\r
524 //-----------------
\r
525 CDStoWAVFileImage.t変換( strファイル名, out byArrWAVファイルイメージ );
\r
526 //-----------------
\r
533 this.tDirectSoundサウンドを作成する( byArrWAVファイルイメージ, DirectSound );
\r
535 public void tDirectSoundサウンドを作成するXAOGG( string strファイル名, DirectSound DirectSound )
\r
537 this.e作成方法 = E作成方法.ファイルから;
\r
538 this.strファイル名 = strファイル名;
\r
543 // Cxa xa = new Cxa();
\r
544 // xa.Decode( strファイル名, out this.byArrWAVファイルイメージ );
\r
546 WaveFormat wfx = new WaveFormat();
\r
547 int nPCMデータの先頭インデックス = 0;
\r
548 // int nPCMサイズbyte = (int) ( xa.xaheader.nSamples * xa.xaheader.nChannels * 2 ); // nBytes = Bass.BASS_ChannelGetLength( this.hBassStream );
\r
551 CWin32.WAVEFORMATEX cw32wfx;
\r
552 tオンメモリ方式でデコードする( strファイル名, out this.byArrWAVファイルイメージ,
\r
553 out nPCMデータの先頭インデックス, out nPCMサイズbyte, out cw32wfx );
\r
555 wfx.AverageBytesPerSecond = (int) cw32wfx.nAvgBytesPerSec;
\r
556 wfx.BitsPerSample = (short) cw32wfx.wBitsPerSample;
\r
557 wfx.BlockAlignment = (short) cw32wfx.nBlockAlign;
\r
558 wfx.Channels = (short) cw32wfx.nChannels;
\r
559 wfx.FormatTag = WaveFormatTag.Pcm; // xa.waveformatex.wFormatTag;
\r
560 wfx.SamplesPerSecond = (int) cw32wfx.nSamplesPerSec;
\r
562 // セカンダリバッファを作成し、PCMデータを書き込む。
\r
564 this.Buffer = new SecondarySoundBuffer( DirectSound, new SoundBufferDescription()
\r
567 Flags = CSoundDeviceDirectSound.DefaultFlags,
\r
568 SizeInBytes = nPCMサイズbyte,
\r
570 this.Buffer.Write( byArrWAVファイルイメージ, nPCMデータの先頭インデックス, nPCMサイズbyte, 0, LockFlags.None );
\r
573 n総演奏時間ms = (int) ( ( (double) nPCMサイズbyte ) / ( this.Buffer.Format.AverageBytesPerSecond * 0.001 ) );
\r
574 nBytes = nPCMサイズbyte;
\r
578 this.eデバイス種別 = ESoundDeviceType.DirectSound;
\r
579 this.DirectSoundBufferFlags = CSoundDeviceDirectSound.DefaultFlags;
\r
584 CSound.listインスタンス.Add( this );
\r
588 public void tDirectSoundサウンドを作成する( byte[] byArrWAVファイルイメージ, DirectSound DirectSound )
\r
590 this.tDirectSoundサウンドを作成する( byArrWAVファイルイメージ, DirectSound, CSoundDeviceDirectSound.DefaultFlags );
\r
592 public void tDirectSoundサウンドを作成する( byte[] byArrWAVファイルイメージ, DirectSound DirectSound, BufferFlags flags )
\r
594 if( this.e作成方法 == E作成方法.Unknown )
\r
595 this.e作成方法 = E作成方法.WAVファイルイメージから;
\r
597 WaveFormat wfx = null;
\r
598 int nPCMデータの先頭インデックス = -1;
\r
599 int nPCMサイズbyte = -1;
\r
601 #region [ byArrWAVファイルイメージ[] から上記3つのデータを取得。]
\r
602 //-----------------
\r
603 var ms = new MemoryStream( byArrWAVファイルイメージ );
\r
604 var br = new BinaryReader( ms );
\r
608 // 'RIFF'+RIFFデータサイズ
\r
610 if( br.ReadUInt32() != 0x46464952 )
\r
611 throw new InvalidDataException( "RIFFファイルではありません。" );
\r
615 if( br.ReadUInt32() != 0x45564157 )
\r
616 throw new InvalidDataException( "WAVEファイルではありません。" );
\r
619 while( ( ms.Position + 8 ) < ms.Length ) // +8 は、チャンク名+チャンクサイズ。残り8バイト未満ならループ終了。
\r
621 uint chunkName = br.ReadUInt32();
\r
624 if( chunkName == 0x20746D66 )
\r
626 long chunkSize = (long) br.ReadUInt32();
\r
628 var tag = (WaveFormatTag) br.ReadUInt16();
\r
630 if( tag == WaveFormatTag.Pcm ) wfx = new WaveFormat();
\r
631 else if( tag == WaveFormatTag.Extensible ) wfx = new SlimDX.Multimedia.WaveFormatExtensible(); // このクラスは WaveFormat を継承している。
\r
633 throw new InvalidDataException( string.Format( "未対応のWAVEフォーマットタグです。(Tag:{0})", tag.ToString() ) );
\r
635 wfx.FormatTag = tag;
\r
636 wfx.Channels = br.ReadInt16();
\r
637 wfx.SamplesPerSecond = br.ReadInt32();
\r
638 wfx.AverageBytesPerSecond = br.ReadInt32();
\r
639 wfx.BlockAlignment = br.ReadInt16();
\r
640 wfx.BitsPerSample = br.ReadInt16();
\r
642 long nフォーマットサイズbyte = 16;
\r
644 if( wfx.FormatTag == WaveFormatTag.Extensible )
\r
646 br.ReadUInt16(); // 拡張領域サイズbyte
\r
647 var wfxEx = (SlimDX.Multimedia.WaveFormatExtensible) wfx;
\r
648 wfxEx.ValidBitsPerSample = br.ReadInt16();
\r
649 wfxEx.ChannelMask = (Speakers) br.ReadInt32();
\r
650 wfxEx.SubFormat = new Guid( br.ReadBytes( 16 ) ); // GUID は 16byte (128bit)
\r
652 nフォーマットサイズbyte += 24;
\r
655 ms.Seek( chunkSize - nフォーマットサイズbyte, SeekOrigin.Current );
\r
660 else if( chunkName == 0x61746164 )
\r
662 nPCMサイズbyte = br.ReadInt32();
\r
663 nPCMデータの先頭インデックス = (int) ms.Position;
\r
665 ms.Seek( nPCMサイズbyte, SeekOrigin.Current );
\r
672 long chunkSize = (long) br.ReadUInt32();
\r
673 ms.Seek( chunkSize, SeekOrigin.Current );
\r
679 throw new InvalidDataException( "fmt チャンクが存在しません。不正なサウンドデータです。" );
\r
680 if( nPCMサイズbyte < 0 )
\r
681 throw new InvalidDataException( "data チャンクが存在しません。不正なサウンドデータです。" );
\r
688 //-----------------
\r
692 // セカンダリバッファを作成し、PCMデータを書き込む。
\r
694 this.Buffer = new SecondarySoundBuffer( DirectSound, new SoundBufferDescription() {
\r
695 Format = ( wfx.FormatTag == WaveFormatTag.Pcm) ? wfx : (SlimDX.Multimedia.WaveFormatExtensible) wfx,
\r
697 SizeInBytes = nPCMサイズbyte,
\r
699 this.Buffer.Write( byArrWAVファイルイメージ, nPCMデータの先頭インデックス, nPCMサイズbyte, 0, LockFlags.None );
\r
702 n総演奏時間ms = (int) ( ( (double) nPCMサイズbyte ) / ( this.Buffer.Format.AverageBytesPerSecond * 0.001 ) );
\r
706 this.eデバイス種別 = ESoundDeviceType.DirectSound;
\r
707 this.DirectSoundBufferFlags = flags;
\r
708 this.byArrWAVファイルイメージ = byArrWAVファイルイメージ;
\r
713 CSound.listインスタンス.Add( this );
\r
716 #region [ DTXMania用の変換 ]
\r
717 public void tサウンドを破棄する( CSound cs )
\r
721 public void t再生を開始する()
\r
726 public void t再生を開始する( bool bループする )
\r
728 if ( bBASSサウンドである )
\r
732 Bass.BASS_ChannelFlags( this.hBassStream, BASSFlag.BASS_SAMPLE_LOOP, BASSFlag.BASS_SAMPLE_LOOP );
\r
736 Bass.BASS_ChannelFlags( this.hBassStream, BASSFlag.BASS_DEFAULT, BASSFlag.BASS_DEFAULT );
\r
742 public void t再生を停止する()
\r
747 public void t再生を一時停止する()
\r
752 public void t再生を再開する( long t ) // ★★★★★★★★★★★★★★★★★★★★★★★★★★★★
\r
754 Debug.WriteLine( "t再生を再開する(long " + t + ")" );
\r
763 if ( this.bBASSサウンドである )
\r
765 bool ret = ( BassMix.BASS_Mixer_ChannelIsActive( this.hBassStream ) == BASSActive.BASS_ACTIVE_PAUSED ) &
\r
766 ( BassMix.BASS_Mixer_ChannelGetPosition( this.hBassStream ) > 0 );
\r
771 return ( this.n一時停止回数 > 0 );
\r
779 if ( this.eデバイス種別 == ESoundDeviceType.DirectSound )
\r
781 return ( ( this.Buffer.Status & BufferStatus.Playing ) != BufferStatus.None );
\r
785 // 基本的にはBASS_ACTIVE_PLAYINGなら再生中だが、最後まで再生しきったchannelも
\r
786 // BASS_ACTIVE_PLAYINGのままになっているので、小細工が必要。
\r
787 bool ret = ( BassMix.BASS_Mixer_ChannelIsActive( this.hBassStream ) == BASSActive.BASS_ACTIVE_PLAYING );
\r
788 if ( BassMix.BASS_Mixer_ChannelGetPosition( this.hBassStream ) >= nBytes )
\r
796 //public lint t時刻から位置を返す( long t )
\r
798 // double num = ( n時刻 * this.db再生速度 ) * this.db周波数倍率;
\r
799 // return (int) ( ( num * 0.01 ) * this.nSamplesPerSecond );
\r
804 public void t解放する()
\r
806 bool bManagedも解放する = true;
\r
807 bool bインスタンス削除 = false; // インスタンスは存続する。
\r
808 this.Dispose( bManagedも解放する, bインスタンス削除 );
\r
810 public void tサウンドを再生する()
\r
812 //Debug.WriteLine( "tサウンドを再生する(): " + this.strファイル名 );
\r
813 if( this.bBASSサウンドである )
\r
815 //Debug.WriteLine( "再生中?: " + System.IO.Path.GetFileName(this.strファイル名) + " status=" + BassMix.BASS_Mixer_ChannelIsActive( this.hBassStream ) + " current=" + BassMix.BASS_Mixer_ChannelGetPosition( this.hBassStream ) + " nBytes=" + nBytes );
\r
816 bool b = BassMix.BASS_Mixer_ChannelPlay( this.hBassStream );
\r
819 Debug.WriteLine( "再生しようとしたが、Mixerに登録されていなかった: " + Path.GetFileName( this.strファイル名 ) );
\r
820 //Debug.WriteLine( "ErrCode= " +Bass.BASS_ErrorGetCode() );
\r
822 bool bb = tBASSサウンドをミキサーに追加する(false);
\r
825 Debug.WriteLine( "Mixerへの登録に失敗: " + Path.GetFileName( this.strファイル名 ) + ": " + Bass.BASS_ErrorGetCode() );
\r
829 Debug.WriteLine( "Mixerへの登録に成功: " + Path.GetFileName( this.strファイル名 ) + ": " + Bass.BASS_ErrorGetCode() );
\r
831 // bool bbb = BassMix.BASS_Mixer_ChannelPlay( this.hBassStream );
\r
834 //Debug.WriteLine( "更に再生に失敗 : " + Path.GetFileName( this.strファイル名 ) );
\r
835 //Debug.WriteLine( "ErrCode= " +Bass.BASS_ErrorGetCode() );
\r
839 //Debug.WriteLine( "再生成功(ミキサー追加後) : " + Path.GetFileName( this.strファイル名 ) );
\r
844 Debug.WriteLine( "再生成功 : " + Path.GetFileName( this.strファイル名 ) );
\r
847 else if( this.bDirectSoundである )
\r
849 this.Buffer.Play( 0, PlayFlags.None );
\r
852 public void tサウンドを先頭から再生する()
\r
854 this.t再生位置を先頭に戻す();
\r
857 public void tサウンドを停止する()
\r
859 if( this.bBASSサウンドである )
\r
861 Debug.WriteLine( "停止: " + System.IO.Path.GetFileName( this.strファイル名 ) + " status=" + BassMix.BASS_Mixer_ChannelIsActive( this.hBassStream ) + " current=" + BassMix.BASS_Mixer_ChannelGetPosition( this.hBassStream ) + " nBytes=" + nBytes );
\r
862 BassMix.BASS_Mixer_ChannelPause( this.hBassStream );
\r
864 //tBASSサウンドをミキサーから削除する();
\r
866 else if( this.bDirectSoundである )
\r
868 this.Buffer.Stop();
\r
872 public void t再生位置を先頭に戻す()
\r
874 if( this.bBASSサウンドである )
\r
876 BassMix.BASS_Mixer_ChannelSetPosition( this.hBassStream, 0 );
\r
879 else if( this.bDirectSoundである )
\r
881 this.Buffer.CurrentPlayPosition = 0;
\r
884 public void t再生位置を変更する( long n位置ms )
\r
886 if( this.bBASSサウンドである )
\r
888 BassMix.BASS_Mixer_ChannelSetPosition( this.hBassStream, Bass.BASS_ChannelSeconds2Bytes( this.hBassStream, n位置ms / 1000.0 ), BASSMode.BASS_POS_BYTES );
\r
890 else if( this.bDirectSoundである )
\r
892 int n位置sample = (int) ( this.Buffer.Format.SamplesPerSecond * n位置ms * 0.001 );
\r
893 this.Buffer.CurrentPlayPosition = n位置sample * this.Buffer.Format.BlockAlignment;
\r
897 public static void tすべてのサウンドを初期状態に戻す()
\r
899 foreach( var sound in CSound.listインスタンス )
\r
902 public static void tすべてのサウンドを再構築する( ISoundDevice device )
\r
904 if( CSound.listインスタンス.Count == 0 )
\r
908 // サウンドを再生する際にインスタンスリストも更新されるので、配列にコピーを取っておき、リストはクリアする。
\r
910 var sounds = CSound.listインスタンス.ToArray();
\r
911 CSound.listインスタンス.Clear();
\r
914 // 配列に基づいて個々のサウンドを作成する。
\r
916 for( int i = 0; i < sounds.Length; i++ )
\r
918 switch( sounds[ i ].e作成方法 )
\r
921 string strファイル名 = sounds[ i ].strファイル名;
\r
922 sounds[ i ].Dispose( true, false );
\r
923 device.tサウンドを作成する( strファイル名, ref sounds[ i ] );
\r
926 case E作成方法.WAVファイルイメージから:
\r
927 if( sounds[ i ].bBASSサウンドである )
\r
929 byte[] byArrWaveファイルイメージ = sounds[ i ].byArrWAVファイルイメージ;
\r
930 sounds[ i ].Dispose( true, false );
\r
931 device.tサウンドを作成する( byArrWaveファイルイメージ, ref sounds[ i ] );
\r
933 else if( sounds[ i ].bDirectSoundである )
\r
935 byte[] byArrWaveファイルイメージ = sounds[ i ].byArrWAVファイルイメージ;
\r
936 var flags = sounds[ i ].DirectSoundBufferFlags;
\r
937 sounds[ i ].Dispose( true, false );
\r
938 ( (CSoundDeviceDirectSound) device ).tサウンドを作成する( byArrWaveファイルイメージ, flags, ref sounds[ i ] );
\r
945 #region [ Dispose-Finalizeパターン実装 ]
\r
946 //-----------------
\r
947 public void Dispose()
\r
949 this.Dispose( true, true );
\r
950 GC.SuppressFinalize( this );
\r
952 protected void Dispose( bool bManagedも解放する, bool bインスタンス削除 )
\r
954 if( this.bBASSサウンドである )
\r
956 #region [ ASIO, WASAPI の解放 ]
\r
957 //-----------------
\r
958 BassMix.BASS_Mixer_ChannelRemove( this.hBassStream );
\r
959 Bass.BASS_StreamFree( this.hBassStream );
\r
960 this.hBassStream = -1;
\r
961 //-----------------
\r
965 if( bManagedも解放する )
\r
967 if( this.eデバイス種別 == ESoundDeviceType.DirectSound )
\r
969 #region [ DirectSound の解放 ]
\r
970 //-----------------
\r
971 if( this.Buffer != null )
\r
975 this.Buffer.Stop();
\r
979 // 演奏終了後、長時間解放しないでいると、たまに AccessViolationException が発生することがある。
\r
981 C共通.tDisposeする( ref this.Buffer );
\r
983 //-----------------
\r
987 if( this.e作成方法 == E作成方法.WAVファイルイメージから &&
\r
988 this.eデバイス種別 != ESoundDeviceType.DirectSound ) // DirectSound は hGC 未使用。
\r
991 this.hGC = default( GCHandle );
\r
995 CSound.listインスタンス.Remove( this );
\r
1000 this.Dispose( false, true );
\r
1002 //-----------------
\r
1005 #region [ protected ]
\r
1006 //-----------------
\r
1007 protected enum E作成方法 { ファイルから, WAVファイルイメージから, Unknown }
\r
1008 protected E作成方法 e作成方法 = E作成方法.Unknown;
\r
1009 protected ESoundDeviceType eデバイス種別 = ESoundDeviceType.Unknown;
\r
1010 protected string strファイル名 = null;
\r
1011 protected byte[] byArrWAVファイルイメージ = null; // WAVファイルイメージ、もしくはchunkのDATA部のみ
\r
1012 protected GCHandle hGC;
\r
1013 protected int hBassStream = -1; // ASIO, WASAPI 用
\r
1014 protected SecondarySoundBuffer Buffer = null; // DirectSound 用
\r
1015 protected int hMixer = -1; // 設計壊してゴメン Mixerに後で登録するときに使う
\r
1016 //-----------------
\r
1019 #region [ private ]
\r
1020 //-----------------
\r
1021 private bool bDirectSoundである
\r
1023 get { return ( this.eデバイス種別 == ESoundDeviceType.DirectSound ); }
\r
1025 private bool bBASSサウンドである
\r
1030 this.eデバイス種別 == ESoundDeviceType.ASIO ||
\r
1031 this.eデバイス種別 == ESoundDeviceType.ExclusiveWASAPI ||
\r
1032 this.eデバイス種別 == ESoundDeviceType.SharedWASAPI );
\r
1035 private int _n位置 = 0;
\r
1036 private int _n位置db;
\r
1037 private int _n音量 = 100;
\r
1038 private int _n音量db;
\r
1039 private long nBytes = 0;
\r
1040 private int n一時停止回数 = 0;
\r
1042 private void tBASSサウンドを作成する( string strファイル名, int hMixer, BASSFlag flags )
\r
1044 if ( String.Compare( Path.GetExtension( strファイル名 ), ".xa", true ) == 0 ) // caselessで文字列比較
\r
1046 tBASSサウンドを作成するXA( strファイル名, hMixer, flags );
\r
1050 this.e作成方法 = E作成方法.ファイルから;
\r
1051 this.strファイル名 = strファイル名;
\r
1054 // BASSファイルストリームを作成。
\r
1056 this.hBassStream = Bass.BASS_StreamCreateFile( strファイル名, 0, 0, flags );
\r
1057 if( this.hBassStream == 0 )
\r
1058 throw new Exception( string.Format( "サウンドストリームの生成に失敗しました。(BASS_StreamCreateFile)[{0}]", Bass.BASS_ErrorGetCode().ToString() ) );
\r
1061 // ミキサーにBASSファイルストリームを追加。
\r
1063 if ( !BassMix.BASS_Mixer_StreamAddChannel( hMixer, this.hBassStream, BASSFlag.BASS_SPEAKER_FRONT | BASSFlag.BASS_MIXER_PAUSE | BASSFlag.BASS_MIXER_NORAMPIN ) )
\r
1064 // if ( !tBASSサウンドをミキサーに追加する() )
\r
1067 throw new Exception( string.Format( "サウンドストリームの生成に失敗しました。(BASS_Mixer_StreamAddChannel)[{0}]", Bass.BASS_ErrorGetCode().ToString() ) );
\r
1072 CSound.listインスタンス.Add( this );
\r
1074 // nBytesとn総演奏時間の取得; DTXMania用に追加。
\r
1075 nBytes = Bass.BASS_ChannelGetLength( this.hBassStream );
\r
1076 double seconds = Bass.BASS_ChannelBytes2Seconds( this.hBassStream, nBytes );
\r
1077 this.n総演奏時間ms = (int) ( seconds * 1000 );
\r
1079 this.hMixer = hMixer;
\r
1081 private void tBASSサウンドを作成する( byte[] byArrWAVファイルイメージ, int hMixer, BASSFlag flags )
\r
1083 this.e作成方法 = E作成方法.WAVファイルイメージから;
\r
1084 this.byArrWAVファイルイメージ = byArrWAVファイルイメージ;
\r
1085 this.hGC = GCHandle.Alloc( byArrWAVファイルイメージ, GCHandleType.Pinned ); // byte[] をピン留め
\r
1088 // BASSファイルストリームを作成。
\r
1090 this.hBassStream = Bass.BASS_StreamCreateFile( hGC.AddrOfPinnedObject(), 0, byArrWAVファイルイメージ.Length, flags );
\r
1091 if( this.hBassStream == 0 )
\r
1092 throw new Exception( string.Format( "サウンドストリームの生成に失敗しました。(BASS_StreamCreateFile)[{0}]", Bass.BASS_ErrorGetCode().ToString() ) );
\r
1095 // ミキサーにBASSファイルストリームを追加。
\r
1097 if ( !BassMix.BASS_Mixer_StreamAddChannel( hMixer, this.hBassStream, BASSFlag.BASS_SPEAKER_FRONT | BASSFlag.BASS_MIXER_PAUSE | BASSFlag.BASS_MIXER_NORAMPIN ) )
\r
1098 // if ( !tBASSサウンドをミキサーに追加する() )
\r
1101 throw new Exception( string.Format( "サウンドストリームの生成に失敗しました。(BASS_Mixer_StreamAddChannel)[{0}]", Bass.BASS_ErrorGetCode().ToString() ) );
\r
1107 CSound.listインスタンス.Add( this );
\r
1109 // nBytesとn総演奏時間の取得; DTXMania用に追加。
\r
1110 nBytes = Bass.BASS_ChannelGetLength( this.hBassStream );
\r
1111 double seconds = Bass.BASS_ChannelBytes2Seconds( this.hBassStream, nBytes );
\r
1112 this.n総演奏時間ms = (int) ( seconds * 1000 );
\r
1114 this.hMixer = hMixer;
\r
1116 private void tBASSサウンドを作成するXA( string strファイル名, int hMixer, BASSFlag flags )
\r
1118 int nPCMデータの先頭インデックス;
\r
1119 CWin32.WAVEFORMATEX wfx;
\r
1122 tオンメモリ方式でデコードする( strファイル名, out this.byArrWAVファイルイメージ,
\r
1123 out nPCMデータの先頭インデックス, out totalPCMSize, out wfx );
\r
1125 nBytes = totalPCMSize;
\r
1127 this.e作成方法 = E作成方法.WAVファイルイメージから;
\r
1128 this.strファイル名 = strファイル名;
\r
1129 this.hGC = GCHandle.Alloc( this.byArrWAVファイルイメージ, GCHandleType.Pinned ); // byte[] をピン留め
\r
1132 _myStreamCreate = new STREAMPROC( CallbackPlayingXA );
\r
1134 // BASSファイルストリームを作成。
\r
1136 //this.hBassStream = Bass.BASS_StreamCreate( xa.xaheader.nSamplesPerSec, xa.xaheader.nChannels, BASSFlag.BASS_STREAM_DECODE, _myStreamCreate, IntPtr.Zero );
\r
1137 this.hBassStream = Bass.BASS_StreamCreate( (int)wfx.nSamplesPerSec, (int)wfx.nChannels, BASSFlag.BASS_STREAM_DECODE, _myStreamCreate, IntPtr.Zero );
\r
1138 if ( this.hBassStream == 0 )
\r
1141 throw new Exception( string.Format( "サウンドストリームの生成に失敗しました。(BASS_SampleCreate)[{0}]", Bass.BASS_ErrorGetCode().ToString() ) );
\r
1144 // ミキサーにBASSファイルストリームを追加。
\r
1146 if ( !BassMix.BASS_Mixer_StreamAddChannel( hMixer, this.hBassStream, BASSFlag.BASS_SPEAKER_FRONT | BASSFlag.BASS_MIXER_PAUSE | BASSFlag.BASS_MIXER_NORAMPIN ) )
\r
1147 // if ( !tBASSサウンドをミキサーに追加する() )
\r
1150 throw new Exception( string.Format( "サウンドストリームの生成に失敗しました。(BASS_Mixer_StreamAddChannel)[{0}]", Bass.BASS_ErrorGetCode().ToString() ) );
\r
1154 CSound.listインスタンス.Add( this );
\r
1156 // nBytesとn総演奏時間の取得; DTXMania用に追加。
\r
1157 //nBytes = (int) ( xa.xaheader.nSamples * xa.xaheader.nChannels * 2 ); // nBytes = Bass.BASS_ChannelGetLength( this.hBassStream );
\r
1158 //nBytes = Bass.BASS_ChannelGetLength( this.hBassStream );
\r
1159 //if ( nBytes < 0 )
\r
1162 // BASSError err = Bass.BASS_ErrorGetCode();
\r
1163 // throw new Exception( "サウンドストリームの生成に失敗しました。(BASS_ChannelGetLength: " + err + ")" );
\r
1165 //nBytes = (int) this.byArrWAVファイルイメージ.Length;
\r
1167 double seconds = Bass.BASS_ChannelBytes2Seconds( this.hBassStream, nBytes );
\r
1168 this.n総演奏時間ms = (int) ( seconds * 1000 );
\r
1170 this.hMixer = hMixer;
\r
1172 //-----------------
\r
1174 private int pos = 0;
\r
1175 private int CallbackPlayingXA( int handle, IntPtr buffer, int length, IntPtr user )
\r
1177 int bytesread = ( pos + length > Convert.ToInt32(nBytes) ) ? Convert.ToInt32(nBytes) - pos : length;
\r
1179 Marshal.Copy( byArrWAVファイルイメージ, pos, buffer, bytesread );
\r
1182 if ( pos >= nBytes )
\r
1184 // set indicator flag
\r
1185 bytesread |= (int) BASSStreamProc.BASS_STREAMPROC_END;
\r
1191 public bool tBASSサウンドをミキサーに追加する()
\r
1193 return tBASSサウンドをミキサーに追加する( true );
\r
1199 /// <param name="pause">falseなら、追加と同時に再生する</param>
\r
1200 /// <returns></returns>
\r
1201 public bool tBASSサウンドをミキサーに追加する( bool pause )
\r
1203 BASSFlag bf = BASSFlag.BASS_SPEAKER_FRONT | BASSFlag.BASS_MIXER_NORAMPIN;
\r
1206 bf |= BASSFlag.BASS_MIXER_PAUSE;
\r
1208 return BassMix.BASS_Mixer_StreamAddChannel( this.hMixer, this.hBassStream, bf );
\r
1210 public bool tBASSサウンドをミキサーから削除する()
\r
1212 return BassMix.BASS_Mixer_ChannelRemove( this.hBassStream );
\r
1216 public void tオンメモリ方式でデコードする( string strファイル名, out byte[] buffer,
\r
1217 out int nPCMデータの先頭インデックス, out int totalPCMSize, out CWin32.WAVEFORMATEX wfx )
\r
1219 nPCMデータの先頭インデックス = 0;
\r
1220 //int nPCMサイズbyte = (int) ( xa.xaheader.nSamples * xa.xaheader.nChannels * 2 ); // nBytes = Bass.BASS_ChannelGetLength( this.hBassStream );
\r
1222 SoundDecoder sounddecoder;
\r
1224 if ( String.Compare( Path.GetExtension( strファイル名 ), ".xa", true ) == 0 )
\r
1226 sounddecoder = new Cxa();
\r
1228 else if ( String.Compare( Path.GetExtension( strファイル名 ), ".ogg", true ) == 0 )
\r
1230 sounddecoder = new Cogg();
\r
1234 throw new NotImplementedException();
\r
1237 int nHandle = sounddecoder.Open( strファイル名 );
\r
1238 if ( nHandle < 0 )
\r
1240 throw new Exception( string.Format( "Open() に失敗しました。({0})({1})", nHandle, strファイル名 ) );
\r
1242 wfx = new CWin32.WAVEFORMATEX();
\r
1243 if ( sounddecoder.GetFormat( nHandle, ref wfx ) < 0 )
\r
1245 sounddecoder.Close( nHandle );
\r
1246 throw new Exception( string.Format( "GetFormat() に失敗しました。({0})", strファイル名 ) );
\r
1248 //totalPCMSize = (int) sounddecoder.nTotalPCMSize; // tデコード後のサイズを調べる()で既に取得済みの値を流用する。ms単位の高速化だが、チップ音がたくさんあると塵積で結構効果がある
\r
1249 totalPCMSize = (int) sounddecoder.GetTotalPCMSize( nHandle );
\r
1250 if ( totalPCMSize == 0 )
\r
1252 sounddecoder.Close( nHandle );
\r
1253 throw new Exception( string.Format( "GetTotalPCMSize() に失敗しました。({0})", strファイル名 ) );
\r
1255 totalPCMSize += ( ( totalPCMSize % 2 ) != 0 ) ? 1 : 0;
\r
1256 buffer = new byte[ totalPCMSize ];
\r
1257 GCHandle handle = GCHandle.Alloc( buffer, GCHandleType.Pinned );
\r
1260 if ( sounddecoder.Decode( nHandle, handle.AddrOfPinnedObject(), (uint) totalPCMSize, 0 ) < 0 )
\r
1263 throw new Exception( string.Format( "デコードに失敗しました。({0})", strファイル名 ) );
\r
1269 sounddecoder.Close( nHandle );
\r
1271 sounddecoder = null;
\r