Add Flutter GUI example for VAD with a microphone. (#905)
This commit is contained in:
@@ -345,6 +345,12 @@ typedef SherpaOnnxReadWaveNative = Pointer<SherpaOnnxWave> Function(
|
||||
|
||||
typedef SherpaOnnxReadWave = SherpaOnnxReadWaveNative;
|
||||
|
||||
typedef SherpaOnnxWriteWaveNative = Int32 Function(
|
||||
Pointer<Float>, Int32, Int32, Pointer<Utf8>);
|
||||
|
||||
typedef SherpaOnnxWriteWave = int Function(
|
||||
Pointer<Float>, int, int, Pointer<Utf8>);
|
||||
|
||||
typedef SherpaOnnxFreeWaveNative = Void Function(Pointer<SherpaOnnxWave>);
|
||||
|
||||
typedef SherpaOnnxFreeWave = void Function(Pointer<SherpaOnnxWave>);
|
||||
@@ -448,6 +454,8 @@ class SherpaOnnxBindings {
|
||||
|
||||
static SherpaOnnxReadWave? readWave;
|
||||
|
||||
static SherpaOnnxWriteWave? writeWave;
|
||||
|
||||
static SherpaOnnxFreeWave? freeWave;
|
||||
|
||||
static void init(DynamicLibrary dynamicLibrary) {
|
||||
@@ -686,6 +694,11 @@ class SherpaOnnxBindings {
|
||||
.lookup<NativeFunction<SherpaOnnxReadWaveNative>>('SherpaOnnxReadWave')
|
||||
.asFunction();
|
||||
|
||||
writeWave ??= dynamicLibrary
|
||||
.lookup<NativeFunction<SherpaOnnxWriteWaveNative>>(
|
||||
'SherpaOnnxWriteWave')
|
||||
.asFunction();
|
||||
|
||||
freeWave ??= dynamicLibrary
|
||||
.lookup<NativeFunction<SherpaOnnxFreeWaveNative>>('SherpaOnnxFreeWave')
|
||||
.asFunction();
|
||||
|
||||
@@ -113,7 +113,7 @@ class CircularBuffer {
|
||||
}
|
||||
|
||||
class VoiceActivityDetector {
|
||||
VoiceActivityDetector._({required this.ptr});
|
||||
VoiceActivityDetector._({required this.ptr, required this.config});
|
||||
|
||||
// The user has to invoke VoiceActivityDetector.free() to avoid memory leak.
|
||||
factory VoiceActivityDetector(
|
||||
@@ -144,7 +144,7 @@ class VoiceActivityDetector {
|
||||
calloc.free(modelPtr);
|
||||
calloc.free(c);
|
||||
|
||||
return VoiceActivityDetector._(ptr: ptr);
|
||||
return VoiceActivityDetector._(ptr: ptr, config: config);
|
||||
}
|
||||
|
||||
void free() {
|
||||
@@ -210,4 +210,5 @@ class VoiceActivityDetector {
|
||||
}
|
||||
|
||||
Pointer<SherpaOnnxVoiceActivityDetector> ptr;
|
||||
final VadModelConfig config;
|
||||
}
|
||||
|
||||
27
sherpa-onnx/flutter/lib/src/wave_writer.dart
Normal file
27
sherpa-onnx/flutter/lib/src/wave_writer.dart
Normal file
@@ -0,0 +1,27 @@
|
||||
// Copyright (c) 2024 Xiaomi Corporation
|
||||
import 'dart:ffi';
|
||||
import 'dart:typed_data';
|
||||
import 'package:ffi/ffi.dart';
|
||||
|
||||
import './sherpa_onnx_bindings.dart';
|
||||
|
||||
bool writeWave(
|
||||
{required String filename,
|
||||
required Float32List samples,
|
||||
required int sampleRate}) {
|
||||
final Pointer<Utf8> filenamePtr = filename.toNativeUtf8();
|
||||
|
||||
final n = samples.length;
|
||||
final Pointer<Float> p = calloc<Float>(n);
|
||||
|
||||
final pList = p.asTypedList(n);
|
||||
pList.setAll(0, samples);
|
||||
|
||||
int ok =
|
||||
SherpaOnnxBindings.writeWave?.call(p, n, sampleRate, filenamePtr) ?? 0;
|
||||
|
||||
calloc.free(p);
|
||||
calloc.free(filenamePtr);
|
||||
|
||||
return ok == 1;
|
||||
}
|
||||
Reference in New Issue
Block a user