JavaScript SDK

MoonshineJS is our SDK for JavaScript, built to make it easy for web developers to build modern, speech-driven web experiences without sacrificing user privacy.

Installation

You can use MoonshineJS via CDN, or you can install it with npm. Simply import the package depending on your preferred method.

Via CDN

import * as Moonshine from 'https://cdn.jsdelivr.net/npm/@moonshine-ai/moonshine-js@latest/dist/moonshine.min.js'

Via npm

Install the package first:

npm install @moonshine-ai/moonshine-js

Then import:

import * as Moonshine from '@moonshine-ai/moonshine-js'

Quickstart

Let’s get started with a simple example. We’ll create a transcriber to print speech from the microphone to the console. We can use the MicrophoneTranscriber for that. You can control the behavior of the transcriber by passing it a set of callbacks when you create it:

var transcriber = new Moonshine.MicrophoneTranscriber(
    "model/tiny", // the fastest and smallest Moonshine model
    {
        onTranscriptionCommitted(text) {
            console.log(text)
        }
    }
)

transcriber.start();

When we start the transcriber, the browser will request mic permissions and begin printing everything the user says to the console. By default, the transcriber uses voice activity detection (VAD) to determine when to transcribe speech. It will wait until long pauses in speech to output a transcript.

MoonshineJS also supports a streaming mode, where the transcript is continually updated as you speak. To do that, we disable VAD mode and add a new callback that will continually receive the latest update to the transcript:

var transcriber = new Moonshine.MicrophoneTranscriber(
    "model/tiny",
    {
        onTranscriptionCommitted(text) {
            console.log(`commit: ${text}`)
        }
        onTranscriptionUpdated(text) {
            console.log(`update: ${text}`)
        },
    },
    false // use streaming mode instead of VAD mode
)

transcriber.start();

Now the transcription updates continually in a streaming fashion, and commits when longer pauses in speech are detected.

That’s all it takes to get started! Read the guides to learn how to transcribe audio from other sources, or to build voice-controlled applications.