Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Sounds are not reference counted? — Gideros Forum

Sounds are not reference counted?

nat12nat12 Member
edited August 2012 in General questions
Hello,
I noticed in the execution log of our game, when we use the same png twice it is loaded once then the refcount increased (very good thing), but when we use the same wav twice the log message say it is created twice (see extract from our log at the end of this message)
It is the wav really loaded twice in memory? if yes, do exist a quick way to evitate the duplication without to have to rewrite our game scripts to explicitly share sounds?

log: create sound: Audio/select.wav
log: Inserting Menu/Popup/buttons_yes.png to the texture cache. Total memory is 42048 KB.
log: Inserting Menu/Popup/buttons_no.png to the texture cache. Total memory is 42112 KB.
log: create sound: Audio/select.wav
log: Increasing refcount of Menu/Popup/buttons_yes.png. New refcount is 2.
log: Increasing refcount of Menu/Popup/buttons_no.png. New refcount is 2.

Comments

  • You could load all your sounds once at the top of main into a global table and then reference them yourself.

    Could this be due to the fact that if you try and play a sound again whilst it's already playing then things might get messed up?

    If that is the case then you might need to load multiple copies of any sounds you intend to play that might be triggered at the same time.
    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
  • atilimatilim Maintainer
    edited August 2012 Accepted Answer
    An addition to @techdojo, yes, they are not reference counted as Textures. Therefore you should be more careful about not loading Sounds more than once (on the other hand, the only side effect is memory usage and nothing else)

    This issue is on the roadmap http://bugs.giderosmobile.com/issues/71 and I'll implement reference counting for Sounds.
  • understood, unluckily memory usage is our main issue, so we start to review sound management in our code
  • atilimatilim Maintainer
    edited August 2012
    Before starting to change your code, let me paste here an alternative Sound class that allows caching/reusing sounds. Give me a couple of minutes...
  • atilimatilim Maintainer
    Accepted Answer
    Can you put this to your init.lua? This new Sound class reuses a Sound instance if it's already on memory.
    OriginalSound = Sound
     
    local soundInstances = setmetatable({}, {__mode = "v"})
     
    Sound = Core.class()
     
    function Sound:init(filename)
    	if soundInstances[filename] ~= nil then
    		self.sound = soundInstances[filename]
    	else
    		self.sound = OriginalSound.new(filename)
    		soundInstances[filename] = self.sound
    	end
    end
     
    function Sound:play(...)
    	return self.sound:play(...)
    end
     
    function Sound:getLength()
    	return self.sound:getLength()
    end
  • Thank You Atilim, we will try this as soon as possible.
Sign In or Register to comment.