Add Flush to VAD so that the last segment can be detected. (#1099)

This commit is contained in:
Fangjun Kuang
2024-07-09 16:15:56 +08:00
committed by GitHub
parent 3e4307e2fb
commit c2cc9dec58
35 changed files with 237 additions and 29 deletions

View File

@@ -17,7 +17,7 @@ topics:
- voice-activity-detection
# remember to change the version in ../sherpa_onnx_macos/macos/sherpa_onnx.podspec
version: 1.10.6
version: 1.10.12
homepage: https://github.com/k2-fsa/sherpa-onnx

View File

@@ -53,6 +53,11 @@ namespace SherpaOnnx
SherpaOnnxVoiceActivityDetectorReset(_handle.Handle);
}
public void Flush()
{
SherpaOnnxVoiceActivityDetectorFlush(_handle.Handle);
}
public void Dispose()
{
Cleanup();
@@ -106,5 +111,7 @@ namespace SherpaOnnx
[DllImport(Dll.Filename)]
private static extern void SherpaOnnxVoiceActivityDetectorReset(IntPtr handle);
[DllImport(Dll.Filename)]
private static extern void SherpaOnnxVoiceActivityDetectorFlush(IntPtr handle);
}
}

View File

@@ -856,6 +856,10 @@ func (vad *VoiceActivityDetector) Reset() {
C.SherpaOnnxVoiceActivityDetectorReset(vad.impl)
}
func (vad *VoiceActivityDetector) Flush() {
C.SherpaOnnxVoiceActivityDetectorFlush(vad.impl)
}
// Spoken language identification
type SpokenLanguageIdentificationWhisperConfig struct {

View File

@@ -29,7 +29,7 @@ class CircularBuffer {
}
reset() {
return addon.circularBufferReset(this.handle);
addon.circularBufferReset(this.handle);
}
}
@@ -79,7 +79,11 @@ config = {
}
reset() {
return addon.VoiceActivityDetectorResetWrapper(this.handle);
addon.VoiceActivityDetectorResetWrapper(this.handle);
}
flush() {
addon.VoiceActivityDetectorFlushWrapper(this.handle);
}
}

View File

@@ -590,6 +590,31 @@ static void VoiceActivityDetectorResetWrapper(const Napi::CallbackInfo &info) {
SherpaOnnxVoiceActivityDetectorReset(vad);
}
static void VoiceActivityDetectorFlushWrapper(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
if (info.Length() != 1) {
std::ostringstream os;
os << "Expect only 1 argument. Given: " << info.Length();
Napi::TypeError::New(env, os.str()).ThrowAsJavaScriptException();
return;
}
if (!info[0].IsExternal()) {
Napi::TypeError::New(env, "Argument 0 should be a VAD pointer.")
.ThrowAsJavaScriptException();
return;
}
SherpaOnnxVoiceActivityDetector *vad =
info[0].As<Napi::External<SherpaOnnxVoiceActivityDetector>>().Data();
SherpaOnnxVoiceActivityDetectorFlush(vad);
}
void InitVad(Napi::Env env, Napi::Object exports) {
exports.Set(Napi::String::New(env, "createCircularBuffer"),
Napi::Function::New(env, CreateCircularBufferWrapper));
@@ -636,4 +661,7 @@ void InitVad(Napi::Env env, Napi::Object exports) {
exports.Set(Napi::String::New(env, "voiceActivityDetectorReset"),
Napi::Function::New(env, VoiceActivityDetectorResetWrapper));
exports.Set(Napi::String::New(env, "voiceActivityDetectorFlush"),
Napi::Function::New(env, VoiceActivityDetectorFlushWrapper));
}