You may find the need to include basic audio features such as haptic feedback or tune generation to support an existing application. The advantage of using I2S for this is the very low CPU overhead.
This sample demonstrates how simple audio can be added to an application using the I2S ToneGenerator and RingTone libraries. The sample contains a selection of tunes.
The sample may be controlled via serial terminal, or by web interface.
Because I2S uses the serial RX pin, the serial port uses the alternate pin mappings. See ToneGenerator
for connection details.
You'll still need the regular serial port for programming. Here's a sample setup for Windows:
- Using NodeMCU with standard serial port connected to COM4
- Second USB/UART converter connected to alternate serial pins as COM8
The command line to build, flash and open a terminal would be:
make -j COM_PORT=COM8 COM_SPEED=115200 COM_PORT_ESPTOOL=COM4 COM_SPEED_ESPTOOL=921600 SPI_MODE=dio SPI_SIZE=4M
make flashinit
make flash
This is built using Bootstrap 4.3.1 and jQuery 3.4.1.
- Reboot
Un-installs the I2S driver then restarts the system after 5-seconds. The memory consumption drops as the DMA buffers are de-allocated.
- Play controls
- 9634 Stop
Tune playback stops at the current position, and the I2S driver is stopped so I2S interrupts are disabled. DMA buffers remain allocated.
- || Pause
Tune playback stops at the current position, but I2S interrupts remain active.
- 9655 Play
Resumes tune playback from stop/pause states.
- Voice and Mode
Selects available voice for the tone generator (Sine, Triangular, Sawtooth or Square wave).
Playback mode
- 8674 Sequential
- 128256 Random
- 8612 Previous
Play previous tune
- 8626 Rewind
Rewind to start of current tune
- 8614 Next
Play next tune
- 8594 Skip
Skip to a random tune
Shows the current tune and elapsed play time. A drop-down list shows all available tunes.
In addition to play controls, there are three graphs showing some statistics over a two-minute period. The data is sent from the ESP8266 via websocket once per second.
- CPU Usage
-
Gives an indication of processor loading. Try connecting a second web client to see what happens.
- Fill Time
-
Shows the time taken by the
ToneGenerator
to create and buffer tone waveforms.This graph has three traces, indicating time in milliseconds over the 1-second update period:
- Red: Longest time taken
- Green: Shortest time
- Black: Average time
You'll see more activity during complex, fast tunes but also for lower notes which require more samples (and larger buffers).
The progress bar indicates the total time taken over the update period.
- Memory Usage
-
The graph has three traces:
- Black: Available memory
- Red: Maximum memory used during the update period
- Green: Memory in use at the time of update
The red/green traces generally follow each other and represent the allocation/deallocation of tone buffers.