play_file.lua 915 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. -- ****************************************************************************
  2. -- Play file with pcm module.
  3. --
  4. -- Upload jump_8k.u8 to spiffs before running this script.
  5. --
  6. -- ****************************************************************************
  7. function cb_drained(d)
  8. print("drained "..node.heap())
  9. file.seek("set", 0)
  10. -- uncomment the following line for continuous playback
  11. --d:play(pcm.RATE_8K)
  12. end
  13. function cb_stopped(d)
  14. print("playback stopped")
  15. file.seek("set", 0)
  16. end
  17. function cb_paused(d)
  18. print("playback paused")
  19. end
  20. file.open("jump_8k.u8", "r")
  21. drv = pcm.new(pcm.SD, 1)
  22. -- fetch data in chunks of FILE_READ_CHUNK (1024) from file
  23. drv:on("data", function(drv) return file.read() end)
  24. -- get called back when all samples were read from the file
  25. drv:on("drained", cb_drained)
  26. drv:on("stopped", cb_stopped)
  27. drv:on("paused", cb_paused)
  28. -- start playback
  29. drv:play(pcm.RATE_8K)