Beispiel #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);
     }
 }
Beispiel #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);
     }
 }
Beispiel #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);
        }
Beispiel #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);
     }
 }
Beispiel #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;
            }
        }
Beispiel #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;
            }
        }
Beispiel #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);
 }
Beispiel #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);
        }
Beispiel #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);
        }
Beispiel #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);
        }
Beispiel #13
0
 internal override void WriteData(FlacBitStreamWriter writer, int bitsPerSample, int[] data)
 {
     writer.WriteSigned(data[0], bitsPerSample);
 }
Beispiel #14
0
 internal abstract void WriteHeaderData(FlacBitStreamWriter writer, int bitsPerSample, int[] data);
Beispiel #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);
        }
Beispiel #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);
            }
        }
Beispiel #17
0
 internal override void WriteData(FlacBitStreamWriter writer, int bitsPerSample, int[] data)
 {
     writer.WriteSigned(data[0], bitsPerSample);
 }
Beispiel #18
0
 internal void WriteData(FlacBitStreamWriter bitWriter)
 {
     Method.WriteData(bitWriter, BitsPerSample, Data);
 }
Beispiel #19
0
 internal abstract void WriteHeaderData(FlacBitStreamWriter writer, int bitsPerSample, int[] data);
Beispiel #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);
     }
 }