fixed tenvad

This commit is contained in:
rahulpscraftech360 2025-08-18 15:18:11 +05:30
parent 3371c45608
commit 1d0aaa5e15
7 changed files with 187 additions and 139 deletions

4
.gitignore vendored
View file

@ -161,6 +161,10 @@ main/manager-web/node_modules
# model files - exclude entire models directory since they auto-download # model files - exclude entire models directory since they auto-download
main/xiaozhi-server/models/ main/xiaozhi-server/models/
models/sherpa-onnx-zipformer-gigaspeech-2023-12-12/ models/sherpa-onnx-zipformer-gigaspeech-2023-12-12/
# Exception for ten-vad-onnx - include this model with its libraries
!main/xiaozhi-server/models/ten-vad-onnx/
!main/xiaozhi-server/models/ten-vad-onnx/**
/main/xiaozhi-server/audio_ref/ /main/xiaozhi-server/audio_ref/
/audio_ref/ /audio_ref/
/asr-models/iic/SenseVoiceSmall/ /asr-models/iic/SenseVoiceSmall/

View file

@ -1,139 +1,139 @@
# #
# Copyright © 2025 Agora # Copyright © 2025 Agora
# This file is part of TEN Framework, an open source project. # This file is part of TEN Framework, an open source project.
# Licensed under the Apache License, Version 2.0, with certain conditions. # Licensed under the Apache License, Version 2.0, with certain conditions.
# Refer to the "LICENSE" file in the root directory for more information. # Refer to the "LICENSE" file in the root directory for more information.
# #
from ctypes import c_int, c_int32, c_float, c_size_t, CDLL, c_void_p, POINTER from ctypes import c_int, c_int32, c_float, c_size_t, CDLL, c_void_p, POINTER
import numpy as np import numpy as np
import os import os
import platform import platform
class TenVad: class TenVad:
def __init__(self, hop_size: int = 256, threshold: float = 0.5): def __init__(self, hop_size: int = 256, threshold: float = 0.5):
self.hop_size = hop_size self.hop_size = hop_size
self.threshold = threshold self.threshold = threshold
if platform.system() == "Linux" and platform.machine() == "x86_64": if platform.system() == "Linux" and platform.machine() == "x86_64":
git_path = os.path.join( git_path = os.path.join(
os.path.dirname(os.path.relpath(__file__)), os.path.dirname(os.path.relpath(__file__)),
"../lib/Linux/x64/libten_vad.so" "../lib/Linux/x64/libten_vad.so"
) )
if os.path.exists(git_path): if os.path.exists(git_path):
self.vad_library = CDLL(git_path) self.vad_library = CDLL(git_path)
else: else:
pip_path = os.path.join( pip_path = os.path.join(
os.path.dirname(os.path.relpath(__file__)), os.path.dirname(os.path.relpath(__file__)),
"./ten_vad_library/libten_vad.so" "./ten_vad_library/libten_vad.so"
) )
self.vad_library = CDLL(pip_path) self.vad_library = CDLL(pip_path)
elif platform.system() == "Darwin": elif platform.system() == "Darwin":
git_path = os.path.join( git_path = os.path.join(
os.path.dirname(os.path.relpath(__file__)), os.path.dirname(os.path.relpath(__file__)),
"../lib/macOS/ten_vad.framework/Versions/A/ten_vad" "../lib/macOS/ten_vad.framework/Versions/A/ten_vad"
) )
if os.path.exists(git_path): if os.path.exists(git_path):
self.vad_library = CDLL(git_path) self.vad_library = CDLL(git_path)
else: else:
pip_path = os.path.join( pip_path = os.path.join(
os.path.dirname(os.path.relpath(__file__)), os.path.dirname(os.path.relpath(__file__)),
"./ten_vad_library/libten_vad" "./ten_vad_library/libten_vad"
) )
self.vad_library = CDLL(pip_path) self.vad_library = CDLL(pip_path)
elif platform.system().upper() == 'WINDOWS': elif platform.system().upper() == 'WINDOWS':
if platform.machine().upper() in ['X64', 'X86_64', 'AMD64']: if platform.machine().upper() in ['X64', 'X86_64', 'AMD64']:
git_path = os.path.join( git_path = os.path.join(
os.path.dirname(os.path.realpath(__file__)), os.path.dirname(os.path.realpath(__file__)),
"../lib/Windows/x64/ten_vad.dll" "../lib/Windows/x64/ten_vad.dll"
) )
if os.path.exists(git_path): if os.path.exists(git_path):
self.vad_library = CDLL(git_path) self.vad_library = CDLL(git_path)
else: else:
pip_path = os.path.join( pip_path = os.path.join(
os.path.dirname(os.path.realpath(__file__)), os.path.dirname(os.path.realpath(__file__)),
"./ten_vad_library/ten_vad.dll" "./ten_vad_library/ten_vad.dll"
) )
self.vad_library = CDLL(pip_path) self.vad_library = CDLL(pip_path)
else: else:
git_path = os.path.join( git_path = os.path.join(
os.path.dirname(os.path.realpath(__file__)), os.path.dirname(os.path.realpath(__file__)),
"../lib/Windows/x86/ten_vad.dll" "../lib/Windows/x86/ten_vad.dll"
) )
if os.path.exists(git_path): if os.path.exists(git_path):
self.vad_library = CDLL(git_path) self.vad_library = CDLL(git_path)
else: else:
pip_path = os.path.join( pip_path = os.path.join(
os.path.dirname(os.path.realpath(__file__)), os.path.dirname(os.path.realpath(__file__)),
"./ten_vad_library/ten_vad.dll" "./ten_vad_library/ten_vad.dll"
) )
self.vad_library = CDLL(pip_path) self.vad_library = CDLL(pip_path)
else: else:
raise NotImplementedError(f"Unsupported platform: {platform.system()} {platform.machine()}") raise NotImplementedError(f"Unsupported platform: {platform.system()} {platform.machine()}")
self.vad_handler = c_void_p(0) self.vad_handler = c_void_p(0)
self.out_probability = c_float() self.out_probability = c_float()
self.out_flags = c_int32() self.out_flags = c_int32()
self.vad_library.ten_vad_create.argtypes = [ self.vad_library.ten_vad_create.argtypes = [
POINTER(c_void_p), POINTER(c_void_p),
c_size_t, c_size_t,
c_float, c_float,
] ]
self.vad_library.ten_vad_create.restype = c_int self.vad_library.ten_vad_create.restype = c_int
self.vad_library.ten_vad_destroy.argtypes = [POINTER(c_void_p)] self.vad_library.ten_vad_destroy.argtypes = [POINTER(c_void_p)]
self.vad_library.ten_vad_destroy.restype = c_int self.vad_library.ten_vad_destroy.restype = c_int
self.vad_library.ten_vad_process.argtypes = [ self.vad_library.ten_vad_process.argtypes = [
c_void_p, c_void_p,
c_void_p, c_void_p,
c_size_t, c_size_t,
POINTER(c_float), POINTER(c_float),
POINTER(c_int32), POINTER(c_int32),
] ]
self.vad_library.ten_vad_process.restype = c_int self.vad_library.ten_vad_process.restype = c_int
self.create_and_init_handler() self.create_and_init_handler()
def create_and_init_handler(self): def create_and_init_handler(self):
assert ( assert (
self.vad_library.ten_vad_create( self.vad_library.ten_vad_create(
POINTER(c_void_p)(self.vad_handler), POINTER(c_void_p)(self.vad_handler),
c_size_t(self.hop_size), c_size_t(self.hop_size),
c_float(self.threshold), c_float(self.threshold),
) )
== 0 == 0
), "[TEN VAD]: create handler failure!" ), "[TEN VAD]: create handler failure!"
def __del__(self): def __del__(self):
assert ( assert (
self.vad_library.ten_vad_destroy( self.vad_library.ten_vad_destroy(
POINTER(c_void_p)(self.vad_handler) POINTER(c_void_p)(self.vad_handler)
) )
== 0 == 0
), "[TEN VAD]: destroy handler failure!" ), "[TEN VAD]: destroy handler failure!"
def get_input_data(self, audio_data: np.ndarray): def get_input_data(self, audio_data: np.ndarray):
audio_data = np.squeeze(audio_data) audio_data = np.squeeze(audio_data)
assert ( assert (
len(audio_data.shape) == 1 len(audio_data.shape) == 1
and audio_data.shape[0] == self.hop_size and audio_data.shape[0] == self.hop_size
), "[TEN VAD]: audio data shape should be [%d]" % ( ), "[TEN VAD]: audio data shape should be [%d]" % (
self.hop_size self.hop_size
) )
assert ( assert (
type(audio_data[0]) == np.int16 type(audio_data[0]) == np.int16
), "[TEN VAD]: audio data type error, must be int16" ), "[TEN VAD]: audio data type error, must be int16"
data_pointer = audio_data.__array_interface__["data"][0] data_pointer = audio_data.__array_interface__["data"][0]
return c_void_p(data_pointer) return c_void_p(data_pointer)
def process(self, audio_data: np.ndarray): def process(self, audio_data: np.ndarray):
input_pointer = self.get_input_data(audio_data) input_pointer = self.get_input_data(audio_data)
self.vad_library.ten_vad_process( self.vad_library.ten_vad_process(
self.vad_handler, self.vad_handler,
input_pointer, input_pointer,
c_size_t(self.hop_size), c_size_t(self.hop_size),
POINTER(c_float)(self.out_probability), POINTER(c_float)(self.out_probability),
POINTER(c_int32)(self.out_flags), POINTER(c_int32)(self.out_flags),
) )
return self.out_probability.value, self.out_flags.value return self.out_probability.value, self.out_flags.value

View file

@ -0,0 +1,44 @@
#!/usr/bin/env python3
"""Test TEN VAD ONNX installation on Linux"""
import os
import sys
import platform
from pathlib import Path
print(f"Platform: {platform.system()} {platform.machine()}")
print(f"Python: {sys.version}")
# Add the model path to sys.path
model_path = Path("models/ten-vad-onnx")
print(f"Model path: {model_path.absolute()}")
# Check if files exist
ten_vad_py = model_path / "ten_vad.py"
lib_so = model_path / "lib" / "Linux" / "x64" / "libten_vad.so"
lib_so_alt = model_path / "ten_vad_library" / "libten_vad.so"
print(f"\nFile checks:")
print(f"ten_vad.py exists: {ten_vad_py.exists()}")
print(f"libten_vad.so (main) exists: {lib_so.exists()}")
print(f"libten_vad.so (alt) exists: {lib_so_alt.exists()}")
# Set library path
os.environ['LD_LIBRARY_PATH'] = f"{lib_so.parent}:{lib_so_alt.parent}:{os.environ.get('LD_LIBRARY_PATH', '')}"
print(f"\nLD_LIBRARY_PATH: {os.environ['LD_LIBRARY_PATH']}")
# Try to import
sys.path.insert(0, str(model_path))
try:
print("\nAttempting to import ten_vad...")
from ten_vad import TenVad
print("✅ Successfully imported TenVad")
# Try to create instance
vad = TenVad(hop_size=256, threshold=0.5)
print("✅ Successfully created TenVad instance")
except Exception as e:
print(f"❌ Import failed: {e}")
import traceback
traceback.print_exc()