使用 Kokoro 实现本地、低 CPU 占用且高质量的语音合成 (TTS)
Local, CPU-Friendly, High-Quality TTS (Text-to-Speech) with Kokoro

原始链接: https://ariya.io/2026/03/local-cpu-friendly-high-quality-tts-text-to-speech-with-kokoro/

得益于 **Kokoro** 等模型,本地语音生成变得既高度易用又注重隐私。Kokoro 仅有 8200 万参数,仅需使用计算机 CPU 即可生成逼真的多语言语音,从而将 GPU 释放出来,用于大语言模型推理等其他任务。 使用 **Kokoro-FastAPI** 容器可以轻松部署 Kokoro,该容器提供了 Web 用户界面和兼容 OpenAI 的 API。这使得开发者能够利用 Python 或 JavaScript,将高质量的语音合成(TTS)轻松集成到现有应用程序中。该系统效率极高,即使在 12 年前的英特尔 i7-4770K 处理器上,也能在几秒钟内生成清晰的音频。用户还可以从 50 种不同的音色中进行选择,以自定义输出效果。 对于有更多功能需求的用户,**Speaches** 是一个值得关注的替代方案。它提供了兼容 OpenAI 的接口,并内置了 Whisper 模型,为语音转文字(STT)和文字转语音(TTS)需求提供了一站式解决方案。通过在本地运行这些工具,用户无需牺牲数据隐私,即可将大语言模型的交互方式从文本转变为语音。

这篇 Hacker News 讨论介绍了 **Kokoro**,这是一款本地、对 CPU 友好的文本转语音(TTS)引擎,即使在对资源要求严苛的环境(如对讲系统)中,也能保持高质量的输出。 该讨论引发了关于性能最佳 TTS 替代方案的辩论。用户将 Kokoro 与 **Pocket TTS** 进行了比较,参与者对于 Pocket TTS 的 PyTorch 版本和 ONNX 版本在速度与质量上的优劣各执一词。一些用户支持 ONNX (int8) 版本,认为其在 CPU 上的表现更高效;另一些用户则倾向于 PyTorch 实现。此外,讨论中还指出,利用 Apple M2 Pro 或 AMD Ryzen 7 等现代芯片上的集成显卡进行硬件加速,可以进一步优化这些模型的推理速度。
相关文章

原文

Just a few years ago, realistic local speech generation seemed unimaginable. Today, its quality is exceptional and, crucially, it delivers these results without compromising privacy.

The video above showcases audio generated from a sample text, running entirely on the local machine previously discussed in the GTX 1080 Ti for Local LLM article. While this machine has a dedicated GPU, the GPU is fully reserved for LLM inference and the speech synthesis is powered entirely by the CPU.

The model used is Kokoro, which, despite having only 82M parameters, produces realistic speech in multiple languages including English, Mandarin, and Hindi. It provides around 50 distinct voices, primarily optimized for English.

There are several ways to set up a server for Kokoro. The simplest method involves using a pre-made container image called Kokoro-FastAPI, which includes pre-downloaded voice models. Because of that, the container image is rather large, at about 5 GB in size.

To launch the container using Docker or Podman, use the following command:

podman run -p 8880:8880 ghcr.io/remsky/kokoro-fastapi-cpu

To quickly verify that it runs correcly, the container serves a simple web UI at localhost:8880/web. Here you can generate (and automatically play) an audio given some text.

Kokoro web UI

In addition to the simple web UI, this container also serves a TTS interface compatible with the OpenAI speech API, making it easy to adapt existing programs that already use the OpenAI speech API. To facilitate a quick test, sample code in both JavaScript and Python is available at github.com/remotebrowser/speak. Cloning this repository will enable you to follow the subsequent demonstration.

For JavaScript:

export TTS_API_BASE_URL=http://127.0.0.1:8880/v1
./speak.js "Good morning! How are you today?"

For Python, the command is very similar:

export TTS_API_BASE_URL=http://127.0.0.1:8880/v1
./speak.py "Good morning! How are you today?"

The generated audio will be saved as an MP3 file. If SoX or Sound eXchange (see sox.sf.net for details) is installed on your machine, the audio will also play back automatically.

You can also select a different voice by setting the TTS_VOICE environment variable:

export TTS_API_BASE_URL=http://127.0.0.1:8880/v1
export TTS_VOICE="am_eric"
./speak.js "Good morning! How are you today?"

A complete list of available voices can be found on the official Kokoro project page: huggingface.co/hexgrad/Kokoro-82M/blob/main/VOICES.md.

How fast is the synthesis? Here are some measurements using the am_eric voice on a short test paragraph:

Jupiter is the largest and most massive planet in our solar system. This gas giant, made mostly of hydrogen and helium, is known for its Great Red Spot—a massive storm observed for centuries.

The following list summarizes the generation time (best of 3 runs) across different CPUs:

  • Intel Core i7-4770K: 4.7 seconds
  • Apple M2 Pro: 4.5 seconds
  • AMD Ryzen 7 8745HS: 1.5 seconds

The first CPU in the list was released 12 years ago. If that ancient CPU can do the job just fine, you know that this is a highly capable TTS system.

Finally, for an alternative OpenAI-compatible containerized TTS service, consider Speaches (speaches.ai). Unlike Kokoro-FastAPI, Speaches requires you to explicitly download voice weights via its API, as they are not bundled in the container image. However, Speaches offers an advantage by including Whisper, OpenAI’s renowned high-quality Speech-to-Text (STT) system. If your application needs both TTS and STT functionality, Speaches could be your one-stop solution.

When combined with a local LLM, a speech synthesis system like this allows you to enjoy listening to LLM answers instead of reading them!

Note: This article originally appeared on the Remote Browser Substack.

联系我们 contact @ memedata.com