audio - NodeMCU 播放 .wav 或 .mp3 文件

标签 audio lua esp8266 nodemcu

我不知道该走哪条路才能让 NodeMCU 播放音频。我想使用一到两秒的 wav 文件并驱动一个微型扬声器。目标是听到人声,而不是超高保真度。另外,我不想使用音频屏蔽或 SD 卡。我的文件足够小,可以直接从芯片运行所有内容。无需录制样本,只需回放即可。我应该使用什么?有什么例子吗?看来 sigma-delta 模块是一个很好的起点。

最佳答案

一次https://github.com/nodemcu/nodemcu-firmware/pull/1255已登陆 dev 分支,您可以执行以下操作 as documented :

-- ****************************************************************************
-- Play file with pcm module.
--
-- Upload jump_8k.u8 to spiffs before running this script.
--
-- ****************************************************************************


function cb_drained(d)
  print("drained "..node.heap())

  file.seek("set", 0)
  -- uncomment the following line for continuous playback
  --d:play(pcm.RATE_8K)
end

function cb_stopped(d)
  print("playback stopped")
  file.seek("set", 0)
end

function cb_paused(d)
  print("playback paused")
end

file.open("jump_8k.u8", "r")

drv = pcm.new(pcm.SD, 1)

-- fetch data in chunks of LUA_BUFFERSIZE (1024) from file
drv:on("data", file.read)

-- get called back when all samples were read from the file
drv:on("drained", cb_drained)

drv:on("stopped", cb_stopped)
drv:on("paused", cb_paused)

-- start playback
drv:play(pcm.RATE_8K)

Audio is expected as a mono raw unsigned 8 bit stream at sample rates between 1 k and 16 k samples per second. Regular WAV files can be converted with OSS tools like Audacity or SoX. Adjust the volume before the conversion.

关于audio - NodeMCU 播放 .wav 或 .mp3 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36324828/

相关文章:

python - 使用scipy.io.wavfile.write,librosa.output.write_wav之后获取变形的WAV文件

c# - 检测用户何时按下Shift键

arrays - Lua - 在循环中扩展表

lua - 一个Lua "Class"的两个实例是相同的 "object"

http - Arduino 集成开发环境/ESP8266 : Why does a button have to be pressed twice to get the correct output?

arduino - NodeMCU (ESP8266) 连接到 MQTT 代理时出现异常 28

php - 是否可以在 Web 应用程序上托管录音机和编辑器?如何?

html - 按顺序播放声音 HTML5

Lua解析并计算一个字符串

c++ - 休眠功能但不阻止其他功能和代码