示例#1
0
        private int ReadByte()
        {
            int i = BaseStream.ReadByte();

            if (i < 0)
            {
                throw new FlacException("Unexpected end of stream: bit reader needs data");
            }
            crc16 = CrcUtils.Crc16(crc16, (byte)i);
            return(i);
        }
示例#2
0
        private void WriteFullFrame(byte[] buffer, int offset, int count, bool lastFrame, bool overflow)
        {
            // TODO add logic for "overflow" pages

            int segments = (count + 254) / 255;

            byte[] frameHeader = new byte[27 + segments];
            Array.Copy(OggHeader, frameHeader, OggHeader.Length);
            frameHeader[4] = 0x00;
            int frameType = 0;

            if (!isFirstFrameWitten)
            {
                frameType         |= FirstPageFlag;
                isFirstFrameWitten = true;
            }
            if (lastFrame)
            {
                frameType         |= LastPageFlag;
                isLastFrameWritten = true;
            }
            frameHeader[5] = (byte)frameType;
            Array.Copy(BitConverter.GetBytes(position), 0, frameHeader, 6, 8);
            Array.Copy(BitConverter.GetBytes(streamId), 0, frameHeader, 14, 4);
            Array.Copy(BitConverter.GetBytes(pageNumber), 0, frameHeader, 18, 4);
            // 22 - 26 crc32
            frameHeader[26] = (byte)segments;
            for (int i = 0; i < segments - 1; i++)
            {
                frameHeader[27 + i] = 255;
            }
            frameHeader[27 + segments - 1] = (byte)(count - (segments - 1) * 255);

            uint crc32 = CrcUtils.Crc32(0, frameHeader);

            crc32 = CrcUtils.Crc32(crc32, buffer, offset, count);

            Array.Copy(BitConverter.GetBytes(crc32), 0, frameHeader, 22, 4);

            baseStream.Write(frameHeader, 0, frameHeader.Length);
            baseStream.Write(buffer, offset, count);

            pageNumber++;
            position += (ulong)count;
        }
示例#3
0
 private void WriteByte(byte b)
 {
     baseStream.WriteByte(b);
     crc16 = CrcUtils.Crc16(crc16, b);
     ++count;
 }
示例#4
0
        public void WriteFrame(FlacMethodAndDataPair[] methods, SoundChannelAssignmentType channelAssignment)
        {
            EnsureFramesMode();

            sink.StartFrame(streamPosition, currentSample);

            if (methods == null)
            {
                throw new ArgumentNullException("methods");
            }
            if (methods.Length != Streaminfo.ChannelsCount)
            {
                throw new ArgumentNullException("Methods items does not correspond to amount of channels");
            }

            int samplesCount = methods[0].Data.Length;

            const byte Blocking = 0x00; // fixed

            MemoryStream frameHeader = new MemoryStream();

            // sync code + reserved = 0 + blocking
            frameHeader.WriteByte(0xFF); frameHeader.WriteByte(0xF8 | Blocking);

            int interChannelSamplesTypeIndex = Array.IndexOf(FlacCommons.StaticBlockSizeSamples, samplesCount);
            int interChannelSamplesType;

            if (interChannelSamplesTypeIndex > 0)
            {
                interChannelSamplesType = interChannelSamplesTypeIndex;
            }
            else if (samplesCount > 256)
            {
                interChannelSamplesType = FlacCommons.Bit16BlockSizeSamplesType;
            }
            else
            {
                interChannelSamplesType = FlacCommons.Bit8BlockSizeSamplesType;
            }

            int sampleRateTypeIndex = Array.IndexOf(FlacCommons.StaticSampleRates, Streaminfo.SampleRate);
            int sampleRateType      = sampleRateTypeIndex > 0
                ? sampleRateTypeIndex : FlacCommons.StreaminfoSampleRateType;

            frameHeader.WriteByte((byte)(interChannelSamplesType << 4 | sampleRateType));

            int channelAssignmetType = (int)channelAssignment;

            int sampleSizeInBitsTypeIndex = Array.IndexOf(FlacCommons.StaticSampleSizeInBits, Streaminfo.BitsPerSample);
            int sampleSizeInBitsType      = sampleSizeInBitsTypeIndex > 0
                ? sampleSizeInBitsTypeIndex : FlacCommons.StreaminfoSizeInBitsType;

            frameHeader.WriteByte((byte)(channelAssignmetType << 4 | sampleSizeInBitsType << 1));

            WriteUtf8Number(frameHeader, frameNumber);
            switch (interChannelSamplesType)
            {
            case FlacCommons.Bit8BlockSizeSamplesType:
                frameHeader.WriteByte((byte)(samplesCount - 1)); break;

            case FlacCommons.Bit16BlockSizeSamplesType:
                frameHeader.WriteByte((byte)((samplesCount - 1) >> 8));
                frameHeader.WriteByte((byte)(samplesCount - 1));
                break;
            }
            frameHeader.Dispose();

            byte[] frameHeaderData = frameHeader.ToArray();

            byte crc8 = CrcUtils.Crc8(0, frameHeaderData);

            BaseStream.Write(frameHeaderData, 0, frameHeaderData.Length);
            BaseStream.WriteByte(crc8);

            ++frameNumber;

            ushort crc16Seed = CrcUtils.Crc16(CrcUtils.Crc16(0, frameHeaderData), crc8);

            // write channels
            FlacBitStreamWriter bitWriter = new FlacBitStreamWriter(BaseStream, crc16Seed);

            for (int i = 0; i < methods.Length; i++)
            {
                WriteSubframe(bitWriter, methods[i]);
            }

            int    subframesLength;
            ushort crc16;

            bitWriter.Complete(out crc16, out subframesLength);

            // write footer
            BaseStream.WriteByte((byte)(crc16 >> 8)); BaseStream.WriteByte((byte)crc16);


            int frameSize = frameHeaderData.Length + 1 + subframesLength + 2;

            streamPosition += frameSize;
            currentSample  += samplesCount;

            sink.EndFrame(streamPosition, currentSample);
        }
示例#5
0
 private static uint Crc32(uint seed, byte[] data)
 {
     return(CrcUtils.Crc32(seed, data));
 }