Crash on Attempt to Access Microphone

Hey yall,

I was trying to make a test app to mess with the microphone and ran into a crash. The following is my code

import "CoreLibs/graphics"
import "CoreLibs/sprites"
import "CoreLibs/animation"
import "CoreLibs/easing"

local gfx <const> = playdate.graphics
local geom <const> = playdate.geometry
local snd <const> = playdate.sound

local recording_rect
local recording_indicator
local speed_rect
local speed_indicator

local sound_buffer
local player

function startup()
    recording_rect = geom.rect.new(103, 3, 10, 10)
    recording_indicator = geom.rect.new(3, 7, 10, 8)
    speed_rect = geom.rect.new(103, 3, 10, 15)
    speed_indicator = geom.rect.new(3, 7, 10, 13)

    sound_buffer = snd.sample.new(5, snd.kFormat16bitMono)
    if sound_buffer == nil then
        print('Hey!!')
    end
    player = snd.sampleplayer.new(sound_buffer)
end

startup()

local rec_finished = false

function record_callback(buffer)
    rec_finished = true
end

function playdate.update()
    print("flag0")
    if playdate.buttonIsPressed(playdate.kButtonB) then
        print("flag1")
        rec_finished = false
        print("flag2")
        snd.micinput.recordToSample(sound_buffer, record_callback)
        print("flag3")
    else
        print("flag3.5")
        -- stop recording ( if not stopped already)
        if not rec_finished then
            snd.micinput.stopRecording()
            player:setSample(sound_buffer)
            rec_finished = true
        end
    end
    print("flag4")

    if playdate.buttonIsPressed(playdate.kButtonA) and not playdate.isButtonPressed(playdate.kButtonB) then
        if not player:isPlaying() then
            player:play()
        end
    end
    print("flag5")
end

Which produces the following output upon pressing B (note that flag0, flag4, and flag5 are spammed in console as expected)

flag0
flag1
flag2
flag3
flag4
flag5
update failed: lua_exec() expected 2 item on stack, but it has 4

This implies that the lua update function completes, and crashes before re-entering.
Hopefully I'm just making a dumb mistake somewhere!

This is the same bug as Resizing the Console can crash the Playdate Simulator which will be fixed in the next release. Sorry for the troubles.

Took a look at it this morning. This appears to be a different error than the one listed there, that is, the cause is different though the underlying end result may be the same.

The error was caused because I was attempting to start recording multiple times as long as B was held down. I changed my code to use callbacks, and it behaves as expected. Note that the error occured regardless of whether print or micinput.stopRecording() were called, and crashed both the simulator and the hardware.

import "CoreLibs/graphics"
import "CoreLibs/sprites"
import "CoreLibs/animation"
import "CoreLibs/easing"

local gfx <const> = playdate.graphics
local geom <const> = playdate.geometry
local snd <const> = playdate.sound

local recording_rect
local recording_indicator
local speed_rect
local speed_indicator

local sound_buffer
local player

function startup()
    recording_rect = geom.rect.new(103, 3, 10, 10)
    recording_indicator = geom.rect.new(3, 7, 10, 8)
    speed_rect = geom.rect.new(103, 3, 10, 15)
    speed_indicator = geom.rect.new(3, 7, 10, 13)

    sound_buffer = snd.sample.new(5, snd.kFormat16bitMono)
    if sound_buffer == nil then
        print('Hey!!')
    end
    player = snd.sampleplayer.new(sound_buffer)
end

startup()

local rec_finished = false

function record_callback(buffer)
    rec_finished = true
end

function playdate.BButtonDown()
    rec_finished = false
    snd.micinput.recordToSample(sound_buffer, record_callback)
end

function playdate.BButtonUp()
    if not rec_finished then
        snd.micinput.stopRecording()
        player:setSample(sound_buffer)
        rec_finished = true
    end
end

function playdate.AButtonDown()
    if not playdate.buttonIsPressed(playdate.kButtonB) then
        if not player:isPlaying() then
            player:play()
        end
    end
end


function playdate.update()
end