Page 1 of 1

Convert a Source to a string? (Or some other solution)

Posted: Wed Mar 18, 2015 9:20 pm
by dizzykiwi3
So currently my dilemma is being able to change the volume to my game.

As it stands all my audio file are added in as sources, and their volumes have been set to a specific amount that makes all the sources sound great comparatively, but doesn't allow me to change the volume easily, or mute the volume.

So my current solution is this: currently almost all of my audio is run through a function whenever it is called to be played, so I'm thinking that whenever that function is called, it will run through a simple check:

Code: Select all

masterVolume = 0
sounds = {}
if sounds.toString(source) == nil then
source:setVolume(source:getVolume() * masterVolume)
sounds.toString(source) = true
end

and if the player ever needs to change the volume mid game, sounds will be set to an empty list.

However, I'm having trouble toStringifying the source, does anyone know of how to do this?

Or, if there's some obvious solution I'm missing, what is a better way to keep track of my audio?

Re: Convert a Source to a string? (Or some other solution)

Posted: Wed Mar 18, 2015 10:05 pm
by slime
You can use a Source as a key in a table directly, like this:

Code: Select all

masterVolume = 0
sounds = {}

if not sounds[source] then
    source:setVolume(source:getVolume() * masterVolume)
    sounds[source] = true
end
If you just want to set a global master volume scale factor though, you can use [wiki]love.audio.setVolume[/wiki] instead of doing that.

Or you could keep track of each Source's different volume settings separately and then do Source:setVolume(settingA * settingB * settingC * .. etc.) before playing it.

Re: Convert a Source to a string? (Or some other solution)

Posted: Wed Mar 18, 2015 10:07 pm
by s-ol
Why not keep all the sources in a table of tables with their "relative" volume?

Code: Select all

sounds = {
  { source = love.audio.newSource(...), volume = 0.3 },
  { source = love.audio.newSource(...), volume = 0.1 },
  { source = love.audio.newSource(...), volume = 0.9 }
}

function changeVolumeTo(volume)
  master_volume = volume
  for _,sound in ipairs(sounds) do
    sound.source:setVolume( sound.volume * master_volume )
  end
end