Better print for sampleplayer

Currently, in the Lua SDK, printing a playdate.sound.sampleplayer object outputs the following to the terminal:

playdate.sound.sampleplayer: 0x7fc99b862628

It’d be great to have more content here, for example exposing the length, volume, rate, offset and paused status. Here’s a polyfill I'm using at the moment.

playdate.sound.sampleplayer.__tostring = function(this)
	local output = "{\n"
	output = output .. "\t[__name] = " .. this.__name .. ",\n"
	output = output .. "\t[length] = " .. this:getLength() .. ",\n"
	output = output .. "\t[offset] = " .. this:getOffset() .. ",\n"
	output = output .. "\t[volume] = " .. this:getVolume() .. ",\n"
	output = output .. "\t[rate] = " .. this:getRate() .. ",\n"
	output = output .. "\t[isPlaying] = " .. tostring(this:isPlaying()) .. ",\n"
	output = output .. "}"
	return output
end

And here’s the output in the terminal:

{
	[__name] = playdate.sound.sampleplayer,
	[length] = 171.0552,
	[offset] = 0.9333333,
	[volume] = 1.0,
	[rate] = 1.0,
	[isPlaying] = true,
}
4 Likes

Brilliant! I completely forgot about the __tostring metamethod. I can imagine a CoreLibs/tostring.lua file that has implementations for all the Playdate types being really useful. I'll file that!

5 Likes