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

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
dizzykiwi3
Citizen
Posts: 58
Joined: Tue Jan 14, 2014 2:03 am

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

Post 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?
User avatar
slime
Solid Snayke
Posts: 3132
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

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

Post 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.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

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

Post 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

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
Post Reply

Who is online

Users browsing this forum: No registered users and 251 guests