main.lua 870 B

123456789101112131415161718192021222324252627282930313233
  1. -- Measures amplitude of microphone over time and uses it to scale a circle
  2. amplitude = 0
  3. function lovr.load()
  4. name = lovr.audio.getMicrophoneNames()[1]
  5. assert(name, 'No microphone found!')
  6. microphone = lovr.audio.newMicrophone(name)
  7. microphone:startRecording()
  8. print('Recording from ' .. name)
  9. end
  10. function lovr.update(dt)
  11. if microphone:isRecording() then
  12. local soundData = microphone:getData()
  13. if soundData then
  14. local samples = soundData:getSampleCount()
  15. local totalAmplitude = 0
  16. for i = 0, samples - 1 do
  17. totalAmplitude = totalAmplitude + math.abs(soundData:getSample(i))
  18. end
  19. amplitude = amplitude + (totalAmplitude / samples - amplitude) * (1 - .05 ^ dt)
  20. end
  21. end
  22. end
  23. function lovr.draw()
  24. lovr.graphics.setColor(1, 1, 1, .5)
  25. lovr.graphics.circle('line', 0, 1.7, -1, amplitude * 20 + .01)
  26. end