Overview
The Transcriber class is the main entry point for Moonshine Voice in Swift. It handles loading models and managing transcription sessions.
Initialization
init(modelPath:modelArch:options:)
Initialize a transcriber from model files on disk.
public init(
modelPath: String,
modelArch: ModelArch = .base,
options: [TranscriberOption]? = nil
) throws
Path to the directory containing model files (encoder_model.ort, decoder_model_merged.ort, tokenizer.bin)
Model architecture to use. Options: .tiny, .base, .tinyStreaming, .baseStreaming, .smallStreaming, .mediumStreaming
Optional transcriber options for advanced configuration
Throws: MoonshineError if the transcriber cannot be loaded
Example:
import MoonshineVoice
do {
let transcriber = try Transcriber(
modelPath: "/path/to/models",
modelArch: .base
)
} catch {
print("Failed to initialize transcriber: \(error)")
}
Non-Streaming Transcription
transcribeWithoutStreaming(audioData:sampleRate:flags:)
Transcribe audio data without streaming.
public func transcribeWithoutStreaming(
audioData: [Float],
sampleRate: Int32 = 16000,
flags: UInt32 = 0
) throws -> Transcript
Array of PCM audio samples (float values from -1.0 to 1.0)
Returns: A Transcript object containing the transcription lines
Throws: MoonshineError if transcription fails
Example:
let audioData: [Float] = // ... load audio data
do {
let transcript = try transcriber.transcribeWithoutStreaming(
audioData: audioData,
sampleRate: 16000
)
for line in transcript.lines {
print("[\(line.startTime)s] \(line.text)")
}
} catch {
print("Transcription failed: \(error)")
}
Streaming Transcription (Default Stream)
The transcriber provides convenience methods that operate on a default stream:
start()
Start the default stream.
public func start() throws
Throws: MoonshineError if starting fails
stop()
Stop the default stream and process any remaining audio.
public func stop() throws
Throws: MoonshineError if stopping fails
addAudio(_:sampleRate:)
Add audio data to the default stream.
public func addAudio(_ audioData: [Float], sampleRate: Int32) throws
Array of PCM audio samples (float values from -1.0 to 1.0)
Throws: MoonshineError if adding audio fails
updateTranscription()
Manually update the transcription from the default stream.
public func updateTranscription() throws -> Transcript
Returns: The current transcript
Throws: MoonshineError if updating fails
Event Listeners
addListener(_:)
Add an event listener to the default stream. Supports both closure-based and protocol-based listeners.
public func addListener(_ listener: @escaping (TranscriptEvent) throws -> Void) throws
public func addListener(_ listener: TranscriptEventListener) throws
Example with closure:
try transcriber.addListener { event in
if let lineCompleted = event as? LineCompleted {
print("Completed: \(lineCompleted.line.text)")
}
}
Example with protocol:
class MyListener: TranscriptEventListener {
func onLineStarted(_ event: LineStarted) {
print("Line started: \(event.line.text)")
}
func onLineCompleted(_ event: LineCompleted) {
print("Line completed: \(event.line.text)")
}
}
let listener = MyListener()
try transcriber.addListener(listener)
removeListener(_:)
Remove an event listener from the default stream.
public func removeListener(_ listener: @escaping (TranscriptEvent) throws -> Void) throws
public func removeListener(_ listener: TranscriptEventListener) throws
removeAllListeners()
Remove all event listeners from the default stream.
public func removeAllListeners() throws
Stream Management
createStream(updateInterval:flags:)
Create a new stream for real-time transcription.
public func createStream(
updateInterval: TimeInterval = 0.5,
flags: UInt32 = 0
) throws -> Stream
updateInterval
TimeInterval
default:"0.5"
Interval in seconds between automatic updates
Flags for stream creation
Returns: A Stream object for real-time transcription
Throws: MoonshineError if stream creation fails
Example:
let stream = try transcriber.createStream(updateInterval: 0.3)
stream.addListener { event in
if let lineTextChanged = event as? LineTextChanged {
print("Updated text: \(lineTextChanged.line.text)")
}
}
try stream.start()
// Feed audio to stream
let audioData: [Float] = // ... audio data
try stream.addAudio(audioData, sampleRate: 16000)
try stream.stop()
stream.close()
getDefaultStream()
Get the default stream.
public func getDefaultStream() throws -> Stream
Returns: The default stream
Throws: MoonshineError if stream creation fails
Other Methods
getVersion()
Get the version of the loaded Moonshine library.
public func getVersion() -> Int32
Returns: The version number
close()
Free the transcriber resources.
Complete Example
import MoonshineVoice
class MyListener: TranscriptEventListener {
func onLineStarted(_ event: LineStarted) {
print("Started: \(event.line.text)")
}
func onLineTextChanged(_ event: LineTextChanged) {
print("Updated: \(event.line.text)")
}
func onLineCompleted(_ event: LineCompleted) {
print("Completed: \(event.line.text)")
}
}
do {
// Initialize transcriber
let transcriber = try Transcriber(
modelPath: "/path/to/models",
modelArch: .base
)
// Add listener
let listener = MyListener()
try transcriber.addListener(listener)
// Start transcription
try transcriber.start()
// Feed audio data
let audioData: [Float] = // ... load audio
try transcriber.addAudio(audioData, sampleRate: 16000)
// Stop transcription
try transcriber.stop()
// Clean up
transcriber.close()
} catch {
print("Error: \(error)")
}