예제 #1
0
        private void RefreshFrame(FlacStream inStream, ref Frame frame, long offset)
        {
            inStream_.Reader.Seek(offset);

            frame = new Frame();
            frame.ReadHeader(inStream);
            frame.ReadData(inStream);
            frame.ReadFooter(inStream);
        }
예제 #2
0
        public void Decode()
        {
            signature_ = new Signature();
            signature_.Read(this);

            streamInfo_ = MetadataBlock.New(this) as MetadataBlockStreamInfo;
            Validation.IsValid(streamInfo_ != null);

            MetadataBlock block = streamInfo_;
            if (MetadataRead != null)
            {
                MetadataRead(streamInfo_);
            }

            while (!block.Header.IsLastMetadataBlock)
            {
                block = MetadataBlock.New(this);
                if (MetadataRead != null)
                {
                    MetadataRead(block);
                }
            }

            while (!reader_.Done)
            {
                Frame frame = new Frame();
                long offset = reader_.Debug_BytesRead;
                frame.ReadHeader(this);
                bool haveRead = false;
                if (BeforeFrameDataRead != null)
                {
                    FrameCallbackArgs a = new FrameCallbackArgs(frame, this);
                    a.Offset = offset;
                    BeforeFrameDataRead(a);
                    if (!a.ContinueDecoding)
                    {
                        break;
                    }
                    haveRead = a.HaveReadData;
                }
                if (!haveRead)
                {
                    frame.ReadData(this);
                    frame.ReadFooter(this);
                }
            }
        }
예제 #3
0
        private void WriteFrame(Frame frame, FrameHeader header, long start, long end)
        {
            frame.ReadData(stream_);
            frame.ReadFooter(stream_);

            long startSample = frame.Header.StartingSampleNumber;
            int count = (int)(end - start);

            header.StartingSampleNumber = sampleCount_;
            header.BlockingStrategy = FrameHeader.BLOCKING_STRATEGY_VARIABLE;

            if (count == header.BlockSize)
            {
                frame.Write(outStream_);
            }
            else
            {
                header.BlockSize = count;
                frame.Write(outStream_, (int)(start - startSample), count);
            }

            trackFrameCount_++;
            sampleCount_ += header.BlockSize;
        }
예제 #4
0
        // read n bytes of virtual track starting at x offset into provided buffer
        public int Read(byte[] bytes, long offset, int count)
        {
            count = Math.Min((int)(track_.Size - offset), count);

            int bytesToRead = count;
            long currentOffset = offset;
            long endOffset = Math.Max(currentOffset, offset + bytesToRead - 1);

            // if header, copy it from memory
            long max = Math.Min(endOffset + 1, header_.Length);
            while (currentOffset < max)
            {
                bytes[currentOffset - offset] = header_[currentOffset];
                currentOffset++;
                bytesToRead--;
            }

            if (bytesToRead <= 0)
            {
                goto exit;
            }

            List<TrackFrame> pairs;

            int currentIndex = GetOffsetPairs(bytesToRead, currentOffset, out pairs);
            foreach (TrackFrame op in pairs)
            {
                inStream_.Reader.Seek(op.imageOffset);
                Frame f = new Frame();
                f.ReadHeader(inStream_);
                f.ReadData(inStream_);
                f.ReadFooter(inStream_);

                f.Header.BlockingStrategy = FrameHeader.BLOCKING_STRATEGY_VARIABLE;
                f.Header.StartingSampleNumber = op.startSample;
                f.Header.BlockSize = op.virtualBlockSize;

                outStream_.Writer.Truncate(0);

                bool countFunction = false;
                if ((currentIndex == 0 && op.imageStartSample != track_.StartSample) || (currentIndex == track_.Frames.Count -1 &&  op.imageEndSample != track_.EndSample))
                {
                    // partial frame
                    long start = Math.Max(track_.StartSample, op.imageStartSample);
                    long end = Math.Min(track_.EndSample, op.imageEndSample);
                    f.Write(outStream_, (int)(start - op.imageStartSample), (int)(end - start));
                    countFunction = true;
                }
                else
                {
                    f.Write(outStream_);
                }

                byte[] data = ReadAllBytes();

                max = Math.Min(endOffset + 1, op.virtualOffset + op.virtualSize);
                if (!countFunction)
                {
                    Debug.Assert(data.Length == op.virtualSize);
                }

                int i = Convert.ToInt32(currentOffset - op.virtualOffset);
                while (currentOffset < max)
                {
                    bytes[currentOffset - offset] = data[i];
                    i++;
                    currentOffset++;
                    bytesToRead--;
                }

                if (bytesToRead <= 0)
                {
                    break;
                }
                currentIndex++;
            }

            exit:
            return count;
        }