/// <summary> /// Reads bytes from this wave stream /// </summary> /// <param name="destBuffer">The destination buffer</param> /// <returns>Number of bytes read.</returns> public int Read(Span <byte> destBuffer) { int samplesRequired = destBuffer.Length / 2; sourceBuffer = BufferHelpers.Ensure(sourceBuffer, samplesRequired); var s = new Span <float>(sourceBuffer, 0, samplesRequired); int sourceSamples = sourceProvider.Read(s); var outBuf = MemoryMarshal.Cast <byte, short>(destBuffer); //destBuffer.NonPortableCast<byte,short>(); for (int sample = 0; sample < sourceSamples; sample++) { // adjust volume float sample32 = sourceBuffer[sample] * volume; // clip if (sample32 > 1.0f) { sample32 = 1.0f; } if (sample32 < -1.0f) { sample32 = -1.0f; } outBuf[sample++] = (short)(sample32 * 32767); } return(sourceSamples * 2); }
/// <summary> /// Ensure the source buffer exists and is big enough /// </summary> /// <param name="sourceBytesRequired">Bytes required</param> protected void EnsureSourceBuffer(int sourceBytesRequired) { sourceBuffer = BufferHelpers.Ensure(sourceBuffer, sourceBytesRequired); }