Пример #1
0
        public OggPacket BuildBooksPacket(VorbisInfo info)
        {
            var buffer = new EncodeBuffer(4096);

            PackBooks(buffer, info);

            var bytes = buffer.GetBytes();

            return(new OggPacket(bytes, false, 0, 2));
        }
Пример #2
0
        public OggPacket BuildInfoPacket(VorbisInfo info)
        {
            var buffer = new EncodeBuffer();

            PackInfo(buffer, info);

            var bytes = buffer.GetBytes();

            return(new OggPacket(bytes, false, 0, 0));
        }
Пример #3
0
        private static void PackMapping(EncodeBuffer buffer, VorbisInfo info, Mapping mapping)
        {
            /* another 'we meant to do it this way' hack...  up to beta 4, we
             * packed 4 binary zeros here to signify one submapping in use.  We
             * now redefine that to mean four bitflags that indicate use of
             * deeper features; bit0:submappings, bit1:coupling,
             * bit2,3:reserved. This is backward compatible with all actual uses
             * of the beta code. */
            if (mapping.SubMaps > 1)
            {
                buffer.Write(1, 1);
                buffer.Write((uint)mapping.SubMaps - 1, 4);
            }
            else
            {
                buffer.Write(0, 1);
            }

            if (mapping.CouplingSteps > 0)
            {
                buffer.Write(1, 1);
                buffer.Write((uint)mapping.CouplingSteps - 1, 8);

                var couplingBits = Encoding.Log(info.Channels - 1);
                for (var i = 0; i < mapping.CouplingSteps; i++)
                {
                    buffer.Write((uint)mapping.CouplingMag[i], couplingBits);
                    buffer.Write((uint)mapping.CouplingAng[i], couplingBits);
                }
            }
            else
            {
                buffer.Write(0, 1);
            }

            buffer.Write(0, 2); // 2,3:reserved

            // we don't write the channel submappings if we only have one...
            if (mapping.SubMaps > 1)
            {
                for (var i = 0; i < info.Channels; i++)
                {
                    buffer.Write((uint)mapping.ChannelMuxList[i], 4);
                }
            }

            for (var i = 0; i < mapping.SubMaps; i++)
            {
                buffer.Write(0, 8); // time submap unused
                buffer.Write((uint)mapping.FloorSubMap[i], 8);
                buffer.Write((uint)mapping.ResidueSubMap[i], 8);
            }
        }
Пример #4
0
        private int _sequence = 3; // compressed audio packets start after the headers with sequence number 3

        private ProcessingState(
            VorbisInfo vorbisInfo,
            LookupCollection lookups,
            float[][] pcm,
            int[] window,
            int centerWindow)
        {
            _vorbisInfo   = vorbisInfo;
            _lookups      = lookups;
            _pcm          = pcm;
            _window       = window;
            _centerWindow = centerWindow;
        }
Пример #5
0
        private void PackBooks(EncodeBuffer buffer, VorbisInfo info)
        {
            var codecSetup = info.CodecSetup;

            buffer.Write(0x05, 8);
            buffer.WriteString(VorbisString);

            buffer.Write((uint)(codecSetup.BookParams.Count - 1), 8);
            foreach (var book in codecSetup.BookParams)
            {
                PackStaticBook(buffer, book);
            }

            // times; hook placeholders
            buffer.Write(0, 6);
            buffer.Write(0, 16);

            buffer.Write((uint)(codecSetup.FloorParams.Count - 1), 6);
            foreach (var floor in codecSetup.FloorParams)
            {
                buffer.Write(1, 16); // For now we're only using floor type 1
                PackFloor(buffer, floor);
            }

            buffer.Write((uint)(codecSetup.ResidueParams.Count - 1), 6);
            foreach (var residue in codecSetup.ResidueParams)
            {
                buffer.Write((uint)residue.ResidueType, 16);
                PackResidue(buffer, residue);
            }

            buffer.Write((uint)(codecSetup.MapParams.Count - 1), 6);
            foreach (var mapping in codecSetup.MapParams)
            {
                buffer.Write(0, 16); // Mapping type is always zero
                PackMapping(buffer, info, mapping);
            }

            buffer.Write((uint)(codecSetup.ModeParams.Count - 1), 6);
            for (var i = 0; i < codecSetup.ModeParams.Count; i++)
            {
                PackModes(buffer, codecSetup, i);
            }

            buffer.Write(1, 1);
        }
Пример #6
0
        private static void PackInfo(EncodeBuffer buffer, VorbisInfo info)
        {
            var codecSetup = info.CodecSetup;

            // preamble
            buffer.Write(0x01, 8);
            buffer.WriteString(VorbisString);

            // basic information about the stream
            buffer.Write(0x00, 32);
            buffer.Write((uint)info.Channels, 8);
            buffer.Write((uint)info.SampleRate, 32);

            buffer.Write(0, 32); // Bit rate upper not used
            buffer.Write((uint)info.BitRateNominal, 32);
            buffer.Write(0, 32); // Bit rate lower not used

            buffer.Write((uint)Encoding.Log(codecSetup.BlockSizes[0] - 1), 4);
            buffer.Write((uint)Encoding.Log(codecSetup.BlockSizes[1] - 1), 4);
            buffer.Write(1, 1);
        }
Пример #7
0
        public static ProcessingState Create(VorbisInfo info)
        {
            if (info == null)
            {
                throw new ArgumentNullException(nameof(info));
            }

            var codecSetup = info.CodecSetup;

            // initialize the storage vectors. blocksize[1] is small for encode, but the correct size for decode
            var pcmStorage = codecSetup.BlockSizes[1];

            var pcm = new float[info.Channels][];

            for (var i = 0; i < pcm.Length; i++)
            {
                pcm[i] = new float[pcmStorage];
            }

            // Vorbis I uses only window type 0
            var window = new int[2];

            window[0] = Encoding.Log(codecSetup.BlockSizes[0]) - 7;
            window[1] = Encoding.Log(codecSetup.BlockSizes[1]) - 7;

            var centerWindow = codecSetup.BlockSizes[1] / 2;

            var lookups = LookupCollection.Create(info);

            return(new ProcessingState(
                       info,
                       lookups,
                       pcm,
                       window,
                       centerWindow));
        }
Пример #8
0
        public static LookupCollection Create(VorbisInfo info)
        {
            var codecSetup = info.CodecSetup;

            var psyGlobal = new PsyGlobalLookup(codecSetup.PsyGlobalParam);
            var envelope  = new EnvelopeLookup(codecSetup.PsyGlobalParam, info);

            // MDCT is tranform 0
            var transform = new MdctLookup[2];

            transform[0] = new MdctLookup(codecSetup.BlockSizes[0]);
            transform[1] = new MdctLookup(codecSetup.BlockSizes[1]);

            // analysis always needs an fft
            var fftLookup = new DrftLookup[2];

            fftLookup[0] = new DrftLookup(codecSetup.BlockSizes[0]);
            fftLookup[1] = new DrftLookup(codecSetup.BlockSizes[1]);

            // finish the codebooks
            if (codecSetup.FullBooks == null)
            {
                codecSetup.FullBooks = new CodeBook[codecSetup.BookParams.Count];
                for (var i = 0; i < codecSetup.BookParams.Count; i++)
                {
                    codecSetup.FullBooks[i] = CodeBook.InitEncode(codecSetup.BookParams[i]);
                }
            }

            var psyLookup = new PsyLookup[codecSetup.PsyParams.Count];

            for (var i = 0; i < psyLookup.Length; i++)
            {
                psyLookup[i] = new PsyLookup(
                    codecSetup.PsyParams[i],
                    codecSetup.PsyGlobalParam,
                    codecSetup.BlockSizes[codecSetup.PsyParams[i].BlockFlag] / 2,
                    info.SampleRate);
            }

            // initialize all the backend lookups
            var floor = new FloorLookup[codecSetup.FloorParams.Count];

            for (var i = 0; i < floor.Length; i++)
            {
                floor[i] = new FloorLookup(codecSetup.FloorParams[i]);
            }

            var residue = new ResidueLookup[codecSetup.ResidueParams.Count];

            for (var i = 0; i < residue.Length; i++)
            {
                residue[i] = new ResidueLookup(codecSetup.ResidueParams[i], new List <CodeBook>(codecSetup.FullBooks));
            }

            return(new LookupCollection(
                       envelope,
                       new List <MdctLookup>(transform),
                       psyGlobal,
                       new List <PsyLookup>(psyLookup),
                       new List <DrftLookup>(fftLookup),
                       new List <FloorLookup>(floor),
                       new List <ResidueLookup>(residue)));
        }