public static WaveFileHeader ReadWaveData(byte[] waveData) { WaveFileHeader waveHeader = new WaveFileHeader(); using (MemoryStream fs = new MemoryStream(waveData)) { waveReaderCore(waveHeader, fs); } return(waveHeader); }
private static void waveReaderCore(WaveFileHeader waveHeader, Stream fs) { BinaryReader br = new BinaryReader(fs); waveHeader.RiffHeader = Encoding.GetEncoding(20127).GetString(br.ReadBytes(4)); waveHeader.FileSize = BitConverter.ToInt32(br.ReadBytes(4), 0); waveHeader.WaveHeaderData = Encoding.GetEncoding(20127).GetString(br.ReadBytes(4)); bool readFmtChunk = false; bool readDataChunk = false; while (!readFmtChunk || !readDataChunk) { // ChunkIDを取得する string chunk = Encoding.GetEncoding(20127).GetString(br.ReadBytes(4)); if (chunk.ToLower().CompareTo("fmt ") == 0) { // fmtチャンクの読み込み waveHeader.FormatChunk = chunk; waveHeader.FormatChunkSize = BitConverter.ToInt32(br.ReadBytes(4), 0); waveHeader.FormatID = BitConverter.ToInt16(br.ReadBytes(2), 0); waveHeader.Channel = BitConverter.ToInt16(br.ReadBytes(2), 0); waveHeader.SampleRate = BitConverter.ToInt32(br.ReadBytes(4), 0); waveHeader.BytePerSec = BitConverter.ToInt32(br.ReadBytes(4), 0); waveHeader.BlockSize = BitConverter.ToInt16(br.ReadBytes(2), 0); waveHeader.BitPerSample = BitConverter.ToInt16(br.ReadBytes(2), 0); readFmtChunk = true; } else if (chunk.ToLower().CompareTo("data") == 0) { // dataチャンクの読み込み waveHeader.DataChunk = chunk; waveHeader.DataChunkSize = BitConverter.ToInt32(br.ReadBytes(4), 0); waveHeader.Data = br.ReadBytes(waveHeader.DataChunkSize); // 再生時間を算出する int bytesPerSec = waveHeader.SampleRate * waveHeader.BlockSize; waveHeader.PlayTimeMsec = (int)(((double)waveHeader.DataChunkSize / (double)bytesPerSec) * 1000); readDataChunk = true; } else { // 不要なチャンクの読み捨て Int32 size = BitConverter.ToInt32(br.ReadBytes(4), 0); if (0 < size) { br.ReadBytes(size); } } } }
public static WaveFileHeader ReadWaveFile(string waveFilePath) { // ファイルの存在を確認する if (!File.Exists(waveFilePath)) { return(null); } WaveFileHeader waveHeader = new WaveFileHeader(); using (FileStream fs = new FileStream(waveFilePath, FileMode.Open, FileAccess.Read, FileShare.Read)) { waveReaderCore(waveHeader, fs); } return(waveHeader); }