Пример #1
0
            internal override void Apply(PacketData packetData, float[] residue)
            {
                PacketData0 packetData2 = packetData as PacketData0;

                if (packetData2 == null)
                {
                    throw new ArgumentException("Incorrect packet data!");
                }
                int num = packetData2.BlockSize / 2;

                if (packetData2.Amp > 0f)
                {
                    int[]   array  = _barkMaps[packetData2.BlockSize];
                    float[] array2 = _wMap[packetData2.BlockSize];
                    int     num2   = 0;
                    for (num2 = 0; num2 < _order; num2++)
                    {
                        packetData2.Coeff[num2] = 2f * (float)Math.Cos(packetData2.Coeff[num2]);
                    }
                    num2 = 0;
                    while (num2 < num)
                    {
                        int   num3 = array[num2];
                        float num4 = 0.5f;
                        float num5 = 0.5f;
                        float num6 = array2[num3];
                        int   i;
                        for (i = 1; i < _order; i += 2)
                        {
                            num5 *= num6 - packetData2.Coeff[i - 1];
                            num4 *= num6 - packetData2.Coeff[i];
                        }
                        if (i == _order)
                        {
                            num5 *= num6 - packetData2.Coeff[i - 1];
                            num4 *= num4 * (4f - num6 * num6);
                            num5 *= num5;
                        }
                        else
                        {
                            num4 *= num4 * (2f - num6);
                            num5 *= num5 * (2f + num6);
                        }
                        num5           = packetData2.Amp / (float)Math.Sqrt(num4 + num5) - (float)_ampOfs;
                        num5           = (float)Math.Exp(num5 * 0.115129247f);
                        residue[num2] *= num5;
                        while (array[++num2] == num3)
                        {
                            residue[num2] *= num5;
                        }
                    }
                }
                else
                {
                    Array.Clear(residue, 0, num);
                }
            }
Пример #2
0
            internal override PacketData UnpackPacket(DataPacket packet, int blockSize, int channel)
            {
                PacketData0 packetData = _reusablePacketData[channel];

                packetData.BlockSize     = blockSize;
                packetData.ForceEnergy   = false;
                packetData.ForceNoEnergy = false;
                packetData.Amp           = (float)(double)packet.ReadBits(_ampBits);
                if (packetData.Amp > 0f)
                {
                    Array.Clear(packetData.Coeff, 0, packetData.Coeff.Length);
                    packetData.Amp = packetData.Amp / (float)_ampDiv * (float)_ampOfs;
                    uint num = (uint)packet.ReadBits(_bookBits);
                    if (num >= _books.Length)
                    {
                        packetData.Amp = 0f;
                        return(packetData);
                    }
                    VorbisCodebook vorbisCodebook = _books[num];
                    int            i = 0;
                    while (i < _order)
                    {
                        int num2 = vorbisCodebook.DecodeScalar(packet);
                        if (num2 == -1)
                        {
                            packetData.Amp = 0f;
                            return(packetData);
                        }
                        int num3 = 0;
                        for (; i < _order; i++)
                        {
                            if (num3 >= vorbisCodebook.Dimensions)
                            {
                                break;
                            }
                            packetData.Coeff[i] = vorbisCodebook[num2, num3];
                            num3++;
                        }
                    }
                    float num4 = 0f;
                    int   num5 = 0;
                    while (num5 < _order)
                    {
                        int num6 = 0;
                        while (num5 < _order && num6 < vorbisCodebook.Dimensions)
                        {
                            packetData.Coeff[num5] += num4;
                            num5++;
                            num6++;
                        }
                        num4 = packetData.Coeff[num5 - 1];
                    }
                }
                return(packetData);
            }
Пример #3
0
            protected override void Init(DataPacket packet)
            {
                // this is pretty well stolen directly from libvorbis...  BSD license
                _order         = (int)packet.ReadBits(8);
                _rate          = (int)packet.ReadBits(16);
                _bark_map_size = (int)packet.ReadBits(16);
                _ampBits       = (int)packet.ReadBits(6);
                _ampOfs        = (int)packet.ReadBits(8);
                _books         = new VorbisCodebook[(int)packet.ReadBits(4) + 1];

                if (_order < 1 || _rate < 1 || _bark_map_size < 1 || _books.Length == 0)
                {
                    throw new InvalidDataException();
                }

                _ampDiv = (1 << _ampBits) - 1;

                for (int i = 0; i < _books.Length; i++)
                {
                    var num = (int)packet.ReadBits(8);
                    if (num < 0 || num >= _vorbis.Books.Length)
                    {
                        throw new InvalidDataException();
                    }
                    var book = _vorbis.Books[num];

                    if (book.MapType == 0 || book.Dimensions < 1)
                    {
                        throw new InvalidDataException();
                    }

                    _books[i] = book;
                }
                _bookBits = Utils.ilog(_books.Length);

                _barkMaps = new Dictionary <int, int[]>();
                _barkMaps[_vorbis.Block0Size] = SynthesizeBarkCurve(_vorbis.Block0Size / 2);
                _barkMaps[_vorbis.Block1Size] = SynthesizeBarkCurve(_vorbis.Block1Size / 2);

                _wMap = new Dictionary <int, float[]>();
                _wMap[_vorbis.Block0Size] = SynthesizeWDelMap(_vorbis.Block0Size / 2);
                _wMap[_vorbis.Block1Size] = SynthesizeWDelMap(_vorbis.Block1Size / 2);

                _reusablePacketData = new PacketData0[_vorbis._channels];
                for (int i = 0; i < _reusablePacketData.Length; i++)
                {
                    _reusablePacketData[i] = new PacketData0()
                    {
                        Coeff = new float[_order + 1]
                    };
                }
            }
Пример #4
0
 public override void Init(DataPacket packet)
 {
     _order         = (int)packet.ReadBits(8);
     _rate          = (int)packet.ReadBits(16);
     _bark_map_size = (int)packet.ReadBits(16);
     _ampBits       = (int)packet.ReadBits(6);
     _ampOfs        = (int)packet.ReadBits(8);
     _books         = new VorbisCodebook[(int)packet.ReadBits(4) + 1];
     if (_order < 1 || _rate < 1 || _bark_map_size < 1 || _books.Length == 0)
     {
         throw new InvalidDataException();
     }
     _ampDiv = (1 << _ampBits) - 1;
     for (int i = 0; i < _books.Length; i++)
     {
         int num = (int)packet.ReadBits(8);
         if (num < 0 || num >= _vorbis.Books.Length)
         {
             throw new InvalidDataException();
         }
         VorbisCodebook vorbisCodebook = _vorbis.Books[num];
         if (vorbisCodebook.MapType == 0 || vorbisCodebook.Dimensions < 1)
         {
             throw new InvalidDataException();
         }
         _books[i] = vorbisCodebook;
     }
     _bookBits = Utils.ilog(_books.Length);
     _barkMaps = new Dictionary <int, int[]>();
     _barkMaps[_vorbis.Block0Size] = SynthesizeBarkCurve(_vorbis.Block0Size / 2);
     _barkMaps[_vorbis.Block1Size] = SynthesizeBarkCurve(_vorbis.Block1Size / 2);
     _wMap = new Dictionary <int, float[]>();
     _wMap[_vorbis.Block0Size] = SynthesizeWDelMap(_vorbis.Block0Size / 2);
     _wMap[_vorbis.Block1Size] = SynthesizeWDelMap(_vorbis.Block1Size / 2);
     _reusablePacketData       = new PacketData0[_vorbis._channels];
     for (int j = 0; j < _reusablePacketData.Length; j++)
     {
         _reusablePacketData[j] = new PacketData0
         {
             Coeff = new float[_order + 1]
         };
     }
 }
Пример #5
0
            internal override PacketData UnpackPacket(DataPacket packet, int blockSize)
            {
                var data = new PacketData0 { BlockSize = blockSize };
                data.Amp = packet.ReadBits(_ampBits);
                if (data.Amp > 0f)
                {
                    try
                    {
                        var coefficients = new List<float>();
                        var bookNum = (uint)packet.ReadBits(_bookBits);
                        if (bookNum >= _books.Length) throw new InvalidDataException();
                        var book = _books[bookNum];

                        for (int i = 0; i < _order; i++)
                        {
                            var entry = book.DecodeScalar(packet);
                            for (int d = 0; d < book.Dimensions; d++)
                            {
                                coefficients.Add(book[entry, d]);
                            }
                            //book.DecodeVQ(packet, t => coefficients.Add(t));
                        }

                        data.Coeff = coefficients.ToArray();
                    }
                    catch (EndOfStreamException)
                    {
                        // per the spec, an end of packet condition here indicates "no floor"
                        data.Amp = 0.0f;
                    }
                }
                return data;
            }
Пример #6
0
            protected override void Init(DataPacket packet)
            {
                // this is pretty well stolen directly from libvorbis...  BSD license
                _order = (int)packet.ReadBits(8);
                _rate = (int)packet.ReadBits(16);
                _bark_map_size = (int)packet.ReadBits(16);
                _ampBits = (int)packet.ReadBits(6);
                _ampOfs = (int)packet.ReadBits(8);
                _books = new VorbisCodebook[(int)packet.ReadBits(4) + 1];

                if ( _order < 1 || _rate < 1 || _bark_map_size < 1 || _books.Length == 0 ) throw new ArgumentException ();

                _ampDiv = (1 << _ampBits) - 1;

                for (int i = 0; i < _books.Length; i++)
                {
                    var num = (int)packet.ReadBits(8);
                    if ( num < 0 || num >= _vorbis.Books.Length ) throw new ArgumentException ();
                    var book = _vorbis.Books[num];

                    if ( book.MapType == 0 || book.Dimensions < 1 ) throw new ArgumentException ();

                    _books[i] = book;
                }
                _bookBits = Utils.ilog(_books.Length);

                _barkMaps = new Dictionary<int, int[]>();
                _barkMaps[_vorbis.Block0Size] = SynthesizeBarkCurve(_vorbis.Block0Size / 2);
                _barkMaps[_vorbis.Block1Size] = SynthesizeBarkCurve(_vorbis.Block1Size / 2);

                _wMap = new Dictionary<int, float[]>();
                _wMap[_vorbis.Block0Size] = SynthesizeWDelMap(_vorbis.Block0Size / 2);
                _wMap[_vorbis.Block1Size] = SynthesizeWDelMap(_vorbis.Block1Size / 2);

                _reusablePacketData = new PacketData0[_vorbis._channels];
                for (int i = 0; i < _reusablePacketData.Length; i++)
                {
                    _reusablePacketData[i] = new PacketData0() { Coeff = new float[_order + 1] };
                }
            }
Пример #7
0
            protected override void Init(DataPacket packet)
            {
                // this is pretty well stolen directly from libvorbis...  BSD license
                _order         = (int)packet.ReadUBits(8);
                _rate          = (int)packet.ReadUBits(16);
                _bark_map_size = (int)packet.ReadUBits(16);
                _ampBits       = (int)packet.ReadUBits(6);
                _ampOfs        = (int)packet.ReadUBits(8);
                _books         = new VorbisCodebook[(int)packet.ReadUBits(4) + 1];

                if (_order < 1 || _rate < 1 || _bark_map_size < 1 || _books.Length == 0)
                {
                    throw new InvalidDataException();
                }

                _ampDiv = (1 << _ampBits) - 1;

                for (int i = 0; i < _books.Length; i++)
                {
                    int num = (int)packet.ReadUBits(8);
                    if (num < 0 || num >= _vorbis.Books.Length)
                    {
                        throw new InvalidDataException();
                    }
                    VorbisCodebook book = _vorbis.Books[num];

                    if (book.MapType == 0 || book.Dimensions < 1)
                    {
                        throw new InvalidDataException();
                    }

                    _books[i] = book;
                }
                _bookBits = Utils.ILog(_books.Length);

                _barkMaps = new Dictionary <int, int[]>();
                _barkMaps[_vorbis.Block0Size] = SynthesizeBarkCurve(_vorbis.Block0Size / 2);
                _barkMaps[_vorbis.Block1Size] = SynthesizeBarkCurve(_vorbis.Block1Size / 2);

                _wMap = new Dictionary <int, float[]>();
                _wMap[_vorbis.Block0Size] = SynthesizeWDelMap(_vorbis.Block0Size / 2);
                _wMap[_vorbis.Block1Size] = SynthesizeWDelMap(_vorbis.Block1Size / 2);

                _reusablePacketData = new PacketData0[_vorbis._channels];
                for (int i = 0; i < _reusablePacketData.Length; i++)
                {
                    _reusablePacketData[i] = new PacketData0()
                    {
                        Coeff = new float[_order + 1]
                    }
                }
                ;
            }

            int[] SynthesizeBarkCurve(int n)
            {
                float scale = _bark_map_size / ToBARK(_rate / 2);

                int[] map = new int[n + 1];

                for (int i = 0; i < n - 1; i++)
                {
                    map[i] = Math.Min(_bark_map_size - 1, (int)MathF.Floor(ToBARK(_rate / 2f / n * i) * scale));
                }

                map[n] = -1;
                return(map);
            }