Sound.sampleplayer and memory

The optimizations continue! I have a clarifying question: if I have one long sample object and I load it into memory, will that memory be shared by two or more samplePlayers? I understand each samplePlayer will have a memory footprint, but I don't want to use this approach if it means the entire sample must be loaded for each samplePlayer.

I tried to answer this question for myself. However when I look at the memory window of the simulator I don't see anything changing. I can't tell if loaded assets are supposed to be represented in the window or not. Here's the code, any clarification would be helpful!

local theOneSample = playdate.sound.sample.new("any_audio_file")

local samplePlayers = {}

function playdate.update()
	playdate.graphics.clear()
	playdate.graphics.drawText("Sample Players loaded: " .. #samplePlayers, 10, 10)
end

function playdate.AButtonDown()
	-- adding several sample players doesn't appear to increase
	-- memory usage in the simulator
	table.insert(samplePlayers, playdate.sound.sampleplayer.new(theOneSample))
end

I think what you want is Window > Malloc Log (cmd+shift+i) (not the Lua Memory window). That will show that your understanding is correct! As long as you initialize the sample first and pass the sample to sampleplayer.new() instead of the path, the new sampleplayers will use the same sample with no additional (or at least negligible) memory use.

One thing I just noticed is that the reported active memory in Malloc Log isn't cleared between game launches so for the most accurate reading you should relaunch the Simulator before checking this.

2 Likes

Whoa! a window I didn't even know about! I can't access that window, it's greyed out in the menu for me. Any idea of the prerequisites?

Oh! Right. You need to enable the malloc pool. This limits the Simulator to the same RAM available on the device. Playdate > Malloc Pool > 16MB. I think it's disabled by default because it's not 100% 1-to-1 accurate with the hardware (pointers in the Simulator are 8 bytes versus 4 on the hardware) but I find it immensely helpful to keep enabled--especially after starting to add music and sound effects :sweat_smile:

5 Likes

Shaun you're officially my hero!

1 Like