public void Initialize(Stream stream, AudioInfo info, AudioMetadata metadata, SettingDictionary settings) { _bitsPerSample = info.BitsPerSample; _bytesPerSample = (int)Math.Ceiling(info.BitsPerSample / 8.0); _writer = new RiffWriter(stream); // Pre-allocate the entire stream to avoid fragmentation var estimatedSize = 44 + info.FrameCount * info.Channels * _bytesPerSample; estimatedSize += estimatedSize % 2; stream.SetLength(estimatedSize); _writer.Initialize("WAVE"); WriteFmtChunk(info); // ReSharper disable once PossibleNullReferenceException _writer.BeginChunk("data"); }