BitOrchestra.EvaluatorStream.Read C# (CSharp) Method

Read() public method

public Read ( byte buffer, int offset, int count ) : int
buffer byte
offset int
count int
return int
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (this._Evaluator == null)
            {
                return 0;
            }

            // Its more useful to have a sample count rather than a byte count.
            int samplecount = (count / this._SampleSize);

            // If exporting, make sure only to write "Options.Length" samples
            if (this._Exporting)
                samplecount = Math.Min(samplecount, (int)(this._Options.Length - this._Parameter));

            // Find sample size and shift amount
            int sampsize = this._SampleSize;
            int shift = this._Shift;

            int ocount = samplecount * sampsize;
            while (true)
            {
                int sampsleft = this._Buffer.Length - this._Offset;
                int toread = Math.Min(samplecount, sampsleft);
                int ofs = this._Offset;
                for (int t = 0; t < toread; t++)
                {
                    Value val = this._Buffer[ofs];

                    if (this._SampleSize == 1)
                    {
                        // Correct byte sign
                        val += (128 >> shift);
                    }

                    if (shift == 0 && this._SampleSize == 1)
                    {
                        // Direct byte copying is faster
                        buffer[offset] = (byte)val;
                        offset++;
                    }
                    else
                    {
                        // Copy the first byte manually, since a unique leftshift must be applied.
                        buffer[offset] = (byte)(val << shift);
                        offset++;

                        int sf = 8 - shift;
                        for (int i = 1; i < sampsize; i++)
                        {
                            buffer[offset] = (byte)(val >> sf);
                            sf += 8;
                            offset++;
                        }
                    }

                    ofs++;
                }
                this._Offset = ofs;
                samplecount -= toread;

                if (this._Offset >= this._Buffer.Length)
                {
                    this._Advance();
                    continue;
                }
                else
                {
                    break;
                }
            }
            return ocount;
        }