public void Pack(string file, WaveCompression compression, WaveEncryption encryption) { using (var stream = new FileStream(file, FileMode.Create)) { Pack(stream, compression, encryption); } }
private Stream CreateCompressionStream(Stream stream, WaveCompression type, CompressionMode mode) { switch (type) { case WaveCompression.Uncompressed: return(stream); case WaveCompression.GZip: return(new GZipStream(stream, mode, true)); case WaveCompression.DEFLATE: return(new DeflateStream(stream, mode, true)); case WaveCompression.LZMA: return(mode == CompressionMode.Compress ? LzmaStream.Encode(stream) : LzmaStream.Decode(stream)); case WaveCompression.Bzip2: return(mode == CompressionMode.Compress ? Bzip2Stream.Output(new SkipStream(stream, 2), false, 9) : Bzip2Stream.Input(stream, true)); case WaveCompression.Flac: return(new FlacStream(stream, true, BitsPerSample)); default: throw new CustomException("This compression method is not supported."); } }
public void Pack(Stream output, WaveCompression compression, WaveEncryption encryption) { Pack(new BinaryWriter(output), compression, encryption); }
public void Pack(BinaryWriter writer, WaveCompression compression, WaveEncryption encryption) { byte[] cdata; using (var buffer = new MemoryStream()) { var enc = CreateEncryptionStream(buffer, encryption, CryptoStreamMode.Write); if (compression == WaveCompression.LZMA) //needs buffer { using (var compbuffer = new MemoryStream()) { using (var comp = CreateCompressionStream(compbuffer, compression, CompressionMode.Compress)) { comp.Write(RawData, 0, RawData.Length); } compbuffer.Position = 0; compbuffer.CopyTo(enc); } } else { using (var comp = CreateCompressionStream(enc, compression, CompressionMode.Compress)) { comp.Write(RawData, 0, RawData.Length); } } enc.Close(); cdata = buffer.ToArray(); } short cbSize = 2 + 4 + 16 + 4; int fmtSize = 16 + 2 + cbSize; int riffSize = 4 + 8 + fmtSize + 8 + 4 + AdditionalChunks.Sum(c => 8 + c.Value.Length) + 8 + cdata.Length; writer.Write("RIFF".ToCharArray()); writer.Write(riffSize); writer.Write("WAVE".ToCharArray()); writer.Write("fmt ".ToCharArray()); writer.Write(fmtSize); writer.Write((ushort)0xFFFE); writer.Write(NumChannels); writer.Write(SampleRate); writer.Write((int)Math.Round(cdata.Length / ((float)NumSamples / SampleRate))); writer.Write(BlockAlign); writer.Write(BitsPerSample); writer.Write(cbSize); writer.Write((short)0); writer.Write(0); //writer.Write(FormatGuids[compressionMethod].ToByteArray()); writer.Write(Version1Guid.ToByteArray()); writer.Write((byte)compression); writer.Write((byte)encryption); writer.Write(Format); writer.Write("fact".ToCharArray()); writer.Write(4); writer.Write(NumSamples); foreach (var chunk in AdditionalChunks) { writer.Write(chunk.Key.ToCharArray()); writer.Write(chunk.Value.Length); writer.Write(chunk.Value); } writer.Write("data".ToCharArray()); writer.Write(cdata.Length); writer.Write(cdata); }