/// <summary> /// Reads the "fmt " sub chunk. Should start at offset 12 (in bytes). /// </summary> /// <param name="stream"></param> private void ReadFmtSubChunk(Stream stream) { int fmtSubChunkSize = 24; // Reads only 16 bytes. The rest are irrelevant for us. stream.Seek(12, SeekOrigin.Begin); byte[] fmtSubChunkBytes = new byte[fmtSubChunkSize]; stream.Read(fmtSubChunkBytes, 0, fmtSubChunkSize); int indexReader = 0; CheckExcpectedBytes(fmtSubChunkBytes.SubArray(indexReader, 4, false), FMT_STR); indexReader += 4; mSubChunk1Size = (int)BitConvertorWrapper.ConvertByteArrayToInt( fmtSubChunkBytes.SubArray(indexReader, sizeof(int), true), BitConvertorWrapper.IntType.Int32); indexReader += sizeof(int); mAudioFormat = (short)BitConvertorWrapper.ConvertByteArrayToInt( fmtSubChunkBytes.SubArray(indexReader, sizeof(short), true), BitConvertorWrapper.IntType.Int16); indexReader += sizeof(short); NumChannels = (short)BitConvertorWrapper.ConvertByteArrayToInt( fmtSubChunkBytes.SubArray(indexReader, sizeof(short), true), BitConvertorWrapper.IntType.Int16); indexReader += sizeof(short); SampleRate = (int)BitConvertorWrapper.ConvertByteArrayToInt( fmtSubChunkBytes.SubArray(indexReader, sizeof(int), true), BitConvertorWrapper.IntType.Int32); indexReader += sizeof(int); int byteRate = (int)BitConvertorWrapper.ConvertByteArrayToInt( fmtSubChunkBytes.SubArray(indexReader, sizeof(int), true), BitConvertorWrapper.IntType.Int32); indexReader += sizeof(int); short bytesPerSample = (short)BitConvertorWrapper.ConvertByteArrayToInt( fmtSubChunkBytes.SubArray(indexReader, sizeof(short), true), BitConvertorWrapper.IntType.Int16); indexReader += sizeof(short); BitsPerSample = (short)BitConvertorWrapper.ConvertByteArrayToInt( fmtSubChunkBytes.SubArray(indexReader, sizeof(short), true), BitConvertorWrapper.IntType.Int16); CheckCalculateableVariables(byteRate, bytesPerSample); }
private void ReadRiffChunkDescriptor(Stream stream) { int riffChunkSize = 12; stream.Seek(0, SeekOrigin.Begin); byte[] fmtSubChunkBytes = new byte[riffChunkSize]; stream.Read(fmtSubChunkBytes, 0, riffChunkSize); CheckExcpectedBytes(fmtSubChunkBytes.SubArray(0, 4, false), RIFF_STR); // Must change the reading order for little endien bytes. mChunkSize = (int)BitConvertorWrapper.ConvertByteArrayToInt( fmtSubChunkBytes.SubArray <byte>(4, sizeof(int), true), BitConvertorWrapper.IntType.Int32); CheckExcpectedBytes(fmtSubChunkBytes.SubArray(8, 4, false), WAVE_STR); }
/// <summary> /// Maybe there is another metadata (Example: LIST, INFO...) that we are ignoring. /// </summary> /// <param name="stream"></param> private void ReadDataSubChunk(Stream stream) { stream.Seek(mSubChunk1Size + 20, SeekOrigin.Begin); byte[] dataSubChunkBytes = new byte[mChunkSize - mSubChunk1Size - 3 * 4]; stream.Read(dataSubChunkBytes, 0, dataSubChunkBytes.Length); int?offsetInArr = GetStringOffset(dataSubChunkBytes, Encoding.UTF8.GetBytes(DATA_STR)); if (!offsetInArr.HasValue) { throw new InvalidDataException($"{DATA_STR} was not found"); } DataSizeInBytes = (int)BitConvertorWrapper.ConvertByteArrayToInt( dataSubChunkBytes.SubArray(offsetInArr.Value + 4, sizeof(int), true), BitConvertorWrapper.IntType.Int32); SoundData = dataSubChunkBytes.SubArray( offsetInArr.Value + 8, DataSizeInBytes, false); }