csogg.StreamState.pagein C# (CSharp) Méthode

pagein() public méthode

public pagein ( Page og ) : int
og Page
Résultat int
        public int pagein(Page og)
        {
            byte[] header_base=og.header_base;
            int header=og.header;
            byte[] body_base=og.body_base;
            int body=og.body;
            int bodysize=og.body_len;
            int segptr=0;

            int version=og.version();
            int continued=og.continued();
            int bos=og.bos();
            int eos=og.eos();
            long granulepos=og.granulepos();
            int _serialno=og.serialno();
            int _pageno=og.pageno();
            int segments=header_base[header+26]&0xff;

            // clean up 'returned data'
            {
            int lr=lacing_returned;
            int br=body_returned;

            // body data

            //System.out.println("br="+br+", body_fill="+body_fill);

            if(br!=0)
            {
                body_fill-=br;
                if(body_fill!=0)
                {
                    Array.Copy(body_data, br, body_data, 0, body_fill);
                }
                body_returned=0;
            }

            //System.out.println("?? br="+br+", body_fill="+body_fill+" body_returned="+body_returned);

            if(lr!=0)
            {
                // segment table
                if((lacing_fill-lr)!=0)
                {
                    Array.Copy(lacing_vals, lr, lacing_vals, 0, lacing_fill-lr);
                    Array.Copy(granule_vals, lr, granule_vals, 0, lacing_fill-lr);
                }
                lacing_fill-=lr;
                lacing_packet-=lr;
                lacing_returned=0;
            }
            }

            // check the serial number
            if(_serialno!=serialno)return(-1);
            if(version>0)return(-1);

            lacing_expand(segments+1);

            // are we in sequence?
            if(_pageno!=pageno)
            {
                int i;

                // unroll previous partial packet (if any)
                for(i=lacing_packet;i<lacing_fill;i++)
                {
                    body_fill-=lacing_vals[i]&0xff;
                    //System.out.println("??");
                }
                lacing_fill=lacing_packet;

                // make a note of dropped data in segment table
                if(pageno!=-1)
                {
                    lacing_vals[lacing_fill++]=0x400;
                    lacing_packet++;
                }

                // are we a 'continued packet' page?  If so, we'll need to skip
                // some segments
                if(continued!=0)
                {
                    bos=0;
                    for(;segptr<segments;segptr++)
                    {
                        int val=(header_base[header+27+segptr]&0xff);
                        body+=val;
                        bodysize-=val;
                        if(val<255)
                        {
                            segptr++;
                            break;
                        }
                    }
                }
            }

            //System.out.println("bodysize="+bodysize);

            if(bodysize!=0)
            {
                body_expand(bodysize);
                Array.Copy(body_base, body, body_data, body_fill, bodysize);
                body_fill+=bodysize;
            }

            //System.out.println("bodyfill="+body_fill);

            {
            int saved=-1;
            while(segptr<segments)
            {
                int val=(header_base[header+27+segptr]&0xff);
                lacing_vals[lacing_fill]=val;
                granule_vals[lacing_fill]=-1;

                if(bos!=0)
                {
                    lacing_vals[lacing_fill]|=0x100;
                    bos=0;
                }

                if(val<255)saved=lacing_fill;

                lacing_fill++;
                segptr++;

                if(val<255)lacing_packet=lacing_fill;
            }

            /* set the granulepos on the last pcmval of the last full packet */
            if(saved!=-1)
            {
                granule_vals[saved]=granulepos;
            }
            }

            if(eos!=0)
            {
                e_o_s=1;
                if(lacing_fill>0)
                    lacing_vals[lacing_fill-1]|=0x200;
            }

            pageno=_pageno+1;
            return(0);
        }

Usage Example

        public AudioSample OggToWav(Stream ogg, float volume, float pitch)
        {
            AudioSample sample = new AudioSample();
            TextWriter s_err = new StringWriter();
            Stream input = null;
            MemoryStream output = null;

            input = ogg;
            output = new MemoryStream();

            SyncState oy = new SyncState();
            StreamState os = new StreamState();
            Page og = new Page();
            Packet op = new Packet();

            Info vi = new Info();
            Comment vc = new Comment();
            DspState vd = new DspState();
            Block vb = new Block(vd);

            byte[] buffer;
            int bytes = 0;
            oy.init();
            while (true)
            {
                int eos = 0;
                int index = oy.buffer(4096);
                buffer = oy.data;
                try
                {
                    bytes = input.Read(buffer, index, 4096);
                }
                catch (Exception e)
                {
                    s_err.WriteLine(e);
                }
                oy.wrote(bytes);
                if (oy.pageout(og) != 1)
                {
                    if (bytes < 4096) break;
                    s_err.WriteLine("Input does not appear to be an Ogg bitstream.");
                }
                os.init(og.serialno());
                vi.init();
                vc.init();
                if (os.pagein(og) < 0)
                {
                    s_err.WriteLine("Error reading first page of Ogg bitstream data.");
                }

                if (os.packetout(op) != 1)
                {
                    s_err.WriteLine("Error reading initial header packet.");
                }

                if (vi.synthesis_headerin(vc, op) < 0)
                {
                    s_err.WriteLine("This Ogg bitstream does not contain Vorbis audio data.");
                }

                int i = 0;
                while (i < 2)
                {
                    while (i < 2)
                    {
                        int result = oy.pageout(og);
                        if (result == 0) break;
                        if (result == 1)
                        {
                            os.pagein(og);
                            while (i < 2)
                            {
                                result = os.packetout(op);
                                if (result == 0) break;
                                vi.synthesis_headerin(vc, op);
                                i++;
                            }
                        }
                    }
                    index = oy.buffer(4096);
                    buffer = oy.data;
                    try
                    {
                        bytes = input.Read(buffer, index, 4096);
                    }
                    catch (Exception e)
                    {
                        s_err.WriteLine(e);
                    }
                    oy.wrote(bytes);
                }
                sample.Channels = vi.channels;
                sample.Rate = (int)((float)vi.rate * pitch);
                convsize = 4096 / vi.channels;
                vd.synthesis_init(vi);
                vb.init(vd);
                float[][][] _pcm = new float[1][][];
                int[] _index = new int[vi.channels];
                while (eos == 0)
                {
                    while (eos == 0)
                    {
                        int result = oy.pageout(og);
                        if (result == 0) break;
                        if (result != -1)
                        {
                            os.pagein(og);
                            while (true)
                            {
                                result = os.packetout(op);
                                if (result == 0) break;
                                if (result != -1)
                                {
                                    int samples;
                                    if (vb.synthesis(op) == 0)
                                    {
                                        vd.synthesis_blockin(vb);
                                    }
                                    while ((samples = vd.synthesis_pcmout(_pcm, _index)) > 0)
                                    {
                                        float[][] pcm = _pcm[0];
                                        int bout = (samples < convsize ? samples : convsize);
                                        for (i = 0; i < vi.channels; i++)
                                        {
                                            int ptr = i * 2;
                                            int mono = _index[i];
                                            for (int j = 0; j < bout; j++)
                                            {
                                                int val = (int)(pcm[i][mono + j] * 32767.0);
                                                if (val > 32767)
                                                {
                                                    val = 32767;
                                                }
                                                if (val < -32768)
                                                {
                                                    val = -32768;
                                                }
                                                val = (int)((float)val * volume);
                                                if (val < 0) val = val | 0x8000;
                                                convbuffer[ptr] = (byte)(val);
                                                convbuffer[ptr + 1] = (byte)((uint)val >> 8);
                                                ptr += 2 * (vi.channels);
                                            }
                                        }
                                        output.Write(convbuffer, 0, 2 * vi.channels * bout);
                                        vd.synthesis_read(bout);
                                    }
                                }
                            }
                            if (og.eos() != 0) eos = 1;
                        }
                    }
                    if (eos == 0)
                    {
                        index = oy.buffer(4096);
                        buffer = oy.data;
                        try
                        {
                            bytes = input.Read(buffer, index, 4096);
                        }
                        catch (Exception e)
                        {
                            s_err.WriteLine(e);
                        }
                        oy.wrote(bytes);
                        if (bytes == 0) eos = 1;
                    }
                }
                os.clear();
                vb.clear();
                vd.clear();
                vi.clear();
                break;
            }

            oy.clear();
            input.Close();
            sample.Pcm = output.ToArray();
            return sample;
        }
All Usage Examples Of csogg.StreamState::pagein