NativeBackend class

High-level wrapper around the RunAnywhere native C API.

This class provides a Dart-friendly interface to native backends, handling memory management and type conversions.

The new architecture supports multiple backends:

  • LlamaCPP: LLM text generation
  • ONNX: STT, TTS, VAD

Architecture Note

  • RACommons provides the generic component APIs (rac_llm_component_*, rac_stt_component_*, etc.)
  • Backend libraries (LlamaCPP, ONNX) register themselves with RACommons

For LlamaCPP, component functions are loaded from RACommons, matching the pattern used in Swift's CppBridge and React Native's C++ bridges.

Usage

// For LlamaCPP
final llamacpp = NativeBackend.llamacpp();
llamacpp.initialize();
llamacpp.loadModel('/path/to/model.gguf');
final result = llamacpp.generate('Hello, world!');
llamacpp.dispose();

// For ONNX
final onnx = NativeBackend.onnx();
onnx.initialize();
onnx.loadSttModel('/path/to/whisper');
final text = onnx.transcribe(audioSamples);
onnx.dispose();

Constructors

NativeBackend()
Create a NativeBackend using RACommons for all component operations.
factory
NativeBackend.llamacpp()
Create a NativeBackend for LLM operations.
factory
NativeBackend.onnx()
Create a NativeBackend for STT/TTS/VAD operations.
factory

Properties

backendName String
Get the backend type.
no setter
handle RacHandle?
Get the backend handle (for advanced operations).
no setter
hashCode int
The hash code for this object.
no setterinherited
isInitialized bool
Check if the backend is initialized.
no setter
isSttModelLoaded bool
Check if an STT model is loaded.
no setter
isTextModelLoaded bool
Check if a text model is loaded.
no setter
isTtsModelLoaded bool
Check if a TTS model is loaded.
no setter
isVadModelLoaded bool
Check if a VAD model is loaded.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
sttSupportsStreaming bool
Check if STT supports streaming.
no setter
version String
Get the library version.
no setter

Methods

cancelTextGeneration() → void
Cancel ongoing text generation.
create(String backendName, {Map<String, dynamic>? config}) → void
Create and initialize the backend.
dispose() → void
Destroy the backend and release resources.
generate(String prompt, {String? systemPrompt, int maxTokens = 512, double temperature = 0.7}) Map<String, dynamic>
Generate text (non-streaming).
getAvailableBackends() List<String>
Get list of available backend names.
getBackendInfo() Map<String, dynamic>
Get backend info as a map.
getTtsVoices() List<String>
Get available TTS voices.
initialize() → void
Initialize the backend (simplified for new architecture).
loadSttModel(String modelPath, {String modelType = 'whisper', Map<String, dynamic>? config}) → void
Load an STT model.
loadTextModel(String modelPath, {Map<String, dynamic>? config}) → void
Load a text generation model (LLM).
loadTtsModel(String modelPath, {String modelType = 'vits', Map<String, dynamic>? config}) → void
Load a TTS model.
loadVadModel(String? modelPath, {Map<String, dynamic>? config}) → void
Load a VAD model.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
processVad(Float32List samples, {int sampleRate = 16000}) Map<String, dynamic>
Process audio for voice activity detection.
supportsCapability(int capability) bool
Check if backend supports a specific capability.
synthesize(String text, {String? voiceId, double speed = 1.0, double pitch = 0.0}) Map<String, dynamic>
Synthesize speech from text.
toString() String
A string representation of this object.
inherited
transcribe(Float32List samples, {int sampleRate = 16000, String? language}) Map<String, dynamic>
Transcribe audio samples (batch mode).
unloadSttModel() → void
Unload the STT model.
unloadTextModel() → void
Unload the text model.
unloadTtsModel() → void
Unload the TTS model.
unloadVadModel() → void
Unload the VAD model.

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

tryCreate() NativeBackend?
Try to create a native backend, returning null if it fails.