Example #1
0
        /// <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);
        }
Example #2
0
        /// <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);
        }