csvorbis.VorbisFile.read C# (CSharp) Method

read() public method

public read ( byte buffer, int length, int bigendianp, int word, int sgned, int bitstream ) : int
buffer byte
length int
bigendianp int
word int
sgned int
bitstream int
return int
        public int read(byte[] buffer,int length,
			int bigendianp, int word, int sgned, int[] bitstream)
        {
            int host_endian = host_is_big_endian();
            int index=0;

            while(true)
            {
                if(decode_ready)
                {
                    float[][] pcm;
                    float[][][] _pcm=new float[1][][];
                    int[] _index=new int[getInfo(-1).channels];
                    int samples=vd.synthesis_pcmout(_pcm, _index);
                    pcm=_pcm[0];
                    if(samples!=0)
                    {
                        // yay! proceed to pack data into the byte buffer
                        int channels=getInfo(-1).channels;
                        int bytespersample=word * channels;
                        if(samples>length/bytespersample)samples=length/bytespersample;

                        // a tight loop to pack each size
                    {
                        int val;
                        if(word==1)
                        {
                            int off=(sgned!=0?0:128);
                            for(int j=0;j<samples;j++)
                            {
                                for(int i=0;i<channels;i++)
                                {
                                    val=(int)(pcm[i][_index[i]+j]*128.0 + 0.5);
                                    if(val>127)val=127;
                                    else if(val<-128)val=-128;
                                    buffer[index++]=(byte)(val+off);
                                }
                            }
                        }
                        else
                        {
                            int off=(sgned!=0?0:32768);

                            if(host_endian==bigendianp)
                            {
                                if(sgned!=0)
                                {
                                    for(int i=0;i<channels;i++)
                                    { // It's faster in this order
                                        int src=_index[i];
                                        int dest=i*2;
                                        for(int j=0;j<samples;j++)
                                        {
                                            val=(int)(pcm[i][src+j]*32767.0);
                                            if(val>32767)val=32767;
                                            else if(val<-32768)val=-32768;
                                            buffer[dest]=(byte)(val);
                                            buffer[dest+1]=(byte)((uint)val >> 8);
                                            dest+=bytespersample;
                                        }
                                    }
                                }
                                else
                                {
                                    for(int i=0;i<channels;i++)
                                    {
                                        float[] src=pcm[i];
                                        int dest=i;
                                        for(int j=0;j<samples;j++)
                                        {
                                            val=(int)(src[j]*32768.0 + 0.5);
                                            if(val>32767)val=32767;
                                            else if(val<-32768)val=-32768;
                                            buffer[dest]=(byte)((uint)(val+off) >> 8);
                                            buffer[dest+1]=(byte)(val+off);
                                            dest+=channels*2;
                                        }
                                    }
                                }
                            }
                            else if(bigendianp!=0)
                            {
                                for(int j=0;j<samples;j++)
                                {
                                    for(int i=0;i<channels;i++)
                                    {
                                        val=(int)(pcm[i][j]*32768.0 + 0.5);
                                        if(val>32767)val=32767;
                                        else if(val<-32768)val=-32768;
                                        val+=off;
                                        buffer[index++]=(byte)((uint)val >> 8);
                                        buffer[index++]=(byte)val;
                                    }
                                }
                            }
                            else
                            {
                                //int val;
                                for(int j=0;j<samples;j++)
                                {
                                    for(int i=0;i<channels;i++)
                                    {
                                        val=(int)(pcm[i][j]*32768.0 + 0.5);
                                        if(val>32767)val=32767;
                                        else if(val<-32768)val=-32768;
                                        val+=off;
                                        buffer[index++]=(byte)val;
                                        buffer[index++]=(byte)((uint)val >> 8);
                                    }
                                }
                            }
                        }
                    }

                        vd.synthesis_read(samples);
                        pcm_offset+=samples;
                        if(bitstream!=null)bitstream[0]=current_link;
                        return(samples*bytespersample);
                    }
                }

                // suck in another packet
                switch(process_packet(1))
                {
                    case 0:
                        return(0);
                    case -1:
                        return -1;
                    default:
                        break;
                }
            }
            return -1;
        }

Usage Example

Esempio n. 1
0
        /// <summary>
        /// Decodes the ogg-vorbis file
        /// </summary>
        /// <param name="input">Stream of the ogg-vorbis file</param>
        /// <returns>PCM-Wave version of the input</returns>
        public WaveFile Decode(Stream input)
        {
            MemoryStream output = new MemoryStream();
            WaveFile wf = new WaveFile();

            VorbisFile vf = new VorbisFile((FileStream)input, null, 0);
            Info inf = vf.getInfo(-1);

            wf.Channels = (short)inf.channels;
            wf.Frequency = inf.rate;
            wf.Bits = 16;

            Axiom.Core.LogManager.Instance.Write("SoundSystem: File is Ogg Vorbis "+inf.version.ToString()+" "+inf.rate.ToString()+"Hz, "+inf.channels.ToString()+" channels");

            int bufferlen = 4096;
            int result = 1;
            byte[] buffer = new byte[bufferlen];
            int[] section = new int[1];
            while(result != 0)
            {
                result = vf.read(buffer, bufferlen, 0, 2, 1, section);
                output.Write(buffer, 0, result);
            }

            output.Seek(0, SeekOrigin.Begin);
            wf.Data = output;

            return wf;
        }
All Usage Examples Of csvorbis.VorbisFile::read