recordToSample callback only working once - Bug or Lua nOOb?

I've had this problem before but can't rememeber how I coded around it, I've run into it again so have broken it down to the simplest code I can, the output of the code below is always:

recordSample()
recordSample()

The callback only works once, is this something I'm missing in Lua or a bug with the API?

buffer = nil
slot = 0
function recordSample()
	print("recordSample()")
	
	buffer = playdate.sound.sample.new(5, playdate.sound.kFormat16bitMono)
	playdate.sound.micinput.recordToSample(buffer, function(sample)
		slot += 1
		sample:save("" .. slot)
		recordSample()
	end)
end

recordSample()

function playdate.update()
end

(dupe of Dave Hayden šŸŠ: "@oppen@merveilles.town So the root problem is I jā€¦" - pdx.social so we have a copy here)

So the root problem is I just didn't expect anyone would recurse in this way. :sweat_smile: When recording finishes we get the callback function from a global reference, call it, then clear it--missing that it'd been reset in the callback. I'll try and get this working right, but in the mean time a workaround is to instead set a flag in the callback that you pick up in update() to indicate you should start recording again.

1 Like