示例#1
0
 internal override void WriteData(FlacBitStreamWriter writer, int bitsPerSample, int[] data)
 {
     for (int i = 0; i < data.Length; i++)
     {
         writer.WriteSigned(data[i], bitsPerSample);
     }
 }
示例#2
0
 internal override void WriteHeaderData(FlacBitStreamWriter writer, int bitsPerSample, int[] data)
 {
     // samples
     for (int i = 0; i < Order; i++)
     {
         writer.WriteSigned(data[i], bitsPerSample);
     }
 }
示例#3
0
        internal override void WriteData(FlacBitStreamWriter writer, int bitsPerSample, int[] data)
        {
            WriteHeaderData(writer, bitsPerSample, data);

            IPredictor predictor = CreatePredictor(ArrayUtils.CutArray(data, 0, Order - 1));

            residual.WriteResidual(writer, predictor, Order, data);
        }
示例#4
0
 internal override void WriteHeaderData(FlacBitStreamWriter writer, int bitsPerSample, int[] data)
 {
     // samples
     for (int i = 0; i < Order; i++)
     {
         writer.WriteSigned(data[i], bitsPerSample);
     }
 }
示例#5
0
        internal void WriteResidual(FlacBitStreamWriter writer, IPredictor predictor, int predictorOrder, int[] samples)
        {
            int totalPartitions = 1 << Order;

            System.Diagnostics.Debug.Assert((samples.Length % totalPartitions) == 0 &&
                                            (samples.Length / totalPartitions) > predictorOrder && RiceParameters.Length == totalPartitions);

            if (!IsExtended)
            {
                writer.WriteUnsigned(0, 2);
            }
            else
            {
                writer.WriteUnsigned(1, 2);
            }

            writer.WriteUnsigned((uint)Order, 4);

            int j = predictorOrder;
            int samplePerPartition         = samples.Length / totalPartitions;
            int encodingParameterPrecision = IsExtended ? 5 : 4;
            IEnumerator <int> residualData = GetResidual(samples, predictorOrder, predictor);

            for (int i = 0; i < totalPartitions; i++)
            {
                if (RiceParameters[i] >= 0)
                {
                    int riceParameter = RiceParameters[i];
                    writer.WriteUnsigned((uint)riceParameter, encodingParameterPrecision);
                    while (j++ < samplePerPartition)
                    {
                        if (!residualData.MoveNext())
                        {
                            throw new FlacException("Invalid amount of residual data");
                        }

                        writer.WriteRice(residualData.Current, riceParameter);
                    }
                }
                else // escaped
                {
                    writer.WriteUnsigned(~0U, encodingParameterPrecision);
                    int samplePrecision = ~RiceParameters[i];
                    writer.WriteUnsigned((uint)samplePrecision, 5);
                    while (j++ < samplePerPartition)
                    {
                        if (!residualData.MoveNext())
                        {
                            throw new FlacException("Invalid amount of residual data");
                        }

                        writer.WriteSigned(residualData.Current, samplePrecision);
                    }
                }
                j = 0;
            }
        }
示例#6
0
        private void WriteSubframe(FlacBitStreamWriter bitWriter, FlacMethodAndDataPair flacMethodAndDataPair)
        {
            bitWriter.WriteUnsigned(0, 1);

            int subframeType = flacMethodAndDataPair.Method.SubframeTypeCode;

            bitWriter.WriteUnsigned((uint)subframeType, 6);

            bitWriter.WriteUnsigned(0, 1);

            flacMethodAndDataPair.WriteData(bitWriter);
        }
        internal void WriteResidual(FlacBitStreamWriter writer, IPredictor predictor, int predictorOrder, int[] samples)
        {
            int totalPartitions = 1 << Order;
            System.Diagnostics.Debug.Assert((samples.Length % totalPartitions) == 0
                && (samples.Length / totalPartitions) > predictorOrder && RiceParameters.Length == totalPartitions);

            if (!IsExtended)
                writer.WriteUnsigned(0, 2);
            else
                writer.WriteUnsigned(1, 2);

            writer.WriteUnsigned((uint)Order, 4);

            int j = predictorOrder;
            int samplePerPartition = samples.Length / totalPartitions;
            int encodingParameterPrecision = IsExtended ? 5 : 4;
            IEnumerator<int> residualData = GetResidual(samples, predictorOrder, predictor);

            for (int i = 0; i < totalPartitions; i++)
            {
                if (RiceParameters[i] >= 0)
                {
                    int riceParameter = RiceParameters[i];
                    writer.WriteUnsigned((uint)riceParameter, encodingParameterPrecision);
                    while (j++ < samplePerPartition)
                    {
                        if (!residualData.MoveNext())
                            throw new FlacException("Invalid amount of residual data");

                        writer.WriteRice(residualData.Current, riceParameter);
                    }
                }
                else // escaped
                {
                    writer.WriteUnsigned(~0U, encodingParameterPrecision);
                    int samplePrecision = ~RiceParameters[i];
                    writer.WriteUnsigned((uint)samplePrecision, 5);
                    while(j++ < samplePerPartition)
                    {
                        if (!residualData.MoveNext())
                            throw new FlacException("Invalid amount of residual data");

                        writer.WriteSigned(residualData.Current, samplePrecision);
                    }
                }
                j = 0;
            }
        }
示例#8
0
        internal override void WriteHeaderData(FlacBitStreamWriter writer, int bitsPerSample, int[] data)
        {
            System.Diagnostics.Debug.Assert(0 < Coefficients.CoefficientsPrecision &&
                                            Coefficients.CoefficientsPrecision < 16);

            // samples
            for (int i = 0; i < Order; i++)
            {
                writer.WriteSigned(data[i], bitsPerSample);
            }

            writer.WriteUnsigned((uint)(Coefficients.CoefficientsPrecision - 1), 4);

            writer.WriteSigned(Coefficients.ResultShift, 5);

            for (int i = 0; i < Order; i++)
            {
                writer.WriteSigned(Coefficients.Coefficients[i], Coefficients.CoefficientsPrecision);
            }
        }
 internal void WriteData(FlacBitStreamWriter bitWriter)
 {
     Method.WriteData(bitWriter, BitsPerSample, Data);
 }
示例#10
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);
        }
示例#11
0
        private void WriteSubframe(FlacBitStreamWriter bitWriter, FlacMethodAndDataPair flacMethodAndDataPair)
        {
            bitWriter.WriteUnsigned(0, 1);

            int subframeType = flacMethodAndDataPair.Method.SubframeTypeCode;
            bitWriter.WriteUnsigned((uint)subframeType, 6);

            bitWriter.WriteUnsigned(0, 1);

            flacMethodAndDataPair.WriteData(bitWriter);
        }
示例#12
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.Close();

            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);
        }
示例#13
0
 internal override void WriteData(FlacBitStreamWriter writer, int bitsPerSample, int[] data)
 {
     writer.WriteSigned(data[0], bitsPerSample);
 }
示例#14
0
 internal abstract void WriteHeaderData(FlacBitStreamWriter writer, int bitsPerSample, int[] data);
示例#15
0
        internal override void WriteData(FlacBitStreamWriter writer, int bitsPerSample, int[] data)
        {
            WriteHeaderData(writer, bitsPerSample, data);

            IPredictor predictor = CreatePredictor(ArrayUtils.CutArray(data, 0, Order - 1));

            residual.WriteResidual(writer, predictor, Order, data);
        }
示例#16
0
        internal override void WriteHeaderData(FlacBitStreamWriter writer, int bitsPerSample, int[] data)
        {
            System.Diagnostics.Debug.Assert(0 < Coefficients.CoefficientsPrecision &&
                Coefficients.CoefficientsPrecision < 16);

            // samples
            for (int i = 0; i < Order; i++)
            {
                writer.WriteSigned(data[i], bitsPerSample);
            }

            writer.WriteUnsigned((uint)(Coefficients.CoefficientsPrecision - 1), 4);

            writer.WriteSigned(Coefficients.ResultShift, 5);

            for (int i = 0; i < Order; i++)
            {
                writer.WriteSigned(Coefficients.Coefficients[i], Coefficients.CoefficientsPrecision);
            }
        }
示例#17
0
 internal override void WriteData(FlacBitStreamWriter writer, int bitsPerSample, int[] data)
 {
     writer.WriteSigned(data[0], bitsPerSample);
 }
示例#18
0
 internal void WriteData(FlacBitStreamWriter bitWriter)
 {
     Method.WriteData(bitWriter, BitsPerSample, Data);
 }
示例#19
0
 internal abstract void WriteHeaderData(FlacBitStreamWriter writer, int bitsPerSample, int[] data);
示例#20
0
 internal override void WriteData(FlacBitStreamWriter writer, int bitsPerSample, int[] data)
 {
     for (int i = 0; i < data.Length; i++)
     {
         writer.WriteSigned(data[i], bitsPerSample);
     }
 }