cmusphinx/pocketsphinx
A small speech recognizer
PocketSphinx – lightweight, offline speech recognizer
What it is – PocketSphinx is an open‑source, continuous‑speech recognizer originally from Carnegie Mellon University. It implements classic hidden‑Markov‑model (HMM) decoding for large‑vocabulary, speaker‑independent speech, but it is deliberately kept small and fast so it can run on low‑power devices (e.g., Raspberry Pi, embedded Linux, Windows). The codebase is written in C, with optional Python bindings.
Why it matters – Unlike many modern neural‑network recognizers that need a GPU or a cloud API, PocketSphinx works entirely offline and has a tiny footprint (a few megabytes). This makes it useful for privacy‑sensitive or resource‑constrained applications such as voice‑controlled IoT gadgets, robotics, or hobbyist projects that need on‑device speech‑to‑text.
How to get it – The project uses CMake for building the C library and the pocketsphinx command‑line tool. On Debian‑based Linux you can install the optional audio‑handling dependencies (ffmpeg, portaudio, sox, etc.) with apt. After cloning the repo you can:
# Build the C library
cmake -S . -B build
cmake --build build
sudo cmake --build build --target install # or use -DCMAKE_INSTALL_PREFIX for a custom location
For Python, create a virtual environment and run pip install . from the top‑level directory.
Running it – The pocketsphinx executable reads 16‑bit mono PCM audio (from a file or stdin) and outputs recognition results as line‑delimited JSON. The main sub‑commands are:
live– streams audio, detects speech segments, and prints a JSON object for each segment (bstart‑time,dduration,pconfidence,ttranscript,wword‑level details).single– treats each input file as a single utterance and returns one JSON object.align– force‑aligns a given audio file to a supplied transcript, useful for generating timestamps for words or phones.soxflags– prints thesoxarguments needed to convert arbitrary audio formats into the required PCM stream, enabling pipelines likesox audio.mp3 $(pocketsphinx soxflags) | pocketsphinx -.
Programming – The repository ships example programs in examples/ for both C and Python. The C API is documented via Doxygen (built automatically with CMake), and the Python API is hosted at https://pocketsphinx.readthedocs.io. Typical usage in Python looks like:
import pocketsphinx as ps
config = ps.Decoder.default_config()
config.set_string('-hmm', '/path/to/acoustic/model')
config.set_string('-lm', '/path/to/language/model')
config.set_string('-dict', '/path/to/dictionary')
decoder = ps.Decoder(config)
decoder.start_utt()
with open('speech.wav', 'rb') as f:
decoder.process_raw(f.read(), False, True)
decoder.end_utt()
print('Result:', decoder.hyp().hypstr)
Development & testing – The project includes a regression/unit test suite (make check or ctest). Documentation can be built locally (cmake --build build --target docs for the C API, make -C docs html for the Python docs). Continuous‑integration workflows on GitHub Actions handle releases and API‑doc updates.
License – See the LICENSE file in the repo; PocketSphinx is released under a BSD‑style license, allowing free use and redistribution.
PocketSphinx is a genuine, actively maintained speech‑recognition library that fits squarely within the AI/ML domain, focusing on efficient, offline inference rather than large neural models.
Related
- Project
- Project
- Project
- Project
- Project