Page 1 of 2

How to set volume of sfx and music differently?

Posted: Sun Sep 26, 2021 6:15 pm
by Gunroar:Cannon()
I saw that love.audio.setVolume is the master volume (for like everything) so everytime I make a source/sfx I set it to the volume I want, but it has the volume of what I set previously. How do I fix it? Am I doing it wrong?

Code: Select all

love.audio.setVolume(.5)
...
media.sfx.example_sound = love.audio.newSource(...)
media.sfx.example_sound:setVolume(1.0)--typed 10 here before, mistake, still not working
media.sfx.example_sound:play()--sounds soft

Re: How to set volume of sfx and music differently?

Posted: Sun Sep 26, 2021 6:31 pm
by BrotSagtMist
Wiki:
The volume for a Source, where 1.0 is normal volume. Volume cannot be raised above 1.0.

Re: How to set volume of sfx and music differently?

Posted: Sun Sep 26, 2021 10:23 pm
by Gunroar:Cannon()
:rofl: Oh, sorry, my mistake in that example I just quickly typed. Yes, I mean 1.0 instead of 10 (since 1 is max, though shouldn't 10 still just set it to highest, 1?). The sfx sound is still lower than if I set audio to 1 instead of .5, like the setVolume for source is ignored :?

-Also, another thing is that setPosition doesn't work for stereo(non mono) sounds. Is there a way around this? Can love2d change a source to mono at runtime or is it a must that I have to manually edit the sfx (a lot, dang you soundjay.com) thats a stereo to mono?

Re: How to set volume of sfx and music differently?

Posted: Sun Sep 26, 2021 10:35 pm
by pgimeno
love.audio.setVolume is applied at the end; it's a master volume control, as if your computer was connected to an amplifier and you turned the knob. If you turn the knob to zero, there's no sound anymore no matter the individual volumes of the samples.

Each sound has its own volume control. If you want certain sounds (effects) to have one volume and certain other sounds (music) to have another, you need some way in your code to set the volume of the sample depending on the type. For example, you could have a playMusic function that adjusts the volume of the sound to that of music, and a playEffect function that does the same for sound effects. Or do that with newMusic and newEffect, or whatever.

As for stereo/mono and setPosition, no, no way around it. Convert it to mono; if you need both channels separately then convert each channel to a mono one.

Re: How to set volume of sfx and music differently?

Posted: Sun Sep 26, 2021 10:37 pm
by Gunroar:Cannon()
Hmmm...So I shouldn't use audio.setVolume?
More on setPosition is that sometimes the sound seems quiet when the source is close (16x16 game, gun shots aren't audible at 5 tiles away). Is there a way to fix this?

Re: How to set volume of sfx and music differently?

Posted: Sun Sep 26, 2021 10:50 pm
by pgimeno
I'd adjust the relative volumes of effects and music so that one is set to 1.0; once satisfied, if everything is too loud you can use love.audio.setVolume to lower it, and offer a volume option to the player.

As for distances, try with Source:setRolloff and/or Source:setAttenuationDistances.

Re: How to set volume of sfx and music differently?

Posted: Sun Sep 26, 2021 10:55 pm
by Gunroar:Cannon()
Any example with the love.audio. Unfortunately I'm confused *sigh* and sleepy. So I only use one setVolume that works for both? But the music is really louder than the sfx by default...

Re: How to set volume of sfx and music differently?

Posted: Sun Sep 26, 2021 11:02 pm
by pgimeno
Gunroar:Cannon() wrote: Sun Sep 26, 2021 10:55 pm Any example with the love.audio. Unfortunately I'm confused *sigh* and sleepy. So I only use one setVolume that works for both? But the music is really louder than the sfx by default...
OK, if the music is the louder one, consider something like this:

Code: Select all

local MUSIC_VOLUME = 0.5 -- play with this value until music and sfx sound balanced
local function newMusic(...)
  local source = love.audio.newSource(...)
  source:setVolume(MUSIC_VOLUME)
  ...
  return source
end
For completion you could have a similar one for sfx too, up to you; but since sfx will have volume 1 and that is the default, it's not strictly necessary.

If everything (not just the music or the sfx alone) still sounds too loud, you can adjust the global volume with love.audio.setVolume. If you want, you can offer a GUI for the user to change the volume.

Re: How to set volume of sfx and music differently?

Posted: Sun Sep 26, 2021 11:13 pm
by BrotSagtMist
Volume is sound*master*system*amp.
Its wise to keep the volumes you can control as close to 1 as possible. Lowering volume from within löve simply degrades the signal to noise ratio you get from the speaker, thats not player friendly, let the OS handle the final volume.
More on setPosition is that sometimes the sound seems quiet when the source is close (16x16 game, gun shots aren't audible at 5 tiles away). Is there a way to fix this?
Define close, note that the sound lib has no idea what the size of a tile is first, you need to either tell it or scale the input number according to your tile sizes, aka multiply X/Y by a random numbers till you think it sounds OK.

Re: How to set volume of sfx and music differently?

Posted: Tue Sep 28, 2021 6:50 pm
by Gunroar:Cannon()
Okay, pgimeno! Nice, that should work, thnx.

@BrotSagtMist
Okay, so what you're saying is that I should edit the volume through editing the sound file externally?
Close is like 4 tiles away. Should I times by 4*16 :P Though I'm still yet to try Source:setAttenuationDistances . Though what use is rolloff though?