Skip to main content

Fast, accurate, on-device AI library for building interactive voice applications

Project description

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, intent, 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

We also provide voice command recognition using the IntentRecognizer module. It captures transcribed audio from a MicTranscriber and invokes callback functions that match your programmed intents. Since it relies on an embedding model, you can use a helper function to get started:

from moonshine_voice import (
    MicTranscriber,
    IntentRecognizer,
    ModelArch,
    EmbeddingModelArch,
    get_embedding_model,
    get_model_for_language
)

# Download and load the embedding model for intent recognition
embedding_model_path, embedding_model_arch = get_embedding_model()

Next, create a recognizer and register your intent callbacks:

intent_recognizer = IntentRecognizer(
    model_path=embedding_model_path,
    model_arch=embedding_model_arch
)

def on_lights_on(trigger: str, utterance: str, similarity: float):
    """Handler for turning lights on."""
    print(f"\n💡 LIGHTS ON! (matched '{trigger}' with {similarity:.0%} confidence)")

def on_lights_off(trigger: str, utterance: str, similarity: float):
    """Handler for turning lights off."""
    print(f"\n🌑 LIGHTS OFF! (matched '{trigger}' with {similarity:.0%} confidence)")

intent_recognizer.register_intent("turn on the lights", on_lights_on)
intent_recognizer.register_intent("turn off the lights", on_lights_off)

Finally, create a MicTranscriber, connect it to your IntentRecognizer, and start the audio stream:

# Get the transcription model and initialize a MicTranscriber
model_path, model_arch = get_model_for_language("en")
mic_transcriber = MicTranscriber(model_path=model_path, model_arch=model_arch)

# The intent recognizer will process completed transcript lines and invoke trigger handlers
mic_transcriber.add_listener(intent_recognizer)

mic_transcriber.start()
try:
    while True:
        time.sleep(0.1)
except KeyboardInterrupt:
    print("\n\nStopping...", file=sys.stderr)
finally:
    intent_recognizer.close()
    mic_transcriber.stop()
    mic_transcriber.close()

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.

Project details


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.0.69-py3-none-win_amd64.whl (50.5 MB view details)

Uploaded Python 3Windows x86-64

moonshine_voice-0.0.69-py3-none-manylinux_2_34_x86_64.whl (56.3 MB view details)

Uploaded Python 3manylinux: glibc 2.34+ x86-64

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

Uploaded Python 3manylinux: glibc 2.34+ ARM64

moonshine_voice-0.0.69-py3-none-manylinux_2_31_aarch64.manylinux_2_39_aarch64.whl (55.0 MB view details)

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

moonshine_voice-0.0.69-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.0.69-py3-none-win_amd64.whl.

File metadata

File hashes

Hashes for moonshine_voice-0.0.69-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 8d71ac173711a7528bbed663cb7a68e5cb06dc4ca05530e375f14b3fb83fe48b
MD5 a56c34a3cd89380cf5ef96194561a5d5
BLAKE2b-256 5aa9c9d004255bba77efe854946caa5bb592982383852ab96eb9263689aa211c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for moonshine_voice-0.0.69-py3-none-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 4e0da7167f2c87a7a8bba145b672ccad2a44115ca01d10a1dd15ec4f4b1c3c73
MD5 f03bb56e09ae79908dd015dec7b9bcaa
BLAKE2b-256 e75237be3f40f6041ea419fafca694995c40febcac60923893d4d75845ad56af

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for moonshine_voice-0.0.69-py3-none-manylinux_2_34_aarch64.whl
Algorithm Hash digest
SHA256 d742aef68e6d1f677f261c77b674f484c31d6a6f75f26a290346c75519707f19
MD5 f28400849828f2da6adf9d87d2baa8e4
BLAKE2b-256 6bc581a7bc140db7c1999bfbc0d26ab4c333d610e0d1ad9e88def6637353cf28

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for moonshine_voice-0.0.69-py3-none-manylinux_2_31_aarch64.manylinux_2_39_aarch64.whl
Algorithm Hash digest
SHA256 1cda44b5cd3869e1b9165715de211d342b6de52bdd51bf99b79a1e44bd0f20e7
MD5 bad4883b8556d2cc4eca1cbb316876d9
BLAKE2b-256 b9b306fe4bd8e89c3ca70686584a40c5619c2a91f2abcaf3de31716fc0afbd2e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for moonshine_voice-0.0.69-py3-none-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 7fa9d8da74d86ca279abc074fc6aa2b1570768f215080e43628c7b51e4ec428f
MD5 0c5538c684a0eb198184e1f98762f205
BLAKE2b-256 e07687aa9e29ab8c2f131a0119faaa286e83a234515a9ca82c5099d04e09ecd4

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