OSDN Git Service

Merge branch 'feature/#36529_SlimDXからSharpDXへの移行' into develop
[dtxmania/dtxmania.git] / FDK17プロジェクト / コード / 03.サウンド / CSound.cs
index 93d495d..4a21da8 100644 (file)
@@ -6,9 +6,9 @@ using System.Runtime.InteropServices;
 using System.IO;\r
 using System.Runtime.CompilerServices;\r
 using System.Threading;\r
-using SlimDX;\r
-using SlimDX.DirectSound;\r
-using SlimDX.Multimedia;\r
+using SharpDX;\r
+using SharpDX.DirectSound;\r
+using SharpDX.Multimedia;\r
 using Un4seen.Bass;\r
 using Un4seen.BassAsio;\r
 using Un4seen.BassWasapi;\r
@@ -742,7 +742,7 @@ namespace FDK
                                throw new NotImplementedException();\r
                        }\r
                        CSound clone = (CSound) MemberwiseClone();      // これだけだとCY連打が途切れる&タイトルに戻る際にNullRef例外発生\r
-                       this.DirectSound.DuplicateSoundBuffer( this.Buffer, out clone.Buffer );\r
+                       clone.Buffer = this.DirectSound.DuplicateSoundBuffer( this.Buffer );\r
 \r
                        // CSound.listインスタンス.Add( this );                   // インスタンスリストに登録。\r
                        // 本来これを加えるべきだが、Add後Removeできなくなっている。Clone()の仕方の問題であろう。\r
@@ -791,9 +791,9 @@ namespace FDK
                                //-----------------\r
                                try\r
                                {\r
-                                       using ( var ws = new WaveStream( strファイル名 ) )\r
+                                       using( var ws = new SoundStream( new FileStream( strファイル名, FileMode.Open ) ) )\r
                                        {\r
-                                               if ( ws.Format.FormatTag != WaveFormatTag.Pcm )\r
+                                               if( ws.Format.Encoding != WaveFormatEncoding.Pcm )\r
                                                        bファイルがWAVかつPCMフォーマットである = false;\r
                                        }\r
                                }\r
@@ -839,7 +839,6 @@ namespace FDK
                        this.strファイル名 = strファイル名;\r
 \r
 \r
-                       WaveFormat wfx = new WaveFormat();\r
                        int nPCMデータの先頭インデックス = 0;\r
 //                     int nPCMサイズbyte = (int) ( xa.xaheader.nSamples * xa.xaheader.nChannels * 2 );     // nBytes = Bass.BASS_ChannelGetLength( this.hBassStream );\r
 \r
@@ -847,13 +846,7 @@ namespace FDK
                        CWin32.WAVEFORMATEX cw32wfx;\r
                        tオンメモリ方式でデコードする( strファイル名, out this.byArrWAVファイルイメージ,\r
                        out nPCMデータの先頭インデックス, out nPCMサイズbyte, out cw32wfx, false );\r
-\r
-                       wfx.AverageBytesPerSecond = (int) cw32wfx.nAvgBytesPerSec;\r
-                       wfx.BitsPerSample = (short) cw32wfx.wBitsPerSample;\r
-                       wfx.BlockAlignment = (short) cw32wfx.nBlockAlign;\r
-                       wfx.Channels = (short) cw32wfx.nChannels;\r
-                       wfx.FormatTag = WaveFormatTag.Pcm;      // xa.waveformatex.wFormatTag;\r
-                       wfx.SamplesPerSecond = (int) cw32wfx.nSamplesPerSec;\r
+                       WaveFormat wfx = WaveFormat.CreateCustomFormat( WaveFormatEncoding.Pcm, (int) cw32wfx.nSamplesPerSec, (int) cw32wfx.nChannels, (int) cw32wfx.nAvgBytesPerSec, (int) cw32wfx.nBlockAlign, (int) cw32wfx.wBitsPerSample );\r
 \r
                        // セカンダリバッファを作成し、PCMデータを書き込む。\r
                        tDirectSoundサウンドを作成する_セカンダリバッファの作成とWAVデータ書き込み\r
@@ -901,29 +894,34 @@ namespace FDK
                                        {\r
                                                long chunkSize = (long) br.ReadUInt32();\r
 \r
-                                               var tag = (WaveFormatTag) br.ReadUInt16();\r
+                                               var tag = (WaveFormatEncoding) br.ReadUInt16();\r
+                                               var channels = br.ReadInt16();\r
+                                               var samplesPerSecond = br.ReadInt32();\r
+                                               var averageBytesPerSecond = br.ReadInt32();\r
+                                               var blockAlignment = br.ReadInt16();\r
+                                               var bitsPerSample = br.ReadInt16();\r
 \r
-                                               if( tag == WaveFormatTag.Pcm ) wfx = new WaveFormat();\r
-                                               else if( tag == WaveFormatTag.Extensible ) wfx = new SlimDX.Multimedia.WaveFormatExtensible();  // このクラスは WaveFormat を継承している。\r
+                                               if( tag == WaveFormatEncoding.Pcm )\r
+                                               {\r
+                                                       wfx = WaveFormat.CreateCustomFormat( tag, samplesPerSecond, channels, averageBytesPerSecond, blockAlignment, bitsPerSample );\r
+                                               }\r
+                                               else if( tag == WaveFormatEncoding.Extensible )\r
+                                               {\r
+                                                       wfx = SharpDX.Multimedia.WaveFormatExtensible.CreateCustomFormat( // このクラスは WaveFormat を継承している。\r
+                                                               tag, samplesPerSecond, channels, averageBytesPerSecond, blockAlignment, bitsPerSample );\r
+                                               }\r
                                                else\r
                                                        throw new InvalidDataException( string.Format( "未対応のWAVEフォーマットタグです。(Tag:{0})", tag.ToString() ) );\r
 \r
-                                               wfx.FormatTag = tag;\r
-                                               wfx.Channels = br.ReadInt16();\r
-                                               wfx.SamplesPerSecond = br.ReadInt32();\r
-                                               wfx.AverageBytesPerSecond = br.ReadInt32();\r
-                                               wfx.BlockAlignment = br.ReadInt16();\r
-                                               wfx.BitsPerSample = br.ReadInt16();\r
-\r
                                                long nフォーマットサイズbyte = 16;\r
 \r
-                                               if( wfx.FormatTag == WaveFormatTag.Extensible )\r
+                                               if( wfx.Encoding == WaveFormatEncoding.Extensible )\r
                                                {\r
-                                                       br.ReadUInt16();        // 拡張領域サイズbyte\r
-                                                       var wfxEx = (SlimDX.Multimedia.WaveFormatExtensible) wfx;\r
-                                                       wfxEx.ValidBitsPerSample = br.ReadInt16();\r
+                                                       br.ReadUInt16();    // 拡張領域サイズbyte\r
+                                                       var wfxEx = (SharpDX.Multimedia.WaveFormatExtensible) wfx;\r
+                                                       /*wfxEx.ValidBitsPerSample = */br.ReadInt16(); // 対応するメンバがない?\r
                                                        wfxEx.ChannelMask = (Speakers) br.ReadInt32();\r
-                                                       wfxEx.SubFormat = new Guid( br.ReadBytes( 16 ) );       // GUID は 16byte (128bit)\r
+                                                       wfxEx.GuidSubFormat = new Guid( br.ReadBytes( 16 ) );   // GUID は 16byte (128bit)\r
 \r
                                                        nフォーマットサイズbyte += 24;\r
                                                }\r
@@ -976,11 +974,13 @@ namespace FDK
                {\r
                        // セカンダリバッファを作成し、PCMデータを書き込む。\r
 \r
+                       this._Format = wfx;\r
+\r
                        this.Buffer = new SecondarySoundBuffer( DirectSound, new SoundBufferDescription()\r
                        {\r
-                               Format = ( wfx.FormatTag == WaveFormatTag.Pcm ) ? wfx : (SlimDX.Multimedia.WaveFormatExtensible) wfx,\r
+                               Format = this._Format,\r
                                Flags = flags,\r
-                               SizeInBytes = nPCMサイズbyte,\r
+                               BufferBytes = nPCMサイズbyte,\r
                        } );\r
                        this.Buffer.Write( byArrWAVファイルイメージ, nPCMデータの先頭インデックス, nPCMサイズbyte, 0, LockFlags.None );\r
 \r
@@ -992,12 +992,10 @@ namespace FDK
                        this.DirectSound = DirectSound;\r
 \r
                        // DTXMania用に追加\r
-                       this.nオリジナルの周波数 = wfx.SamplesPerSecond;\r
-                       n総演奏時間ms = (int) ( ( (double) nPCMサイズbyte ) / ( this.Buffer.Format.AverageBytesPerSecond * 0.001 ) );\r
-\r
+                       this.nオリジナルの周波数 = wfx.SampleRate;\r
+                       n総演奏時間ms = (int) ( ( (double) nPCMサイズbyte ) / ( this._Format.AverageBytesPerSecond * 0.001 ) );\r
 \r
                        // インスタンスリストに登録。\r
-\r
                        CSound.listインスタンス.Add( this );\r
                }\r
 \r
@@ -1067,7 +1065,7 @@ namespace FDK
                        {\r
                                if ( this.eデバイス種別 == ESoundDeviceType.DirectSound )\r
                                {\r
-                                       return ( ( this.Buffer.Status & BufferStatus.Playing ) != BufferStatus.None );\r
+                                       return ( ( this.Buffer.Status & (int) BufferStatus.Playing ) != (int) BufferStatus.None );\r
                                }\r
                                else\r
                                {\r
@@ -1207,7 +1205,7 @@ Debug.WriteLine("更に再生に失敗: " + Path.GetFileName(this.strファイ
                        }\r
                        else if( this.bDirectSoundである )\r
                        {\r
-                               this.Buffer.CurrentPlayPosition = 0;\r
+                               this.Buffer.CurrentPosition = 0;\r
                        }\r
                }\r
                public void t再生位置を変更する( long n位置ms )\r
@@ -1238,12 +1236,12 @@ Debug.WriteLine("更に再生に失敗: " + Path.GetFileName(this.strファイ
                        }\r
                        else if( this.bDirectSoundである )\r
                        {\r
-                               int n位置sample = (int) ( this.Buffer.Format.SamplesPerSecond * n位置ms * 0.001 * _db周波数倍率 * _db再生速度 );   // #30839 2013.2.24 yyagi; add _db周波数倍率 and _db再生速度\r
+                               int n位置sample = (int) ( this._Format.SampleRate * n位置ms * 0.001 * _db周波数倍率 * _db再生速度 );  // #30839 2013.2.24 yyagi; add _db周波数倍率 and _db再生速度\r
                                try\r
                                {\r
-                                       this.Buffer.CurrentPlayPosition = n位置sample * this.Buffer.Format.BlockAlignment;\r
+                                       this.Buffer.CurrentPosition = n位置sample * this._Format.BlockAlign;\r
                                }\r
-                               catch ( DirectSoundException e )\r
+                               catch ( Exception e )\r
                                {\r
                                        Trace.TraceError( "{0}: Seek error: {1}", Path.GetFileName( this.strファイル名 ), n位置ms, e.Message );\r
                                }\r
@@ -1267,8 +1265,9 @@ Debug.WriteLine("更に再生に失敗: " + Path.GetFileName(this.strファイ
                        }\r
                        else if ( this.bDirectSoundである )\r
                        {\r
-                               n位置byte = this.Buffer.CurrentPlayPosition;\r
-                               db位置ms = n位置byte / this.Buffer.Format.SamplesPerSecond / 0.001 / _db周波数倍率 / _db再生速度;\r
+                               this.Buffer.GetCurrentPosition( out int pos, out _ );\r
+                               n位置byte = (long) pos;\r
+                               db位置ms = n位置byte / this._Format.SampleRate / 0.001 / _db周波数倍率 / _db再生速度;\r
                        }\r
                        else\r
                        {\r
@@ -1494,6 +1493,7 @@ Debug.WriteLine("更に再生に失敗: " + Path.GetFileName(this.strファイ
                private double _db周波数倍率 = 1.0;\r
                private double _db再生速度 = 1.0;\r
                private bool bIs1倍速再生 = true;\r
+               private WaveFormat _Format;\r
 \r
                private void tBASSサウンドを作成する( string strファイル名, int hMixer, BASSFlag flags )\r
                {\r
@@ -1565,10 +1565,10 @@ Debug.WriteLine("更に再生に失敗: " + Path.GetFileName(this.strファイ
                        //-----------------\r
                        try\r
                        {\r
-                               using ( var ws = new WaveStream( strファイル名 ) )\r
+                               using( var ws = new SoundStream( new FileStream( strファイル名, FileMode.Open ) ) )\r
                                {\r
-                                       if ( ws.Format.FormatTag == (WaveFormatTag) 0x6770 ||   // Ogg Vorbis Mode 2+\r
-                                                ws.Format.FormatTag == (WaveFormatTag) 0x6771 )        // Ogg Vorbis Mode 3+\r
+                                       if( ws.Format.Encoding == WaveFormatEncoding.OggVorbisMode2Plus ||\r
+                                               ws.Format.Encoding == WaveFormatEncoding.OggVorbisMode3Plus )\r
                                        {\r
                                                Trace.TraceInformation( Path.GetFileName( strファイル名 ) + ": RIFF chunked Vorbis. Decode to raw Wave first, to avoid BASS.DLL troubles" );\r
                                                try\r