Skip to content

Repository files navigation

silero-vad-kmp

Silero VAD (Voice Activity Detector) for Kotlin Multiplatform, powered by onnxruntime-kmp. The ONNX model is embedded into the library with the cn.rtast.kembeddable Gradle plugin.

Platform Backend
JVM (desktop) com.microsoft.onnxruntime:onnxruntime via onnxruntime-kmp
Android com.microsoft.onnxruntime:onnxruntime-android via onnxruntime-kmp
macOS arm64 / x64 prebuilt libonnxruntime via onnxruntime-kmp cinterop
Linux x64 prebuilt libonnxruntime via onnxruntime-kmp cinterop
Windows x64 prebuilt onnxruntime.dll via onnxruntime-kmp cinterop

Installation

Multiplatform project

// settings.gradle.kts
dependencyResolutionManagement {
    repositories {
        mavenCentral()
        // kembeddable runtime artifacts
        maven("https://repo.maven.rtast.cn/releases")
    }
}

// module build.gradle.kts
kotlin {
    sourceSets {
        commonMain.dependencies {
            implementation("cn.enaium:silero-vad-kmp:1.0.0")
        }
    }
}

JVM only

dependencies {
    implementation("cn.enaium:silero-vad-kmp-jvm:1.0.0")
}

Android

dependencies {
    implementation("cn.enaium:silero-vad-kmp-android:1.0.0")
}

Kotlin/Native

The model is embedded via the kembeddable plugin (no extra configuration needed). The libonnxruntime shared library must be shipped next to the final binary (see onnxruntime-kmp for the artifact/table):

Platform Runtime file to ship
macOS arm64 libonnxruntime.1.dylib
macOS x64 libonnxruntime.1.23.2.dylib
Linux x64 libonnxruntime.so.1
Windows x64 onnxruntime.dll

Usage

import cn.enaium.silero.vad.SileroVad
import cn.enaium.silero.vad.config.SampleRate

val vad = SileroVad(
    sampleRate = SampleRate.SAMPLE_RATE_16K,   // 8K or 16K
    minSpeechDurationMs = 100,
    minSilenceDurationMs = 100,
)

// 16 kHz -> 512 samples (32 ms) per frame, 8 kHz -> 256 samples.
val speech = vad.isSpeech(shortSamples)   // ShortArray / FloatArray / ByteArray (16-bit LE PCM)

vad.close()
  • isSpeech(ShortArray) / isSpeech(FloatArray) / isSpeech(ByteArray) (little-endian 16-bit PCM).
  • The constructor feeds one frame at a time; the model keeps its internal state.
  • reset() clears the internal state (call before a new utterance/stream).

Example

The example module shares a Compose Multiplatform UI; only the microphone reader differs per platform:

  • JVM desktopjavax.sound TargetDataLine: run with ./gradlew :example:run.
  • AndroidAudioRecord: host the example AAR in an app (contains the MainActivity).
  • macOS nativeAVFAudio AVAudioEngine: run with ./gradlew :example:runDebugExecutableMacosArm64 (console output; the shared Compose UI is JVM-only, so the native example is a CLI app).

Building

./gradlew publishToMavenLocal        # requires onnxruntime-kmp 1.0.0 in ~/.m2
./gradlew jvmTest                    # JVM tests (embedded model over a 60 s test WAV)
./gradlew macosArm64Test             # native test (copies libonnxruntime next to the binary)
./gradlew :example:run               # JVM desktop example
./gradlew :example:runDebugExecutableMacosArm64   # macOS native example (AVAudioEngine)

License

MIT, see LICENSE. The embedded silero_vad.onnx model is from the snakers4/silero-vad project (MIT).

About

Silero VAD: pre-trained enterprise-grade Voice Activity Detector

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages