micinput.getLevel() always returns zero

As title says, I upgraded an old project to SDK 1.4.0 (from something like 0.11.0 I think) where it worked fine and now playdate.sound.micinput.getLevel() always returns 0.

Recording audio via the microphone works just fine, it's just getLevel() that returns 0 no matter what.

Happens in Simulator and on Device.

1 Like

Are you calling playdate.sound.micinput.startListening() first? Greg filed a bug about getLevel() returning 0 a while back, turns out that was the problem. Let me know if that doesn't do it!

I guess startListening() was not necessary in earlier SDK versions. When I add a call to that function it works :+1:.
But it seems counterintuitive, since in my case I'm already calling recordToSample() so the microphone should be active anyway.

Is there a reason for me to not just call startListening() once at startup and then never worry about it? (I guess battery consumption is the answer)

Related to that I found that when I call playdate.sound.micinput.startListening() while a call to playdate.sound.micinput.recordToSample() is still recording, the game will crash on the simulator but not on device:

PlaydateSimulator: ../source/pd_platform.c:304: startRecording: Assertion `record_callback == NULL' failed.

On the other hand calling recordToSample again, while another call is still recording, will crash on simulator as well as device: (I have no use case for recording two samples at once, it's just something I noticed)

update failed: lua_exec() expected 1 items on stack, but it has 3

A minimal example to reproduce it:

local gfx = playdate.graphics
local snd = playdate.sound

function playdate.AButtonDown()
  local s = snd.sample.new(1, snd.kFormat16bitMono)
  snd.micinput.recordToSample(s,
      function()
          s:play()
          snd.micinput.stopListening()
      end
  )
  snd.micinput.startListening()
end

function playdate.update()
  gfx.clear()
  gfx.setColor(gfx.kColorBlack)
  gfx.setLineWidth(7)

  local level = snd.micinput.getLevel()
  print(level)
  gfx.drawLine(10, 240 - level * 240 - 10, 400 - 10, 240 - level * 240 - 10)
end