using System; using System.Collections.Generic; using System.Text; namespace NAudio.Wave { /// /// Helper class allowing us to modify the volume of a 16 bit stream without converting to IEEE float /// public class VolumeWaveProvider16 : IWaveProvider { private readonly IWaveProvider sourceProvider; private float volume; /// /// Constructs a new VolumeWaveProvider16 /// /// Source provider, must be 16 bit PCM public VolumeWaveProvider16(IWaveProvider sourceProvider) { this.Volume = 1.0f; this.sourceProvider = sourceProvider; if (sourceProvider.WaveFormat.Encoding != WaveFormatEncoding.Pcm) throw new ArgumentException("Expecting PCM input"); if (sourceProvider.WaveFormat.BitsPerSample != 16) throw new ArgumentException("Expecting 16 bit"); } /// /// Gets or sets volume. /// 1.0 is full scale, 0.0 is silence, anything over 1.0 will amplify but potentially clip /// public float Volume { get { return volume; } set { volume = value; } } /// /// WaveFormat of this WaveProvider /// public WaveFormat WaveFormat { get { return sourceProvider.WaveFormat; } } /// /// Read bytes from this WaveProvider /// /// Buffer to read into /// Offset within buffer to read to /// Bytes desired /// Bytes read public int Read(byte[] buffer, int offset, int count) { // always read from the source int bytesRead = sourceProvider.Read(buffer, offset, count); if (this.volume == 0.0f) { for (int n = 0; n < bytesRead; n++) { buffer[offset++] = 0; } } else if (this.volume != 1.0f) { for (int n = 0; n < bytesRead; n += 2) { short sample = (short)((buffer[offset + 1] << 8) | buffer[offset]); var newSample = sample * this.volume; sample = (short)newSample; // clip if necessary if (this.Volume > 1.0f) { if (newSample > Int16.MaxValue) sample = Int16.MaxValue; else if (newSample < Int16.MinValue) sample = Int16.MinValue; } buffer[offset++] = (byte)(sample & 0xFF); buffer[offset++] = (byte)(sample >> 8); } } return bytesRead; } } }