Skip to main content

Moonshine Voice Python Package

A fast, accurate, on-device AI library for building interactive voice applications. Join our Discord to get help and support.

Installation

pip install moonshine-voice

Quick Start

# Listens to the microphone, logging to the console when there are 
# speech updates.
moonshine-voice mic

Installing the package adds a moonshine-voice command (with a shorter moonshine alias) that groups the built-in tools as subcommands: mic, transcribe, tts, dialog, download, and g2p. Run moonshine-voice --help, or moonshine-voice <command> --help for a specific tool. Each subcommand is equivalent to python -m moonshine_voice.<module>, so either invocation style works.

Example

"""Transcribes live audio from the default microphone"""
import time
from moonshine_voice import (
    MicTranscriber,
    TranscriptEventListener,
    get_model_for_language,
)

# This will download the model files and cache them.
model_path, model_arch = get_model_for_language("en")

# MicTranscriber handles connecting to the microphone, capturing
# the audio data, detecting voice activity, breaking the speech
# up into segments, transcribing the speech, and sending events
# as the results are updated over time.
mic_transcriber = MicTranscriber(
    model_path=model_path, model_arch=model_arch)

# We use an event-driven interface to respond in real time
# as speech is detected.
class TestListener(TranscriptEventListener):
    def on_line_started(self, event):
        print(f"Line started: {event.line.text}")

    def on_line_text_changed(self, event):
        print(f"Line text changed: {event.line.text}")

    def on_line_completed(self, event):
        print(f"Line completed: {event.line.text}")

listener = TestListener()
mic_transcriber.add_listener(listener)
mic_transcriber.start()
print("Listening to the microphone, press Ctrl+C to stop...")

while True:
    time.sleep(0.1)

Other Sources

If you have a different source you're capturing audio from you can supply it directly to a transcriber.

"""Transcribes live audio from an arbitrary audio source."""
from moonshine_voice import (
    Transcriber,
    TranscriptEventListener,
    get_model_for_language,
    load_wav_file,
    get_assets_path,
)
import os
from typing import Iterator, Tuple


def audio_chunk_generator(
    wav_file_path: str, chunk_duration: float = 0.1
) -> Iterator[Tuple[list, int]]:
    """
    Example function that loads a WAV file and yields audio chunks.

    This demonstrates how you can integrate your own proprietary
    audio data capture sources. Replace this function with your own
    implementation that yields (audio_chunk, sample_rate) tuples.

    Args:
        wav_file_path: Path to the WAV file to load
        chunk_duration: Duration of each chunk in seconds

    Yields:
        Tuple of (audio_chunk, sample_rate) where:
        - audio_chunk: List of float audio samples
        - sample_rate: Sample rate in Hz
    """
    audio_data, sample_rate = load_wav_file(wav_file_path)
    chunk_size = int(chunk_duration * sample_rate)

    for i in range(0, len(audio_data), chunk_size):
        chunk = audio_data[i: i + chunk_size]
        yield (chunk, sample_rate)


model_path, model_arch = get_model_for_language("en")

transcriber = Transcriber(
    model_path=model_path, model_arch=model_arch)

stream = transcriber.create_stream(update_interval=0.5)
stream.start()


class TestListener(TranscriptEventListener):
    def on_line_started(self, event):
        print(f"{event.line.start_time:.2f}s: Line started: {event.line.text}")

    def on_line_text_changed(self, event):
        print(
            f"{event.line.start_time:.2f}s: Line text changed: {event.line.text}")

    def on_line_completed(self, event):
        print(f"{event.line.start_time:.2f}s: Line completed: {event.line.text}")


listener = TestListener()
stream.add_listener(listener)

# Feed audio chunks from the generator into the stream.
wav_file_path = os.path.join(get_assets_path(), "two_cities.wav")
for chunk, sample_rate in audio_chunk_generator(wav_file_path):
    stream.add_audio(chunk, sample_rate)

stream.stop()
stream.close()

Voice Commands

Voice commands go through DialogFlow. Register the phrases you want to listen for and it handles the rest: it downloads the speech recognition, speech synthesis and phrase-matching models, opens the microphone, matches what the user said semantically rather than by exact wording, and runs your handler.

from moonshine_voice import DialogFlow

def lights_on(d):
    print("\n💡 LIGHTS ON!")

def lights_off(d):
    print("\n🌑 LIGHTS OFF!")

runner = (
    DialogFlow()
    .always("turn on the lights", lights_on)
    .always("turn off the lights", lights_off)
)

load() downloads and opens everything it needs, and start_listening() opens the microphone. There's nothing else to construct:

runner.load()
runner.start_listening()
try:
    while True:
        time.sleep(0.1)
except KeyboardInterrupt:
    print("\n\nStopping...", file=sys.stderr)
finally:
    runner.close()

Configuration is chainable, and everything has a working default — language(), voice(), trigger_threshold(), on_heard() / on_said() / on_error() for observing the conversation, and use_mic_transcriber() / use_text_to_speech() when you'd rather supply your own. To drive a runner from text instead of audio, turn the microphone off with microphone(False) and feed it handle_utterance().

For multi-turn conversations — asking a question, confirming an answer, spelling out a password — register a flow with listen_for() instead of a global. See examples/python/dialog_flow.py, or run moonshine-voice dialog.

Multiple Languages

The framework currently supports English, Spanish, Mandarin, Japanese, Korean, Vietnamese, Arabic, and Ukrainian. We are working on wider language support, and you can see which are supported in your version by calling supported_languages(). To use a language, request it using get_model_for_language() passing in the two-letter language code. For example get_model_for_language("es") will download the Spanish models and pass the information you need to create Transcriber objects using them.

Documentation

For more information, see the main Moonshine Voice documentation.

License

The code and English-language models are released under the MIT License - see the main project repository for details. The models used for other languages are released under the Moonshine Community License.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

moonshine_voice-0.1.0-py3-none-win_amd64.whl (50.5 MB view details)

Uploaded Python 3Windows x86-64

moonshine_voice-0.1.0-py3-none-manylinux_2_34_x86_64.whl (56.4 MB view details)

Uploaded Python 3manylinux: glibc 2.34+ x86-64

moonshine_voice-0.1.0-py3-none-manylinux_2_34_aarch64.whl (55.1 MB view details)

Uploaded Python 3manylinux: glibc 2.34+ ARM64

moonshine_voice-0.1.0-py3-none-manylinux_2_31_aarch64.manylinux_2_39_aarch64.whl (55.1 MB view details)

Uploaded Python 3manylinux: glibc 2.31+ ARM64manylinux: glibc 2.39+ ARM64

moonshine_voice-0.1.0-py3-none-macosx_15_0_arm64.whl (54.4 MB view details)

Uploaded Python 3macOS 15.0+ ARM64

File details

Details for the file moonshine_voice-0.1.0-py3-none-win_amd64.whl.

File metadata

File hashes

Hashes for moonshine_voice-0.1.0-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 af2b8536ab08c48f33d5b1e126656b05ceae9a8980fc1c95dd70665b0b57c8e7
MD5 757bc73ec2ccc23c557956db6ef9ca28
BLAKE2b-256 77782e0fb469c6d236eb2782d6129d056a317e75d85efb340680617cebc8d82b

See more details on using hashes here.

File details

Details for the file moonshine_voice-0.1.0-py3-none-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for moonshine_voice-0.1.0-py3-none-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 0f833deb43bad5dcfb4cfd3257b6df83ef9abd3f27be3199622fe41932e8d916
MD5 bedd022e652a5c46f3eca182d9ce8f3b
BLAKE2b-256 fda3e3c0156664e9505af7b23072c3e947cec3a7ee938764c70424051366c368

See more details on using hashes here.

File details

Details for the file moonshine_voice-0.1.0-py3-none-manylinux_2_34_aarch64.whl.

File metadata

File hashes

Hashes for moonshine_voice-0.1.0-py3-none-manylinux_2_34_aarch64.whl
Algorithm Hash digest
SHA256 da54a99587edb987c8755f2c746cab72fada0e67cf847f7edfb8105447f405af
MD5 0f48da45f3558693f1b11b28397f0c45
BLAKE2b-256 050839d25f1ccc106720eb6122bc7a7ab029ac3c407be0f010a8855056b1f593

See more details on using hashes here.

File details

Details for the file moonshine_voice-0.1.0-py3-none-manylinux_2_31_aarch64.manylinux_2_39_aarch64.whl.

File metadata

File hashes

Hashes for moonshine_voice-0.1.0-py3-none-manylinux_2_31_aarch64.manylinux_2_39_aarch64.whl
Algorithm Hash digest
SHA256 58d5546a656f65061d1d4a376818b08109fe25600a22dfdbbba0179ccb3f0d07
MD5 b61a5b35133b48ca94c09626ecc5e638
BLAKE2b-256 45996157236653594449575de6ef41abce70242c1a5a57c6dbaac408c2f99fe3

See more details on using hashes here.

File details

Details for the file moonshine_voice-0.1.0-py3-none-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for moonshine_voice-0.1.0-py3-none-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 9c70431836ed805f9ab30adcdfe97018c17b1e932ba7e205fff0562950dc0913
MD5 247308fba4422ef1a9d0db514321e1f2
BLAKE2b-256 5ea46e128bb0f2fce9993e0ffd8e8b6214d1d7f694f880182339eedbb6f3f375

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page