Skip to content

Commit

Permalink
Add VAD examples using ALSA for recording (#739)
Browse files Browse the repository at this point in the history
  • Loading branch information
csukuangfj authored Apr 8, 2024
1 parent a5f8fbc commit 6fb8ced
Show file tree
Hide file tree
Showing 17 changed files with 601 additions and 9 deletions.
5 changes: 2 additions & 3 deletions .github/scripts/test-nodejs-npm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ rm sherpa-onnx-streaming-zipformer-ctc-multi-zh-hans-2023-12-13.tar.bz2
node ./test-online-zipformer2-ctc.js
rm -rf sherpa-onnx-streaming-zipformer-ctc-multi-zh-hans-2023-12-13


curl -LS -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-streaming-zipformer-ctc-small-2024-03-18.tar.bz2
tar xvf sherpa-onnx-streaming-zipformer-ctc-small-2024-03-18.tar.bz2
rm sherpa-onnx-streaming-zipformer-ctc-small-2024-03-18.tar.bz2
Expand All @@ -70,9 +69,9 @@ rm -rf sherpa-onnx-streaming-zipformer-ctc-small-2024-03-18
curl -LS -O https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/vits-piper-en_US-amy-low.tar.bz2
tar xf vits-piper-en_US-amy-low.tar.bz2
node ./test-offline-tts-en.js
rm vits-piper-en_US-amy-low*
rm -rf vits-piper-en_US-amy-low*

curl -LS -O https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/vits-icefall-zh-aishell3.tar.bz2
tar xvf vits-icefall-zh-aishell3.tar.bz2
node ./test-offline-tts-zh.js
rm vits-icefall-zh-aishell3*
rm -rf vits-icefall-zh-aishell3*
21 changes: 20 additions & 1 deletion .github/workflows/build-wheels-aarch64.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,27 @@ jobs:
run: |
ls -lh ./wheelhouse/
- name: Install patchelf
if: matrix.os == 'ubuntu-latest'
shell: bash
run: |
sudo apt-get update -q
sudo apt-get install -q -y patchelf
patchelf --help
- name: Patch wheels
shell: bash
if: matrix.os == 'ubuntu-latest'
run: |
mkdir ./wheels
sudo ./scripts/wheel/patch_wheel.py --in-dir ./wheelhouse --out-dir ./wheels
ls -lh ./wheels/
rm -rf ./wheelhouse
mv ./wheels ./wheelhouse
- name: Publish to huggingface
if: matrix.python-version == 'cp38' && matrix.manylinux == 'manylinux2014'
if: (matrix.python-version == 'cp38' || matrix.python-version == 'cp39' ) && matrix.manylinux == 'manylinux2014'
env:
HF_TOKEN: ${{ secrets.HF_TOKEN }}
uses: nick-fields/retry@v3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ class MainActivity : AppCompatActivity() {
// https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/vits-icefall-zh-aishell3.tar.bz2
// modelDir = "vits-icefall-zh-aishell3"
// modelName = "model.onnx"
// ruleFsts = "vits-icefall-zh-aishell3/phone.fst,vits-icefall-zh-aishell3/date.fst,vits-icefall-zh-aishell3/number.fst,"
// ruleFsts = "vits-icefall-zh-aishell3/phone.fst,vits-icefall-zh-aishell3/date.fst,vits-icefall-zh-aishell3/number.fst,vits-icefall-zh-aishell3/new_heteronym.fst"
// ruleFars = "vits-icefall-zh-aishell3/rule.far"
// lexicon = "lexicon.txt"

Expand Down
1 change: 1 addition & 0 deletions cmake/cmake_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def get_binaries():
"sherpa-onnx-alsa-offline",
"sherpa-onnx-alsa-offline-speaker-identification",
"sherpa-onnx-offline-tts-play-alsa",
"sherpa-onnx-vad-alsa",
]

if is_windows():
Expand Down
4 changes: 4 additions & 0 deletions cmake/openfst.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ function(download_openfst)
set_target_properties(fst PROPERTIES OUTPUT_NAME "sherpa-onnx-fst")
set_target_properties(fstfar PROPERTIES OUTPUT_NAME "sherpa-onnx-fstfar")

if(LINUX)
target_compile_options(fst PUBLIC -Wno-missing-template-keyword)
endif()

target_include_directories(fst
PUBLIC
${openfst_SOURCE_DIR}/src/include
Expand Down
107 changes: 107 additions & 0 deletions python-api-examples/vad-alsa.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#!/usr/bin/env python3

"""
This script works only on Linux. It uses ALSA for recording.
"""

import argparse
from pathlib import Path

import sherpa_onnx


def get_args():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)

parser.add_argument(
"--silero-vad-model",
type=str,
required=True,
help="Path to silero_vad.onnx",
)

parser.add_argument(
"--device-name",
type=str,
required=True,
help="""
The device name specifies which microphone to use in case there are several
on your system. You can use
arecord -l
to find all available microphones on your computer. For instance, if it outputs
**** List of CAPTURE Hardware Devices ****
card 3: UACDemoV10 [UACDemoV1.0], device 0: USB Audio [USB Audio]
Subdevices: 1/1
Subdevice #0: subdevice #0
and if you want to select card 3 and the device 0 on that card, please use:
plughw:3,0
as the device_name.
""",
)

return parser.parse_args()


def main():
args = get_args()
if not Path(args.silero_vad_model).is_file():
raise RuntimeError(
f"{args.silero_vad_model} does not exist. Please download it from "
"https://github.com/snakers4/silero-vad/blob/master/files/silero_vad.onnx"
)

device_name = args.device_name
print(f"device_name: {device_name}")
alsa = sherpa_onnx.Alsa(device_name)

sample_rate = 16000
samples_per_read = int(0.1 * sample_rate) # 0.1 second = 100 ms

config = sherpa_onnx.VadModelConfig()
config.silero_vad.model = args.silero_vad_model
config.sample_rate = sample_rate

vad = sherpa_onnx.VoiceActivityDetector(config, buffer_size_in_seconds=30)

print("Started! Please speak. Press Ctrl C to exit")

printed = False
k = 0
try:
while True:
samples = alsa.read(samples_per_read) # a blocking read

vad.accept_waveform(samples)

if vad.is_speech_detected() and not printed:
print("Detected speech")
printed = True

if not vad.is_speech_detected():
printed = False

while not vad.empty():
samples = vad.front.samples
duration = len(samples) / sample_rate
filename = f"seg-{k}-{duration:.3f}-seconds.wav"
k += 1
sherpa_onnx.write_wave(filename, samples, sample_rate)
print(f"Duration: {duration:.3f} seconds")
print(f"Saved to {filename}")
print("----------")

vad.pop()
except KeyboardInterrupt:
print("\nCaught Ctrl + C. Exit")


if __name__ == "__main__":
main()
125 changes: 125 additions & 0 deletions python-api-examples/vad-microphone.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#!/usr/bin/env python3

import argparse
import os
import sys
from pathlib import Path

try:
import sounddevice as sd
except ImportError:
print("Please install sounddevice first. You can use")
print()
print(" pip install sounddevice")
print()
print("to install it")
sys.exit(-1)

import sherpa_onnx


def get_args():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)

parser.add_argument(
"--silero-vad-model",
type=str,
required=True,
help="Path to silero_vad.onnx",
)

return parser.parse_args()


def main():
args = get_args()
if not Path(args.silero_vad_model).is_file():
raise RuntimeError(
f"{args.silero_vad_model} does not exist. Please download it from "
"https://github.com/snakers4/silero-vad/blob/master/files/silero_vad.onnx"
)

mic_sample_rate = 16000
if "SHERPA_ONNX_MIC_SAMPLE_RATE" in os.environ:
mic_sample_rate = int(os.environ.get("SHERPA_ONNX_MIC_SAMPLE_RATE"))
print(f"Change microphone sample rate to {mic_sample_rate}")

sample_rate = 16000
samples_per_read = int(0.1 * sample_rate) # 0.1 second = 100 ms

config = sherpa_onnx.VadModelConfig()
config.silero_vad.model = args.silero_vad_model
config.sample_rate = sample_rate

vad = sherpa_onnx.VoiceActivityDetector(config, buffer_size_in_seconds=30)

# python3 -m sounddevice
# can also be used to list all devices

devices = sd.query_devices()
if len(devices) == 0:
print("No microphone devices found")
print(
"If you are using Linux and you are sure there is a microphone "
"on your system, please use "
"./vad-alsa.py"
)
sys.exit(0)

print(devices)

if "SHERPA_ONNX_MIC_DEVICE" in os.environ:
input_device_idx = int(os.environ.get("SHERPA_ONNX_MIC_DEVICE"))
sd.default.device[0] = input_device_idx
print(f'Use selected device: {devices[input_device_idx]["name"]}')
else:
input_device_idx = sd.default.device[0]
print(f'Use default device: {devices[input_device_idx]["name"]}')

print("Started! Please speak. Press Ctrl C to exit")

printed = False
k = 0
try:
with sd.InputStream(
channels=1, dtype="float32", samplerate=mic_sample_rate
) as s:
while True:
samples, _ = s.read(samples_per_read) # a blocking read
samples = samples.reshape(-1)

if mic_sample_rate != sample_rate:
import librosa

samples = librosa.resample(
samples, orig_sr=mic_sample_rate, target_sr=sample_rate
)

vad.accept_waveform(samples)

if vad.is_speech_detected() and not printed:
print("Detected speech")
printed = True

if not vad.is_speech_detected():
printed = False

while not vad.empty():
samples = vad.front.samples
duration = len(samples) / sample_rate
filename = f"seg-{k}-{duration:.3f}-seconds.wav"
k += 1
sherpa_onnx.write_wave(filename, samples, sample_rate)
print(f"Duration: {duration:.3f} seconds")
print(f"Saved to {filename}")
print("----------")

vad.pop()
except KeyboardInterrupt:
print("\nCaught Ctrl + C. Exit")


if __name__ == "__main__":
main()
Loading

0 comments on commit 6fb8ced

Please sign in to comment.