shiroxxblank

audiotest

Jun 29th, 2022 (edited)
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. local dfpwm = require("cc.audio.dfpwm")
  2. local speaker = peripheral.find("speaker")
  3.  
  4. -- Speakers play at 48kHz, so 1.5 seconds is 72k samples. We first fill our buffer
  5. -- with 0s, as there's nothing to echo at the start of the track!
  6. local samples_i, samples_n = 1, 48000 * 1.5
  7. local samples = {}
  8. for i = 1, samples_n do samples[i] = 0 end
  9.  
  10. local decoder = dfpwm.make_decoder()
  11. for chunk in io.lines("data/example.dfpwm", 16 * 1024) do
  12. local buffer = decoder(chunk)
  13.  
  14. for i = 1, #buffer do
  15. local original_value = buffer[i]
  16.  
  17. -- Replace this sample with its current amplitude plus the amplitude from 1.5 seconds ago.
  18. -- We scale both to ensure the resulting value is still between -128 and 127.
  19. buffer[i] = original_value * 0.6 + samples[samples_i] * 0.4
  20.  
  21. -- Now store the current sample, and move the "head" of our ring buffer forward one place.
  22. samples[samples_i] = original_value
  23. samples_i = samples_i + 1
  24. if samples_i > samples_n then samples_i = 1 end
  25. end
  26.  
  27. while not speaker.playAudio(buffer) do
  28. os.pullEvent("speaker_audio_empty")
  29. end
  30.  
  31. -- The audio processing above can be quite slow and preparing the first batch of audio
  32. -- may timeout the computer. We sleep to avoid this.
  33. -- There's definitely better ways of handling this - this is just an example!
  34. sleep(0.05)
  35. end
Add Comment
Please, Sign In to add comment