void span(Span.Type type, long start, long length) { Span s = new Span(start, length); s.type = type; spans.Add(s); }
/// <summary> /// attempts to find a match for the provided data in the library. /// if it is present, it writes the long pointer to the location record in the library /// if it is absent, it adds a new record to the library and returns a pointer to that /// </summary> void matchOrAdd(BinaryWriter outfile, byte[] data, Span.Type spanType) { foreach (LibraryBlob lb in libraryBlobs) { if (areBuffersEqual(data, lb.blob)) { //if(lb.blob.Length > 1000) Console.WriteLine("{0} long match!",lb.blob.Length); outfile.Write(lb.start); return; } } MemoryStream mstemp = new MemoryStream(); BinaryWriter bw = new BinaryWriter(mstemp); //didnt find a match.. add to library //perhaps encode the blob if ((spanType & Span.Type.Flac) != 0) { byte[] flacdata = FLACDotNet.FLACWriter.Encode(data, (spanType & Span.Type.Bits16) != 0 ? 16 : 8, (spanType & Span.Type.Stereo) != 0 ? 2 : 1, (spanType & Span.Type.StereoPlanar) != 0, (spanType & Span.Type.Unsigned) != 0, (spanType & Span.Type.XMDelta) != 0 ); //sometimes flac will do a bad job. write the raw bytes out in that case if (flacdata.Length >= data.Length) { Console.WriteLine("Dumping {0} raw sample bytes with bad flac performance", data.Length); bw.Write((byte)0); bw.Flush(); //write the encoding type mstemp.Write(data, 0, data.Length); } else { bw.Write((byte)spanType); bw.Flush(); //write the encoding type bw.Write(flacdata.Length); bw.Flush(); //write the encoded length mstemp.Write(flacdata, 0, flacdata.Length); Console.WriteLine("Flac [{0}]: {1} ({2})", spanType, (decimal)flacdata.Length / (decimal)data.Length, data.Length); } } else { bw.Write((byte)0); bw.Flush(); //write the encoding type mstemp.Write(data, 0, data.Length); } LibraryBlob newBlob = new LibraryBlob(); newBlob.start = libraryCursor; newBlob.blob = (byte[])data.Clone(); newBlob.blob_encoded = mstemp.ToArray();; libraryCursor += newBlob.blob_encoded.Length; libraryBlobs.Add(newBlob); outfile.Write(newBlob.start); }
public unsafe void processWav(string fname) { spans.Clear(); using (FileStream fsin = new FileStream(fname, FileMode.Open, FileAccess.Read, FileShare.Read)) { BinaryReader br = new BinaryReader(fsin); WAVHDR hdr; hdr.riff = br.ReadInt32(); hdr.len1 = br.ReadInt32(); hdr.wave = br.ReadInt32(); hdr.fmt = br.ReadInt32(); hdr.len2 = br.ReadInt32(); hdr.wFormatTag = br.ReadUInt16(); hdr.wChannels = br.ReadUInt16(); hdr.dwSamplesPerSec = br.ReadUInt32(); hdr.dwAvgBytesPerSec = br.ReadUInt32(); hdr.wBlockAlign = br.ReadUInt16(); hdr.wBitsPerSample = br.ReadUInt16(); hdr.data = br.ReadInt32(); hdr.len3 = br.ReadInt32(); if (hdr.len1 == 0 || hdr.riff != 0x46464952 || hdr.wave != 0x45564157 || hdr.len2 != 16 || hdr.fmt != 0x20746D66 || hdr.wFormatTag != 1 || (hdr.wChannels != 1 && hdr.wChannels != 2) || (hdr.wBitsPerSample != 8 && hdr.wBitsPerSample != 16) || hdr.data != 0x61746164) { Console.WriteLine(" * Unknown wave format - packing uncompressed into library"); goto bail; } Span.Type bits = hdr.wBitsPerSample == 16?Span.Type.Bits16:Span.Type.Unsigned; Span.Type stereo = hdr.wChannels == 2?Span.Type.Stereo:Span.Type.None; span(Span.Type.Flac | bits | stereo, fsin.Position, hdr.len3); //if(bcompr) { } else // if(b16) // if(bstereo) span(Span.Type.Flac | Span.Type.Stereo | Span.Type.Bits16, ptr, length * 4); // else span(Span.Type.Flac | Span.Type.Bits16, ptr, length * 2); // else // if(bstereo) span(Span.Type.Flac | Span.Type.Stereo, ptr, length * 2); // else span(Span.Type.Flac, ptr, length); process(fname, fsin); } return; bail: processSimple(fname); }