question about handles

Hi,

when I load the same soundfile twice like so:

s1 = audio.loadSound( soundFile )
s2 = audio.loadSound( soundFile )

the two handles point to the same userdata:

print(s1)

print(s2)

=>

userdata: 0x2014bc910
userdata: 0x2014bc910

I can dispose both separately, whereas disposing s1 twice raises an error. I’m confused.

When I do the above, will I need 2 times the memory?

In other words: Is it safe to write audio.playSound( audio.loadSound( sfilename ) ) with the same sfilename more than once or will I fill up the memory with the same loaded sound file?

(I know I shouldn’t, because I can’t dispose the sounds this way. But the game is small and there are not many sounds so I’d like to leave memory management to the os when I leave the game.)

Hi @johannes_lalala,

It’s only necessary to load a specific audio file once. After it is loaded to a handle, you can play that file as many times as you need, in different places.

Also, “audio.playSound()” is not a valid API. I think you mean “audio.play()”… and when you use it, I suggest that you load the sound in advance, not “inline” as shown in your code. In other words, establish the audio handle first, then play it using audio.play().

Hope this helps,

Brent Sorrentino

Thanks Brent, for the prompt reply!

You’re right, I meant audio.play:)

I was going to write a wrapper function somewhere in a module like this:

local soundHandles = {}

playSound = function( filename )

 soundHandles[filename] = soundHandles[filename] or audio.loadSound(filename)

 return audio.play(soundHandles(filename))

end

I just wondered if the engine already does exactly this on my behalf internally, like it does with textures. If it does and if I don’t care about releasing the handles (for example when I have a very basic game consisting only of one scene), the wrapper wouldn’t be neccessary even counterproductive.

Sorry, the question wasn’t so clearly formulated, thanks in acvance, Johannes

Hi Johannes,

I will need to confirm with the engineering team if sounds work like textures, for example, if you intentionally (or accidentally) load the same sound 3 times, will it occupy 3 times that memory block? Or, if like textures, will it automatically know if the sound is loaded and simply point to the same memory block? I believe the engine is smart enough to know that it’s already loaded… your tests of how it prints the same “userdata” value would suggest this is true.

In general, if you’re doing a simple game, loading the “known” set of sounds is good practice, and you don’t need to dispose them if you’re going to continue using those sounds throughout the game.

Best regards,

Brent

Hi @johannes_lalala,

It’s only necessary to load a specific audio file once. After it is loaded to a handle, you can play that file as many times as you need, in different places.

Also, “audio.playSound()” is not a valid API. I think you mean “audio.play()”… and when you use it, I suggest that you load the sound in advance, not “inline” as shown in your code. In other words, establish the audio handle first, then play it using audio.play().

Hope this helps,

Brent Sorrentino

Thanks Brent, for the prompt reply!

You’re right, I meant audio.play:)

I was going to write a wrapper function somewhere in a module like this:

local soundHandles = {}

playSound = function( filename )

 soundHandles[filename] = soundHandles[filename] or audio.loadSound(filename)

 return audio.play(soundHandles(filename))

end

I just wondered if the engine already does exactly this on my behalf internally, like it does with textures. If it does and if I don’t care about releasing the handles (for example when I have a very basic game consisting only of one scene), the wrapper wouldn’t be neccessary even counterproductive.

Sorry, the question wasn’t so clearly formulated, thanks in acvance, Johannes

Hi Johannes,

I will need to confirm with the engineering team if sounds work like textures, for example, if you intentionally (or accidentally) load the same sound 3 times, will it occupy 3 times that memory block? Or, if like textures, will it automatically know if the sound is loaded and simply point to the same memory block? I believe the engine is smart enough to know that it’s already loaded… your tests of how it prints the same “userdata” value would suggest this is true.

In general, if you’re doing a simple game, loading the “known” set of sounds is good practice, and you don’t need to dispose them if you’re going to continue using those sounds throughout the game.

Best regards,

Brent