### Install Prebuilt Quantized ONNX Source: https://github.com/moonshine-ai/moonshine/blob/main/core/moonshine-tts/data/kokoro/README.md Install the smaller, prebuilt quantized ONNX model. Requires huggingface_hub and onnx to be installed. ```bash python scripts/fetch_hf_kokoro_quantized_onnx.py --backup ``` -------------------------------- ### Install and Run Microphone Transcriber (Python) Source: https://github.com/moonshine-ai/moonshine/blob/main/README.md Installs the Moonshine Voice library and runs a microphone transcriber for real-time transcriptions. Use this to get started with basic audio input processing. ```bash pip install moonshine-voice python -m moonshine_voice.mic_transcriber --language en ``` -------------------------------- ### Run Example with Custom Language and Model Source: https://github.com/moonshine-ai/moonshine/blob/main/examples/python/ollama-voice/README.md Run the example specifying both the transcription language and the Ollama model for chat responses. ```bash python ollama_voice.py --language en --ollama-model qwen3.5:9b ``` -------------------------------- ### Run Ollama Voice Example Source: https://github.com/moonshine-ai/moonshine/blob/main/examples/python/ollama-voice/README.md Execute the Python script to start the voice transcription and Ollama chat interaction. Press Ctrl+C to stop. ```bash python ollama_voice.py ``` -------------------------------- ### Run Example with Custom Ollama Model Source: https://github.com/moonshine-ai/moonshine/blob/main/examples/python/ollama-voice/README.md Run the example specifying a different Ollama model for chat responses using the --ollama-model argument. ```bash python ollama_voice.py --ollama-model llama3.2 ``` -------------------------------- ### Build with Swift Package Manager Source: https://github.com/moonshine-ai/moonshine/blob/main/examples/macos/BasicTranscription/README.md Use Swift Package Manager to build the command-line application. Navigate to the example directory first. ```bash cd examples/macos/BasicTranscription swift build ``` -------------------------------- ### Dalek Script Output Example Source: https://github.com/moonshine-ai/moonshine/blob/main/examples/raspberry-pi/my-dalek/README.md Example output when running the my-dalek.py script, showing registered intents and listening status. ```text Registered 6 intents ============================================================ šŸŽ¤ Listening for voice commands... Try saying phrases with the same meaning as these actions: - 'move forward' - 'move backward' - 'turn left' - 'turn right' - 'kill all humans' - 'exterminate' We're doing fuzzy matching of natural language, so phrases like 'Go forward' or 'Move ahead' or 'Advance' will trigger the 'move forward' action, for example. ============================================================ Press Ctrl+C to stop. ``` -------------------------------- ### Install Ollama Python Client Source: https://github.com/moonshine-ai/moonshine/blob/main/examples/python/ollama-voice/README.md Install the Ollama Python client library using pip. This is required to interact with the Ollama API. ```bash pip install ollama ``` -------------------------------- ### Start Ollama Server Source: https://github.com/moonshine-ai/moonshine/blob/main/examples/python/ollama-voice/README.md Start the Ollama local server. This command must be run before executing the Python script. ```bash ollama serve ``` -------------------------------- ### Install Dependencies Source: https://github.com/moonshine-ai/moonshine/blob/main/core/moonshine-tts/data/kokoro/README.md Install the necessary Python packages for Kokoro TTS. Align versions with the compatible Kokoro release if export fails. ```bash pip install kokoro torch onnx onnxruntime onnxruntime-extensions huggingface_hub ``` ```bash # optional weight-pack experiments: pip install onnx-shrink-ray onnx-graphsurgeon ``` -------------------------------- ### Running the Dialog Flow Example Source: https://github.com/moonshine-ai/moonshine/blob/main/README.md This command executes the built-in dialog flow example application for Moonshine Voice. It serves as a practical demonstration of the conversational agent capabilities. ```bash python -m moonshine_voice.dialog_flow ``` -------------------------------- ### Build Swift Application with Swift Package Manager Source: https://github.com/moonshine-ai/moonshine/blob/main/examples/macos/MicTranscription/README.md Use Swift Package Manager to build the command-line application. Navigate to the example directory first. ```bash cd examples/macos/MicTranscription swift build ``` -------------------------------- ### Install Moonshine AI for Python Source: https://github.com/moonshine-ai/moonshine/blob/main/README.md Install the Python package using pip. This is the primary method for Python integration. ```bash pip install moonshine-voice ``` -------------------------------- ### Install Moonshine Voice with uv Source: https://github.com/moonshine-ai/moonshine/blob/main/examples/raspberry-pi/my-dalek/README.md Install Moonshine Voice using the uv package manager. Ensure you activate the virtual environment before running the script. ```bash curl -LsSf https://astral.sh/uv/install.sh | sh uv venv source .venv/bin/activate uv pip install moonshine-voice ``` -------------------------------- ### Agent Setup with Transcriber, Intent Recognizer, and Text-to-Speech Source: https://github.com/moonshine-ai/moonshine/blob/main/README.md This snippet demonstrates the initialization of an agent's core components: a MicTranscriber for input, an IntentRecognizer for understanding, and a TextToSpeech object for responses. It also sets up the DialogFlow and registers commands. ```python embedding_model_path, embedding_model_arch = get_embedding_model() intent_recognizer = IntentRecognizer( model_path=embedding_model_path, model_arch=embedding_model_arch ) tts = TextToSpeech(args.tts_language) model_path, model_arch = get_model_for_language(args.language) mic_transcriber = MicTranscriber( model_path=model_path, model_arch=model_arch ) dialog_flow = DialogFlow( tts=tts, intent_recognizer=intent_recognizer ) add_commands(dialog_flow, tts) mic_transcriber.add_listener(dialog_flow) mic_transcriber.start() ``` -------------------------------- ### Run Basic Transcription (Default) Source: https://github.com/moonshine-ai/moonshine/blob/main/examples/macos/BasicTranscription/README.md Execute the basic transcription application using the default test audio file. Ensure you are in the example directory. ```bash swift run BasicTranscription ``` -------------------------------- ### Build and Run C++ Example (Linux) Source: https://github.com/moonshine-ai/moonshine/blob/main/README.md Builds the Moonshine C++ core library and runs a test executable. This is for users who want to integrate Moonshine directly into C++ applications on Linux. ```bash cd core mkdir -p build cd build cmake .. cmake --build . ./moonshine-cpp-test ``` -------------------------------- ### Model Download Log Output Source: https://github.com/moonshine-ai/moonshine/blob/main/README.md Example output from the model download script, showing progress and final download path. ```text encoder_model.ort: 100%|ā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆ| 29.9M/29.9M [00:00<00:00, 34.5MB/s] decoder_model_merged.ort: 100%|ā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆ| 104M/104M [00:02<00:00, 52.6MB/s] tokenizer.bin: 100%|ā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆā–ˆ| 244k/244k [00:00<00:00, 1.44MB/s] Model download url: https://download.moonshine.ai/model/base-en/quantized/base-en Model components: ['encoder_model.ort', 'decoder_model_merged.ort', 'tokenizer.bin'] Model arch: 1 Downloaded model path: /Users/petewarden/Library/Caches/moonshine_voice/download.moonshine.ai/model/base-en/quantized/base-en ``` -------------------------------- ### Start Audio Transcription and Intent Recognition Source: https://github.com/moonshine-ai/moonshine/blob/main/python/README.md Initialize a MicTranscriber with the appropriate language model and add the IntentRecognizer as a listener. Start the audio stream and handle interruptions. ```python # 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() ``` -------------------------------- ### start Source: https://github.com/moonshine-ai/moonshine/blob/main/README.md Begins a new transcription session. This must be called before adding any audio data. ```APIDOC ## start() ### Description Begins a new transcription session. You need to call this after you've created the `Transcriber` and before you add any audio. ### Method `start()` ### Endpoint None ### Parameters None ### Request Example ```json {} ``` ### Response #### Success Response (200) * Transcription session started successfully. #### Response Example ```json { "status": "started" } ``` ``` -------------------------------- ### Test Moonshine Voice Installation Source: https://github.com/moonshine-ai/moonshine/blob/main/python/getting-started-with-moonshine-voice.ipynb Run this command to test the installation by executing the Transcriber class test. It will process a sample audio file and output transcription logs. ```python !python -m moonshine_voice.transcriber ``` -------------------------------- ### Install Moonshine Voice Source: https://github.com/moonshine-ai/moonshine/blob/main/examples/python/ollama-voice/README.md Install the Moonshine Voice Python package using pip. Ensure Python 3 is installed. ```bash pip install moonshine-voice ``` -------------------------------- ### Install Moonshine Voice Source: https://github.com/moonshine-ai/moonshine/blob/main/examples/raspberry-pi/my-dalek/README.md Install the Moonshine Voice package using pip. Use --break-system-packages if necessary, or preferably use a virtual environment. ```bash pip install moonshine-voice ``` ```bash pip install --break-system-packages moonshine-voice ``` -------------------------------- ### Install and Run Moonshine Voice Transcriber on Raspberry Pi Source: https://github.com/moonshine-ai/moonshine/blob/main/README.md Installs the moonshine-voice Python package and runs the microphone transcriber. Ensure a USB microphone is connected. Use a virtual environment if you prefer not to use --break-system-packages. ```bash sudo pip install --break-system-packages moonshine-voice python -m moonshine_voice.mic_transcriber --language en ``` -------------------------------- ### Install Moonshine Voice Source: https://github.com/moonshine-ai/moonshine/blob/main/python/getting-started-with-moonshine-voice.ipynb Install the Moonshine Voice library using pip. This command installs the latest version and supports various operating systems. ```python !pip install moonshine-voice ``` -------------------------------- ### Run the Dalek Script Source: https://github.com/moonshine-ai/moonshine/blob/main/examples/raspberry-pi/my-dalek/README.md Execute the main Python script to start the voice interface. Ensure your USB microphone is plugged in. ```bash python my-dalek.py ``` -------------------------------- ### Download Speech to Text Models with Bogus Language Source: https://github.com/moonshine-ai/moonshine/blob/main/README.md Example of running the download script with an invalid language code to see supported languages. ```bash python -m moonshine_voice.download --language foo ``` -------------------------------- ### Attach Listener and Start Transcription Session Source: https://github.com/moonshine-ai/moonshine/blob/main/python/getting-started-with-moonshine-voice.ipynb This code initializes a custom listener, attaches it to the transcriber, and starts a new transcription session. This is a prerequisite for adding audio data for processing. ```python # Create an instance of our listener, and attach it to the Transcriber. listener = TestListener() transcriber.add_listener(listener) # Begin a new session. Required before you start adding audio chunks. transcriber.start() ``` -------------------------------- ### Simulate Live Audio Input Source: https://github.com/moonshine-ai/moonshine/blob/main/python/getting-started-with-moonshine-voice.ipynb Start the transcriber, then simulate incoming live audio by adding audio chunks. The action events will be triggered once the audio chunks containing each matching sentence have been added. Stop the transcriber afterwards. ```python # Simulate incoming live audio. The action events will be triggered once the # audio chunks containing each matching sentence have been added. transcriber.start() chunk_duration = 0.2 chunk_size = int(chunk_duration * intent_sample_rate) for i in range(0, len(intent_audio_data), chunk_size): chunk = intent_audio_data[i : i + chunk_size] transcriber.add_audio(chunk, intent_sample_rate) _ = transcriber.stop() ``` -------------------------------- ### Build Moonshine Core Engine with CMake Source: https://github.com/moonshine-ai/moonshine/blob/main/README.md Steps to compile the core C++ engine using CMake. Ensure you are in the 'core' directory before starting. ```bash cd core mkdir -p build cd build cmake .. cmake --build . ``` -------------------------------- ### Transcribe Audio from Arbitrary Source with Event Listeners Source: https://github.com/moonshine-ai/moonshine/blob/main/python/README.md Transcribes live audio from an arbitrary audio source, such as a WAV file. This example demonstrates integrating custom audio data capture sources and uses an event-driven interface for real-time transcription updates. ```python """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() ``` -------------------------------- ### Transcribe Audio File with Moonshine Voice Source: https://github.com/moonshine-ai/moonshine/blob/main/python/getting-started-with-moonshine-voice.ipynb Download an audio file, set up the Moonshine Voice transcriber for English, load the audio data, and transcribe it. This example demonstrates basic file transcription. ```bash ! curl -O -L 'https://github.com/moonshine-ai/moonshine/raw/refs/heads/main/test-assets/beckett.wav' ``` ```python import moonshine_voice model_path, model_arch = moonshine_voice.get_model_for_language("en") transcriber = moonshine_voice.Transcriber(model_path=model_path, model_arch=model_arch) audio_data, sample_rate = moonshine_voice.load_wav_file("beckett.wav") transcript = transcriber.transcribe_without_streaming(audio_data) ``` -------------------------------- ### Regenerate Xcode Project with xcodegen Source: https://github.com/moonshine-ai/moonshine/blob/main/examples/macos/BasicTranscription/README.md If you need to regenerate the Xcode project files, use xcodegen after navigating to the example directory. ```bash cd examples/macos/BasicTranscription xcodegen generate ``` -------------------------------- ### Regenerate Xcode Project with xcodegen Source: https://github.com/moonshine-ai/moonshine/blob/main/examples/macos/MicTranscription/README.md If you need to regenerate the Xcode project files from the configuration, use xcodegen. Ensure you are in the example directory. ```bash cd examples/macos/MicTranscription xcodegen generate ``` -------------------------------- ### Build Arabic MSA Lexicon from CAMeL Tools Source: https://github.com/moonshine-ai/moonshine/blob/main/core/moonshine-tts/data/ar_msa/README.md This script builds an undiacritized Arabic key to IPA lexicon from wiki text using CAMeL Tools. Ensure Camel Tools and MSA data are installed. The output is a tab-separated dictionary file. ```bash python scripts/build_ar_msa_lexicon_from_camel_tools.py \ --wiki data/ar/wiki-text.txt \ --out data/ar_msa/dict.tsv ``` -------------------------------- ### JSON Output with Word Timestamps Source: https://github.com/moonshine-ai/moonshine/blob/main/docs/word-level-timestamps.md Example JSON output from Moonshine ASR when word-level timestamps are enabled. Each word includes its start time, end time, and confidence score. ```json { "text": "And God said, Let there be light, and there was light.", "start_time": 3.045, "duration": 3.535, "words": [ {"word": "And", "start": 3.057, "end": 3.395, "confidence": 0.73}, {"word": "God", "start": 3.395, "end": 3.758, "confidence": 0.96}, {"word": "said,", "start": 3.758, "end": 4.025, "confidence": 0.86}, {"word": "Let", "start": 4.025, "end": 4.169, "confidence": 0.68}, {"word": "there", "start": 4.169, "end": 4.315, "confidence": 0.99}, {"word": "be", "start": 4.315, "end": 4.871, "confidence": 1.00}, {"word": "light,", "start": 4.871, "end": 5.258, "confidence": 0.88}, {"word": "and", "start": 5.258, "end": 5.402, "confidence": 0.96}, {"word": "there", "start": 5.402, "end": 5.548, "confidence": 1.00}, {"word": "was", "start": 5.548, "end": 6.030, "confidence": 1.00}, {"word": "light.", "start": 6.030, "end": 6.575, "confidence": 0.97} ] } ``` -------------------------------- ### Display Help Message Source: https://github.com/moonshine-ai/moonshine/blob/main/examples/macos/MicTranscription/README.md To see all available command-line options and their descriptions, use the --help flag. ```bash swift run MicTranscription --help ``` -------------------------------- ### Build and Run Benchmark Tool Source: https://github.com/moonshine-ai/moonshine/blob/main/README.md Compile and execute the benchmarking tool to simulate live audio processing. This helps in understanding model performance and latency. ```bash cd core md build cd build cmake .. cmake --build . --config Release ./benchmark ``` -------------------------------- ### Play Sample Audio Source: https://github.com/moonshine-ai/moonshine/blob/main/python/getting-started-with-moonshine-voice.ipynb Import necessary libraries and use IPython.display.Audio to play a sample audio file. This helps verify audio playback capabilities. ```python import moonshine_voice import os from IPython.display import Audio Audio(os.path.join(moonshine_voice.get_assets_path(), "two_cities.wav")) ``` -------------------------------- ### TextToSpeech Initialization Source: https://github.com/moonshine-ai/moonshine/blob/main/README.md Creates a synthesizer instance and optionally downloads necessary TTS assets. Dependencies can be downloaded from a CDN or provided locally. ```APIDOC ## TextToSpeech `__init__()` ### Description Creates a synthesizer and optionally downloads dependencies into the package cache (or a custom root). ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body * **language** (string) - Required - BCP-47-style tag for the speaking locale (e.g., `en_us`, `de`, `fr`). Aliases like `en-us` are normalized. * **voice** (string) - Optional - Voice ID. Prefix with `kokoro_` or `piper_` to select the vocoder (e.g., `kokoro_af_heart`). If `download` is true, a cataloged voice not yet on disk is downloaded automatically. * **options** (mapping) - Optional - Mapping of string keys to strings, numbers, or booleans, passed through to the native option parser. The Python binding sets `g2p_root` to the resolved asset directory. * **asset_root** (string) - Optional - Directory to use as the TTS cache or on-disk asset tree. If `download` is true, downloads go under this root. If `download` is false, this path must contain the expected `g2p_root` layout. * **download** (boolean) - Optional - Default: true. If true, missing TTS assets are downloaded from `https://download.moonshine.ai/tts/`. If false, `asset_root` is required and must contain the files the native layer expects. ### Request Example ```json { "language": "en_us", "voice": "piper_en_us_fast", "download": true } ``` ### Response #### Success Response (200) None (initializes the object) #### Response Example None ``` -------------------------------- ### Build CLI Transcriber from Command Line Source: https://github.com/moonshine-ai/moonshine/blob/main/examples/windows/cli-transcriber/README.md Build the solution using msbuild for Release and x64 configurations. This is an alternative to building within Visual Studio. ```batch msbuild cli-transcriber.sln /p:Configuration=Release /p:Platform=x64 ``` -------------------------------- ### Transcribe Bundled Sample WAV File Source: https://github.com/moonshine-ai/moonshine/blob/main/examples/windows/cli-transcriber/README.md Run the CLI transcriber executable with the path to a WAV file. This command uses the bundled medium-streaming English model. ```batch x64\Release\cli-transcriber.exe --wav-path beckett.wav ``` -------------------------------- ### Transcript Data Structure Source: https://github.com/moonshine-ai/moonshine/blob/main/README.md A collection of TranscriberLines, ordered by time. The transcript is reset on each Transcriber start. ```APIDOC ## Transcript ### Description A Transcript contains a list of TranscriberLines, arranged in descending time order. The transcript is reset at every `Transcriber.start()` call, so if you need to retain information from it, you should make explicit copies. Most applications won't work with this structure, since all of the same information is available through event callbacks. ``` -------------------------------- ### Download and Export ONNX and KokoroVoice Source: https://github.com/moonshine-ai/moonshine/blob/main/core/moonshine-tts/data/kokoro/README.md Download weights, config, and voice files, then export ONNX and .kokorovoice files into a staging directory. Use --verify for numeric parity checks. ```bash python scripts/download_kokoro_onnx.py --out-dir models/kokoro --verify ``` -------------------------------- ### Get TTS Voice Catalog Source: https://github.com/moonshine-ai/moonshine/blob/main/README.md Retrieves a detailed catalog of available TTS voices, including vocoder information. ```APIDOC ## `get_tts_voice_catalog()` ### Description Retrieves a catalog of supported TTS voices, detailing vocoder and language information. ### Parameters None ### Request Example ```json {} ``` ### Response #### Success Response (200) * **catalog** (object) - An object where keys are voice IDs and values contain details about each voice (e.g., vocoder type, supported languages). #### Response Example ```json { "catalog": { "kokoro_af_heart": { "vocoder": "kokoro", "languages": ["af"] }, "piper_en_us_fast": { "vocoder": "piper", "languages": ["en_us"] } } } ``` ``` -------------------------------- ### Build macOS Framework Source: https://github.com/moonshine-ai/moonshine/blob/main/swift/README.md Builds the macOS framework from the core directory. Ensure you are in the 'core' directory before running. ```bash cd core mkdir -p build/build-macos cd build/build-macos cmake -G Xcode -DCMAKE_OSX_DEPLOYMENT_TARGET=13.0 .. cmake --build . --config Release ``` -------------------------------- ### Basic Text-to-Speech Initialization and Speaking Source: https://github.com/moonshine-ai/moonshine/blob/main/README.md Initialize a TextToSpeech object for a specific language and make it speak a given phrase. The `say()` method queues the text for synthesis and playback. ```python from moonshine_voice import TextToSpeech tts = TextToSpeech("fr") tts.say("Bonjour, mon ami") tts.wait() # block until playback finishes ``` -------------------------------- ### Create a Transcriber Instance Source: https://github.com/moonshine-ai/moonshine/blob/main/README.md Instantiate the Transcriber with the path and architecture of your chosen model. Ensure the model files are accessible. ```python transcriber = Transcriber(model_path=model_path, model_arch=model_arch) ``` -------------------------------- ### Ask for User Input Source: https://github.com/moonshine-ai/moonshine/blob/main/README.md Prompts the user for input within a conversational flow. This example shows how to ask for a Wi-Fi network name. ```python input_ssid = yield d.ask("What's the name of your Wi-Fi network?...") ``` -------------------------------- ### Connecting to a Wi-Fi Network with Error Handling Source: https://github.com/moonshine-ai/moonshine/blob/main/README.md This snippet demonstrates connecting to a Wi-Fi network using `nmcli` and includes robust error handling for common issues such as `FileNotFoundError` if `nmcli` is not found, and `subprocess.TimeoutExpired` if the connection attempt times out. ```python try: result = subprocess.run( ["sudo", "nmcli", "device", "wifi", "connect", found_ssid, "password", password], capture_output=True, text=True, timeout=30, ) except FileNotFoundError: yield d.say("Sorry, network manager was not found on this system.") return except subprocess.TimeoutExpired: yield d.say("Sorry, the connection attempt timed out.") return ``` -------------------------------- ### Show Help Message Source: https://github.com/moonshine-ai/moonshine/blob/main/examples/macos/BasicTranscription/README.md Display the help message for the BasicTranscription command-line application to see all available options and their usage. ```bash swift run BasicTranscription --help ``` -------------------------------- ### Configure word-alignment-benchmark executable Source: https://github.com/moonshine-ai/moonshine/blob/main/core/CMakeLists.txt Sets up the 'word-alignment-benchmark' executable, specifying C++ standard, include directories, and linking necessary libraries including 'moonshine'. ```cmake add_executable(word-alignment-benchmark word-alignment-benchmark.cpp) set_target_properties(word-alignment-benchmark PROPERTIES CXX_STANDARD 20 CXX_STANDARD_REQUIRED YES CXX_EXTENSIONS NO ) target_include_directories(word-alignment-benchmark PRIVATE ${CMAKE_CURRENT_LIST_DIR} ${CMAKE_CURRENT_LIST_DIR}/moonshine-utils ${CMAKE_CURRENT_LIST_DIR}/ort-utils ${CMAKE_CURRENT_LIST_DIR}/bin-tokenizer ${CMAKE_CURRENT_LIST_DIR}/third-party/onnxruntime/include ) target_link_libraries(word-alignment-benchmark PRIVATE moonshine) ``` -------------------------------- ### Run Basic Transcription with Model Architecture Specification Source: https://github.com/moonshine-ai/moonshine/blob/main/examples/macos/BasicTranscription/README.md Specify the model architecture for transcription using the --model-arch or -m flag, followed by the model name and the audio file path. ```bash swift run BasicTranscription --model-arch tiny path/to/audio.wav ``` -------------------------------- ### Get Supported Languages Source: https://github.com/moonshine-ai/moonshine/blob/main/python/README.md Call `supported_languages()` to retrieve a list of languages for which transcription models are available. This helps in selecting the correct model for multi-language support. ```python supported_languages() ``` -------------------------------- ### Write Directly to Bundle Source: https://github.com/moonshine-ai/moonshine/blob/main/core/moonshine-tts/data/kokoro/README.md Export ONNX and .kokorovoice files directly into the target bundle directory, overwriting existing files. ```bash python scripts/download_kokoro_onnx.py --out-dir data/kokoro --verify ``` -------------------------------- ### Listing Available Text-to-Speech Languages Source: https://github.com/moonshine-ai/moonshine/blob/main/README.md Call the `list_tts_languages()` function to get a list of all supported TTS languages. This helps in selecting the appropriate language for voice synthesis. ```python from moonshine_voice import list_tts_languages list_tts_languages() ``` -------------------------------- ### JavaScript/TypeScript WordTiming Interface Source: https://github.com/moonshine-ai/moonshine/blob/main/docs/word-level-timestamps.md Defines the WordTiming interface in TypeScript, specifying the structure for word, start time, end time, and confidence. This is used for word-level timestamp data. ```typescript export interface WordTiming { word: string; start: number; end: number; confidence: number; } ``` -------------------------------- ### Create Arabic Rule-Based G2P CLI Executable Source: https://github.com/moonshine-ai/moonshine/blob/main/core/moonshine-tts/CMakeLists.txt Builds a command-line interface executable for Arabic rule-based Grapheme-to-Phoneme conversion. It links against the ONNX Runtime library and includes Windows DLL copying. ```cmake add_executable(arabic_rule_g2p tools/arabic-rule-g2p-cli.cpp) target_link_libraries(arabic_rule_g2p PRIVATE moonshine_tts_ort) moonshine_tts_set_ort_rpath(arabic_rule_g2p) if(WIN32) copy_onnxruntime_dll(arabic_rule_g2p) endif() ``` -------------------------------- ### Android/Java WordTiming Class Source: https://github.com/moonshine-ai/moonshine/blob/main/docs/word-level-timestamps.md Defines the WordTiming class in Java for Android, holding word, start time, end time, and confidence. This is used for word-level timestamp data. ```java public class WordTiming { public String word; public float start; public float end; public float confidence; } ``` -------------------------------- ### Run Basic Transcription with Specified Files Source: https://github.com/moonshine-ai/moonshine/blob/main/examples/macos/BasicTranscription/README.md Run the transcription application and specify one or more audio files as input. The application will process each file. ```bash swift run BasicTranscription path/to/audio1.wav path/to/audio2.wav ``` -------------------------------- ### Set CMake Minimum Version and Project Name Source: https://github.com/moonshine-ai/moonshine/blob/main/core/third-party/doctest/CMakeLists.txt Specifies the minimum required CMake version and defines the project name. This is a standard starting point for CMake projects. ```cmake cmake_minimum_required(VERSION 3.22.1) project("doctest") ``` -------------------------------- ### Swift WordTiming Structure Source: https://github.com/moonshine-ai/moonshine/blob/main/docs/word-level-timestamps.md Defines the structure for word timing information in Swift, including the word, start and end times, and confidence score. Used when word-level timestamps are enabled. ```swift public struct WordTiming { public let word: String public let start: Float public let end: Float public let confidence: Float } ``` -------------------------------- ### Python Data Class for Word Timing Source: https://github.com/moonshine-ai/moonshine/blob/main/docs/word-level-timestamps.md Defines the Python dataclass `WordTiming` for word-level transcription details, including the word itself, start and end times, and confidence, suitable for use with the Moonshine API. ```python @dataclass class WordTiming: word: str start: float end: float confidence: float @dataclass class TranscriptLine: # ... existing fields ... words: Optional[List[WordTiming]] = None ``` -------------------------------- ### Create Japanese ONNX G2P CLI Executable Source: https://github.com/moonshine-ai/moonshine/blob/main/core/moonshine-tts/CMakeLists.txt Builds a command-line interface executable for Japanese Grapheme-to-Phoneme conversion using ONNX. It links against the ONNX Runtime library and includes Windows DLL copying. ```cmake add_executable(japanese_onnx_g2p tools/japanese-onnx-g2p-cli.cpp) target_link_libraries(japanese_onnx_g2p PRIVATE moonshine_tts_ort) moonshine_tts_set_ort_rpath(japanese_onnx_g2p) if(WIN32) copy_onnxruntime_dll(japanese_onnx_g2p) endif() ``` -------------------------------- ### Create Chinese Rule-Based G2P CLI Executable Source: https://github.com/moonshine-ai/moonshine/blob/main/core/moonshine-tts/CMakeLists.txt Builds a command-line interface executable for Chinese rule-based Grapheme-to-Phoneme conversion. It links against the ONNX Runtime library and includes Windows DLL copying. ```cmake add_executable(chinese_rule_g2p tools/chinese-rule-g2p-cli.cpp) target_link_libraries(chinese_rule_g2p PRIVATE moonshine_tts_ort) moonshine_tts_set_ort_rpath(chinese_rule_g2p) if(WIN32) copy_onnxruntime_dll(chinese_rule_g2p) endif() ``` -------------------------------- ### C API Transcript Word Structure Source: https://github.com/moonshine-ai/moonshine/blob/main/docs/word-level-timestamps.md Defines the C structure `transcript_word_t` used to represent individual words in a transcription, including the word text, start and end times, and confidence score. ```c struct transcript_word_t { const char *text; /* UTF-8 word text */ float start; /* Start time in seconds (absolute) */ float end; /* End time in seconds */ float confidence; /* 0.0 to 1.0 */ }; struct transcript_line_t { /* ... existing fields ... */ const struct transcript_word_t *words; /* NULL if not enabled */ uint64_t word_count; /* 0 if not enabled */ }; ``` -------------------------------- ### Build and Link ONNX Runtime Test Executable Source: https://github.com/moonshine-ai/moonshine/blob/main/core/third-party/onnxruntime/CMakeLists.txt Creates an executable for testing ONNX Runtime, setting its include directories and linking it against the ONNX Runtime library. Platform-specific linking flags and post-build commands are included. ```cmake add_executable(onnxruntime-test onnxruntime-test.cpp) target_include_directories(onnxruntime-test PRIVATE ${ONNXRUNTIME_INCLUDE_DIR} ) if (IOS OR MOONSHINE_BUILD_SWIFT) set_target_properties(onnxruntime-test PROPERTIES MACOSX_BUNDLE TRUE MACOSX_BUNDLE_GUI_IDENTIFIER "ai.moonshine.voice.onnxruntime-test" MACOSX_BUNDLE_BUNDLE_VERSION "1.0" MACOSX_BUNDLE_SHORT_VERSION_STRING "1.0" ) target_link_libraries(onnxruntime-test PRIVATE onnxruntime "-framework CoreFoundation" "-framework Foundation" ) elseif(WIN32) target_link_libraries(onnxruntime-test PRIVATE onnxruntime psapi ) copy_onnxruntime_dll(onnxruntime-test) else() target_link_libraries(onnxruntime-test onnxruntime ) endif() ``` -------------------------------- ### Enable and Use Word Timestamps in Swift Source: https://github.com/moonshine-ai/moonshine/blob/main/docs/word-level-timestamps.md Create a Moonshine transcriber instance in Swift, specifying `word_timestamps` as `true` in the options. Iterate through the transcription results to display the start time, end time, and text for each word. ```swift let transcriber = try Transcriber( modelPath: modelPath, modelArch: .tiny, options: [TranscriberOption(name: "word_timestamps", value: "true")]) let transcript = try transcriber.transcribeWithoutStreaming(audioData: audio) for line in transcript.lines { for word in line.words { print("[\(word.start)s - \(word.end)s] \(word.word)") } } ``` -------------------------------- ### Enable and Use Word Timestamps in Python Source: https://github.com/moonshine-ai/moonshine/blob/main/docs/word-level-timestamps.md Instantiate a Moonshine transcriber in Python with the `word_timestamps` option set to `true`. Process the transcription output to access and print word-level start times, end times, and the word itself. ```python from moonshine_voice import Transcriber, ModelArch transcriber = Transcriber(model_path, ModelArch.TINY, options={"word_timestamps": "true"}) transcript = transcriber.transcribe_without_streaming(audio_data) for line in transcript.lines: if line.words: for word in line.words: print(f"[{word.start:.3f}s - {word.end:.3f}s] {word.word}") ``` -------------------------------- ### Download and Shrink ONNX Weights Source: https://github.com/moonshine-ai/moonshine/blob/main/core/moonshine-tts/data/kokoro/README.md Use this command to download and shrink ONNX weights for experimentation. Validate audio output as numeric parity may not be preserved. ```bash python scripts/download_kokoro_onnx.py --out-dir data/kokoro --only-shrink ``` ```bash # or after export: --shrink-weights ``` -------------------------------- ### Download Audio for Intent Recognition Source: https://github.com/moonshine-ai/moonshine/blob/main/python/getting-started-with-moonshine-voice.ipynb Downloads a sample audio file containing phrases relevant to robot movement intents. This is useful for testing the intent recognition functionality. ```python # Download an audio file with some phrases that match the robot movement intents. ! curl -O -L 'https://github.com/moonshine-ai/moonshine/raw/refs/heads/main/test-assets/intent.wav' intent_audio_data, intent_sample_rate = moonshine_voice.load_wav_file("intent.wav") Audio("intent.wav") ``` -------------------------------- ### Implement Transcript Event Listener Source: https://github.com/moonshine-ai/moonshine/blob/main/python/getting-started-with-moonshine-voice.ipynb This Python code defines a custom listener class that inherits from TranscriptEventListener. It overrides methods to handle speech events like line start, text change, and line completion, printing relevant information for each event. ```python # Code that the application defines to control what happens when a speech # event is detected. class TestListener(moonshine_voice.TranscriptEventListener): # Someone started speaking a new phrase. def on_line_started(self, event: moonshine_voice.LineStarted): print(f"Line started: {event.line.text}") # The extracted text for this line has changed. def on_line_text_changed(self, event: moonshine_voice.LineTextChanged): if event.line.has_speaker_id: speaker_prefix = f". Speaker #{event.line.speaker_index}" else: speaker_prefix = "" print(f"Line text changed{speaker_prefix}: {event.line.text}") # There's been a pause in speech, so the active line is completed. # Further speech will start a new line. def on_line_completed(self, event: moonshine_voice.LineCompleted): speaker_prefix = f"Speaker #{event.line.speaker_index}: " print(f"{speaker_prefix}{event.line.text}") ``` -------------------------------- ### Enable and Use Word Timestamps in C++ Wrapper Source: https://github.com/moonshine-ai/moonshine/blob/main/docs/word-level-timestamps.md Initialize a Moonshine transcriber with word timestamp support enabled in C++. Then, transcribe audio and iterate through the results to print word-level details. ```cpp moonshine::Transcriber transcriber(modelPath, moonshine::ModelArch::TINY, {{"word_timestamps", "true"}}); auto transcript = transcriber.transcribeWithoutStreaming(audioData, 16000); for (auto &line : transcript.lines) { for (auto &word : line.words) { std::cout << "[" << word.start << "s - " << word.end << "s] " << word.word << " (conf: " << word.confidence << ")\n"; } } ``` -------------------------------- ### Export Arabic Diacritizer ONNX Model Source: https://github.com/moonshine-ai/moonshine/blob/main/core/moonshine-tts/data/ar_msa/README.md Use this script to export the Arabic diacritizer model to ONNX format. Install dependencies first. The script can optionally shrink weights to int8. To skip shrinking, use --no-shrink-weights. To shrink an existing FP32 model, use --only-shrink. ```bash python scripts/export_arabic_msa_diacritizer_onnx.py \ --out-dir data/ar_msa/arabertv02_tashkeel_fadel_onnx ``` -------------------------------- ### Configure Bin-Tokenizer Test Executable Source: https://github.com/moonshine-ai/moonshine/blob/main/core/bin-tokenizer/CMakeLists.txt Defines the bin-tokenizer-test executable, sets C++ standard to 20, and links against the bin-tokenizer library and moonshine-utils. Includes necessary directories for headers and tests. ```cmake add_executable(bin-tokenizer-test bin-tokenizer-test.cpp) target_include_directories(bin-tokenizer-test PRIVATE ${CMAKE_CURRENT_LIST_DIR} ${CMAKE_CURRENT_LIST_DIR}/../moonshine-utils ${CMAKE_CURRENT_LIST_DIR}/../third-party/doctest ) set_target_properties(bin-tokenizer-test PROPERTIES CXX_STANDARD 20 CXX_STANDARD_REQUIRED YES CXX_EXTENSIONS NO ) target_link_libraries(bin-tokenizer-test bin-tokenizer moonshine-utils ) ``` -------------------------------- ### Initialize Intent Recognizer and Register Callbacks Source: https://github.com/moonshine-ai/moonshine/blob/main/python/README.md Load the embedding model and create an IntentRecognizer. Register callback functions for specific intents like 'turn on the lights' and 'turn off the lights'. ```python embedding_model_path, embedding_model_arch = get_embedding_model() 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) ``` -------------------------------- ### Create Dutch G2P Batch CLI Executable Source: https://github.com/moonshine-ai/moonshine/blob/main/core/moonshine-tts/CMakeLists.txt Builds an executable for the Dutch G2P batch command-line interface. It links against the 'moonshine_tts_core' library. ```cmake add_executable(dutch_g2p_batch tools/dutch-g2p-batch-cli.cpp) target_link_libraries(dutch_g2p_batch PRIVATE moonshine_tts_core) ``` -------------------------------- ### Create Dutch Rule G2P CLI Executable Source: https://github.com/moonshine-ai/moonshine/blob/main/core/moonshine-tts/CMakeLists.txt Builds an executable for the Dutch rule-based G2P conversion command-line interface. It links against the 'moonshine_tts_core' library. ```cmake add_executable(dutch_rule_g2p tools/dutch-rule-g2p-cli.cpp) target_link_libraries(dutch_rule_g2p PRIVATE moonshine_tts_core) ``` -------------------------------- ### Run Transcription with Specified Model Architecture Source: https://github.com/moonshine-ai/moonshine/blob/main/examples/macos/MicTranscription/README.md Specify the model architecture to use for transcription with the --model-arch flag. Options include 'tiny', 'base', etc. ```bash swift run MicTranscription --model-arch tiny path/to/audio.wav ``` -------------------------------- ### Run Basic Transcription Source: https://github.com/moonshine-ai/moonshine/blob/main/examples/macos/MicTranscription/README.md Execute the transcription application using Swift. By default, it uses a predefined test audio file. ```bash swift run MicTranscription ``` -------------------------------- ### Create Korean Rule G2P CLI Executable Source: https://github.com/moonshine-ai/moonshine/blob/main/core/moonshine-tts/CMakeLists.txt Builds an executable for the Korean rule-based G2P conversion command-line interface. It links against the 'moonshine_tts_core' library. ```cmake add_executable(korean_rule_g2p tools/korean-rule-g2p-cli.cpp) target_link_libraries(korean_rule_g2p PRIVATE moonshine_tts_core) ``` -------------------------------- ### Create macOS XCFramework Slice Source: https://github.com/moonshine-ai/moonshine/blob/main/swift/README.md Creates a new XCFramework by combining the newly built macOS framework with existing iOS frameworks. Adjust paths as necessary. ```bash cd ../../ios xcodebuild -create-xcframework \ -framework ../core/build/build-macos/Release/moonshine.framework \ -framework Moonshine.xcframework/ios-arm64/moonshine.framework \ -framework Moonshine.xcframework/ios-arm64_x86_64-simulator/moonshine.framework \ -output Moonshine.xcframework ``` -------------------------------- ### Create Italian Rule G2P CLI Executable Source: https://github.com/moonshine-ai/moonshine/blob/main/core/moonshine-tts/CMakeLists.txt Defines an executable for the Italian rule-based G2P conversion command-line tool. It requires linking with the 'moonshine_tts_core' library. ```cmake add_executable(italian_rule_g2p tools/italian-rule-g2p-cli.cpp) target_link_libraries(italian_rule_g2p PRIVATE moonshine_tts_core) ``` -------------------------------- ### IntentRecognizer.__init__ Source: https://github.com/moonshine-ai/moonshine/blob/main/README.md Constructs a new IntentRecognizer, loading required models. It requires the path to the model files and the model architecture. ```APIDOC ## IntentRecognizer.__init__ ### Description Constructs a new recognizer, loading required models. ### Parameters #### Path Parameters - **model_path** (string) - Required - Path to a folder containing the required embedding model files. - **model_arch** (EmbeddingModelArch) - Required - An EmbeddingModelArch object obtained from `download_embedding_model()`. #### Query Parameters - **model_variant** (string) - Optional - The precision to run the model at. "q4" is recommended. - **threshold** (float) - Optional - How close an utterance has to be to the target sentence to trigger an event. ``` -------------------------------- ### Create German Rule G2P CLI Executable Source: https://github.com/moonshine-ai/moonshine/blob/main/core/moonshine-tts/CMakeLists.txt Creates an executable for the German rule-based G2P conversion command-line tool. It depends on the 'moonshine_tts_core' library. ```cmake add_executable(german_rule_g2p tools/german-rule-g2p-cli.cpp) target_link_libraries(german_rule_g2p PRIVATE moonshine_tts_core) ``` -------------------------------- ### Dialog.choose() Source: https://github.com/moonshine-ai/moonshine/blob/main/README.md Presents a set of options to the user and resumes the flow with the key of the selected option. ```APIDOC ## Dialog.choose() ### Description Returns a prompt that asks the user to pick from a set of named options and resumes the flow with the key of the matched option as a string. Each option key has a list of associated phrases; matching is done against the union of the key and its phrases using the embedding model. ### Method `choose(prompt: str, options: dict[str, list[str]], timeout: float = 8.0, max_retries: int = 2)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python # Inside a flow function: choice = yield dialog.choose("What would you like to do?", { "order_pizza": ["order pizza", "get a pizza"], "check_status": ["check status", "where is my order"] }) ``` ### Response #### Success Response - `str`: The key of the matched option. #### Response Example ```json { "example": "order_pizza" } ``` ``` -------------------------------- ### Run Transcription with Specified Audio Files Source: https://github.com/moonshine-ai/moonshine/blob/main/examples/macos/MicTranscription/README.md Provide one or more audio file paths as arguments to the transcription application. ```bash swift run MicTranscription path/to/audio1.wav path/to/audio2.wav ``` -------------------------------- ### Run Basic Transcription with Language Specification Source: https://github.com/moonshine-ai/moonshine/blob/main/examples/macos/BasicTranscription/README.md Specify the language for transcription using the --language or -l flag, followed by the language code and the audio file path. ```bash swift run BasicTranscription --language en path/to/audio.wav ``` -------------------------------- ### Create Portuguese Rule G2P CLI Executable Source: https://github.com/moonshine-ai/moonshine/blob/main/core/moonshine-tts/CMakeLists.txt Defines an executable for the Portuguese rule-based G2P conversion command-line tool. It requires linking with the 'moonshine_tts_core' library. ```cmake add_executable(portuguese_rule_g2p tools/portuguese-rule-g2p-cli.cpp) target_link_libraries(portuguese_rule_g2p PRIVATE moonshine_tts_core) ```