Franky
Franky

Reputation: 45

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

Upvotes: 0

Views: 1618

Answers (2)

Franky
Franky

Reputation: 45

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;
    }
}

Upvotes: 2

Franky
Franky

Reputation: 45

Made with the help of PanningSampleProvider. But for this you have to convert to mono. Also, the reaction to changing Pan - occurs with a slight delay. How can this be avoided? How to work with stereo and just change the volume of its right and left channels? I think this should work much faster.

_audioFile = new AudioFileReader(_fileName) { Volume = _volume };
var mono = new StereoToMonoSampleProvider(_audioFile) { LeftVolume = 1f, RightVolume = 1f };
var panner = new PanningSampleProvider(mono);

_outputDevice.Init(panner);

Upvotes: 0

Related Questions