2 using System.Collections.Generic;
\r
4 using System.Diagnostics;
\r
5 using System.Runtime.InteropServices;
\r
7 using System.Runtime.CompilerServices;
\r
8 using System.Threading;
\r
10 using SlimDX.DirectSound;
\r
11 using SlimDX.Multimedia;
\r
13 using Un4seen.BassAsio;
\r
14 using Un4seen.BassWasapi;
\r
15 using Un4seen.Bass.AddOn.Mix;
\r
16 using DirectShowLib;
\r
20 #region [ DTXMania用拡張 ]
\r
21 public class CSound管理 // : CSound
\r
23 public static ISoundDevice SoundDevice
\r
27 public static ESoundDeviceType SoundDeviceType
\r
31 public static CSoundTimer rc演奏用タイマ = null;
\r
33 public static IntPtr WindowHandle;
\r
35 public static int nMixing = 0;
\r
36 public int GetMixingStreams()
\r
40 public static int nStreams = 0;
\r
41 public int GetStreams()
\r
45 #region [ WASAPI/ASIO/DirectSound設定値 ]
\r
47 /// <para>WASAPI 排他モード出力における再生遅延[ms](の希望値)。最終的にはこの数値を基にドライバが決定する)。</para>
\r
49 public static int SoundDelayExclusiveWASAPI = 0; // SSTでは、50ms
\r
50 public int GetSoundExclusiveWASAPI()
\r
52 return SoundDelayExclusiveWASAPI;
\r
54 public void SetSoundDelayExclusiveWASAPI( int value )
\r
56 SoundDelayExclusiveWASAPI = value;
\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 SoundDelayASIO = 0; // 0にすると、デバイスの設定値をそのまま使う。
\r
80 public int GetSoundDelayASIO()
\r
82 return SoundDelayASIO;
\r
84 public void SetSoundDelayASIO(int value)
\r
86 SoundDelayASIO = value;
\r
88 public static int ASIODevice = 0;
\r
89 public int GetASIODevice()
\r
93 public void SetASIODevice(int value)
\r
98 /// <para>DirectSound 出力における再生遅延[ms]。ユーザが決定する。</para>
\r
100 public static int SoundDelayDirectSound = 100;
\r
102 public long GetSoundDelay()
\r
104 if ( SoundDevice != null )
\r
106 return SoundDevice.n実バッファサイズms;
\r
120 /// <param name="handle"></param>
\r
121 public CSound管理( IntPtr handle, ESoundDeviceType soundDeviceType, int nSoundDelayExclusiveWASAPI, int nSoundDelayASIO, int nASIODevice )
\r
123 WindowHandle = handle;
\r
124 SoundDevice = null;
\r
125 t初期化( soundDeviceType, nSoundDelayExclusiveWASAPI, nSoundDelayASIO, nASIODevice );
\r
127 public void Dispose()
\r
132 public static void t初期化()
\r
134 t初期化( ESoundDeviceType.DirectSound, 0, 0, 0 );
\r
138 public static void t初期化( ESoundDeviceType soundDeviceType, int _nSoundDelayExclusiveWASAPI, int _nSoundDelayASIO, int _nASIODevice )
\r
140 //SoundDevice = null; // 後で再初期化することがあるので、null初期化はコンストラクタに回す
\r
141 rc演奏用タイマ = null; // Global.Bass 依存(つまりユーザ依存)
\r
144 SoundDelayExclusiveWASAPI = _nSoundDelayExclusiveWASAPI;
\r
145 SoundDelayASIO = _nSoundDelayASIO;
\r
146 ASIODevice = _nASIODevice;
\r
148 ESoundDeviceType[] ESoundDeviceTypes = new ESoundDeviceType[ 4 ]
\r
150 ESoundDeviceType.ExclusiveWASAPI,
\r
151 ESoundDeviceType.ASIO,
\r
152 ESoundDeviceType.DirectSound,
\r
153 ESoundDeviceType.Unknown
\r
157 switch ( soundDeviceType )
\r
159 case ESoundDeviceType.ExclusiveWASAPI:
\r
162 case ESoundDeviceType.ASIO:
\r
165 case ESoundDeviceType.DirectSound:
\r
172 for ( SoundDeviceType = ESoundDeviceTypes[ n初期デバイス ]; ; SoundDeviceType = ESoundDeviceTypes[ ++n初期デバイス ] )
\r
176 t現在のユーザConfigに従ってサウンドデバイスとすべての既存サウンドを再構築する();
\r
179 catch ( Exception e )
\r
181 Trace.TraceInformation( e.Message );
\r
182 if ( ESoundDeviceTypes[ n初期デバイス ] == ESoundDeviceType.Unknown )
\r
184 throw new Exception( string.Format( "サウンドデバイスの初期化に失敗しました。" ) );
\r
191 public static void t終了()
\r
193 C共通.tDisposeする( SoundDevice ); SoundDevice = null;
\r
194 C共通.tDisposeする( ref rc演奏用タイマ ); // Global.Bass を解放した後に解放すること。(Global.Bass で参照されているため)
\r
198 public static void t現在のユーザConfigに従ってサウンドデバイスとすべての既存サウンドを再構築する()
\r
200 #region [ すでにサウンドデバイスと演奏タイマが構築されていれば解放する。]
\r
201 //-----------------
\r
202 if ( SoundDevice != null )
\r
204 // すでに生成済みのサウンドがあれば初期状態に戻す。
\r
206 CSound.tすべてのサウンドを初期状態に戻す(); // リソースは解放するが、CSoundのインスタンスは残す。
\r
209 // サウンドデバイスと演奏タイマを解放する。
\r
211 C共通.tDisposeする( SoundDevice ); SoundDevice = null;
\r
212 C共通.tDisposeする( ref rc演奏用タイマ ); // Global.SoundDevice を解放した後に解放すること。(Global.SoundDevice で参照されているため)
\r
214 //-----------------
\r
217 #region [ 新しいサウンドデバイスを構築する。]
\r
218 //-----------------
\r
219 switch ( SoundDeviceType )
\r
221 case ESoundDeviceType.ExclusiveWASAPI:
\r
222 SoundDevice = new CSoundDeviceWASAPI( CSoundDeviceWASAPI.Eデバイスモード.排他, SoundDelayExclusiveWASAPI, SoundUpdatePeriodExclusiveWASAPI );
\r
225 case ESoundDeviceType.SharedWASAPI:
\r
226 SoundDevice = new CSoundDeviceWASAPI( CSoundDeviceWASAPI.Eデバイスモード.共有, SoundDelaySharedWASAPI, SoundUpdatePeriodSharedWASAPI );
\r
229 case ESoundDeviceType.ASIO:
\r
230 SoundDevice = new CSoundDeviceASIO( SoundDelayASIO, ASIODevice );
\r
233 case ESoundDeviceType.DirectSound:
\r
234 SoundDevice = new CSoundDeviceDirectSound( WindowHandle, SoundDelayDirectSound );
\r
238 throw new Exception( string.Format( "未対応の SoundDeviceType です。[{0}]", SoundDeviceType.ToString() ) );
\r
240 //-----------------
\r
242 #region [ 新しい演奏タイマを構築する。]
\r
243 //-----------------
\r
244 rc演奏用タイマ = new CSoundTimer( SoundDevice );
\r
245 //-----------------
\r
248 CSound.tすべてのサウンドを再構築する( SoundDevice ); // すでに生成済みのサウンドがあれば作り直す。
\r
250 public CSound tサウンドを生成する( string filename )
\r
252 if ( SoundDeviceType == ESoundDeviceType.Unknown )
\r
254 throw new Exception( string.Format( "未対応の SoundDeviceType です。[{0}]", SoundDeviceType.ToString() ) );
\r
256 return SoundDevice.tサウンドを作成する( filename );
\r
259 public void t再生中の処理をする()
\r
261 //★★★★★★★★★★★★★★★★★★★★★ダミー★★★★★★★★★★★★★★★★★★
\r
262 // Debug.Write( "再生中の処理をする()" );
\r
265 public void tサウンドを破棄する( CSound csound )
\r
267 csound.t解放する( true ); // インスタンスは存続→破棄にする。
\r
271 public float GetCPUusage()
\r
274 switch ( SoundDeviceType )
\r
276 case ESoundDeviceType.ExclusiveWASAPI:
\r
277 case ESoundDeviceType.SharedWASAPI:
\r
278 f = BassWasapi.BASS_WASAPI_GetCPU();
\r
280 case ESoundDeviceType.ASIO:
\r
281 f = BassAsio.BASS_ASIO_GetCPU();
\r
283 case ESoundDeviceType.DirectSound:
\r
293 public string GetCurrentSoundDeviceType()
\r
295 switch ( SoundDeviceType )
\r
297 case ESoundDeviceType.ExclusiveWASAPI:
\r
298 case ESoundDeviceType.SharedWASAPI:
\r
300 case ESoundDeviceType.ASIO:
\r
302 case ESoundDeviceType.DirectSound:
\r
303 return "DirectSound";
\r
309 public void AddMixer( CSound cs )
\r
311 cs.tBASSサウンドをミキサーに追加する();
\r
313 public void RemoveMixer( CSound cs )
\r
315 cs.tBASSサウンドをミキサーから削除する();
\r
320 // CSound は、サウンドデバイスが変更されたときも、インスタンスを再作成することなく、新しいデバイスで作り直せる必要がある。
\r
321 // そのため、デバイスごとに別のクラスに分割するのではなく、1つのクラスに集約するものとする。
\r
323 public class CSound : IDisposable, ICloneable
\r
325 #region [ DTXMania用拡張 ]
\r
326 public int n総演奏時間ms
\r
331 public int nサウンドバッファサイズ // 取りあえず0固定★★★★★★★★★★★★★★★★★★★★
\r
335 public bool bストリーム再生する // 取りあえずfalse固定★★★★★★★★★★★★★★★★★★★★
\r
336 // trueにすると同一チップ音の多重再生で問題が出る(4POLY音源として動かない)
\r
338 get { return false; }
\r
340 public double db周波数倍率
\r
348 if ( _db周波数倍率 != value )
\r
351 if ( bBASSサウンドである )
\r
353 Bass.BASS_ChannelSetAttribute( this.hBassStream, BASSAttribute.BASS_ATTRIB_FREQ, ( float ) ( _db周波数倍率 * _db再生速度 * nオリジナルの周波数 ) );
\r
359 this.Buffer.Frequency = ( int ) ( _db周波数倍率 * _db再生速度 * nオリジナルの周波数 );
\r
365 public double db再生速度
\r
373 if ( _db再生速度 != value )
\r
376 if ( bBASSサウンドである )
\r
378 Bass.BASS_ChannelSetAttribute( this.hBassStream, BASSAttribute.BASS_ATTRIB_FREQ, ( float ) ( _db周波数倍率 * _db再生速度 * nオリジナルの周波数 ) );
\r
384 this.Buffer.Frequency = ( int ) ( _db周波数倍率 * _db再生速度 * nオリジナルの周波数 );
\r
393 private STREAMPROC _cbStreamXA; // make it global, so that the GC can not remove it
\r
394 // private SYNCPROC _cbEndofStream; // ストリームの終端まで再生されたときに呼び出されるコールバック
\r
395 // private WaitCallback _cbRemoveMixerChannel;
\r
398 /// <para>0:最小~100:原音</para>
\r
404 if( this.bBASSサウンドである )
\r
407 if ( !Bass.BASS_ChannelGetAttribute( this.hBassStream, BASSAttribute.BASS_ATTRIB_VOL, ref f音量 ) )
\r
408 //if ( BassMix.BASS_Mixer_ChannelGetEnvelopePos( this.hBassStream, BASSMIXEnvelope.BASS_MIXER_ENV_VOL, ref f音量 ) == -1 )
\r
410 return (int) ( f音量 * 100 );
\r
412 else if( this.bDirectSoundである )
\r
420 if( this.bBASSサウンドである )
\r
422 float f音量 = Math.Min( Math.Max( value, 0 ), 100 ) / 100.0f; // 0~100 → 0.0~1.0
\r
423 //var nodes = new BASS_MIXER_NODE[ 1 ] { new BASS_MIXER_NODE( 0, f音量 ) };
\r
424 //BassMix.BASS_Mixer_ChannelSetEnvelope( this.hBassStream, BASSMIXEnvelope.BASS_MIXER_ENV_VOL, nodes );
\r
425 Bass.BASS_ChannelSetAttribute( this.hBassStream, BASSAttribute.BASS_ATTRIB_VOL, f音量 );
\r
428 else if( this.bDirectSoundである )
\r
432 if( this._n音量 == 0 )
\r
434 this._n音量db = -10000;
\r
438 this._n音量db = (int) ( ( 20.0 * Math.Log10( ( (double) this._n音量 ) / 100.0 ) ) * 100.0 );
\r
441 this.Buffer.Volume = this._n音量db;
\r
447 /// <para>左:-100~中央:0~100:右。set のみ。</para>
\r
453 if( this.bBASSサウンドである )
\r
456 if ( !Bass.BASS_ChannelGetAttribute( this.hBassStream, BASSAttribute.BASS_ATTRIB_PAN, ref f位置 ) )
\r
457 //if( BassMix.BASS_Mixer_ChannelGetEnvelopePos( this.hBassStream, BASSMIXEnvelope.BASS_MIXER_ENV_PAN, ref f位置 ) == -1 )
\r
459 return (int) ( f位置 * 100 );
\r
461 else if( this.bDirectSoundである )
\r
469 if( this.bBASSサウンドである )
\r
471 float f位置 = Math.Min( Math.Max( value, -100 ), 100 ) / 100.0f; // -100~100 → -1.0~1.0
\r
472 //var nodes = new BASS_MIXER_NODE[ 1 ] { new BASS_MIXER_NODE( 0, f位置 ) };
\r
473 //BassMix.BASS_Mixer_ChannelSetEnvelope( this.hBassStream, BASSMIXEnvelope.BASS_MIXER_ENV_PAN, nodes );
\r
474 Bass.BASS_ChannelSetAttribute( this.hBassStream, BASSAttribute.BASS_ATTRIB_PAN, f位置 );
\r
476 else if( this.bDirectSoundである )
\r
478 this._n位置 = Math.Min( Math.Max( -100, value ), 100 ); // -100~100
\r
480 if( this._n位置 == 0 )
\r
484 else if( this._n位置 == -100 )
\r
486 this._n位置db = -10000;
\r
488 else if( this._n位置 == 100 )
\r
490 this._n位置db = 100000;
\r
492 else if( this._n位置 < 0 )
\r
494 this._n位置db = (int) ( ( 20.0 * Math.Log10( ( (double) ( this._n位置 + 100 ) ) / 100.0 ) ) * 100.0 );
\r
498 this._n位置db = (int) ( ( -20.0 * Math.Log10( ( (double) ( 100 - this._n位置 ) ) / 100.0 ) ) * 100.0 );
\r
501 this.Buffer.Pan = this._n位置db;
\r
507 /// <para>DirectSoundのセカンダリバッファ。</para>
\r
509 //public SecondarySoundBuffer DirectSoundBuffer
\r
510 public SoundBuffer DirectSoundBuffer
\r
512 get { return this.Buffer; }
\r
516 /// <para>DirectSoundのセカンダリバッファ作成時のフラグ。</para>
\r
518 public BufferFlags DirectSoundBufferFlags
\r
525 /// <para>全インスタンスリスト。</para>
\r
526 /// <para>~を作成する() で追加され、t解放する() or Dispose() で解放される。</para>
\r
528 public static List<CSound> listインスタンス = new List<CSound>();
\r
530 public static void ShowAllCSoundFiles()
\r
533 foreach ( CSound cs in listインスタンス )
\r
535 Debug.WriteLine( i++.ToString( "d3" ) + ": " + Path.GetFileName( cs.strファイル名 ) );
\r
543 this._db周波数倍率 = 1.0;
\r
544 this._db再生速度 = 1.0;
\r
545 this.DirectSoundBufferFlags = CSoundDeviceDirectSound.DefaultFlags;
\r
546 // this._cbRemoveMixerChannel = new WaitCallback( RemoveMixerChannelLater );
\r
549 public object Clone()
\r
551 if ( !bDirectSoundである )
\r
553 throw new NotImplementedException();
\r
555 CSound clone = (CSound) MemberwiseClone(); // これだけだとCY連打が途切れる&タイトルに戻る際にNullRef例外発生
\r
556 this.DirectSound.DuplicateSoundBuffer( this.Buffer, out clone.Buffer );
\r
558 // CSound.listインスタンス.Add( this ); // インスタンスリストに登録。
\r
559 // 本来これを加えるべきだが、Add後Removeできなくなっている。Clone()の仕方の問題であろう。
\r
563 public void tASIOサウンドを作成する( string strファイル名, int hMixer )
\r
565 this.tBASSサウンドを作成する( strファイル名, hMixer, BASSFlag.BASS_STREAM_DECODE );
\r
566 this.eデバイス種別 = ESoundDeviceType.ASIO; // 作成後に設定する。(作成に失敗してると例外発出されてここは実行されない)
\r
568 public void tASIOサウンドを作成する( byte[] byArrWAVファイルイメージ, int hMixer )
\r
570 this.tBASSサウンドを作成する( byArrWAVファイルイメージ, hMixer, BASSFlag.BASS_STREAM_DECODE );
\r
571 this.eデバイス種別 = ESoundDeviceType.ASIO; // 作成後に設定する。(作成に失敗してると例外発出されてここは実行されない)
\r
573 public void tWASAPIサウンドを作成する( string strファイル名, int hMixer, ESoundDeviceType eデバイス種別 )
\r
575 this.tBASSサウンドを作成する( strファイル名, hMixer, BASSFlag.BASS_STREAM_DECODE | BASSFlag.BASS_SAMPLE_FLOAT );
\r
576 this.eデバイス種別 = eデバイス種別; // 作成後に設定する。(作成に失敗してると例外発出されてここは実行されない)
\r
578 public void tWASAPIサウンドを作成する( byte[] byArrWAVファイルイメージ, int hMixer, ESoundDeviceType eデバイス種別 )
\r
580 this.tBASSサウンドを作成する( byArrWAVファイルイメージ, hMixer, BASSFlag.BASS_STREAM_DECODE | BASSFlag.BASS_SAMPLE_FLOAT );
\r
581 this.eデバイス種別 = eデバイス種別; // 作成後に設定する。(作成に失敗してると例外発出されてここは実行されない)
\r
583 public void tDirectSoundサウンドを作成する( string strファイル名, DirectSound DirectSound )
\r
585 this.e作成方法 = E作成方法.ファイルから;
\r
586 this.strファイル名 = strファイル名;
\r
588 if ( String.Compare( Path.GetExtension( strファイル名 ), ".xa", true ) == 0 ||
\r
589 String.Compare( Path.GetExtension( strファイル名 ), ".mp3", true ) == 0 ||
\r
590 String.Compare( Path.GetExtension( strファイル名 ), ".ogg", true ) == 0 ) // caselessで文字列比較
\r
592 tDirectSoundサウンドを作成するXaOggMp3( strファイル名, DirectSound );
\r
596 // すべてのファイルを DirectShow でデコードすると時間がかかるので、ファイルが WAV かつ PCM フォーマットでない場合のみ DirectShow でデコードする。
\r
598 byte[] byArrWAVファイルイメージ = null;
\r
599 bool bファイルがWAVかつPCMフォーマットである = true;
\r
602 #region [ ファイルがWAVかつPCMフォーマットか否か調べる。]
\r
603 //-----------------
\r
606 using ( var ws = new WaveStream( strファイル名 ) )
\r
608 if ( ws.Format.FormatTag != WaveFormatTag.Pcm )
\r
609 bファイルがWAVかつPCMフォーマットである = false;
\r
614 bファイルがWAVかつPCMフォーマットである = false;
\r
616 //-----------------
\r
619 if ( bファイルがWAVかつPCMフォーマットである )
\r
621 #region [ ファイルを読み込んで byArrWAVファイルイメージへ格納。]
\r
622 //-----------------
\r
623 var fs = File.Open( strファイル名, FileMode.Open, FileAccess.Read );
\r
624 var br = new BinaryReader( fs );
\r
626 byArrWAVファイルイメージ = new byte[ fs.Length ];
\r
627 br.Read( byArrWAVファイルイメージ, 0, (int) fs.Length );
\r
631 //-----------------
\r
636 #region [ DirectShow でデコード変換し、 byArrWAVファイルイメージへ格納。]
\r
637 //-----------------
\r
638 CDStoWAVFileImage.t変換( strファイル名, out byArrWAVファイルイメージ );
\r
639 //-----------------
\r
646 this.tDirectSoundサウンドを作成する( byArrWAVファイルイメージ, DirectSound );
\r
648 public void tDirectSoundサウンドを作成するXaOggMp3( string strファイル名, DirectSound DirectSound )
\r
650 this.e作成方法 = E作成方法.ファイルから;
\r
651 this.strファイル名 = strファイル名;
\r
654 WaveFormat wfx = new WaveFormat();
\r
655 int nPCMデータの先頭インデックス = 0;
\r
656 // int nPCMサイズbyte = (int) ( xa.xaheader.nSamples * xa.xaheader.nChannels * 2 ); // nBytes = Bass.BASS_ChannelGetLength( this.hBassStream );
\r
659 CWin32.WAVEFORMATEX cw32wfx;
\r
660 tオンメモリ方式でデコードする( strファイル名, out this.byArrWAVファイルイメージ,
\r
661 out nPCMデータの先頭インデックス, out nPCMサイズbyte, out cw32wfx );
\r
663 wfx.AverageBytesPerSecond = (int) cw32wfx.nAvgBytesPerSec;
\r
664 wfx.BitsPerSample = (short) cw32wfx.wBitsPerSample;
\r
665 wfx.BlockAlignment = (short) cw32wfx.nBlockAlign;
\r
666 wfx.Channels = (short) cw32wfx.nChannels;
\r
667 wfx.FormatTag = WaveFormatTag.Pcm; // xa.waveformatex.wFormatTag;
\r
668 wfx.SamplesPerSecond = (int) cw32wfx.nSamplesPerSec;
\r
670 // セカンダリバッファを作成し、PCMデータを書き込む。
\r
671 tDirectSoundサウンドを作成する・セカンダリバッファの作成とWAVデータ書き込み
\r
672 ( ref this.byArrWAVファイルイメージ, DirectSound, CSoundDeviceDirectSound.DefaultFlags, wfx,
\r
673 nPCMサイズbyte, nPCMデータの先頭インデックス );
\r
676 public void tDirectSoundサウンドを作成する( byte[] byArrWAVファイルイメージ, DirectSound DirectSound )
\r
678 this.tDirectSoundサウンドを作成する( byArrWAVファイルイメージ, DirectSound, CSoundDeviceDirectSound.DefaultFlags );
\r
680 public void tDirectSoundサウンドを作成する( byte[] byArrWAVファイルイメージ, DirectSound DirectSound, BufferFlags flags )
\r
682 if( this.e作成方法 == E作成方法.Unknown )
\r
683 this.e作成方法 = E作成方法.WAVファイルイメージから;
\r
685 WaveFormat wfx = null;
\r
686 int nPCMデータの先頭インデックス = -1;
\r
687 int nPCMサイズbyte = -1;
\r
689 #region [ byArrWAVファイルイメージ[] から上記3つのデータを取得。]
\r
690 //-----------------
\r
691 var ms = new MemoryStream( byArrWAVファイルイメージ );
\r
692 var br = new BinaryReader( ms );
\r
696 // 'RIFF'+RIFFデータサイズ
\r
698 if( br.ReadUInt32() != 0x46464952 )
\r
699 throw new InvalidDataException( "RIFFファイルではありません。" );
\r
703 if( br.ReadUInt32() != 0x45564157 )
\r
704 throw new InvalidDataException( "WAVEファイルではありません。" );
\r
707 while( ( ms.Position + 8 ) < ms.Length ) // +8 は、チャンク名+チャンクサイズ。残り8バイト未満ならループ終了。
\r
709 uint chunkName = br.ReadUInt32();
\r
712 if( chunkName == 0x20746D66 )
\r
714 long chunkSize = (long) br.ReadUInt32();
\r
716 var tag = (WaveFormatTag) br.ReadUInt16();
\r
718 if( tag == WaveFormatTag.Pcm ) wfx = new WaveFormat();
\r
719 else if( tag == WaveFormatTag.Extensible ) wfx = new SlimDX.Multimedia.WaveFormatExtensible(); // このクラスは WaveFormat を継承している。
\r
721 throw new InvalidDataException( string.Format( "未対応のWAVEフォーマットタグです。(Tag:{0})", tag.ToString() ) );
\r
723 wfx.FormatTag = tag;
\r
724 wfx.Channels = br.ReadInt16();
\r
725 wfx.SamplesPerSecond = br.ReadInt32();
\r
726 wfx.AverageBytesPerSecond = br.ReadInt32();
\r
727 wfx.BlockAlignment = br.ReadInt16();
\r
728 wfx.BitsPerSample = br.ReadInt16();
\r
730 long nフォーマットサイズbyte = 16;
\r
732 if( wfx.FormatTag == WaveFormatTag.Extensible )
\r
734 br.ReadUInt16(); // 拡張領域サイズbyte
\r
735 var wfxEx = (SlimDX.Multimedia.WaveFormatExtensible) wfx;
\r
736 wfxEx.ValidBitsPerSample = br.ReadInt16();
\r
737 wfxEx.ChannelMask = (Speakers) br.ReadInt32();
\r
738 wfxEx.SubFormat = new Guid( br.ReadBytes( 16 ) ); // GUID は 16byte (128bit)
\r
740 nフォーマットサイズbyte += 24;
\r
743 ms.Seek( chunkSize - nフォーマットサイズbyte, SeekOrigin.Current );
\r
748 else if( chunkName == 0x61746164 )
\r
750 nPCMサイズbyte = br.ReadInt32();
\r
751 nPCMデータの先頭インデックス = (int) ms.Position;
\r
753 ms.Seek( nPCMサイズbyte, SeekOrigin.Current );
\r
760 long chunkSize = (long) br.ReadUInt32();
\r
761 ms.Seek( chunkSize, SeekOrigin.Current );
\r
767 throw new InvalidDataException( "fmt チャンクが存在しません。不正なサウンドデータです。" );
\r
768 if( nPCMサイズbyte < 0 )
\r
769 throw new InvalidDataException( "data チャンクが存在しません。不正なサウンドデータです。" );
\r
776 //-----------------
\r
780 // セカンダリバッファを作成し、PCMデータを書き込む。
\r
781 tDirectSoundサウンドを作成する・セカンダリバッファの作成とWAVデータ書き込み(
\r
782 ref byArrWAVファイルイメージ, DirectSound, flags, wfx, nPCMサイズbyte, nPCMデータの先頭インデックス );
\r
785 private void tDirectSoundサウンドを作成する・セカンダリバッファの作成とWAVデータ書き込み
\r
786 ( ref byte[] byArrWAVファイルイメージ, DirectSound DirectSound, BufferFlags flags, WaveFormat wfx,
\r
787 int nPCMサイズbyte, int nPCMデータの先頭インデックス )
\r
789 // セカンダリバッファを作成し、PCMデータを書き込む。
\r
791 this.Buffer = new SecondarySoundBuffer( DirectSound, new SoundBufferDescription()
\r
793 Format = ( wfx.FormatTag == WaveFormatTag.Pcm ) ? wfx : (SlimDX.Multimedia.WaveFormatExtensible) wfx,
\r
795 SizeInBytes = nPCMサイズbyte,
\r
797 this.Buffer.Write( byArrWAVファイルイメージ, nPCMデータの先頭インデックス, nPCMサイズbyte, 0, LockFlags.None );
\r
801 this.eデバイス種別 = ESoundDeviceType.DirectSound;
\r
802 this.DirectSoundBufferFlags = flags;
\r
803 this.byArrWAVファイルイメージ = byArrWAVファイルイメージ;
\r
804 this.DirectSound = DirectSound;
\r
807 this.nオリジナルの周波数 = wfx.SamplesPerSecond;
\r
808 n総演奏時間ms = (int) ( ( (double) nPCMサイズbyte ) / ( this.Buffer.Format.AverageBytesPerSecond * 0.001 ) );
\r
813 CSound.listインスタンス.Add( this );
\r
816 #region [ DTXMania用の変換 ]
\r
818 public void tサウンドを破棄する( CSound cs )
\r
822 public void t再生を開始する()
\r
827 public void t再生を開始する( bool bループする )
\r
829 if ( bBASSサウンドである )
\r
833 Bass.BASS_ChannelFlags( this.hBassStream, BASSFlag.BASS_SAMPLE_LOOP, BASSFlag.BASS_SAMPLE_LOOP );
\r
837 Bass.BASS_ChannelFlags( this.hBassStream, BASSFlag.BASS_DEFAULT, BASSFlag.BASS_DEFAULT );
\r
841 tサウンドを再生する( bループする );
\r
843 public void t再生を停止する()
\r
848 public void t再生を一時停止する()
\r
853 public void t再生を再開する( long t ) // ★★★★★★★★★★★★★★★★★★★★★★★★★★★★
\r
855 Debug.WriteLine( "t再生を再開する(long " + t + ")" );
\r
864 if ( this.bBASSサウンドである )
\r
866 bool ret = ( BassMix.BASS_Mixer_ChannelIsActive( this.hBassStream ) == BASSActive.BASS_ACTIVE_PAUSED ) &
\r
867 ( BassMix.BASS_Mixer_ChannelGetPosition( this.hBassStream ) > 0 );
\r
872 return ( this.n一時停止回数 > 0 );
\r
880 if ( this.eデバイス種別 == ESoundDeviceType.DirectSound )
\r
882 return ( ( this.Buffer.Status & BufferStatus.Playing ) != BufferStatus.None );
\r
886 // 基本的にはBASS_ACTIVE_PLAYINGなら再生中だが、最後まで再生しきったchannelも
\r
887 // BASS_ACTIVE_PLAYINGのままになっているので、小細工が必要。
\r
888 bool ret = ( BassMix.BASS_Mixer_ChannelIsActive( this.hBassStream ) == BASSActive.BASS_ACTIVE_PLAYING );
\r
889 if ( BassMix.BASS_Mixer_ChannelGetPosition( this.hBassStream ) >= nBytes )
\r
897 //public lint t時刻から位置を返す( long t )
\r
899 // double num = ( n時刻 * this.db再生速度 ) * this.db周波数倍率;
\r
900 // return (int) ( ( num * 0.01 ) * this.nSamplesPerSecond );
\r
905 public void t解放する()
\r
910 public void t解放する( bool _bインスタンス削除 )
\r
912 if ( this.bBASSサウンドである ) // stream数の削減用
\r
914 tBASSサウンドをミキサーから削除する();
\r
915 //_cbEndofStream = null;
\r
916 //_cbStreamXA = null;
\r
917 CSound管理.nStreams--;
\r
919 bool bManagedも解放する = true;
\r
920 bool bインスタンス削除 = _bインスタンス削除; // CSoundの再初期化時は、インスタンスは存続する。
\r
921 this.Dispose( bManagedも解放する, bインスタンス削除 );
\r
922 //Debug.WriteLine( "Disposed: " + _bインスタンス削除 + " : " + Path.GetFileName( this.strファイル名 ) );
\r
924 public void tサウンドを再生する()
\r
926 tサウンドを再生する( false );
\r
928 public void tサウンドを再生する( bool bループする )
\r
930 if ( this.bBASSサウンドである ) // BASSサウンド時のループ処理は、t再生を開始する()側に実装。
\r
932 //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
933 bool b = BassMix.BASS_Mixer_ChannelPlay( this.hBassStream );
\r
936 Debug.WriteLine( "再生しようとしたが、Mixerに登録されていなかった: " + Path.GetFileName( this.strファイル名 ) + ", " + hBassStream );
\r
937 //Debug.WriteLine( "ErrCode= " +Bass.BASS_ErrorGetCode() );
\r
939 bool bb = tBASSサウンドをミキサーに追加する();
\r
942 Debug.WriteLine( "Mixerへの登録に失敗: " + Path.GetFileName( this.strファイル名 ) + ": " + Bass.BASS_ErrorGetCode() );
\r
946 //Debug.WriteLine( "Mixerへの登録に成功: " + Path.GetFileName( this.strファイル名 ) + ": " + Bass.BASS_ErrorGetCode() );
\r
948 //this.t再生位置を先頭に戻す();
\r
950 bool bbb = BassMix.BASS_Mixer_ChannelPlay( this.hBassStream );
\r
953 Debug.WriteLine("更に再生に失敗 : " + Path.GetFileName(this.strファイル名));
\r
954 Debug.WriteLine("ErrCode= " + Bass.BASS_ErrorGetCode());
\r
958 // Debug.WriteLine("再生成功(ミキサー追加後) : " + Path.GetFileName(this.strファイル名));
\r
963 //Debug.WriteLine( "再生成功: " + Path.GetFileName( this.strファイル名 ) + " (" + hBassStream + ")" );
\r
966 else if( this.bDirectSoundである )
\r
968 PlayFlags pf = ( bループする ) ? PlayFlags.Looping : PlayFlags.None;
\r
969 this.Buffer.Play( 0, pf );
\r
972 public void tサウンドを先頭から再生する()
\r
974 this.t再生位置を先頭に戻す();
\r
977 public void tサウンドを停止する()
\r
979 tサウンドを停止する( false );
\r
981 public void tサウンドを停止する( bool pause )
\r
983 if( this.bBASSサウンドである )
\r
985 //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
986 BassMix.BASS_Mixer_ChannelPause( this.hBassStream );
\r
989 // tBASSサウンドをミキサーから削除する(); // PAUSEと再生停止を区別できるようにすること!!
\r
992 else if( this.bDirectSoundである )
\r
994 this.Buffer.Stop();
\r
998 public void t再生位置を先頭に戻す()
\r
1000 if( this.bBASSサウンドである )
\r
1002 BassMix.BASS_Mixer_ChannelSetPosition( this.hBassStream, 0 );
\r
1005 else if( this.bDirectSoundである )
\r
1007 this.Buffer.CurrentPlayPosition = 0;
\r
1010 public void t再生位置を変更する( long n位置ms )
\r
1012 if( this.bBASSサウンドである )
\r
1014 BassMix.BASS_Mixer_ChannelSetPosition( this.hBassStream, Bass.BASS_ChannelSeconds2Bytes( this.hBassStream, n位置ms / 1000.0 ), BASSMode.BASS_POS_BYTES );
\r
1016 else if( this.bDirectSoundである )
\r
1018 int n位置sample = (int) ( this.Buffer.Format.SamplesPerSecond * n位置ms * 0.001 );
\r
1019 this.Buffer.CurrentPlayPosition = n位置sample * this.Buffer.Format.BlockAlignment;
\r
1023 public static void tすべてのサウンドを初期状態に戻す()
\r
1025 foreach ( var sound in CSound.listインスタンス )
\r
1027 sound.t解放する( false );
\r
1030 public static void tすべてのサウンドを再構築する( ISoundDevice device )
\r
1032 if( CSound.listインスタンス.Count == 0 )
\r
1036 // サウンドを再生する際にインスタンスリストも更新されるので、配列にコピーを取っておき、リストはクリアする。
\r
1038 var sounds = CSound.listインスタンス.ToArray();
\r
1039 CSound.listインスタンス.Clear();
\r
1042 // 配列に基づいて個々のサウンドを作成する。
\r
1044 for( int i = 0; i < sounds.Length; i++ )
\r
1046 switch( sounds[ i ].e作成方法 )
\r
1048 #region [ ファイルから ]
\r
1049 case E作成方法.ファイルから:
\r
1050 string strファイル名 = sounds[ i ].strファイル名;
\r
1051 sounds[ i ].Dispose( true, false );
\r
1052 device.tサウンドを作成する( strファイル名, ref sounds[ i ] );
\r
1055 #region [ WAVファイルイメージから ]
\r
1056 case E作成方法.WAVファイルイメージから:
\r
1057 if( sounds[ i ].bBASSサウンドである )
\r
1059 byte[] byArrWaveファイルイメージ = sounds[ i ].byArrWAVファイルイメージ;
\r
1060 sounds[ i ].Dispose( true, false );
\r
1061 device.tサウンドを作成する( byArrWaveファイルイメージ, ref sounds[ i ] );
\r
1063 else if( sounds[ i ].bDirectSoundである )
\r
1065 byte[] byArrWaveファイルイメージ = sounds[ i ].byArrWAVファイルイメージ;
\r
1066 var flags = sounds[ i ].DirectSoundBufferFlags;
\r
1067 sounds[ i ].Dispose( true, false );
\r
1068 ( (CSoundDeviceDirectSound) device ).tサウンドを作成する( byArrWaveファイルイメージ, flags, ref sounds[ i ] );
\r
1076 #region [ Dispose-Finalizeパターン実装 ]
\r
1077 //-----------------
\r
1078 public void Dispose()
\r
1080 this.Dispose( true, true );
\r
1081 GC.SuppressFinalize( this );
\r
1083 protected void Dispose( bool bManagedも解放する, bool bインスタンス削除 )
\r
1085 if( this.bBASSサウンドである )
\r
1087 #region [ ASIO, WASAPI の解放 ]
\r
1088 //-----------------
\r
1089 BassMix.BASS_Mixer_ChannelRemove( this.hBassStream );
\r
1090 Bass.BASS_StreamFree( this.hBassStream );
\r
1091 this.hBassStream = -1;
\r
1092 //-----------------
\r
1096 if( bManagedも解放する )
\r
1098 //int freeIndex = -1;
\r
1100 //if ( CSound.listインスタンス != null )
\r
1102 // freeIndex = CSound.listインスタンス.IndexOf( this );
\r
1103 // if ( freeIndex == -1 )
\r
1105 // Debug.WriteLine( "ERR: freeIndex==-1 : Count=" + CSound.listインスタンス.Count + ", filename=" + Path.GetFileName( this.strファイル名 ) );
\r
1109 if( this.eデバイス種別 == ESoundDeviceType.DirectSound )
\r
1111 #region [ DirectSound の解放 ]
\r
1112 //-----------------
\r
1113 if( this.Buffer != null )
\r
1117 this.Buffer.Stop();
\r
1121 // 演奏終了後、長時間解放しないでいると、たまに AccessViolationException が発生することがある。
\r
1123 C共通.tDisposeする( ref this.Buffer );
\r
1125 //-----------------
\r
1129 if( this.e作成方法 == E作成方法.WAVファイルイメージから &&
\r
1130 this.eデバイス種別 != ESoundDeviceType.DirectSound ) // DirectSound は hGC 未使用。
\r
1132 if ( this.hGC != null && this.hGC.IsAllocated )
\r
1135 this.hGC = default( GCHandle );
\r
1138 if ( this.byArrWAVファイルイメージ != null )
\r
1140 this.byArrWAVファイルイメージ = null;
\r
1147 // CSound.listインスタンス.RemoveAt( freeIndex );
\r
1151 // Debug.WriteLine( "FAILED to remove CSound.listインスタンス: Count=" + CSound.listインスタンス.Count + ", filename=" + Path.GetFileName( this.strファイル名 ) );
\r
1153 bool b = CSound.listインスタンス.Remove( this ); // これだと、Clone()したサウンドのremoveに失敗する
\r
1156 Debug.WriteLine( "FAILED to remove CSound.listインスタンス: Count=" + CSound.listインスタンス.Count + ", filename=" + Path.GetFileName( this.strファイル名 ) );
\r
1164 this.Dispose( false, true );
\r
1166 //-----------------
\r
1169 #region [ protected ]
\r
1170 //-----------------
\r
1171 protected enum E作成方法 { ファイルから, WAVファイルイメージから, Unknown }
\r
1172 protected E作成方法 e作成方法 = E作成方法.Unknown;
\r
1173 protected ESoundDeviceType eデバイス種別 = ESoundDeviceType.Unknown;
\r
1174 public string strファイル名 = null;
\r
1175 protected byte[] byArrWAVファイルイメージ = null; // WAVファイルイメージ、もしくはchunkのDATA部のみ
\r
1176 protected GCHandle hGC;
\r
1177 public int hBassStream = -1; // ASIO, WASAPI 用
\r
1178 protected SoundBuffer Buffer = null; // DirectSound 用
\r
1179 protected DirectSound DirectSound;
\r
1180 public int hMixer = -1; // 設計壊してゴメン Mixerに後で登録するときに使う
\r
1181 //-----------------
\r
1184 #region [ private ]
\r
1185 //-----------------
\r
1186 private bool bDirectSoundである
\r
1188 get { return ( this.eデバイス種別 == ESoundDeviceType.DirectSound ); }
\r
1190 private bool bBASSサウンドである
\r
1195 this.eデバイス種別 == ESoundDeviceType.ASIO ||
\r
1196 this.eデバイス種別 == ESoundDeviceType.ExclusiveWASAPI ||
\r
1197 this.eデバイス種別 == ESoundDeviceType.SharedWASAPI );
\r
1200 private int _n位置 = 0;
\r
1201 private int _n位置db;
\r
1202 private int _n音量 = 100;
\r
1203 private int _n音量db;
\r
1204 private long nBytes = 0;
\r
1205 private int n一時停止回数 = 0;
\r
1206 private int nオリジナルの周波数 = 0;
\r
1207 private double _db周波数倍率 = 1.0;
\r
1208 private double _db再生速度 = 1.0;
\r
1210 private void tBASSサウンドを作成する( string strファイル名, int hMixer, BASSFlag flags )
\r
1212 if ( String.Compare( Path.GetExtension( strファイル名 ), ".xa", true ) == 0 ) // caselessで文字列比較
\r
1214 tBASSサウンドを作成するXA( strファイル名, hMixer, flags );
\r
1218 this.e作成方法 = E作成方法.ファイルから;
\r
1219 this.strファイル名 = strファイル名;
\r
1222 // BASSファイルストリームを作成。
\r
1224 this.hBassStream = Bass.BASS_StreamCreateFile( strファイル名, 0, 0, flags );
\r
1225 if( this.hBassStream == 0 )
\r
1226 throw new Exception( string.Format( "サウンドストリームの生成に失敗しました。(BASS_StreamCreateFile)[{0}]", Bass.BASS_ErrorGetCode().ToString() ) );
\r
1228 nBytes = Bass.BASS_ChannelGetLength( this.hBassStream );
\r
1230 tBASSサウンドを作成する・ストリーム生成後の共通処理( hMixer );
\r
1232 private void tBASSサウンドを作成する( byte[] byArrWAVファイルイメージ, int hMixer, BASSFlag flags )
\r
1234 this.e作成方法 = E作成方法.WAVファイルイメージから;
\r
1235 this.byArrWAVファイルイメージ = byArrWAVファイルイメージ;
\r
1236 this.hGC = GCHandle.Alloc( byArrWAVファイルイメージ, GCHandleType.Pinned ); // byte[] をピン留め
\r
1239 // BASSファイルストリームを作成。
\r
1241 this.hBassStream = Bass.BASS_StreamCreateFile( hGC.AddrOfPinnedObject(), 0, byArrWAVファイルイメージ.Length, flags );
\r
1242 if ( this.hBassStream == 0 )
\r
1243 throw new Exception( string.Format( "サウンドストリームの生成に失敗しました。(BASS_StreamCreateFile)[{0}]", Bass.BASS_ErrorGetCode().ToString() ) );
\r
1245 nBytes = Bass.BASS_ChannelGetLength( this.hBassStream );
\r
1247 tBASSサウンドを作成する・ストリーム生成後の共通処理( hMixer );
\r
1249 private void tBASSサウンドを作成するXA( string strファイル名, int hMixer, BASSFlag flags )
\r
1251 int nPCMデータの先頭インデックス;
\r
1252 CWin32.WAVEFORMATEX wfx;
\r
1255 tオンメモリ方式でデコードする( strファイル名, out this.byArrWAVファイルイメージ,
\r
1256 out nPCMデータの先頭インデックス, out totalPCMSize, out wfx );
\r
1258 nBytes = totalPCMSize;
\r
1260 this.e作成方法 = E作成方法.WAVファイルイメージから; //.ファイルから; // 再構築時はデコード後のイメージを流用する&Dispose時にhGCを解放する
\r
1261 this.strファイル名 = strファイル名;
\r
1262 this.hGC = GCHandle.Alloc( this.byArrWAVファイルイメージ, GCHandleType.Pinned ); // byte[] をピン留め
\r
1265 _cbStreamXA = new STREAMPROC( CallbackPlayingXA );
\r
1267 // BASSファイルストリームを作成。
\r
1269 //this.hBassStream = Bass.BASS_StreamCreate( xa.xaheader.nSamplesPerSec, xa.xaheader.nChannels, BASSFlag.BASS_STREAM_DECODE, _myStreamCreate, IntPtr.Zero );
\r
1270 this.hBassStream = Bass.BASS_StreamCreate( (int) wfx.nSamplesPerSec, (int) wfx.nChannels, BASSFlag.BASS_STREAM_DECODE, _cbStreamXA, IntPtr.Zero );
\r
1271 if ( this.hBassStream == 0 )
\r
1274 throw new Exception( string.Format( "サウンドストリームの生成に失敗しました。(BASS_SampleCreate)[{0}]", Bass.BASS_ErrorGetCode().ToString() ) );
\r
1277 tBASSサウンドを作成する・ストリーム生成後の共通処理( hMixer );
\r
1281 private void tBASSサウンドを作成する・ストリーム生成後の共通処理( int hMixer )
\r
1283 CSound管理.nStreams++;
\r
1285 // ミキサーにBASSファイルストリームを追加。
\r
1287 //if ( !BassMix.BASS_Mixer_StreamAddChannel( hMixer, this.hBassStream, BASSFlag.BASS_SPEAKER_FRONT | BASSFlag.BASS_MIXER_PAUSE | BASSFlag.BASS_MIXER_NORAMPIN ) )
\r
1288 //// if ( !tBASSサウンドをミキサーに追加する() )
\r
1291 // throw new Exception( string.Format( "サウンドストリームの生成に失敗しました。(BASS_Mixer_StreamAddChannel)[{0}]", Bass.BASS_ErrorGetCode().ToString() ) );
\r
1293 //CSound管理.nStreams++;
\r
1295 // _cbEndofStream = new SYNCPROC( CallbackEndofStream );
\r
1296 // Bass.BASS_ChannelSetSync( hBassStream, BASSSync.BASS_SYNC_END |BASSSync.BASS_SYNC_MIXTIME, 0, _cbEndofStream, IntPtr.Zero );
\r
1301 CSound.listインスタンス.Add( this );
\r
1303 // n総演奏時間の取得; DTXMania用に追加。
\r
1304 double seconds = Bass.BASS_ChannelBytes2Seconds( this.hBassStream, nBytes );
\r
1305 this.n総演奏時間ms = (int) ( seconds * 1000 );
\r
1307 this.hMixer = hMixer;
\r
1308 float freq = 0.0f;
\r
1309 if ( !Bass.BASS_ChannelGetAttribute( this.hBassStream, BASSAttribute.BASS_ATTRIB_FREQ, ref freq ) )
\r
1312 throw new Exception( string.Format( "サウンドストリームの周波数取得に失敗しました。(BASS_ChannelGetAttribute)[{0}]", Bass.BASS_ErrorGetCode().ToString() ) );
\r
1314 this.nオリジナルの周波数 = (int) freq;
\r
1316 //-----------------
\r
1318 private int pos = 0;
\r
1319 private int CallbackPlayingXA( int handle, IntPtr buffer, int length, IntPtr user )
\r
1321 int bytesread = ( pos + length > Convert.ToInt32(nBytes) ) ? Convert.ToInt32(nBytes) - pos : length;
\r
1323 Marshal.Copy( byArrWAVファイルイメージ, pos, buffer, bytesread );
\r
1326 if ( pos >= nBytes )
\r
1328 // set indicator flag
\r
1329 bytesread |= (int) BASSStreamProc.BASS_STREAMPROC_END;
\r
1337 public bool tBASSサウンドをミキサーから削除する()
\r
1339 return tBASSサウンドをミキサーから削除する( this.hBassStream );
\r
1341 public bool tBASSサウンドをミキサーから削除する( int channel )
\r
1343 bool b = BassMix.BASS_Mixer_ChannelRemove( channel );
\r
1346 Interlocked.Decrement( ref CSound管理.nMixing );
\r
1347 // Debug.WriteLine( "Removed: " + Path.GetFileName( this.strファイル名 ) + " (" + channel + ")" + " MixedStreams=" + CSound管理.nMixing );
\r
1355 public bool tBASSサウンドをミキサーに追加する()
\r
1357 if ( BassMix.BASS_Mixer_ChannelGetMixer( hBassStream ) == 0 )
\r
1359 BASSFlag bf = BASSFlag.BASS_SPEAKER_FRONT | BASSFlag.BASS_MIXER_NORAMPIN; // | BASSFlag.BASS_MIXER_PAUSE;
\r
1360 Interlocked.Increment( ref CSound管理.nMixing );
\r
1362 // preloadされることを期待して、敢えてflagからはBASS_MIXER_PAUSEを外してAddChannelした上で、すぐにPAUSEする
\r
1363 bool b1 = BassMix.BASS_Mixer_StreamAddChannel( this.hMixer, this.hBassStream, bf );
\r
1364 bool b2 = BassMix.BASS_Mixer_ChannelPause( this.hBassStream );
\r
1365 t再生位置を先頭に戻す(); // StreamAddChannelの後で再生位置を戻さないとダメ。逆だと再生位置が変わらない。
\r
1366 // Debug.WriteLine( "Add Mixer: " + Path.GetFileName( this.strファイル名 ) + " (" + hBassStream + ")" + " MixedStreams=" + CSound管理.nMixing );
\r
1372 #region [ tオンメモリ方式でデコードする() ]
\r
1373 public void tオンメモリ方式でデコードする( string strファイル名, out byte[] buffer,
\r
1374 out int nPCMデータの先頭インデックス, out int totalPCMSize, out CWin32.WAVEFORMATEX wfx )
\r
1376 nPCMデータの先頭インデックス = 0;
\r
1377 //int nPCMサイズbyte = (int) ( xa.xaheader.nSamples * xa.xaheader.nChannels * 2 ); // nBytes = Bass.BASS_ChannelGetLength( this.hBassStream );
\r
1379 SoundDecoder sounddecoder;
\r
1381 if ( String.Compare( Path.GetExtension( strファイル名 ), ".xa", true ) == 0 )
\r
1383 sounddecoder = new Cxa();
\r
1385 else if ( String.Compare( Path.GetExtension( strファイル名 ), ".ogg", true ) == 0 )
\r
1387 sounddecoder = new Cogg();
\r
1389 else if ( String.Compare( Path.GetExtension( strファイル名 ), ".mp3", true ) == 0 )
\r
1391 sounddecoder = new Cmp3();
\r
1395 throw new NotImplementedException();
\r
1398 int nHandle = sounddecoder.Open( strファイル名 );
\r
1399 if ( nHandle < 0 )
\r
1401 throw new Exception( string.Format( "Open() に失敗しました。({0})({1})", nHandle, strファイル名 ) );
\r
1403 wfx = new CWin32.WAVEFORMATEX();
\r
1404 if ( sounddecoder.GetFormat( nHandle, ref wfx ) < 0 )
\r
1406 sounddecoder.Close( nHandle );
\r
1407 throw new Exception( string.Format( "GetFormat() に失敗しました。({0})", strファイル名 ) );
\r
1409 //totalPCMSize = (int) sounddecoder.nTotalPCMSize; // tデコード後のサイズを調べる()で既に取得済みの値を流用する。ms単位の高速化だが、チップ音がたくさんあると塵積で結構効果がある
\r
1410 totalPCMSize = (int) sounddecoder.GetTotalPCMSize( nHandle );
\r
1411 if ( totalPCMSize == 0 )
\r
1413 sounddecoder.Close( nHandle );
\r
1414 throw new Exception( string.Format( "GetTotalPCMSize() に失敗しました。({0})", strファイル名 ) );
\r
1416 totalPCMSize += ( ( totalPCMSize % 2 ) != 0 ) ? 1 : 0;
\r
1417 buffer = new byte[ totalPCMSize ];
\r
1418 GCHandle handle = GCHandle.Alloc( buffer, GCHandleType.Pinned );
\r
1421 if ( sounddecoder.Decode( nHandle, handle.AddrOfPinnedObject(), (uint) totalPCMSize, 0 ) < 0 )
\r
1424 throw new Exception( string.Format( "デコードに失敗しました。({0})", strファイル名 ) );
\r
1430 sounddecoder.Close( nHandle );
\r
1431 sounddecoder = null;
\r