3

I am trying to increase the amplitude of the sound wave in my code. I have the buffer consisting of all the bytes needed to make the wave.

Here is my code for the audio Playback:

public void AddSamples(byte[] buffer)
{
   //somehow adjust the buffer to make the sound louder 

    bufferedWaveProvider.AddSamples(buffer, 0, buffer.Length);
    WaveOut waveout = new WaveOut();
    waveout.Init(bufferedWaveProvider);
    waveout.Play();

    //to make the program more memory efficient
    bufferedWaveProvider.ClearBuffer();
 }
Computeshorts
  • 596
  • 1
  • 7
  • 25
  • Have you looked at the examples? At least the AudioPlaybackDemo has a volume slider: https://github.com/naudio/NAudio/blob/master/NAudioDemo/AudioPlaybackDemo/AudioPlaybackPanel.cs Worked for me. – Marcel Mar 07 '18 at 06:18

2 Answers2

5

You could convert to an ISampleProvider and then try to amplify the signal by passing it through a VolumeSampleProvider with a gain > 1.0. However, you could end up with hard clipping if any samples go above 0.

WaveOut waveout = new WaveOut();
var volumeSampleProvider = new VolumeSampleProvider(bufferedWaveProvider.ToSampleProvider());
volumeSampleProvider.Volume = 2.0f; // double the amplitude of every sample - may go above 0dB
waveout.Init(volumeSampleProvider);
waveout.Play();

A better solution would be to use a dynamic range compressor effect, but NAudio does not come with one out of the box.

Mark Heath
  • 48,273
  • 29
  • 137
  • 194
1

I had a similar problem, too. But Could done with it with this link:

http://mark-dot-net.blogspot.hu/2009/10/playback-of-sine-wave-in-naudio.html

If you know all details about your sound It would be helpful I think

Birek
  • 85
  • 6