Esempio n. 1
0
 /// <summary>
 /// Reads from this provider
 /// </summary>
 public int Read(byte[] buffer, int offset, int count)
 {
     int samplesNeeded = count / 4;
     WaveBuffer wb = new WaveBuffer(buffer);
     int samplesRead = source.Read(wb.FloatBuffer, offset / 4, samplesNeeded);
     return samplesRead * 4;
 }
Esempio n. 2
0
 /// <summary>
 /// Implements the Read method of IWaveProvider by delegating to the abstract
 /// Read method taking a short array
 /// </summary>
 public int Read(byte[] buffer, int offset, int count)
 {
     WaveBuffer waveBuffer = new WaveBuffer(buffer);
     int samplesRequired = count / 2;
     int samplesRead = Read(waveBuffer.ShortBuffer, offset / 2, samplesRequired);
     return samplesRead * 2;
 }
Esempio n. 3
0
 /// <summary>
 /// Implements the Read method of IWaveProvider by delegating to the abstract
 /// Read method taking a float array
 /// </summary>
 public int Read(byte[] buffer, int offset, int count)
 {
     WaveBuffer waveBuffer = new WaveBuffer(buffer);
     int samplesRequired = count / 4;
     int samplesRead = Read(waveBuffer.FloatBuffer, offset / 4, samplesRequired);
     return samplesRead * 4;
 }
 public void LoadNextChunk(IWaveProvider source, int samplePairsRequired)
 {
     int sourceBytesRequired = samplePairsRequired * 2;
     sourceSample = 0;
     sourceBuffer = BufferHelpers.Ensure(sourceBuffer, sourceBytesRequired);
     sourceWaveBuffer = new WaveBuffer(sourceBuffer);
     sourceSamples = source.Read(sourceBuffer, 0, sourceBytesRequired) / 2;
 }
Esempio n. 5
0
        /// <summary>
        /// Reads bytes from this wave stream
        /// </summary>
        /// <param name="destBuffer">The destination buffer</param>
        /// <param name="offset">Offset into the destination buffer</param>
        /// <param name="numBytes">Number of bytes read</param>
        /// <returns>Number of bytes read.</returns>
        public int Read(byte[] destBuffer, int offset, int numBytes)
        {
            int sourceBytesRequired = numBytes / 2;
            sourceBuffer = BufferHelpers.Ensure(sourceBuffer, sourceBytesRequired);
            int sourceBytesRead = sourceProvider.Read(sourceBuffer, offset, sourceBytesRequired);
            WaveBuffer sourceWaveBuffer = new WaveBuffer(sourceBuffer);
            WaveBuffer destWaveBuffer = new WaveBuffer(destBuffer);

            int sourceSamples = sourceBytesRead / 2;
            int destOffset = offset / 4;
            for (int sample = 0; sample < sourceSamples; sample++)
            {
                destWaveBuffer.FloatBuffer[destOffset++] = (sourceWaveBuffer.ShortBuffer[sample] / 32768f) * volume;
            }

            return sourceSamples * 4;
        }
Esempio n. 6
0
        /// <summary>
        /// Reads bytes from this wave stream
        /// </summary>
        /// <param name="destBuffer">The destination buffer</param>
        /// <param name="offset">Offset into the destination buffer</param>
        /// <param name="numBytes">Number of bytes read</param>
        /// <returns>Number of bytes read.</returns>
        public override int Read(byte[] destBuffer, int offset, int numBytes)
        {
            lock (lockObject)
            {
                int bytesWritten = 0;
                WaveBuffer destWaveBuffer = new WaveBuffer(destBuffer);

                // 1. fill with silence
                if (position < 0)
                {
                    bytesWritten = (int) Math.Min(numBytes, 0 - position);
                    for (int n = 0; n < bytesWritten; n++)
                        destBuffer[n + offset] = 0;
                }
                if (bytesWritten < numBytes)
                {
                    this.sampleProvider.LoadNextChunk(sourceStream, (numBytes - bytesWritten)/8);
                    float left, right;

                    int outIndex = (offset/4) + bytesWritten/4;
                    while (this.sampleProvider.GetNextSample(out left, out right) && bytesWritten < numBytes)
                    {
                        // implement better panning laws.
                        left = (pan <= 0) ? left : (left*(1 - pan)/2.0f);
                        right = (pan >= 0) ? right : (right*(pan + 1)/2.0f);
                        left *= volume;
                        right *= volume;
                        destWaveBuffer.FloatBuffer[outIndex++] = left;
                        destWaveBuffer.FloatBuffer[outIndex++] = right;
                        bytesWritten += 8;
                        if (Sample != null) RaiseSample(left, right);
                    }
                }
                // 3. Fill out with zeroes
                if (PadWithZeroes && bytesWritten < numBytes)
                {
                    Array.Clear(destBuffer, offset + bytesWritten, numBytes - bytesWritten);
                    bytesWritten = numBytes;
                }
                position += bytesWritten;
                return bytesWritten;
            }
        }
        /// <summary>
        /// Reads bytes from this WaveProvider
        /// </summary>
        public int Read(byte[] buffer, int offset, int count)
        {
            int sourceBytesRequired = count / 2;
            this.sourceBuffer = BufferHelpers.Ensure(this.sourceBuffer, sourceBytesRequired);
            WaveBuffer sourceWaveBuffer = new WaveBuffer(sourceBuffer);
            WaveBuffer destWaveBuffer = new WaveBuffer(buffer);

            int sourceBytesRead = sourceProvider.Read(sourceBuffer, 0, sourceBytesRequired);
            int samplesRead = sourceBytesRead / 2;
            int destOffset = offset / 2;
            for (int sample = 0; sample < samplesRead; sample++)
            {
                short sampleVal = sourceWaveBuffer.ShortBuffer[sample];
                destWaveBuffer.ShortBuffer[destOffset++] = (short)(LeftVolume * sampleVal);
                destWaveBuffer.ShortBuffer[destOffset++] = (short)(RightVolume * sampleVal);
            }
            return samplesRead * 4;
        }