Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Channel setVolume. How it works? — Gideros Forum

Channel setVolume. How it works?

unlyingunlying Guru
edited January 2013 in General questions
Hello.
Plz, explain me how it works. I spend more then hour and still can't find what i'm doing wrong
Here is code:
 music = Sound.new("mus/heal.wav")
		channel:setVolume(0.0)
		print(channel:getVolume())
		channel  = music:play(0, 1)
		print(channel:getVolume())
and sound plays. I have 0 and 1 in output.
How to set Volume to 0 forever?

Dislikes: underbliss, Fiam

Tagged:
+1 -1 (+0 / -2 )Share on Facebook

Comments

  • hnimhnim Member
    edited January 2013
    @unlying: if you want to set volume to 0 (or other value) forever, why you dont change your own gfx files?
    when you call music:play(), it returns new channel, so volume will back to 1. If you want to play sound/music with custom volume, you can write some util funtions
    function play(snd, vol)
         local channel = snd:play()
         channe:setVolume(vol)
         --return channel
    end
  • It is weird. To set Volume everytime when i play sound. I used it in my first projects by Gideros. But it is really-really weird. After that i just don't care about sound volume.
    It should be normal way to set volume for channel.
  • channel = music:play(0, 1) - this is the moment when channel is created.
    you can set the volume or get the volume after it.
  • I don't think that it is ok. If we really should set volume everytime when we play any sound, then it should be changed.
  • atilimatilim Maintainer
    @unlying When a new object is created (like SoundChannel), all of its properties are set to default values. And the default value of the volume of a SoundChannel object is 1.

    For example alpha is a property of Sprite and its default value is 1:
    sprite = Sprite.new()
    sprite:setAlpha(0)
    print(sprite:getAlpha())	--> prints 0
    sprite = Sprite.new()
    print(sprite:getAlpha())	--> prints 1
    And volume behaves exactly the same way.
  • And it could be easily by passed
    function Sound:setVolume(volume)
        self._volume = volume
    end
     
    Sound._play = Sound.play
     
    function Sound:play(...)
        local sound = self:_play(...)
        if self._volume then
            sound:setVolume(self._volume)
        end
        return sound
    end
    Something similar to this is done in GiderosCodingEasy only there you can control the volume of whole app (all sounds and musics of an app ;) )

    Likes: atilim

    +1 -1 (+1 / -0 )Share on Facebook
  • In russian we spell it like "crutches"
    It should be transparent way to set default values for these things.
  • asakharovasakharov Member
    edited January 2013
    @atilim, Is it possible to add method setDefaultVolume(value) to Sound class? Or pass it to constructor, or play method.
    And why do we need separation of Sound and Sound channel? To download music once and play over and over? (To save memory.)
    It would be great to have a resource pool with lazy loading for music and other resources. Then you could get rid of classes like SoundChannel
  • I guess what you are asking for is like a Master Volume, so if you set the volume to 0.2 then all sounds/music loaded and played thereafter should play at that volume. In the current scenario the only thing required would be to create a wrapper function that would set up the volume as required.
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
  • @unlying - there's volume control at the speaker already so simply make your sounds consistent if you don't want to do anything else. For a volume setting in your app, wrap sound:play to apply setVolume immediately. It's a great rabbit hole..

    I actually like SoundChannel, especially with the events.

    There's also the concept of sound layers (background, fx e.g. weapons, bonuses/pick-ups, music, UI). So maybe we can have the SoundChannel subscribe to event notifications for the "layer" it's on, to keep track of the global layer gain/volume, then it's a matter of wrapping setVolume() to apply that correction. I'm working on something similar, so I'll test this theory..

    http://esem.name/sound ♫ sound and music at ShadyLabs (Clouds of Steel, Aftermath Alvin)
  • gmarinovgmarinov Member
    edited June 2013
    Here's what I ended up with.
    Event.snd_layer_volume_change = "snd_slvc"	-- any string
     
    SoundLayer = Core.class(EventDispatcher)
    SoundLayer.name = "default"
    SoundLayer.vol  = 1
     
    function SoundLayer:setVolume(v)
    	self.vol = v	
    	self:dispatchEvent(Event.new(Event.snd_layer_volume_change))
    end
    function SoundLayer:getVolume()
    	return self.vol
    end
     
     
     
    SoundChannel.layer_ref 	= nil			-- holds SoundLayer reference
    SoundChannel.vol	= 1			-- full whack
    function SoundChannel:set_layer(l)		-- l is a SoundLayer or NIL to detach
    	if not l then
    		if self.layer_ref then 
    			self.layer_ref:removeEventListener(
    				Event.snd_layer_volume_change, 
    				self.on_layer_volume_change, 
    				self ) 
    		end
    	end
    	self.layer_ref = l
    	if (self.layer_ref) then
    		l:addEventListener(
    			Event.snd_layer_volume_change, self.on_layer_volume_change, self)
    	end
    end
     
    function SoundChannel:on_layer_volume_change()
    	self:set_volume_with_globalgain(self.vol)
    end
     
    function SoundChannel:set_volume_with_globalgain(vol) 
    	-- sanity checks
    	if not vol then return end
    	self.vol = vol
    	local global_vol = 1
    	if (self.layer_ref and self.layer_ref.vol) 
    		then global_vol = self.layer_ref:getVolume() 
    	end
     
    	vol = vol * global_vol
    	if (vol < 0) then vol = 0 end
    	if (vol > 1) then vol = 1 end
     
    	self:setVolume(vol);
    end
    http://esem.name/sound ♫ sound and music at ShadyLabs (Clouds of Steel, Aftermath Alvin)
Sign In or Register to comment.