Learn how to detect the language spoken in any audio, whether a recorded file or a live microphone stream, in about 20 lines of Python, using Bat Spoken Language Identification. Bat Spoken Language Identification processes audio entirely on-device, so voice data never leaves the machine and language detection works offline. The same code runs on Linux, macOS, Windows, and Raspberry Pi, and separate SDKs cover Android, iOS, and Web.
Install the Language Identification Python SDK
With Python 3.9 or newer, install pvbat and pvrecorder Python packages using PIP:
pvbat contains the spoken language identification engine and a built-in model for multiple languages. pvrecorder captures the microphone audio for identifying the spoken language in real time.
Get Your AccessKey
Sign up for a Picovoice Console account. Once your account is ready, copy your AccessKey from the Console home page.
Detect the Language of an Audio File
Start with the imports, and a small map of language codes to names:
Take the AccessKey and the audio file as command-line arguments:
Create the engine with your AccessKey, and read in the audio. Bat analyzes audio in fixed-size frames of bat.frame_length samples at 16 kHz (bat.sample_rate), 16-bit, single-channel; Python's built-in wave and struct modules read the WAV file into raw 16-bit samples:
Feed the audio to the engine frame by frame. Each call to .process() returns a dictionary that maps every supported language to a score between 0 and 1, or None when the frame contains no usable voice. The language with the highest score wins:
Full Python Code to Detect the Language of an Audio File
Save the complete script as spoken-language-identification.py:
Run Language Detection Script
Run it with your AccessKey and a WAV file:
Grab test recordings in every supported language from the Bat GitHub repository, or use any WAV of your own.
Identify the Spoken Language from the Microphone in Real Time
For live audio, PvRecorder streams small chunks from the microphone. Collect them in a buffer until a full engine frame of bat.frame_length samples accumulates, process it, then slide the buffer forward by 0.75 seconds so the scores refresh continuously as someone speaks.
Full Python Code for Real-Time Spoken Language Identification
The microphone version, spoken-language-identification-mic.py, takes only the --access_key argument:
Run Real-Time Language Identification Script
Run the python code for spoken language identification, and speak in any of the supported languages to watch the detection update live:
PvRecorder reads the system default microphone; to pick a specific one, list devices with PvRecorder.get_available_devices() and pass its index as device_index.
The pvbatdemo package ships similar file and microphone demos as ready-made bat_demo_file and bat_demo_mic commands.
How to Read the Spoken Language Identification Output
Three details make the output easier to interpret:
- Higher scores mean higher confidence. Each supported language gets a score between 0 and 1, and the top-scoring language is the prediction. A top score near 1 signals a confident match; a low top score, or scores spread across several languages, means the frame carries weaker evidence.
Noneflags silence. The engine skips frames that lack detectable voice. Thevoice_thresholdargument ofpvbat.create()(default0.4) controls how strict that filter is: raise it to reject background noise, lower it to catch quiet speech.UNKNOWNcatches everything else. Bat scores an open set of languages, so speech outside the supported set lands onUNKNOWNinstead of forcing a wrong match.
Both programs stick to the pvbat.create() defaults; the pvbat Python API docs cover every argument, including device for choosing the inference device.
Supported Languages and Accuracy
Bat Spoken Language Identification recognizes English, French, German, Italian, Japanese, Korean, Portuguese, and Spanish. Bat identifies the spoken language with 93% accuracy, about 2x fewer errors than SpeechBrain, while using 62x less memory (5 MB versus 333 MB) and 9x less CPU.
What's Next?
Detecting the language is usually the first step in a longer pipeline. The speech-to-speech translation recipe builds one: Bat Spoken Language Identification detects the spoken language, then on-device streaming speech-to-text, translation, and streaming text-to-speech turn it into speech in the target language.
To handle the translation next, follow the companion tutorial on building an AI translator app in Python.







