9

How can I adjust the volume of a sound in the OpenAL sound library?

Joehot200
  • 1,070
  • 15
  • 44
openfrog
  • 40,201
  • 65
  • 225
  • 373
  • related: http://stackoverflow.com/questions/3982723/audio-processing-playing-with-volume-level – cregox Jan 14 '11 at 21:38

2 Answers2

17
float newVolume = 0.4f;
alSourcef(currentSourceID, AL_GAIN, newVolume);
Jay
  • 4,480
  • 3
  • 25
  • 22
  • I believe he meant adjusting the device volume rather than the sound source gain. At least that's what **I** was hoping for when I found this question. – cregox Feb 21 '11 at 04:43
  • 12
    device volume? Maybe you should look into the listener object? Then you can easily set the listener's AL_GAIN with: alListenerf(currentSourceID, AL_GAIN, newVolume); – Jay Feb 21 '11 at 05:29
  • He said "of a sound". – jv110 Sep 09 '18 at 19:56
3

You can change the global volume by setting the gain of the listener.

void Listener::setVolume(float v)
{
    Assert::isTrue(0 <= v && v <= 1);
    alListenerf(AL_GAIN, v);
}

float Listener::getVolume()
{
    ALfloat v;
    alGetListenerf(AL_GAIN, &v);
    return v;
}
ShenMian
  • 61
  • 5
  • 1
    Welcome to StackOverflow! Thank you for your contribution, but it would be great if you could add some comments or notes about your code too! Will still upvote :) – Haris Nadeem Aug 05 '21 at 15:54