Skip to main content

TranscriptLine

A single line of transcription representing a phrase or segment of speech.
public class TranscriptLine

Fields

text
String
UTF-8 encoded transcription text
audioData
float[]
Audio data for this line if available (16KHz float PCM, -1.0 to 1.0)
startTime
float
Time offset from the start of the audio in seconds
duration
float
Duration of the segment in seconds
id
long
Stable identifier for the line (unique within a session)
isComplete
boolean
Whether the line is complete. When true, the text will not change anymore.
isUpdated
boolean
Whether the line has been updated since the previous call (streaming only)
isNew
boolean
Whether the line was newly added since the previous call (streaming only)
hasTextChanged
boolean
Whether the text has changed since the previous call (streaming only)
hasSpeakerId
boolean
Whether a speaker ID has been calculated for the line
speakerId
long
The speaker ID for the line (only valid if hasSpeakerId is true)
speakerIndex
int
The order the speaker appeared in the current transcript (0-based)
lastTranscriptionLatencyMs
int
The latency of the last transcription operation in milliseconds

Methods

toString

Returns a string representation of the transcript line.
public String toString()
Example:
TranscriptLine line = // ... from event

System.out.println("[" + line.startTime + "s] " + line.text);
System.out.println("Duration: " + line.duration + "s");
System.out.println("Complete: " + line.isComplete);

if (line.hasSpeakerId) {
    System.out.println("Speaker " + (line.speakerIndex + 1));
}

// Full details
System.out.println(line.toString());

Transcript

A complete transcript containing multiple lines.
public class Transcript

Fields

lines
TranscriptLine[]
Array of all lines in the transcript in chronological order
Example:
Transcript transcript = transcriber.transcribeWithoutStreaming(
    audioData,
    16000
);

for (TranscriptLine line : transcript.lines) {
    System.out.println("[" + line.startTime + "s] " + line.text);
}

// Get full text
StringBuilder fullText = new StringBuilder();
for (TranscriptLine line : transcript.lines) {
    if (fullText.length() > 0) {
        fullText.append(" ");
    }
    fullText.append(line.text);
}
System.out.println("Full transcript: " + fullText.toString());

TranscriptEvent (Interface)

Base interface for all transcript events. Uses the Visitor pattern for type-safe event handling.
public interface TranscriptEvent

Methods

accept

Accept a visitor to handle this event.
void accept(Visitor visitor)

Visitor Interface

Interface for handling different event types.
interface Visitor {
    void onLineStarted(LineStarted event);
    void onLineUpdated(LineUpdated event);
    void onLineTextChanged(LineTextChanged event);
    void onLineCompleted(LineCompleted event);
    void onError(Error event);
}
Example:
transcriber.addListener(event -> {
    event.accept(new TranscriptEvent.Visitor() {
        @Override
        public void onLineStarted(TranscriptEvent.LineStarted e) {
            // Handle line started
        }
        
        @Override
        public void onLineUpdated(TranscriptEvent.LineUpdated e) {
            // Handle line updated
        }
        
        @Override
        public void onLineTextChanged(TranscriptEvent.LineTextChanged e) {
            // Handle text changed
        }
        
        @Override
        public void onLineCompleted(TranscriptEvent.LineCompleted e) {
            // Handle line completed
        }
        
        @Override
        public void onError(TranscriptEvent.Error e) {
            // Handle error
        }
    });
});

TranscriptEvent.LineStarted

Event emitted when a new transcription line starts.
class LineStarted implements TranscriptEvent

Fields

line
TranscriptLine
The transcript line associated with this event
streamHandle
int
The handle of the stream that emitted this event
Example:
@Override
public void onLineStarted(TranscriptEvent.LineStarted event) {
    System.out.println("New line: " + event.line.text);
}

TranscriptEvent.LineUpdated

Event emitted when an existing transcription line is updated.
class LineUpdated implements TranscriptEvent

Fields

line
TranscriptLine
The transcript line associated with this event
streamHandle
int
The handle of the stream that emitted this event
Example:
@Override
public void onLineUpdated(TranscriptEvent.LineUpdated event) {
    System.out.println("Updated: " + event.line.text);
}

TranscriptEvent.LineTextChanged

Event emitted when the text of a transcription line changes.
class LineTextChanged implements TranscriptEvent

Fields

line
TranscriptLine
The transcript line associated with this event
streamHandle
int
The handle of the stream that emitted this event
Example:
@Override
public void onLineTextChanged(TranscriptEvent.LineTextChanged event) {
    // Update UI with new text
    runOnUiThread(() -> {
        textView.setText(event.line.text);
    });
}

TranscriptEvent.LineCompleted

Event emitted when a transcription line is completed.
class LineCompleted implements TranscriptEvent

Fields

line
TranscriptLine
The transcript line associated with this event
streamHandle
int
The handle of the stream that emitted this event
Example:
@Override
public void onLineCompleted(TranscriptEvent.LineCompleted event) {
    System.out.println("Final: " + event.line.text);
    // Save to database, process final result, etc.
}

TranscriptEvent.Error

Event emitted when an error occurs.
class Error implements TranscriptEvent

Fields

cause
Throwable
The throwable that caused the error
streamHandle
int
The handle of the stream that emitted this event
Example:
@Override
public void onError(TranscriptEvent.Error event) {
    System.err.println("Error: " + event.cause.getMessage());
    event.cause.printStackTrace();
}

Model Architecture Constants

Model architecture constants are defined in the JNI class:
public class JNI {
    public static final int MOONSHINE_MODEL_ARCH_TINY = 0;
    public static final int MOONSHINE_MODEL_ARCH_BASE = 1;
    public static final int MOONSHINE_MODEL_ARCH_TINY_STREAMING = 2;
    public static final int MOONSHINE_MODEL_ARCH_BASE_STREAMING = 3;
    public static final int MOONSHINE_MODEL_ARCH_SMALL_STREAMING = 4;
    public static final int MOONSHINE_MODEL_ARCH_MEDIUM_STREAMING = 5;
    
    public static final int MOONSHINE_FLAG_FORCE_UPDATE = 1;
}
Example:
import ai.moonshine.voice.JNI;

// Use model architecture constants
transcriber.loadFromFiles(
    modelPath,
    JNI.MOONSHINE_MODEL_ARCH_MEDIUM_STREAMING
);

Helper Classes

TranscriberOption

Represents a configuration option for the transcriber.
public class TranscriberOption {
    public String key;
    public String value;
    
    public TranscriberOption(String key, String value) {
        this.key = key;
        this.value = value;
    }
}
Example:
import ai.moonshine.voice.TranscriberOption;
import java.util.ArrayList;
import java.util.List;

List<TranscriberOption> options = new ArrayList<>();
options.add(new TranscriberOption("max_tokens_per_second", "13.0"));

Transcriber transcriber = new Transcriber(options);

Build docs developers (and LLMs) love