TangNano Vocoder
TinyTTS is a proof of concept of a Neural Text to Speech functionality running entirely on a microcontroller. It is a small VITS-style model with 1.6M parameters,
covering the whole pipeline. The conclusion: it is working but it is too slow, by a factor of about 20
on the fastest ESP32. Most of the work is the vocoder: about 440 million multiply-accumulates
per second of audio, about 90% of the model's total. The flow is most of the rest. No
microcontroller CPU does that in real time.
This project moves the neural model into hardware. A Sipeed Tang Nano 20K FPGA runs all of
it - text encoder, duration predictor, flow and vocoder - fed by its own DMA from SDRAM rather
than by a CPU, and plays the audio itself, over I2S or a PWM pin.
The microcontroller keeps only TinyTTS's G2P: it looks the words up in the
pronunciation dictionary and sends about 100 bytes of phoneme ids per sentence. That needs
1.1MB of flash and a few KB of RAM, so it runs on an RP2040 as well as on an ESP32
(examples/speak_rp2040).
- Speed: the whole model runs at 0.85-0.92 x real time on the board (3.9s of speech
in 3.59s).
- Quality: the board's output is bit-exact with a fixed-point model of the same
integers (all 172,032 samples of a 3.9s sentence, read back). That model matches TinyTTS
in float at 35.9 dB spectral SNR for the whole chain, and its rounded phoneme durations
agree with TinyTTS's for 431 of 432 phonemes.
- Program: the FPGA loads it (2.3MB) from its own flash at power-up, so the
microcontroller only sends sentences.
- Sentences: up to 3.9s each; split longer texts.
The earlier split - TinyTTS's front end on an ESP32-S3, z_p over the link - is still there
(tnv::LatentVocoder), and the FPGA runs both kinds of sentences. So far the library has
run from a PC against the board (tests/test_desktop.cpp, the same code as on a
microcontroller), not yet on an RP2040 or ESP32 wired to it. See
Plan and Status.
To try it, start with Getting Started; all documents are listed under
Documentation.
Architecture
text
|
+---------v-----------------------------------------+
| MCU (RP2040, ESP32, ...): TinyTTS's G2P |
| slim CMU dictionary 1.1MB (+ optional G2P model |
| for unknown words 1.0MB) -> phoneme ids |
+---------+-----------------------------------------+
| about 100 bytes per sentence ('X'), over SPI or UART
+---------v-----------------------------------------+
| Tang Nano 20K FPGA (program image 2.3MB, loaded |
| from its own flash at power-up: front end 39 ops, |
| flow 116 ops, vocoder 102 ops) |
| SPI/UART receiver -> SDRAM (8MB) |
| op by op, from SDRAM, 16-bit fixed point: |
| front end: embedding, text encoder, duration |
| predictor, durations, z_p with noise |
| flow: convolutions on the engine, |
| LayerNorm and attention unit -> z |
| vocoder: 16-lane convolution engine (normal and |
| transposed), requantize, residual, tanh|
| about 0.9 x real time
| -> I2S (onboard MAX98357A) or PWM pin |
+---------------------------------------------------+
- What runs where. The MCU runs G2P only: a dictionary lookup, no neural network, no
floating point (step by step in
using-the-library.md). The FPGA runs
everything else, about 470M multiply-accumulates per second of audio. (On an ESP32-S3,
TinyTTS alone needs about 34s for 1.5s of audio.)
- Only phonemes cross the link. Audio never goes back to the MCU; plain SPI or a UART is
plenty.
- The FPGA runs the model end to end, with no CPU in the inner loop. A CPU in the loop
was the bottleneck of the arduino-tangnano20k accelerator: in the fully optimized run, its
engine was busy for 81s of 34 minutes. The model is a program image of op descriptors and
weights. The FPGA loads it from its own flash at power-up, and a hardware scheduler with
SDRAM DMA runs it (docs/gateware.md).
- Latency. The flow attends over the whole sentence, so the FPGA computes a sentence
before it plays it. The first sound comes after that compute time. After that, the FPGA
computes the next sentence while the current one plays (it holds two). Splitting text into
sentences keeps the first wait short.
- Fixed point. Front end, flow and vocoder run in 16-bit fixed point with 8 to 12-bit
weights. The gateware is simulated bit for bit against a C++ model of the same integers,
and that model against TinyTTS in float (docs/studies.md).
Documentation
| Document |
Contents |
| Installation |
The Arduino library on an ESP32 or RP2040; the development environment: host tools, FPGA toolchains (Gowin EDA, open source), simulators |
| Getting Started |
From a PC: bitstream, program image in flash, speaking (tnv.py say), the desktop test |
| Using the Library |
The Arduino API, what runs on the microcontroller and what on the FPGA, examples (ESP32, RP2040), wiring, data headers |
| Plan and Status |
The project's phases, the current status (what has run on the board), what is open |
| Repository Layout |
Directories and the main source files |
| Studies |
The measurements behind the plan; the vocoder, the flow and the front end in fixed point (arithmetic, quality against TinyTTS); the model tool vocoder_model |
| Gateware |
The FPGA design: blocks, the flow unit, the front end, hardware image format, link protocol, pins, building, simulation, resources, speed |
Related projects
- TinyTTS - the model, the ESP32 code, and
docs/tangnano20k.md with the measurements on this board.
- arduino-tangnano20k - the Arduino
core for the Tang Nano 20K: the open-source FPGA toolchain and openFPGALoader, the INT8
dot-product accelerator this project replaces, and the verified pins.
- TangNanoFaust - the same SPI and UART pins
and the PLL settings.