I am not sure which road to go down in order to get a NodeMCU to play audio. I would like to use one to two second wav files and drive a tiny speaker. The goal is to hear a human voice, nothing super high fidelity. Additionally I don't want to use an audio shield or sd card. My files will be small enough to run everything right from the chip. There is no need to record samples, just playback. What should I use and are there any examples out there? It seems the sigma-delta module is a good place to start.
Asked
Active
Viewed 6,646 times
2
-
You may be interested in https://github.com/nodemcu/nodemcu-firmware/issues/1085 then. – Marcel Stör Mar 31 '16 at 21:06
1 Answers
2
Once 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.

Marcel Stör
- 22,695
- 19
- 92
- 198
-
Thank you for your help. I have checked out the documentation you mentioned and have a firmware build with pcm on it. I am trying to run a small speaker module like this: http://img.dxcdn.com/productimages/sku_138322_2.jpg My issue is that pcm.SD is always nil. Do you have any ideas? – Joe Andolina Apr 26 '16 at 06:01
-
I never tried myself but does your firmware include the sigma-delta module? – Marcel Stör Apr 26 '16 at 06:11
-
No, I don't think I have sigma-delta in my firmware. I will add it and try again. – Joe Andolina Apr 26 '16 at 08:57
-
-
Got a new build together with pcm and sigma-delta. It works! Thank you everyone in the NodeMCU community. – Joe Andolina Apr 27 '16 at 04:31
-
-
Worth noting: https://github.com/nodemcu/nodemcu-firmware/pull/1255/files/517ac37d23f6ed53d447b67d5e8f225f41cfbe50#r61204154 – Marcel Stör Apr 28 '16 at 20:54
-
@JoeAndolina I know its bit older, But I think it would be great if you can let us know how you did this. A sample project will be ideal. Thanks is advance. – John Fonseka Jun 19 '17 at 17:09
-
@JohnFonseka, I don't understand your request. The example I posted and which the OP used as a foundation I believe is straight from the NodeMCU examples at https://github.com/nodemcu/nodemcu-firmware/blob/master/lua_examples/pcm/play_file.lua – Marcel Stör Jun 19 '17 at 18:37
-
@MarcelStör, What I mean is, how to upload .u8 file, which pins to connect speaker/amplifier, etc. Basically a sample project which playing music. Because I am a beginner to this subject and to NodeMCU. Thank you very much for your reply. – John Fonseka Jun 20 '17 at 07:10
-
@JohnFonseka the code above pretty much _is_ the sample project. How to upload: https://nodemcu.readthedocs.io/en/latest/en/upload/, hardware and wiring: https://nodemcu.readthedocs.io/en/latest/en/modules/pcm/. That's all I have. – Marcel Stör Jun 20 '17 at 13:18