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
115 public static int hTempoStream;
\r
121 /// <param name="handle"></param>
\r
122 public CSound管理( IntPtr handle ) // #30803 従来のコンストラクタ相当のI/Fを追加。(DTXC用)
\r
124 WindowHandle = handle;
\r
125 SoundDevice = null;
\r
126 t初期化( ESoundDeviceType.DirectSound, 0, 0, 0 );
\r
128 public CSound管理( IntPtr handle, ESoundDeviceType soundDeviceType, int nSoundDelayExclusiveWASAPI, int nSoundDelayASIO, int nASIODevice )
\r
130 WindowHandle = handle;
\r
131 SoundDevice = null;
\r
132 t初期化( soundDeviceType, nSoundDelayExclusiveWASAPI, nSoundDelayASIO, nASIODevice );
\r
134 public void Dispose()
\r
139 public static void t初期化()
\r
141 t初期化( ESoundDeviceType.DirectSound, 0, 0, 0 );
\r
145 public static void t初期化( ESoundDeviceType soundDeviceType, int _nSoundDelayExclusiveWASAPI, int _nSoundDelayASIO, int _nASIODevice )
\r
147 //SoundDevice = null; // 後で再初期化することがあるので、null初期化はコンストラクタに回す
\r
148 rc演奏用タイマ = null; // Global.Bass 依存(つまりユーザ依存)
\r
151 SoundDelayExclusiveWASAPI = _nSoundDelayExclusiveWASAPI;
\r
152 SoundDelayASIO = _nSoundDelayASIO;
\r
153 ASIODevice = _nASIODevice;
\r
155 ESoundDeviceType[] ESoundDeviceTypes = new ESoundDeviceType[ 4 ]
\r
157 ESoundDeviceType.ExclusiveWASAPI,
\r
158 ESoundDeviceType.ASIO,
\r
159 ESoundDeviceType.DirectSound,
\r
160 ESoundDeviceType.Unknown
\r
164 switch ( soundDeviceType )
\r
166 case ESoundDeviceType.ExclusiveWASAPI:
\r
169 case ESoundDeviceType.ASIO:
\r
172 case ESoundDeviceType.DirectSound:
\r
179 for ( SoundDeviceType = ESoundDeviceTypes[ n初期デバイス ]; ; SoundDeviceType = ESoundDeviceTypes[ ++n初期デバイス ] )
\r
183 t現在のユーザConfigに従ってサウンドデバイスとすべての既存サウンドを再構築する();
\r
186 catch ( Exception e )
\r
188 Trace.TraceInformation( e.Message );
\r
189 if ( ESoundDeviceTypes[ n初期デバイス ] == ESoundDeviceType.Unknown )
\r
191 throw new Exception( string.Format( "サウンドデバイスの初期化に失敗しました。" ) );
\r
198 public static void t終了()
\r
200 C共通.tDisposeする( SoundDevice ); SoundDevice = null;
\r
201 C共通.tDisposeする( ref rc演奏用タイマ ); // Global.Bass を解放した後に解放すること。(Global.Bass で参照されているため)
\r
205 public static void t現在のユーザConfigに従ってサウンドデバイスとすべての既存サウンドを再構築する()
\r
207 #region [ すでにサウンドデバイスと演奏タイマが構築されていれば解放する。]
\r
208 //-----------------
\r
209 if ( SoundDevice != null )
\r
211 // すでに生成済みのサウンドがあれば初期状態に戻す。
\r
213 CSound.tすべてのサウンドを初期状態に戻す(); // リソースは解放するが、CSoundのインスタンスは残す。
\r
216 // サウンドデバイスと演奏タイマを解放する。
\r
218 C共通.tDisposeする( SoundDevice ); SoundDevice = null;
\r
219 C共通.tDisposeする( ref rc演奏用タイマ ); // Global.SoundDevice を解放した後に解放すること。(Global.SoundDevice で参照されているため)
\r
221 //-----------------
\r
224 #region [ 新しいサウンドデバイスを構築する。]
\r
225 //-----------------
\r
226 switch ( SoundDeviceType )
\r
228 case ESoundDeviceType.ExclusiveWASAPI:
\r
229 SoundDevice = new CSoundDeviceWASAPI( CSoundDeviceWASAPI.Eデバイスモード.排他, SoundDelayExclusiveWASAPI, SoundUpdatePeriodExclusiveWASAPI );
\r
232 case ESoundDeviceType.SharedWASAPI:
\r
233 SoundDevice = new CSoundDeviceWASAPI( CSoundDeviceWASAPI.Eデバイスモード.共有, SoundDelaySharedWASAPI, SoundUpdatePeriodSharedWASAPI );
\r
236 case ESoundDeviceType.ASIO:
\r
237 SoundDevice = new CSoundDeviceASIO( SoundDelayASIO, ASIODevice );
\r
240 case ESoundDeviceType.DirectSound:
\r
241 SoundDevice = new CSoundDeviceDirectSound( WindowHandle, SoundDelayDirectSound );
\r
245 throw new Exception( string.Format( "未対応の SoundDeviceType です。[{0}]", SoundDeviceType.ToString() ) );
\r
247 //-----------------
\r
249 #region [ 新しい演奏タイマを構築する。]
\r
250 //-----------------
\r
251 rc演奏用タイマ = new CSoundTimer( SoundDevice );
\r
252 //-----------------
\r
254 if ( SoundDevice.e出力デバイス != ESoundDeviceType.DirectSound && SoundDevice.e出力デバイス != ESoundDeviceType.Unknown )
\r
256 hTempoStream = SoundDevice.hTempoStream;
\r
260 CSound.tすべてのサウンドを再構築する( SoundDevice ); // すでに生成済みのサウンドがあれば作り直す。
\r
262 public CSound tサウンドを生成する( string filename )
\r
264 if ( SoundDeviceType == ESoundDeviceType.Unknown )
\r
266 throw new Exception( string.Format( "未対応の SoundDeviceType です。[{0}]", SoundDeviceType.ToString() ) );
\r
268 return SoundDevice.tサウンドを作成する( filename );
\r
271 public void t再生中の処理をする( object o ) // #26122 2011.9.1 yyagi; delegate経由の呼び出し用
\r
275 public void t再生中の処理をする()
\r
277 //★★★★★★★★★★★★★★★★★★★★★ダミー★★★★★★★★★★★★★★★★★★
\r
278 // Debug.Write( "再生中の処理をする()" );
\r
281 public void tサウンドを破棄する( CSound csound )
\r
283 csound.t解放する( true ); // インスタンスは存続→破棄にする。
\r
287 public float GetCPUusage()
\r
290 switch ( SoundDeviceType )
\r
292 case ESoundDeviceType.ExclusiveWASAPI:
\r
293 case ESoundDeviceType.SharedWASAPI:
\r
294 f = BassWasapi.BASS_WASAPI_GetCPU();
\r
296 case ESoundDeviceType.ASIO:
\r
297 f = BassAsio.BASS_ASIO_GetCPU();
\r
299 case ESoundDeviceType.DirectSound:
\r
309 public string GetCurrentSoundDeviceType()
\r
311 switch ( SoundDeviceType )
\r
313 case ESoundDeviceType.ExclusiveWASAPI:
\r
314 case ESoundDeviceType.SharedWASAPI:
\r
316 case ESoundDeviceType.ASIO:
\r
318 case ESoundDeviceType.DirectSound:
\r
319 return "DirectSound";
\r
325 public void AddMixer( CSound cs )
\r
327 cs.tBASSサウンドをミキサーに追加する();
\r
329 public void RemoveMixer( CSound cs )
\r
331 cs.tBASSサウンドをミキサーから削除する();
\r
336 // CSound は、サウンドデバイスが変更されたときも、インスタンスを再作成することなく、新しいデバイスで作り直せる必要がある。
\r
337 // そのため、デバイスごとに別のクラスに分割するのではなく、1つのクラスに集約するものとする。
\r
339 public class CSound : IDisposable, ICloneable
\r
341 #region [ DTXMania用拡張 ]
\r
342 public int n総演奏時間ms
\r
347 public int nサウンドバッファサイズ // 取りあえず0固定★★★★★★★★★★★★★★★★★★★★
\r
351 public bool bストリーム再生する // 取りあえずfalse固定★★★★★★★★★★★★★★★★★★★★
\r
352 // trueにすると同一チップ音の多重再生で問題が出る(4POLY音源として動かない)
\r
354 get { return false; }
\r
356 public double db周波数倍率
\r
364 if ( _db周波数倍率 != value )
\r
367 if ( bBASSサウンドである )
\r
369 Bass.BASS_ChannelSetAttribute( this.hBassStream, BASSAttribute.BASS_ATTRIB_FREQ, ( float ) ( _db周波数倍率 * _db再生速度 * nオリジナルの周波数 ) );
\r
375 this.Buffer.Frequency = ( int ) ( _db周波数倍率 * _db再生速度 * nオリジナルの周波数 );
\r
381 public double db再生速度
\r
389 if ( _db再生速度 != value )
\r
392 if ( bBASSサウンドである )
\r
394 Bass.BASS_ChannelSetAttribute( this.hBassStream, BASSAttribute.BASS_ATTRIB_FREQ, ( float ) ( _db周波数倍率 * _db再生速度 * nオリジナルの周波数 ) );
\r
395 //Bass.BASS_ChannelSetAttribute( CSound管理.hTempoStream, BASSAttribute.BASS_ATTRIB_TEMPO, (float) (db再生速度 * 100 - 100 ) );
\r
401 this.Buffer.Frequency = ( int ) ( _db周波数倍率 * _db再生速度 * nオリジナルの周波数 );
\r
409 private STREAMPROC _cbStreamXA; // make it global, so that the GC can not remove it
\r
410 // private SYNCPROC _cbEndofStream; // ストリームの終端まで再生されたときに呼び出されるコールバック
\r
411 // private WaitCallback _cbRemoveMixerChannel;
\r
414 /// <para>0:最小~100:原音</para>
\r
420 if( this.bBASSサウンドである )
\r
423 if ( !Bass.BASS_ChannelGetAttribute( this.hBassStream, BASSAttribute.BASS_ATTRIB_VOL, ref f音量 ) )
\r
424 //if ( BassMix.BASS_Mixer_ChannelGetEnvelopePos( this.hBassStream, BASSMIXEnvelope.BASS_MIXER_ENV_VOL, ref f音量 ) == -1 )
\r
426 return (int) ( f音量 * 100 );
\r
428 else if( this.bDirectSoundである )
\r
436 if( this.bBASSサウンドである )
\r
438 float f音量 = Math.Min( Math.Max( value, 0 ), 100 ) / 100.0f; // 0~100 → 0.0~1.0
\r
439 //var nodes = new BASS_MIXER_NODE[ 1 ] { new BASS_MIXER_NODE( 0, f音量 ) };
\r
440 //BassMix.BASS_Mixer_ChannelSetEnvelope( this.hBassStream, BASSMIXEnvelope.BASS_MIXER_ENV_VOL, nodes );
\r
441 Bass.BASS_ChannelSetAttribute( this.hBassStream, BASSAttribute.BASS_ATTRIB_VOL, f音量 );
\r
444 else if( this.bDirectSoundである )
\r
448 if( this._n音量 == 0 )
\r
450 this._n音量db = -10000;
\r
454 this._n音量db = (int) ( ( 20.0 * Math.Log10( ( (double) this._n音量 ) / 100.0 ) ) * 100.0 );
\r
457 this.Buffer.Volume = this._n音量db;
\r
463 /// <para>左:-100~中央:0~100:右。set のみ。</para>
\r
469 if( this.bBASSサウンドである )
\r
472 if ( !Bass.BASS_ChannelGetAttribute( this.hBassStream, BASSAttribute.BASS_ATTRIB_PAN, ref f位置 ) )
\r
473 //if( BassMix.BASS_Mixer_ChannelGetEnvelopePos( this.hBassStream, BASSMIXEnvelope.BASS_MIXER_ENV_PAN, ref f位置 ) == -1 )
\r
475 return (int) ( f位置 * 100 );
\r
477 else if( this.bDirectSoundである )
\r
485 if( this.bBASSサウンドである )
\r
487 float f位置 = Math.Min( Math.Max( value, -100 ), 100 ) / 100.0f; // -100~100 → -1.0~1.0
\r
488 //var nodes = new BASS_MIXER_NODE[ 1 ] { new BASS_MIXER_NODE( 0, f位置 ) };
\r
489 //BassMix.BASS_Mixer_ChannelSetEnvelope( this.hBassStream, BASSMIXEnvelope.BASS_MIXER_ENV_PAN, nodes );
\r
490 Bass.BASS_ChannelSetAttribute( this.hBassStream, BASSAttribute.BASS_ATTRIB_PAN, f位置 );
\r
492 else if( this.bDirectSoundである )
\r
494 this._n位置 = Math.Min( Math.Max( -100, value ), 100 ); // -100~100
\r
496 if( this._n位置 == 0 )
\r
500 else if( this._n位置 == -100 )
\r
502 this._n位置db = -10000;
\r
504 else if( this._n位置 == 100 )
\r
506 this._n位置db = 100000;
\r
508 else if( this._n位置 < 0 )
\r
510 this._n位置db = (int) ( ( 20.0 * Math.Log10( ( (double) ( this._n位置 + 100 ) ) / 100.0 ) ) * 100.0 );
\r
514 this._n位置db = (int) ( ( -20.0 * Math.Log10( ( (double) ( 100 - this._n位置 ) ) / 100.0 ) ) * 100.0 );
\r
517 this.Buffer.Pan = this._n位置db;
\r
523 /// <para>DirectSoundのセカンダリバッファ。</para>
\r
525 //public SecondarySoundBuffer DirectSoundBuffer
\r
526 public SoundBuffer DirectSoundBuffer
\r
528 get { return this.Buffer; }
\r
532 /// <para>DirectSoundのセカンダリバッファ作成時のフラグ。</para>
\r
534 public BufferFlags DirectSoundBufferFlags
\r
541 /// <para>全インスタンスリスト。</para>
\r
542 /// <para>~を作成する() で追加され、t解放する() or Dispose() で解放される。</para>
\r
544 public static List<CSound> listインスタンス = new List<CSound>();
\r
546 public static void ShowAllCSoundFiles()
\r
549 foreach ( CSound cs in listインスタンス )
\r
551 Debug.WriteLine( i++.ToString( "d3" ) + ": " + Path.GetFileName( cs.strファイル名 ) );
\r
559 this._db周波数倍率 = 1.0;
\r
560 this._db再生速度 = 1.0;
\r
561 this.DirectSoundBufferFlags = CSoundDeviceDirectSound.DefaultFlags;
\r
562 // this._cbRemoveMixerChannel = new WaitCallback( RemoveMixerChannelLater );
\r
565 public object Clone()
\r
567 if ( !bDirectSoundである )
\r
569 throw new NotImplementedException();
\r
571 CSound clone = (CSound) MemberwiseClone(); // これだけだとCY連打が途切れる&タイトルに戻る際にNullRef例外発生
\r
572 this.DirectSound.DuplicateSoundBuffer( this.Buffer, out clone.Buffer );
\r
574 // CSound.listインスタンス.Add( this ); // インスタンスリストに登録。
\r
575 // 本来これを加えるべきだが、Add後Removeできなくなっている。Clone()の仕方の問題であろう。
\r
579 public void tASIOサウンドを作成する( string strファイル名, int hMixer )
\r
581 this.tBASSサウンドを作成する( strファイル名, hMixer, BASSFlag.BASS_STREAM_DECODE );
\r
582 this.eデバイス種別 = ESoundDeviceType.ASIO; // 作成後に設定する。(作成に失敗してると例外発出されてここは実行されない)
\r
584 public void tASIOサウンドを作成する( byte[] byArrWAVファイルイメージ, int hMixer )
\r
586 this.tBASSサウンドを作成する( byArrWAVファイルイメージ, hMixer, BASSFlag.BASS_STREAM_DECODE );
\r
587 this.eデバイス種別 = ESoundDeviceType.ASIO; // 作成後に設定する。(作成に失敗してると例外発出されてここは実行されない)
\r
589 public void tWASAPIサウンドを作成する( string strファイル名, int hMixer, ESoundDeviceType eデバイス種別 )
\r
591 this.tBASSサウンドを作成する( strファイル名, hMixer, BASSFlag.BASS_STREAM_DECODE | BASSFlag.BASS_SAMPLE_FLOAT );
\r
592 this.eデバイス種別 = eデバイス種別; // 作成後に設定する。(作成に失敗してると例外発出されてここは実行されない)
\r
594 public void tWASAPIサウンドを作成する( byte[] byArrWAVファイルイメージ, int hMixer, ESoundDeviceType eデバイス種別 )
\r
596 this.tBASSサウンドを作成する( byArrWAVファイルイメージ, hMixer, BASSFlag.BASS_STREAM_DECODE | BASSFlag.BASS_SAMPLE_FLOAT );
\r
597 this.eデバイス種別 = eデバイス種別; // 作成後に設定する。(作成に失敗してると例外発出されてここは実行されない)
\r
599 public void tDirectSoundサウンドを作成する( string strファイル名, DirectSound DirectSound )
\r
601 this.e作成方法 = E作成方法.ファイルから;
\r
602 this.strファイル名 = strファイル名;
\r
604 if ( String.Compare( Path.GetExtension( strファイル名 ), ".xa", true ) == 0 ||
\r
605 String.Compare( Path.GetExtension( strファイル名 ), ".mp3", true ) == 0 ||
\r
606 String.Compare( Path.GetExtension( strファイル名 ), ".ogg", true ) == 0 ) // caselessで文字列比較
\r
608 tDirectSoundサウンドを作成するXaOggMp3( strファイル名, DirectSound );
\r
612 // すべてのファイルを DirectShow でデコードすると時間がかかるので、ファイルが WAV かつ PCM フォーマットでない場合のみ DirectShow でデコードする。
\r
614 byte[] byArrWAVファイルイメージ = null;
\r
615 bool bファイルがWAVかつPCMフォーマットである = true;
\r
618 #region [ ファイルがWAVかつPCMフォーマットか否か調べる。]
\r
619 //-----------------
\r
622 using ( var ws = new WaveStream( strファイル名 ) )
\r
624 if ( ws.Format.FormatTag != WaveFormatTag.Pcm )
\r
625 bファイルがWAVかつPCMフォーマットである = false;
\r
630 bファイルがWAVかつPCMフォーマットである = false;
\r
632 //-----------------
\r
635 if ( bファイルがWAVかつPCMフォーマットである )
\r
637 #region [ ファイルを読み込んで byArrWAVファイルイメージへ格納。]
\r
638 //-----------------
\r
639 var fs = File.Open( strファイル名, FileMode.Open, FileAccess.Read );
\r
640 var br = new BinaryReader( fs );
\r
642 byArrWAVファイルイメージ = new byte[ fs.Length ];
\r
643 br.Read( byArrWAVファイルイメージ, 0, (int) fs.Length );
\r
647 //-----------------
\r
652 #region [ DirectShow でデコード変換し、 byArrWAVファイルイメージへ格納。]
\r
653 //-----------------
\r
654 CDStoWAVFileImage.t変換( strファイル名, out byArrWAVファイルイメージ );
\r
655 //-----------------
\r
662 this.tDirectSoundサウンドを作成する( byArrWAVファイルイメージ, DirectSound );
\r
664 public void tDirectSoundサウンドを作成するXaOggMp3( string strファイル名, DirectSound DirectSound )
\r
666 this.e作成方法 = E作成方法.ファイルから;
\r
667 this.strファイル名 = strファイル名;
\r
670 WaveFormat wfx = new WaveFormat();
\r
671 int nPCMデータの先頭インデックス = 0;
\r
672 // int nPCMサイズbyte = (int) ( xa.xaheader.nSamples * xa.xaheader.nChannels * 2 ); // nBytes = Bass.BASS_ChannelGetLength( this.hBassStream );
\r
675 CWin32.WAVEFORMATEX cw32wfx;
\r
676 tオンメモリ方式でデコードする( strファイル名, out this.byArrWAVファイルイメージ,
\r
677 out nPCMデータの先頭インデックス, out nPCMサイズbyte, out cw32wfx );
\r
679 wfx.AverageBytesPerSecond = (int) cw32wfx.nAvgBytesPerSec;
\r
680 wfx.BitsPerSample = (short) cw32wfx.wBitsPerSample;
\r
681 wfx.BlockAlignment = (short) cw32wfx.nBlockAlign;
\r
682 wfx.Channels = (short) cw32wfx.nChannels;
\r
683 wfx.FormatTag = WaveFormatTag.Pcm; // xa.waveformatex.wFormatTag;
\r
684 wfx.SamplesPerSecond = (int) cw32wfx.nSamplesPerSec;
\r
686 // セカンダリバッファを作成し、PCMデータを書き込む。
\r
687 tDirectSoundサウンドを作成する・セカンダリバッファの作成とWAVデータ書き込み
\r
688 ( ref this.byArrWAVファイルイメージ, DirectSound, CSoundDeviceDirectSound.DefaultFlags, wfx,
\r
689 nPCMサイズbyte, nPCMデータの先頭インデックス );
\r
692 public void tDirectSoundサウンドを作成する( byte[] byArrWAVファイルイメージ, DirectSound DirectSound )
\r
694 this.tDirectSoundサウンドを作成する( byArrWAVファイルイメージ, DirectSound, CSoundDeviceDirectSound.DefaultFlags );
\r
696 public void tDirectSoundサウンドを作成する( byte[] byArrWAVファイルイメージ, DirectSound DirectSound, BufferFlags flags )
\r
698 if( this.e作成方法 == E作成方法.Unknown )
\r
699 this.e作成方法 = E作成方法.WAVファイルイメージから;
\r
701 WaveFormat wfx = null;
\r
702 int nPCMデータの先頭インデックス = -1;
\r
703 int nPCMサイズbyte = -1;
\r
705 #region [ byArrWAVファイルイメージ[] から上記3つのデータを取得。]
\r
706 //-----------------
\r
707 var ms = new MemoryStream( byArrWAVファイルイメージ );
\r
708 var br = new BinaryReader( ms );
\r
712 // 'RIFF'+RIFFデータサイズ
\r
714 if( br.ReadUInt32() != 0x46464952 )
\r
715 throw new InvalidDataException( "RIFFファイルではありません。" );
\r
719 if( br.ReadUInt32() != 0x45564157 )
\r
720 throw new InvalidDataException( "WAVEファイルではありません。" );
\r
723 while( ( ms.Position + 8 ) < ms.Length ) // +8 は、チャンク名+チャンクサイズ。残り8バイト未満ならループ終了。
\r
725 uint chunkName = br.ReadUInt32();
\r
728 if( chunkName == 0x20746D66 )
\r
730 long chunkSize = (long) br.ReadUInt32();
\r
732 var tag = (WaveFormatTag) br.ReadUInt16();
\r
734 if( tag == WaveFormatTag.Pcm ) wfx = new WaveFormat();
\r
735 else if( tag == WaveFormatTag.Extensible ) wfx = new SlimDX.Multimedia.WaveFormatExtensible(); // このクラスは WaveFormat を継承している。
\r
737 throw new InvalidDataException( string.Format( "未対応のWAVEフォーマットタグです。(Tag:{0})", tag.ToString() ) );
\r
739 wfx.FormatTag = tag;
\r
740 wfx.Channels = br.ReadInt16();
\r
741 wfx.SamplesPerSecond = br.ReadInt32();
\r
742 wfx.AverageBytesPerSecond = br.ReadInt32();
\r
743 wfx.BlockAlignment = br.ReadInt16();
\r
744 wfx.BitsPerSample = br.ReadInt16();
\r
746 long nフォーマットサイズbyte = 16;
\r
748 if( wfx.FormatTag == WaveFormatTag.Extensible )
\r
750 br.ReadUInt16(); // 拡張領域サイズbyte
\r
751 var wfxEx = (SlimDX.Multimedia.WaveFormatExtensible) wfx;
\r
752 wfxEx.ValidBitsPerSample = br.ReadInt16();
\r
753 wfxEx.ChannelMask = (Speakers) br.ReadInt32();
\r
754 wfxEx.SubFormat = new Guid( br.ReadBytes( 16 ) ); // GUID は 16byte (128bit)
\r
756 nフォーマットサイズbyte += 24;
\r
759 ms.Seek( chunkSize - nフォーマットサイズbyte, SeekOrigin.Current );
\r
764 else if( chunkName == 0x61746164 )
\r
766 nPCMサイズbyte = br.ReadInt32();
\r
767 nPCMデータの先頭インデックス = (int) ms.Position;
\r
769 ms.Seek( nPCMサイズbyte, SeekOrigin.Current );
\r
776 long chunkSize = (long) br.ReadUInt32();
\r
777 ms.Seek( chunkSize, SeekOrigin.Current );
\r
783 throw new InvalidDataException( "fmt チャンクが存在しません。不正なサウンドデータです。" );
\r
784 if( nPCMサイズbyte < 0 )
\r
785 throw new InvalidDataException( "data チャンクが存在しません。不正なサウンドデータです。" );
\r
792 //-----------------
\r
796 // セカンダリバッファを作成し、PCMデータを書き込む。
\r
797 tDirectSoundサウンドを作成する・セカンダリバッファの作成とWAVデータ書き込み(
\r
798 ref byArrWAVファイルイメージ, DirectSound, flags, wfx, nPCMサイズbyte, nPCMデータの先頭インデックス );
\r
801 private void tDirectSoundサウンドを作成する・セカンダリバッファの作成とWAVデータ書き込み
\r
802 ( ref byte[] byArrWAVファイルイメージ, DirectSound DirectSound, BufferFlags flags, WaveFormat wfx,
\r
803 int nPCMサイズbyte, int nPCMデータの先頭インデックス )
\r
805 // セカンダリバッファを作成し、PCMデータを書き込む。
\r
807 this.Buffer = new SecondarySoundBuffer( DirectSound, new SoundBufferDescription()
\r
809 Format = ( wfx.FormatTag == WaveFormatTag.Pcm ) ? wfx : (SlimDX.Multimedia.WaveFormatExtensible) wfx,
\r
811 SizeInBytes = nPCMサイズbyte,
\r
813 this.Buffer.Write( byArrWAVファイルイメージ, nPCMデータの先頭インデックス, nPCMサイズbyte, 0, LockFlags.None );
\r
817 this.eデバイス種別 = ESoundDeviceType.DirectSound;
\r
818 this.DirectSoundBufferFlags = flags;
\r
819 this.byArrWAVファイルイメージ = byArrWAVファイルイメージ;
\r
820 this.DirectSound = DirectSound;
\r
823 this.nオリジナルの周波数 = wfx.SamplesPerSecond;
\r
824 n総演奏時間ms = (int) ( ( (double) nPCMサイズbyte ) / ( this.Buffer.Format.AverageBytesPerSecond * 0.001 ) );
\r
829 CSound.listインスタンス.Add( this );
\r
832 #region [ DTXMania用の変換 ]
\r
834 public void tサウンドを破棄する( CSound cs )
\r
838 public void t再生を開始する()
\r
843 public void t再生を開始する( bool bループする )
\r
845 if ( bBASSサウンドである )
\r
849 Bass.BASS_ChannelFlags( this.hBassStream, BASSFlag.BASS_SAMPLE_LOOP, BASSFlag.BASS_SAMPLE_LOOP );
\r
853 Bass.BASS_ChannelFlags( this.hBassStream, BASSFlag.BASS_DEFAULT, BASSFlag.BASS_DEFAULT );
\r
857 tサウンドを再生する( bループする );
\r
859 public void t再生を停止する()
\r
864 public void t再生を一時停止する()
\r
869 public void t再生を再開する( long t ) // ★★★★★★★★★★★★★★★★★★★★★★★★★★★★
\r
871 Debug.WriteLine( "t再生を再開する(long " + t + ")" );
\r
880 if ( this.bBASSサウンドである )
\r
882 bool ret = ( BassMix.BASS_Mixer_ChannelIsActive( this.hBassStream ) == BASSActive.BASS_ACTIVE_PAUSED ) &
\r
883 ( BassMix.BASS_Mixer_ChannelGetPosition( this.hBassStream ) > 0 );
\r
888 return ( this.n一時停止回数 > 0 );
\r
896 if ( this.eデバイス種別 == ESoundDeviceType.DirectSound )
\r
898 return ( ( this.Buffer.Status & BufferStatus.Playing ) != BufferStatus.None );
\r
902 // 基本的にはBASS_ACTIVE_PLAYINGなら再生中だが、最後まで再生しきったchannelも
\r
903 // BASS_ACTIVE_PLAYINGのままになっているので、小細工が必要。
\r
904 bool ret = ( BassMix.BASS_Mixer_ChannelIsActive( this.hBassStream ) == BASSActive.BASS_ACTIVE_PLAYING );
\r
905 if ( BassMix.BASS_Mixer_ChannelGetPosition( this.hBassStream ) >= nBytes )
\r
913 //public lint t時刻から位置を返す( long t )
\r
915 // double num = ( n時刻 * this.db再生速度 ) * this.db周波数倍率;
\r
916 // return (int) ( ( num * 0.01 ) * this.nSamplesPerSecond );
\r
921 public void t解放する()
\r
926 public void t解放する( bool _bインスタンス削除 )
\r
928 if ( this.bBASSサウンドである ) // stream数の削減用
\r
930 tBASSサウンドをミキサーから削除する();
\r
931 //_cbEndofStream = null;
\r
932 //_cbStreamXA = null;
\r
933 CSound管理.nStreams--;
\r
935 bool bManagedも解放する = true;
\r
936 bool bインスタンス削除 = _bインスタンス削除; // CSoundの再初期化時は、インスタンスは存続する。
\r
937 this.Dispose( bManagedも解放する, bインスタンス削除 );
\r
938 //Debug.WriteLine( "Disposed: " + _bインスタンス削除 + " : " + Path.GetFileName( this.strファイル名 ) );
\r
940 public void tサウンドを再生する()
\r
942 tサウンドを再生する( false );
\r
944 public void tサウンドを再生する( bool bループする )
\r
946 if ( this.bBASSサウンドである ) // BASSサウンド時のループ処理は、t再生を開始する()側に実装。
\r
948 //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
949 bool b = BassMix.BASS_Mixer_ChannelPlay( this.hBassStream );
\r
952 Debug.WriteLine( "再生しようとしたが、Mixerに登録されていなかった: " + Path.GetFileName( this.strファイル名 ) + ", " + hBassStream );
\r
953 //Debug.WriteLine( "ErrCode= " +Bass.BASS_ErrorGetCode() );
\r
955 bool bb = tBASSサウンドをミキサーに追加する();
\r
958 Debug.WriteLine( "Mixerへの登録に失敗: " + Path.GetFileName( this.strファイル名 ) + ": " + Bass.BASS_ErrorGetCode() );
\r
962 //Debug.WriteLine( "Mixerへの登録に成功: " + Path.GetFileName( this.strファイル名 ) + ": " + Bass.BASS_ErrorGetCode() );
\r
964 //this.t再生位置を先頭に戻す();
\r
966 bool bbb = BassMix.BASS_Mixer_ChannelPlay( this.hBassStream );
\r
969 Debug.WriteLine("更に再生に失敗 : " + Path.GetFileName(this.strファイル名));
\r
970 Debug.WriteLine("ErrCode= " + Bass.BASS_ErrorGetCode());
\r
974 // Debug.WriteLine("再生成功(ミキサー追加後) : " + Path.GetFileName(this.strファイル名));
\r
979 //Debug.WriteLine( "再生成功: " + Path.GetFileName( this.strファイル名 ) + " (" + hBassStream + ")" );
\r
982 else if( this.bDirectSoundである )
\r
984 PlayFlags pf = ( bループする ) ? PlayFlags.Looping : PlayFlags.None;
\r
985 this.Buffer.Play( 0, pf );
\r
988 public void tサウンドを先頭から再生する()
\r
990 this.t再生位置を先頭に戻す();
\r
993 public void tサウンドを停止する()
\r
995 tサウンドを停止する( false );
\r
997 public void tサウンドを停止する( bool pause )
\r
999 if( this.bBASSサウンドである )
\r
1001 //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
1002 BassMix.BASS_Mixer_ChannelPause( this.hBassStream );
\r
1005 // tBASSサウンドをミキサーから削除する(); // PAUSEと再生停止を区別できるようにすること!!
\r
1008 else if( this.bDirectSoundである )
\r
1010 this.Buffer.Stop();
\r
1014 public void t再生位置を先頭に戻す()
\r
1016 if( this.bBASSサウンドである )
\r
1018 BassMix.BASS_Mixer_ChannelSetPosition( this.hBassStream, 0 );
\r
1021 else if( this.bDirectSoundである )
\r
1023 this.Buffer.CurrentPlayPosition = 0;
\r
1026 public void t再生位置を変更する( long n位置ms )
\r
1028 if( this.bBASSサウンドである )
\r
1030 BassMix.BASS_Mixer_ChannelSetPosition( this.hBassStream, Bass.BASS_ChannelSeconds2Bytes( this.hBassStream, n位置ms / 1000.0 ), BASSMode.BASS_POS_BYTES );
\r
1032 else if( this.bDirectSoundである )
\r
1034 int n位置sample = (int) ( this.Buffer.Format.SamplesPerSecond * n位置ms * 0.001 );
\r
1035 this.Buffer.CurrentPlayPosition = n位置sample * this.Buffer.Format.BlockAlignment;
\r
1039 public static void tすべてのサウンドを初期状態に戻す()
\r
1041 foreach ( var sound in CSound.listインスタンス )
\r
1043 sound.t解放する( false );
\r
1046 public static void tすべてのサウンドを再構築する( ISoundDevice device )
\r
1048 if( CSound.listインスタンス.Count == 0 )
\r
1052 // サウンドを再生する際にインスタンスリストも更新されるので、配列にコピーを取っておき、リストはクリアする。
\r
1054 var sounds = CSound.listインスタンス.ToArray();
\r
1055 CSound.listインスタンス.Clear();
\r
1058 // 配列に基づいて個々のサウンドを作成する。
\r
1060 for( int i = 0; i < sounds.Length; i++ )
\r
1062 switch( sounds[ i ].e作成方法 )
\r
1064 #region [ ファイルから ]
\r
1065 case E作成方法.ファイルから:
\r
1066 string strファイル名 = sounds[ i ].strファイル名;
\r
1067 sounds[ i ].Dispose( true, false );
\r
1068 device.tサウンドを作成する( strファイル名, ref sounds[ i ] );
\r
1071 #region [ WAVファイルイメージから ]
\r
1072 case E作成方法.WAVファイルイメージから:
\r
1073 if( sounds[ i ].bBASSサウンドである )
\r
1075 byte[] byArrWaveファイルイメージ = sounds[ i ].byArrWAVファイルイメージ;
\r
1076 sounds[ i ].Dispose( true, false );
\r
1077 device.tサウンドを作成する( byArrWaveファイルイメージ, ref sounds[ i ] );
\r
1079 else if( sounds[ i ].bDirectSoundである )
\r
1081 byte[] byArrWaveファイルイメージ = sounds[ i ].byArrWAVファイルイメージ;
\r
1082 var flags = sounds[ i ].DirectSoundBufferFlags;
\r
1083 sounds[ i ].Dispose( true, false );
\r
1084 ( (CSoundDeviceDirectSound) device ).tサウンドを作成する( byArrWaveファイルイメージ, flags, ref sounds[ i ] );
\r
1092 #region [ Dispose-Finalizeパターン実装 ]
\r
1093 //-----------------
\r
1094 public void Dispose()
\r
1096 this.Dispose( true, true );
\r
1097 GC.SuppressFinalize( this );
\r
1099 protected void Dispose( bool bManagedも解放する, bool bインスタンス削除 )
\r
1101 if( this.bBASSサウンドである )
\r
1103 #region [ ASIO, WASAPI の解放 ]
\r
1104 //-----------------
\r
1105 BassMix.BASS_Mixer_ChannelRemove( this.hBassStream );
\r
1106 Bass.BASS_StreamFree( this.hBassStream );
\r
1107 this.hBassStream = -1;
\r
1108 //-----------------
\r
1112 if( bManagedも解放する )
\r
1114 //int freeIndex = -1;
\r
1116 //if ( CSound.listインスタンス != null )
\r
1118 // freeIndex = CSound.listインスタンス.IndexOf( this );
\r
1119 // if ( freeIndex == -1 )
\r
1121 // Debug.WriteLine( "ERR: freeIndex==-1 : Count=" + CSound.listインスタンス.Count + ", filename=" + Path.GetFileName( this.strファイル名 ) );
\r
1125 if( this.eデバイス種別 == ESoundDeviceType.DirectSound )
\r
1127 #region [ DirectSound の解放 ]
\r
1128 //-----------------
\r
1129 if( this.Buffer != null )
\r
1133 this.Buffer.Stop();
\r
1137 // 演奏終了後、長時間解放しないでいると、たまに AccessViolationException が発生することがある。
\r
1139 C共通.tDisposeする( ref this.Buffer );
\r
1141 //-----------------
\r
1145 if( this.e作成方法 == E作成方法.WAVファイルイメージから &&
\r
1146 this.eデバイス種別 != ESoundDeviceType.DirectSound ) // DirectSound は hGC 未使用。
\r
1148 if ( this.hGC != null && this.hGC.IsAllocated )
\r
1151 this.hGC = default( GCHandle );
\r
1154 if ( this.byArrWAVファイルイメージ != null )
\r
1156 this.byArrWAVファイルイメージ = null;
\r
1163 // CSound.listインスタンス.RemoveAt( freeIndex );
\r
1167 // Debug.WriteLine( "FAILED to remove CSound.listインスタンス: Count=" + CSound.listインスタンス.Count + ", filename=" + Path.GetFileName( this.strファイル名 ) );
\r
1169 bool b = CSound.listインスタンス.Remove( this ); // これだと、Clone()したサウンドのremoveに失敗する
\r
1172 Debug.WriteLine( "FAILED to remove CSound.listインスタンス: Count=" + CSound.listインスタンス.Count + ", filename=" + Path.GetFileName( this.strファイル名 ) );
\r
1180 this.Dispose( false, true );
\r
1182 //-----------------
\r
1185 #region [ protected ]
\r
1186 //-----------------
\r
1187 protected enum E作成方法 { ファイルから, WAVファイルイメージから, Unknown }
\r
1188 protected E作成方法 e作成方法 = E作成方法.Unknown;
\r
1189 protected ESoundDeviceType eデバイス種別 = ESoundDeviceType.Unknown;
\r
1190 public string strファイル名 = null;
\r
1191 protected byte[] byArrWAVファイルイメージ = null; // WAVファイルイメージ、もしくはchunkのDATA部のみ
\r
1192 protected GCHandle hGC;
\r
1193 public int hBassStream = -1; // ASIO, WASAPI 用
\r
1194 protected SoundBuffer Buffer = null; // DirectSound 用
\r
1195 protected DirectSound DirectSound;
\r
1196 public int hMixer = -1; // 設計壊してゴメン Mixerに後で登録するときに使う
\r
1197 //-----------------
\r
1200 #region [ private ]
\r
1201 //-----------------
\r
1202 private bool bDirectSoundである
\r
1204 get { return ( this.eデバイス種別 == ESoundDeviceType.DirectSound ); }
\r
1206 private bool bBASSサウンドである
\r
1211 this.eデバイス種別 == ESoundDeviceType.ASIO ||
\r
1212 this.eデバイス種別 == ESoundDeviceType.ExclusiveWASAPI ||
\r
1213 this.eデバイス種別 == ESoundDeviceType.SharedWASAPI );
\r
1216 private int _n位置 = 0;
\r
1217 private int _n位置db;
\r
1218 private int _n音量 = 100;
\r
1219 private int _n音量db;
\r
1220 private long nBytes = 0;
\r
1221 private int n一時停止回数 = 0;
\r
1222 private int nオリジナルの周波数 = 0;
\r
1223 private double _db周波数倍率 = 1.0;
\r
1224 private double _db再生速度 = 1.0;
\r
1226 private void tBASSサウンドを作成する( string strファイル名, int hMixer, BASSFlag flags )
\r
1228 if ( String.Compare( Path.GetExtension( strファイル名 ), ".xa", true ) == 0 ) // caselessで文字列比較
\r
1230 tBASSサウンドを作成するXA( strファイル名, hMixer, flags );
\r
1234 this.e作成方法 = E作成方法.ファイルから;
\r
1235 this.strファイル名 = strファイル名;
\r
1238 // BASSファイルストリームを作成。
\r
1240 this.hBassStream = Bass.BASS_StreamCreateFile( strファイル名, 0, 0, flags );
\r
1241 if( this.hBassStream == 0 )
\r
1242 throw new Exception( string.Format( "サウンドストリームの生成に失敗しました。(BASS_StreamCreateFile)[{0}]", Bass.BASS_ErrorGetCode().ToString() ) );
\r
1244 nBytes = Bass.BASS_ChannelGetLength( this.hBassStream );
\r
1246 tBASSサウンドを作成する・ストリーム生成後の共通処理( hMixer );
\r
1248 private void tBASSサウンドを作成する( byte[] byArrWAVファイルイメージ, int hMixer, BASSFlag flags )
\r
1250 this.e作成方法 = E作成方法.WAVファイルイメージから;
\r
1251 this.byArrWAVファイルイメージ = byArrWAVファイルイメージ;
\r
1252 this.hGC = GCHandle.Alloc( byArrWAVファイルイメージ, GCHandleType.Pinned ); // byte[] をピン留め
\r
1255 // BASSファイルストリームを作成。
\r
1257 this.hBassStream = Bass.BASS_StreamCreateFile( hGC.AddrOfPinnedObject(), 0, byArrWAVファイルイメージ.Length, flags );
\r
1258 if ( this.hBassStream == 0 )
\r
1259 throw new Exception( string.Format( "サウンドストリームの生成に失敗しました。(BASS_StreamCreateFile)[{0}]", Bass.BASS_ErrorGetCode().ToString() ) );
\r
1261 nBytes = Bass.BASS_ChannelGetLength( this.hBassStream );
\r
1263 tBASSサウンドを作成する・ストリーム生成後の共通処理( hMixer );
\r
1265 private void tBASSサウンドを作成するXA( string strファイル名, int hMixer, BASSFlag flags )
\r
1267 int nPCMデータの先頭インデックス;
\r
1268 CWin32.WAVEFORMATEX wfx;
\r
1271 tオンメモリ方式でデコードする( strファイル名, out this.byArrWAVファイルイメージ,
\r
1272 out nPCMデータの先頭インデックス, out totalPCMSize, out wfx );
\r
1274 nBytes = totalPCMSize;
\r
1276 this.e作成方法 = E作成方法.WAVファイルイメージから; //.ファイルから; // 再構築時はデコード後のイメージを流用する&Dispose時にhGCを解放する
\r
1277 this.strファイル名 = strファイル名;
\r
1278 this.hGC = GCHandle.Alloc( this.byArrWAVファイルイメージ, GCHandleType.Pinned ); // byte[] をピン留め
\r
1281 _cbStreamXA = new STREAMPROC( CallbackPlayingXA );
\r
1283 // BASSファイルストリームを作成。
\r
1285 //this.hBassStream = Bass.BASS_StreamCreate( xa.xaheader.nSamplesPerSec, xa.xaheader.nChannels, BASSFlag.BASS_STREAM_DECODE, _myStreamCreate, IntPtr.Zero );
\r
1286 this.hBassStream = Bass.BASS_StreamCreate( (int) wfx.nSamplesPerSec, (int) wfx.nChannels, BASSFlag.BASS_STREAM_DECODE, _cbStreamXA, IntPtr.Zero );
\r
1287 if ( this.hBassStream == 0 )
\r
1290 throw new Exception( string.Format( "サウンドストリームの生成に失敗しました。(BASS_SampleCreate)[{0}]", Bass.BASS_ErrorGetCode().ToString() ) );
\r
1293 tBASSサウンドを作成する・ストリーム生成後の共通処理( hMixer );
\r
1297 private void tBASSサウンドを作成する・ストリーム生成後の共通処理( int hMixer )
\r
1299 CSound管理.nStreams++;
\r
1301 // ミキサーにBASSファイルストリームを追加。
\r
1303 //if ( !BassMix.BASS_Mixer_StreamAddChannel( hMixer, this.hBassStream, BASSFlag.BASS_SPEAKER_FRONT | BASSFlag.BASS_MIXER_PAUSE | BASSFlag.BASS_MIXER_NORAMPIN ) )
\r
1304 //// if ( !tBASSサウンドをミキサーに追加する() )
\r
1307 // throw new Exception( string.Format( "サウンドストリームの生成に失敗しました。(BASS_Mixer_StreamAddChannel)[{0}]", Bass.BASS_ErrorGetCode().ToString() ) );
\r
1309 //CSound管理.nStreams++;
\r
1311 // _cbEndofStream = new SYNCPROC( CallbackEndofStream );
\r
1312 // Bass.BASS_ChannelSetSync( hBassStream, BASSSync.BASS_SYNC_END |BASSSync.BASS_SYNC_MIXTIME, 0, _cbEndofStream, IntPtr.Zero );
\r
1317 CSound.listインスタンス.Add( this );
\r
1319 // n総演奏時間の取得; DTXMania用に追加。
\r
1320 double seconds = Bass.BASS_ChannelBytes2Seconds( this.hBassStream, nBytes );
\r
1321 this.n総演奏時間ms = (int) ( seconds * 1000 );
\r
1323 this.hMixer = hMixer;
\r
1324 float freq = 0.0f;
\r
1325 if ( !Bass.BASS_ChannelGetAttribute( this.hBassStream, BASSAttribute.BASS_ATTRIB_FREQ, ref freq ) )
\r
1328 throw new Exception( string.Format( "サウンドストリームの周波数取得に失敗しました。(BASS_ChannelGetAttribute)[{0}]", Bass.BASS_ErrorGetCode().ToString() ) );
\r
1330 this.nオリジナルの周波数 = (int) freq;
\r
1332 //-----------------
\r
1334 private int pos = 0;
\r
1335 private int CallbackPlayingXA( int handle, IntPtr buffer, int length, IntPtr user )
\r
1337 int bytesread = ( pos + length > Convert.ToInt32(nBytes) ) ? Convert.ToInt32(nBytes) - pos : length;
\r
1339 Marshal.Copy( byArrWAVファイルイメージ, pos, buffer, bytesread );
\r
1342 if ( pos >= nBytes )
\r
1344 // set indicator flag
\r
1345 bytesread |= (int) BASSStreamProc.BASS_STREAMPROC_END;
\r
1353 public bool tBASSサウンドをミキサーから削除する()
\r
1355 return tBASSサウンドをミキサーから削除する( this.hBassStream );
\r
1357 public bool tBASSサウンドをミキサーから削除する( int channel )
\r
1359 bool b = BassMix.BASS_Mixer_ChannelRemove( channel );
\r
1362 Interlocked.Decrement( ref CSound管理.nMixing );
\r
1363 // Debug.WriteLine( "Removed: " + Path.GetFileName( this.strファイル名 ) + " (" + channel + ")" + " MixedStreams=" + CSound管理.nMixing );
\r
1371 public bool tBASSサウンドをミキサーに追加する()
\r
1373 if ( BassMix.BASS_Mixer_ChannelGetMixer( hBassStream ) == 0 )
\r
1375 BASSFlag bf = BASSFlag.BASS_SPEAKER_FRONT | BASSFlag.BASS_MIXER_NORAMPIN; // | BASSFlag.BASS_MIXER_PAUSE;
\r
1376 Interlocked.Increment( ref CSound管理.nMixing );
\r
1378 // preloadされることを期待して、敢えてflagからはBASS_MIXER_PAUSEを外してAddChannelした上で、すぐにPAUSEする
\r
1379 bool b1 = BassMix.BASS_Mixer_StreamAddChannel( this.hMixer, this.hBassStream, bf );
\r
1380 bool b2 = BassMix.BASS_Mixer_ChannelPause( this.hBassStream );
\r
1381 t再生位置を先頭に戻す(); // StreamAddChannelの後で再生位置を戻さないとダメ。逆だと再生位置が変わらない。
\r
1382 // Debug.WriteLine( "Add Mixer: " + Path.GetFileName( this.strファイル名 ) + " (" + hBassStream + ")" + " MixedStreams=" + CSound管理.nMixing );
\r
1388 #region [ tオンメモリ方式でデコードする() ]
\r
1389 public void tオンメモリ方式でデコードする( string strファイル名, out byte[] buffer,
\r
1390 out int nPCMデータの先頭インデックス, out int totalPCMSize, out CWin32.WAVEFORMATEX wfx )
\r
1392 nPCMデータの先頭インデックス = 0;
\r
1393 //int nPCMサイズbyte = (int) ( xa.xaheader.nSamples * xa.xaheader.nChannels * 2 ); // nBytes = Bass.BASS_ChannelGetLength( this.hBassStream );
\r
1395 SoundDecoder sounddecoder;
\r
1397 if ( String.Compare( Path.GetExtension( strファイル名 ), ".xa", true ) == 0 )
\r
1399 sounddecoder = new Cxa();
\r
1401 else if ( String.Compare( Path.GetExtension( strファイル名 ), ".ogg", true ) == 0 )
\r
1403 sounddecoder = new Cogg();
\r
1405 else if ( String.Compare( Path.GetExtension( strファイル名 ), ".mp3", true ) == 0 )
\r
1407 sounddecoder = new Cmp3();
\r
1411 throw new NotImplementedException();
\r
1414 int nHandle = sounddecoder.Open( strファイル名 );
\r
1415 if ( nHandle < 0 )
\r
1417 throw new Exception( string.Format( "Open() に失敗しました。({0})({1})", nHandle, strファイル名 ) );
\r
1419 wfx = new CWin32.WAVEFORMATEX();
\r
1420 if ( sounddecoder.GetFormat( nHandle, ref wfx ) < 0 )
\r
1422 sounddecoder.Close( nHandle );
\r
1423 throw new Exception( string.Format( "GetFormat() に失敗しました。({0})", strファイル名 ) );
\r
1425 //totalPCMSize = (int) sounddecoder.nTotalPCMSize; // tデコード後のサイズを調べる()で既に取得済みの値を流用する。ms単位の高速化だが、チップ音がたくさんあると塵積で結構効果がある
\r
1426 totalPCMSize = (int) sounddecoder.GetTotalPCMSize( nHandle );
\r
1427 if ( totalPCMSize == 0 )
\r
1429 sounddecoder.Close( nHandle );
\r
1430 throw new Exception( string.Format( "GetTotalPCMSize() に失敗しました。({0})", strファイル名 ) );
\r
1432 totalPCMSize += ( ( totalPCMSize % 2 ) != 0 ) ? 1 : 0;
\r
1433 buffer = new byte[ totalPCMSize ];
\r
1434 GCHandle handle = GCHandle.Alloc( buffer, GCHandleType.Pinned );
\r
1437 if ( sounddecoder.Decode( nHandle, handle.AddrOfPinnedObject(), (uint) totalPCMSize, 0 ) < 0 )
\r
1440 throw new Exception( string.Format( "デコードに失敗しました。({0})", strファイル名 ) );
\r
1446 sounddecoder.Close( nHandle );
\r
1447 sounddecoder = null;
\r