Search code examples
c#.netaudiovolumenaudio

Changing the sound balance in the right and left channels with NAudio


When playing sound, I need to adjust the volume of the right and left channels separately. I have the class to play sound:

public class SoundPlayer
{
    private WaveOutEvent _outputDevice;
    private AudioFileReader _audioFile;
    private float _volume = 1f;

    public float Volume
    {
        get => _volume;
        set
        {
            _volume = value;

            if (_audioFile != null)
                _audioFile.Volume = value;
        }
    }

    public void Play(string fileName)
    {
        if (_outputDevice == null)
        {
            _outputDevice = new WaveOutEvent();
            _outputDevice.PlaybackStopped += (sender, args) =>
            {
                _outputDevice.Dispose();
                _outputDevice = null;
                _audioFile.Dispose();
                _audioFile = null;
            };
        }
        if (_audioFile == null)
        {
            _audioFile = new AudioFileReader(fileName) { Volume = _volume };
            _outputDevice.Init(_audioFile);

        }
        else
        {
            if (string.IsNullOrWhiteSpace(fileName))
                _outputDevice = null;
            else
            {
                if (_audioFile.FileName != fileName)
                {
                    _audioFile = new AudioFileReader(fileName) { Volume = _volume };
                    _outputDevice.Init(_audioFile);
                }
            }
        }

        _outputDevice?.Play();
    }

    public void Stop()
    {
        _outputDevice?.Stop();
    }
}

But in this class you can only adjust the overall volume. How to make such a property: soundPlayer.LeftChannelVolume = 1.0f soundPlayer.RightChannelVolume = 0.5f


Solution

  • I made my provider and it worked!) If anyone needs it, then here:

    /// <summary>
    /// Very simple sample provider supporting adjustable gain
    /// </summary>
    public class VolumeStereoSampleProvider : ISampleProvider
    {
        private readonly ISampleProvider source;
    
        /// <summary>
        /// Allows adjusting the volume left channel, 1.0f = full volume
        /// </summary>
        public float VolumeLeft { get; set; }
    
        /// <summary>
        /// Allows adjusting the volume right channel, 1.0f = full volume
        /// </summary>
        public float VolumeRight { get; set; }
    
        /// <summary>
        /// Initializes a new instance of VolumeStereoSampleProvider
        /// </summary>
        /// <param name="source">Source sample provider, must be stereo</param>
        public VolumeStereoSampleProvider(ISampleProvider source)
        {
            if (source.WaveFormat.Channels != 2)
                throw new ArgumentException("Source sample provider must be stereo");
    
            this.source = source;
            VolumeLeft = 1.0f;
            VolumeRight = 1.0f;
        }
    
        /// <summary>
        /// WaveFormat
        /// </summary>
        public WaveFormat WaveFormat => source.WaveFormat;
    
        /// <summary>
        /// Reads samples from this sample provider
        /// </summary>
        /// <param name="buffer">Sample buffer</param>
        /// <param name="offset">Offset into sample buffer</param>
        /// <param name="sampleCount">Number of samples desired</param>
        /// <returns>Number of samples read</returns>
        public int Read(float[] buffer, int offset, int sampleCount)
        {
            int samplesRead = source.Read(buffer, offset, sampleCount);
    
            for (int n = 0; n < sampleCount; n += 2)
            {
                buffer[offset + n] *= VolumeLeft;
                buffer[offset + n + 1] *= VolumeRight;
            }
    
            return samplesRead;
        }
    }