How to set volume of sfx and music differently?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
Gunroar:Cannon()
Party member
Posts: 598
Joined: Thu Dec 10, 2020 1:57 am

How to set volume of sfx and music differently?

Post 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
Last edited by Gunroar:Cannon() on Sun Sep 26, 2021 10:28 pm, edited 2 times in total.
Why can't pirates code in lua? Because they spend to much time at C! :P

-It might look like nonsense :roll: but there's so much proof.
-Some more if you pls... :? .
It has to be real then, all that talk about the end :shock: .
-How to be saved :huh:
User avatar
BrotSagtMist
Citizen
Posts: 64
Joined: Fri Aug 06, 2021 10:30 pm

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

Post by BrotSagtMist »

Wiki:
The volume for a Source, where 1.0 is normal volume. Volume cannot be raised above 1.0.
obey
User avatar
Gunroar:Cannon()
Party member
Posts: 598
Joined: Thu Dec 10, 2020 1:57 am

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

Post 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?
Why can't pirates code in lua? Because they spend to much time at C! :P

-It might look like nonsense :roll: but there's so much proof.
-Some more if you pls... :? .
It has to be real then, all that talk about the end :shock: .
-How to be saved :huh:
User avatar
pgimeno
Party member
Posts: 2825
Joined: Sun Oct 18, 2015 2:58 pm

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

Post 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.
User avatar
Gunroar:Cannon()
Party member
Posts: 598
Joined: Thu Dec 10, 2020 1:57 am

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

Post 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?
Why can't pirates code in lua? Because they spend to much time at C! :P

-It might look like nonsense :roll: but there's so much proof.
-Some more if you pls... :? .
It has to be real then, all that talk about the end :shock: .
-How to be saved :huh:
User avatar
pgimeno
Party member
Posts: 2825
Joined: Sun Oct 18, 2015 2:58 pm

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

Post 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.
User avatar
Gunroar:Cannon()
Party member
Posts: 598
Joined: Thu Dec 10, 2020 1:57 am

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

Post 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...
Why can't pirates code in lua? Because they spend to much time at C! :P

-It might look like nonsense :roll: but there's so much proof.
-Some more if you pls... :? .
It has to be real then, all that talk about the end :shock: .
-How to be saved :huh:
User avatar
pgimeno
Party member
Posts: 2825
Joined: Sun Oct 18, 2015 2:58 pm

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

Post 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.
User avatar
BrotSagtMist
Citizen
Posts: 64
Joined: Fri Aug 06, 2021 10:30 pm

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

Post 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.
obey
User avatar
Gunroar:Cannon()
Party member
Posts: 598
Joined: Thu Dec 10, 2020 1:57 am

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

Post 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?
Why can't pirates code in lua? Because they spend to much time at C! :P

-It might look like nonsense :roll: but there's so much proof.
-Some more if you pls... :? .
It has to be real then, all that talk about the end :shock: .
-How to be saved :huh:
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 34 guests