NodeMCU 播放 .wav 或 .mp3 文件

NodeMCU playing .wav or .mp3 files

我不确定要让 NodeMCU 播放音频该走哪条路。我想使用一到两个第二个 wav 文件并驱动一个微型扬声器。目标是听到人声,而不是超高保真度。此外,我不想使用音频屏蔽或 SD 卡。我的文件足够小 运行 一切都来自芯片。无需录制样本,只需回放即可。我应该使用什么,那里有什么例子吗?看起来 sigma-delta 模块是一个很好的起点。

一次https://github.com/nodemcu/nodemcu-firmware/pull/1255 has landed on dev branch you can do the following 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.