Example #1
0
        public override int Read(byte[] buffer, int offset, int bytecount)
        {
            int remaining = 0;

            if (_headpos < _wavHeader.Length)
            {
                remaining = Math.Min(bytecount, _wavHeader.Length - _headpos);
                Buffer.BlockCopy(_wavHeader, _headpos, buffer, offset, remaining);
                _headpos  += remaining;
                bytecount -= remaining;
                offset    += remaining;
            }
            int samplecount = (int)Math.Min((long)ByteCountToSampleCount(bytecount), _totalSamples - _pos);

            if (samplecount > 0)
            {
                for (int s = 0; s < samplecount; s++)
                {
                    double samp = _gen.GetSample(_pos);
                    _pos++;

                    short scaled = (short)(samp * 0x7FF8);                     //32760
                    //TODO only supporting 16 bit PCM so hardcoding the bytes
                    buffer[offset + s * 2]     = (byte)(scaled & 0xFF);
                    buffer[offset + s * 2 + 1] = (byte)(scaled >> 8);

                    //32-bit float format
                    //Buffer.BlockCopy(sbuff, 0, buffer, offset, num); //this is kind of magic - copy a float array into a byte array
                }
            }

            return(remaining + SampleCountToByteCount(samplecount));
        }