Audio Causing Memory To Keep Increasing In Each Restart

Before I integrated any audio, the game was smooth and the memory was stable. However, when I placed cheap sounds for the buttons and for during the game scene, the memory used keeps increasing by 8 megabytes each time it goes to the restart scene until the game gets too slow and the memory used gets over 100 megabytes. If I remove the audio, the memory gets stable again.

function onCollision( event )
      if ( event.phase == “began” ) then
        composer.gotoScene( “restart”)
        rotate=50
        media.playSound( “x.wav”)
  
      end
end  

I simply integrated the sounds in the events and in functions and not in the main.lua. What code should I use so that the memory stays stable with the sounds? 

There are a couple of things to consider here.  First is loading vs. playing.  Sounds come in a couple of different categories:

1.  Short sounds that get reused frequently like a beep, click or explosion.

2.  Longer sounds that may only happen at specific events, like say a voice over.

3.  Long sounds that play in the background for a long duration.

For #1 it’s best to load these in main.lua once and have them available globally and then you just call audio.play() when you need them.  You never dispose them, never reload them, etc. 

For #2, these sounds eat up a lot of memory and don’t need to be around outside of the scene you’re in.  There are two schools of thought.  a) load them in scene:create() and dispose them in scene:destroy() or load them in scene:show()'s did phase and dispose them in either of the scene:hide()'s phases. You don’t want loading big files to impact the performance of the scene transition.  If it’s a sound you need right away, use audio.loadStream() instead of audio.loadSound().

For #3, if the background track is scene dependent, then treat as #2 above.  But If the background tracks live beyond the life of a scene, then it’s more like #1, load them in and keep them in memory.

The second thing to consider is the sounds themselves.  Do you really need stereo sounds playing at 48,000 samples per second when it probably sounds just as good on mobile devices at 11,025 samples per second and mono. (same sound comes out both speakers)?  Those high sample rate stereo audio tracks eat up a boat lad of memory.

Rob

Thanks for the reply. I only used short beep sounds. However, the memory is still increasing by 5 megabytes every time it goes to the restart scene. Eventually it accumulates until the game is too slow to play, and the memory used reaches over 120 megabytes. The audio is causing this increase, so how do I dispose of it and keep the memory stable?  

local flapSound = audio.loadSound( “flap.wav” )
local yesSound = audio.loadSound( “yes.wav” )
local bamSound = audio.loadSound( “bam.wav” )
local clickSound = audio.loadSound( “click.wav” )

I just incorporated these in the main.lua, so how do I get it to dispose right after it returns to the start scene to keep the memory low? 
 

That audio that you have should not be causing that kind of memory increase.  I think we are going to see a little more code.

Rob

Sure. I have changed their format to mp3 and it uses a little less. And  what code?

There are a couple of things to consider here.  First is loading vs. playing.  Sounds come in a couple of different categories:

1.  Short sounds that get reused frequently like a beep, click or explosion.

2.  Longer sounds that may only happen at specific events, like say a voice over.

3.  Long sounds that play in the background for a long duration.

For #1 it’s best to load these in main.lua once and have them available globally and then you just call audio.play() when you need them.  You never dispose them, never reload them, etc. 

For #2, these sounds eat up a lot of memory and don’t need to be around outside of the scene you’re in.  There are two schools of thought.  a) load them in scene:create() and dispose them in scene:destroy() or load them in scene:show()'s did phase and dispose them in either of the scene:hide()'s phases. You don’t want loading big files to impact the performance of the scene transition.  If it’s a sound you need right away, use audio.loadStream() instead of audio.loadSound().

For #3, if the background track is scene dependent, then treat as #2 above.  But If the background tracks live beyond the life of a scene, then it’s more like #1, load them in and keep them in memory.

The second thing to consider is the sounds themselves.  Do you really need stereo sounds playing at 48,000 samples per second when it probably sounds just as good on mobile devices at 11,025 samples per second and mono. (same sound comes out both speakers)?  Those high sample rate stereo audio tracks eat up a boat lad of memory.

Rob

Thanks for the reply. I only used short beep sounds. However, the memory is still increasing by 5 megabytes every time it goes to the restart scene. Eventually it accumulates until the game is too slow to play, and the memory used reaches over 120 megabytes. The audio is causing this increase, so how do I dispose of it and keep the memory stable?  

local flapSound = audio.loadSound( “flap.wav” )
local yesSound = audio.loadSound( “yes.wav” )
local bamSound = audio.loadSound( “bam.wav” )
local clickSound = audio.loadSound( “click.wav” )

I just incorporated these in the main.lua, so how do I get it to dispose right after it returns to the start scene to keep the memory low? 
 

That audio that you have should not be causing that kind of memory increase.  I think we are going to see a little more code.

Rob

Sure. I have changed their format to mp3 and it uses a little less. And  what code?