silero-vad 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of silero-vad might be problematic. Click here for more details.
- silero-vad-0.0.1/LICENSE +21 -0
- silero-vad-0.0.1/PKG-INFO +35 -0
- silero-vad-0.0.1/README.md +18 -0
- silero-vad-0.0.1/setup.cfg +4 -0
- silero-vad-0.0.1/setup.py +45 -0
- silero-vad-0.0.1/silero_vad/__init__.py +15 -0
- silero-vad-0.0.1/silero_vad/cli.py +58 -0
- silero-vad-0.0.1/silero_vad/frame_queue.py +93 -0
- silero-vad-0.0.1/silero_vad/inference_session.py +44 -0
- silero-vad-0.0.1/silero_vad/silero_vad.onnx +0 -0
- silero-vad-0.0.1/silero_vad/silero_vad.py +347 -0
- silero-vad-0.0.1/silero_vad/utils.py +42 -0
- silero-vad-0.0.1/silero_vad.egg-info/PKG-INFO +35 -0
- silero-vad-0.0.1/silero_vad.egg-info/SOURCES.txt +16 -0
- silero-vad-0.0.1/silero_vad.egg-info/dependency_links.txt +1 -0
- silero-vad-0.0.1/silero_vad.egg-info/entry_points.txt +3 -0
- silero-vad-0.0.1/silero_vad.egg-info/requires.txt +5 -0
- silero-vad-0.0.1/silero_vad.egg-info/top_level.txt +1 -0
silero-vad-0.0.1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020-present Silero Team
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: silero-vad
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Silero VAD
|
|
5
|
+
Home-page: https://github.com/pengzhendong/silero-vad
|
|
6
|
+
Author: Zhendong Peng
|
|
7
|
+
Author-email: pzd17@tsinghua.org.cn
|
|
8
|
+
License: UNKNOWN
|
|
9
|
+
Platform: UNKNOWN
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: Topic :: Scientific/Engineering
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
License-File: LICENSE
|
|
15
|
+
|
|
16
|
+
## Silero VAD
|
|
17
|
+
|
|
18
|
+
See [Silero VAD](https://github.com/snakers4/silero-vad).
|
|
19
|
+
|
|
20
|
+
### Denoiser
|
|
21
|
+
|
|
22
|
+
See [RnNoise](https://github.com/werman/noise-suppression-for-voice).
|
|
23
|
+
|
|
24
|
+
### Python Usage
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
$ pip install https://github.com/pengzhendong/silero-vad/archive/refs/heads/master.zip
|
|
28
|
+
$ silero_vad --wav_path audio.wav
|
|
29
|
+
$ python
|
|
30
|
+
>>> from silero_vad import SileroVAD
|
|
31
|
+
>>> vad = SileroVAD()
|
|
32
|
+
>>> vad.get_speech_timestamps("audio.wav")
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
## Silero VAD
|
|
2
|
+
|
|
3
|
+
See [Silero VAD](https://github.com/snakers4/silero-vad).
|
|
4
|
+
|
|
5
|
+
### Denoiser
|
|
6
|
+
|
|
7
|
+
See [RnNoise](https://github.com/werman/noise-suppression-for-voice).
|
|
8
|
+
|
|
9
|
+
### Python Usage
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
$ pip install https://github.com/pengzhendong/silero-vad/archive/refs/heads/master.zip
|
|
13
|
+
$ silero_vad --wav_path audio.wav
|
|
14
|
+
$ python
|
|
15
|
+
>>> from silero_vad import SileroVAD
|
|
16
|
+
>>> vad = SileroVAD()
|
|
17
|
+
>>> vad.get_speech_timestamps("audio.wav")
|
|
18
|
+
```
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# Copyright (c) 2023 Zhendong Peng (pzd17@tsinghua.org.cn)
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
from setuptools import setup, find_packages
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
with open("requirements.txt", encoding="utf8") as f:
|
|
19
|
+
requirements = f.readlines()
|
|
20
|
+
|
|
21
|
+
setup(
|
|
22
|
+
name="silero-vad",
|
|
23
|
+
version=open("VERSION", encoding="utf8").read(),
|
|
24
|
+
author="Zhendong Peng",
|
|
25
|
+
author_email="pzd17@tsinghua.org.cn",
|
|
26
|
+
long_description=open("README.md", encoding="utf8").read(),
|
|
27
|
+
long_description_content_type="text/markdown",
|
|
28
|
+
description="Silero VAD",
|
|
29
|
+
url="https://github.com/pengzhendong/silero-vad",
|
|
30
|
+
packages=find_packages(),
|
|
31
|
+
package_data={
|
|
32
|
+
"silero_vad": ["*.onnx"],
|
|
33
|
+
},
|
|
34
|
+
install_requires=requirements,
|
|
35
|
+
entry_points={
|
|
36
|
+
"console_scripts": [
|
|
37
|
+
"silero_vad = silero_vad.cli:main",
|
|
38
|
+
]
|
|
39
|
+
},
|
|
40
|
+
classifiers=[
|
|
41
|
+
"Programming Language :: Python :: 3",
|
|
42
|
+
"Operating System :: OS Independent",
|
|
43
|
+
"Topic :: Scientific/Engineering",
|
|
44
|
+
],
|
|
45
|
+
)
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# Copyright (c) 2024, Zhendong Peng (pzd17@tsinghua.org.cn)
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
from .silero_vad import *
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# Copyright (c) 2024, Zhendong Peng (pzd17@tsinghua.org.cn)
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
import click
|
|
16
|
+
import numpy as np
|
|
17
|
+
import soundfile as sf
|
|
18
|
+
import wave
|
|
19
|
+
|
|
20
|
+
from silero_vad import SileroVAD, VADIterator
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@click.command()
|
|
24
|
+
@click.argument("wav_path", type=click.Path(exists=True, file_okay=True))
|
|
25
|
+
@click.option("--streaming/--no-streaming", default=False, help="Streming mode")
|
|
26
|
+
@click.option("--save-path", help="Save path for output audio")
|
|
27
|
+
def main(wav_path: str, streaming: bool, save_path: str):
|
|
28
|
+
if not streaming:
|
|
29
|
+
vad = SileroVAD()
|
|
30
|
+
speech_timestamps = vad.get_speech_timestamps(
|
|
31
|
+
wav_path, return_seconds=True, save_path=save_path
|
|
32
|
+
)
|
|
33
|
+
print("None streaming result:", list(speech_timestamps))
|
|
34
|
+
else:
|
|
35
|
+
print("Streaming result:", end=" ")
|
|
36
|
+
wav, sr = sf.read(wav_path, dtype=np.float32)
|
|
37
|
+
vad_iterator = VADIterator(sampling_rate=sr)
|
|
38
|
+
|
|
39
|
+
if save_path:
|
|
40
|
+
out_wav = wave.open(save_path, "w")
|
|
41
|
+
out_wav.setnchannels(1)
|
|
42
|
+
out_wav.setsampwidth(2)
|
|
43
|
+
out_wav.setframerate(sr)
|
|
44
|
+
# number of samples in a single audio chunk
|
|
45
|
+
window_size_samples = 10 * sr // 1000
|
|
46
|
+
for i in range(0, len(wav), window_size_samples):
|
|
47
|
+
chunk = wav[i : i + window_size_samples]
|
|
48
|
+
for speech_dict, speech_samples in vad_iterator(chunk, return_seconds=True):
|
|
49
|
+
if speech_dict:
|
|
50
|
+
print(speech_dict, end=" ")
|
|
51
|
+
if save_path and speech_samples is not None:
|
|
52
|
+
out_wav.writeframes((speech_samples * 32768).astype(np.int16))
|
|
53
|
+
# reset model states after each audio
|
|
54
|
+
vad_iterator.reset_states()
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
if __name__ == "__main__":
|
|
58
|
+
main()
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# Copyright (c) 2024, Zhendong Peng (pzd17@tsinghua.org.cn)
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
import numpy as np
|
|
16
|
+
import soxr
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class FrameQueue:
|
|
20
|
+
def __init__(
|
|
21
|
+
self, frame_size_ms, src_sr, speech_pad_ms=0, dst_sr=None, padding=True
|
|
22
|
+
):
|
|
23
|
+
# padding zeros for the last frame
|
|
24
|
+
self.padding = padding
|
|
25
|
+
# cache the original samples for padding and soxr's delay
|
|
26
|
+
self.speech_pad_samples = speech_pad_ms * src_sr // 1000
|
|
27
|
+
# TODO: use the largest delay of soxr
|
|
28
|
+
self.cached_ms = speech_pad_ms + 500
|
|
29
|
+
self.current_sample = 0
|
|
30
|
+
self.remained_samples = np.empty(0, dtype=np.float32)
|
|
31
|
+
|
|
32
|
+
if self.cached_ms > 0:
|
|
33
|
+
self.cached_samples = np.zeros(self.cached_ms * src_sr // 1000)
|
|
34
|
+
self.cache_start = -len(self.cached_samples)
|
|
35
|
+
|
|
36
|
+
if src_sr == dst_sr or dst_sr is None:
|
|
37
|
+
self.step = 1.0
|
|
38
|
+
self.resampler = None
|
|
39
|
+
self.frame_size = frame_size_ms * src_sr // 1000
|
|
40
|
+
else:
|
|
41
|
+
self.step = src_sr / dst_sr
|
|
42
|
+
self.frame_size = frame_size_ms * dst_sr // 1000
|
|
43
|
+
self.resampler = soxr.ResampleStream(src_sr, dst_sr, 1, dtype=np.float32)
|
|
44
|
+
|
|
45
|
+
def add_chunk(self, chunk, last=False):
|
|
46
|
+
# cache
|
|
47
|
+
if self.cached_ms > 0:
|
|
48
|
+
self.cache_start += len(chunk)
|
|
49
|
+
self.cached_samples = np.roll(self.cached_samples, -len(chunk))
|
|
50
|
+
self.cached_samples[-len(chunk) :] = chunk[-len(self.cached_samples) :]
|
|
51
|
+
# resample
|
|
52
|
+
if self.resampler is not None:
|
|
53
|
+
chunk = self.resampler.resample_chunk(chunk, last)
|
|
54
|
+
# enqueue chunk
|
|
55
|
+
self.remained_samples = np.concatenate((self.remained_samples, chunk))
|
|
56
|
+
while len(self.remained_samples) >= self.frame_size:
|
|
57
|
+
frame = self.remained_samples[: self.frame_size]
|
|
58
|
+
self.remained_samples = self.remained_samples[self.frame_size :]
|
|
59
|
+
frame_start = self.current_sample
|
|
60
|
+
self.current_sample += len(frame)
|
|
61
|
+
yield frame_start, self.current_sample, frame
|
|
62
|
+
|
|
63
|
+
if last and len(self.remained_samples) > 0 and self.padding:
|
|
64
|
+
frame = self.remained_samples
|
|
65
|
+
frame_start = self.current_sample
|
|
66
|
+
self.current_sample += len(frame)
|
|
67
|
+
frame = np.pad(frame, (0, self.frame_size - len(frame)))
|
|
68
|
+
yield frame_start, self.current_sample, frame
|
|
69
|
+
|
|
70
|
+
def get_original_frame(self, speech_padding=False):
|
|
71
|
+
frame_start = self.current_sample - self.frame_size
|
|
72
|
+
if speech_padding:
|
|
73
|
+
frame_start -= self.speech_pad_samples
|
|
74
|
+
speech_start = int(frame_start * self.step) - self.cache_start
|
|
75
|
+
speech_end = int(self.current_sample * self.step) - self.cache_start
|
|
76
|
+
return self.cached_samples[speech_start:speech_end]
|
|
77
|
+
|
|
78
|
+
def clear(self):
|
|
79
|
+
if self.cached_ms > 0:
|
|
80
|
+
self.cached_samples.fill(0)
|
|
81
|
+
self.cache_start = -len(self.cached_samples)
|
|
82
|
+
self.current_sample = 0
|
|
83
|
+
self.remained_samples = np.empty(0, dtype=np.float32)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
if __name__ == "__main__":
|
|
87
|
+
queue = FrameQueue(3, 1000)
|
|
88
|
+
frames = [[1, 2, 3], [4, 5], [6, 7, 8]]
|
|
89
|
+
for idx, frame in enumerate(frames):
|
|
90
|
+
for frame_start, frame_end, frame in queue.add_chunk(
|
|
91
|
+
frame, idx == len(frames) - 1
|
|
92
|
+
):
|
|
93
|
+
print(frame_start, frame_end, frame)
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# Copyright (c) 2024, Zhendong Peng (pzd17@tsinghua.org.cn)
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
import onnxruntime as ort
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def init_session(model_path):
|
|
19
|
+
opts = ort.SessionOptions()
|
|
20
|
+
opts.inter_op_num_threads = 1
|
|
21
|
+
opts.intra_op_num_threads = 1
|
|
22
|
+
opts.log_severity_level = 3
|
|
23
|
+
sess = ort.InferenceSession(model_path, sess_options=opts)
|
|
24
|
+
return sess
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class PickableInferenceSession:
|
|
28
|
+
"""
|
|
29
|
+
This is a wrapper to make the current InferenceSession class pickable.
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
def __init__(self, model_path):
|
|
33
|
+
self.model_path = model_path
|
|
34
|
+
self.sess = init_session(self.model_path)
|
|
35
|
+
|
|
36
|
+
def run(self, *args):
|
|
37
|
+
return self.sess.run(*args)
|
|
38
|
+
|
|
39
|
+
def __getstate__(self):
|
|
40
|
+
return {"model_path": self.model_path}
|
|
41
|
+
|
|
42
|
+
def __setstate__(self, values):
|
|
43
|
+
self.model_path = values["model_path"]
|
|
44
|
+
self.sess = init_session(self.model_path)
|
|
Binary file
|
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
# Copyright (c) 2024, Zhendong Peng (pzd17@tsinghua.org.cn)
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
import os
|
|
16
|
+
from functools import partial
|
|
17
|
+
from pathlib import Path
|
|
18
|
+
from typing import Union
|
|
19
|
+
import warnings
|
|
20
|
+
|
|
21
|
+
import numpy as np
|
|
22
|
+
import soundfile as sf
|
|
23
|
+
|
|
24
|
+
from .frame_queue import FrameQueue
|
|
25
|
+
from .inference_session import PickableInferenceSession
|
|
26
|
+
from .utils import get_energy
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class SileroVAD:
|
|
30
|
+
def __init__(self, onnx_model=f"{os.path.dirname(__file__)}/silero_vad.onnx"):
|
|
31
|
+
self.session = PickableInferenceSession(onnx_model)
|
|
32
|
+
self.reset_states()
|
|
33
|
+
self.sample_rates = [8000, 16000]
|
|
34
|
+
|
|
35
|
+
def reset_states(self):
|
|
36
|
+
self._h = np.zeros((2, 1, 64)).astype(np.float32)
|
|
37
|
+
self._c = np.zeros((2, 1, 64)).astype(np.float32)
|
|
38
|
+
|
|
39
|
+
def __call__(self, x, sr: int):
|
|
40
|
+
ort_inputs = {
|
|
41
|
+
"input": x[np.newaxis, :],
|
|
42
|
+
"h": self._h,
|
|
43
|
+
"c": self._c,
|
|
44
|
+
"sr": np.array(sr, dtype=np.int64),
|
|
45
|
+
}
|
|
46
|
+
ort_outs = self.session.run(None, ort_inputs)
|
|
47
|
+
out, self._h, self._c = ort_outs
|
|
48
|
+
return out
|
|
49
|
+
|
|
50
|
+
@staticmethod
|
|
51
|
+
def process_segment(
|
|
52
|
+
idx,
|
|
53
|
+
segment,
|
|
54
|
+
wav,
|
|
55
|
+
sample_rate,
|
|
56
|
+
step,
|
|
57
|
+
save_path,
|
|
58
|
+
flat_layout,
|
|
59
|
+
speech_pad_ms,
|
|
60
|
+
return_seconds,
|
|
61
|
+
):
|
|
62
|
+
if step != 1.0:
|
|
63
|
+
segment["start"] = int(segment["start"] * step)
|
|
64
|
+
segment["end"] = int(segment["end"] * step)
|
|
65
|
+
|
|
66
|
+
speech_pad_samples = speech_pad_ms * sample_rate // 1000
|
|
67
|
+
segment["start"] = max(segment["start"] - speech_pad_samples, 0)
|
|
68
|
+
segment["end"] = min(segment["end"] + speech_pad_samples, len(wav))
|
|
69
|
+
if save_path:
|
|
70
|
+
wav = wav[segment["start"] : segment["end"]]
|
|
71
|
+
if flat_layout:
|
|
72
|
+
sf.write(str(save_path) + f"_{idx:04d}.wav", wav, sample_rate)
|
|
73
|
+
else:
|
|
74
|
+
sf.write(str(Path(save_path) / f"{idx:04d}.wav"), wav, sample_rate)
|
|
75
|
+
if return_seconds:
|
|
76
|
+
segment["start"] = round(segment["start"] / sample_rate, 3)
|
|
77
|
+
segment["end"] = round(segment["end"] / sample_rate, 3)
|
|
78
|
+
return segment
|
|
79
|
+
|
|
80
|
+
def get_speech_timestamps(
|
|
81
|
+
self,
|
|
82
|
+
wav_path: Union[str, Path],
|
|
83
|
+
save_path: Union[str, Path] = None,
|
|
84
|
+
flat_layout: bool = True,
|
|
85
|
+
threshold: float = 0.5,
|
|
86
|
+
min_speech_duration_ms: int = 250,
|
|
87
|
+
max_speech_duration_s: float = float("inf"),
|
|
88
|
+
min_silence_duration_ms: int = 100,
|
|
89
|
+
speech_pad_ms: int = 30,
|
|
90
|
+
window_size_ms: int = 32,
|
|
91
|
+
return_seconds: bool = False,
|
|
92
|
+
):
|
|
93
|
+
"""
|
|
94
|
+
Splitting long audios into speech chunks using silero VAD
|
|
95
|
+
|
|
96
|
+
Parameters
|
|
97
|
+
----------
|
|
98
|
+
wav_path: wav path
|
|
99
|
+
save_path: string or Path (default - None)
|
|
100
|
+
whether the save speech segments
|
|
101
|
+
flat_layout: bool (default - True)
|
|
102
|
+
whether use the flat directory structure
|
|
103
|
+
threshold: float (default - 0.5)
|
|
104
|
+
Speech threshold. Silero VAD outputs speech probabilities for each audio
|
|
105
|
+
chunk, probabilities ABOVE this value are considered as SPEECH. It is
|
|
106
|
+
better to tune this parameter for each dataset separately, but "lazy"
|
|
107
|
+
0.5 is pretty good for most datasets.
|
|
108
|
+
min_speech_duration_ms: int (default - 250 milliseconds)
|
|
109
|
+
Final speech chunks shorter min_speech_duration_ms are thrown out
|
|
110
|
+
max_speech_duration_s: int (default - inf)
|
|
111
|
+
Maximum duration of speech chunks in seconds
|
|
112
|
+
Chunks longer than max_speech_duration_s will be split at the timestamp
|
|
113
|
+
of the last silence that lasts more than 98ms (if any), to prevent
|
|
114
|
+
agressive cutting. Otherwise, they will be split aggressively just
|
|
115
|
+
before max_speech_duration_s.
|
|
116
|
+
min_silence_duration_ms: int (default - 300 milliseconds)
|
|
117
|
+
In the end of each speech chunk wait for min_silence_duration_ms before
|
|
118
|
+
separating it.
|
|
119
|
+
speech_pad_ms: int (default - 100 milliseconds)
|
|
120
|
+
Final speech chunks are padded by speech_pad_ms each side
|
|
121
|
+
window_size_ms: int (default - 32 milliseconds)
|
|
122
|
+
Audio chunks of window_size_ms size are fed to the silero VAD model.
|
|
123
|
+
WARNING! Silero VAD models were trained using 32, 64, 96 milliseconds
|
|
124
|
+
for 8000 sample rate and 16000 sample rate.
|
|
125
|
+
Values other than these may affect model perfomance!!
|
|
126
|
+
return_seconds: bool (default - False)
|
|
127
|
+
whether return timestamps in seconds (default - samples)
|
|
128
|
+
|
|
129
|
+
Returns
|
|
130
|
+
----------
|
|
131
|
+
speeches: list of dicts
|
|
132
|
+
list containing ends and beginnings of speech chunks (samples or seconds
|
|
133
|
+
based on return_seconds)
|
|
134
|
+
"""
|
|
135
|
+
|
|
136
|
+
wav, sr = sf.read(Path(wav_path), dtype=np.float32)
|
|
137
|
+
if len(wav.shape) > 1:
|
|
138
|
+
raise ValueError("Only supported mono wav.")
|
|
139
|
+
if len(wav) / sr * 1000 < 32:
|
|
140
|
+
raise ValueError("Input audio is too short.")
|
|
141
|
+
if window_size_ms not in [32, 64, 96]:
|
|
142
|
+
warnings.warn("Supported window_size_ms: [32, 64, 96]")
|
|
143
|
+
|
|
144
|
+
if sr in self.sample_rates:
|
|
145
|
+
vad_sr = sr
|
|
146
|
+
queue = FrameQueue(window_size_ms, sr)
|
|
147
|
+
else:
|
|
148
|
+
vad_sr = 16000
|
|
149
|
+
queue = FrameQueue(window_size_ms, src_sr=sr, dst_sr=vad_sr)
|
|
150
|
+
|
|
151
|
+
fn = partial(
|
|
152
|
+
self.process_segment,
|
|
153
|
+
wav=wav,
|
|
154
|
+
sample_rate=sr,
|
|
155
|
+
step=queue.step,
|
|
156
|
+
save_path=save_path,
|
|
157
|
+
flat_layout=flat_layout,
|
|
158
|
+
speech_pad_ms=speech_pad_ms,
|
|
159
|
+
return_seconds=return_seconds,
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
speech_pad_samples = speech_pad_ms * vad_sr // 1000
|
|
163
|
+
min_silence_samples_at_max_speech = 98 * vad_sr // 1000
|
|
164
|
+
min_speech_samples = min_speech_duration_ms * vad_sr // 1000
|
|
165
|
+
min_silence_samples = min_silence_duration_ms * vad_sr // 1000
|
|
166
|
+
max_speech_duration_samples = max_speech_duration_s * vad_sr
|
|
167
|
+
max_speech_samples = max_speech_duration_samples - 2 * speech_pad_samples
|
|
168
|
+
self.reset_states()
|
|
169
|
+
|
|
170
|
+
idx = 0
|
|
171
|
+
current_speech = {}
|
|
172
|
+
neg_threshold = threshold - 0.15
|
|
173
|
+
triggered = False
|
|
174
|
+
# to save potential segment end (and tolerate some silence)
|
|
175
|
+
temp_end = 0
|
|
176
|
+
# to save potential segment limits in case of maximum segment size reached
|
|
177
|
+
prev_end = 0
|
|
178
|
+
next_start = 0
|
|
179
|
+
for frame_start, frame_end, frame in queue.add_chunk(wav):
|
|
180
|
+
speech_prob = self(frame, vad_sr)
|
|
181
|
+
# current frame is speech
|
|
182
|
+
if speech_prob >= threshold:
|
|
183
|
+
if temp_end > 0 and next_start < prev_end:
|
|
184
|
+
next_start = frame_end
|
|
185
|
+
temp_end = 0
|
|
186
|
+
if not triggered:
|
|
187
|
+
triggered = True
|
|
188
|
+
current_speech["start"] = frame_end
|
|
189
|
+
continue
|
|
190
|
+
# in speech, and speech duration is more than max speech duration
|
|
191
|
+
if triggered and frame_start - current_speech["start"] > max_speech_samples:
|
|
192
|
+
# prev_end larger than 0 means there is a short silence in the middle avoid aggressive cutting
|
|
193
|
+
if prev_end > 0:
|
|
194
|
+
current_speech["end"] = prev_end
|
|
195
|
+
yield fn(idx, current_speech)
|
|
196
|
+
idx += 1
|
|
197
|
+
current_speech = {}
|
|
198
|
+
# previously reached silence (< neg_thres) and is still not speech (< thres)
|
|
199
|
+
if next_start < prev_end:
|
|
200
|
+
triggered = False
|
|
201
|
+
else:
|
|
202
|
+
current_speech["start"] = next_start
|
|
203
|
+
prev_end = 0
|
|
204
|
+
next_start = 0
|
|
205
|
+
temp_end = 0
|
|
206
|
+
else:
|
|
207
|
+
current_speech["end"] = frame_end
|
|
208
|
+
yield fn(idx, current_speech)
|
|
209
|
+
idx += 1
|
|
210
|
+
current_speech = {}
|
|
211
|
+
prev_end = 0
|
|
212
|
+
next_start = 0
|
|
213
|
+
temp_end = 0
|
|
214
|
+
triggered = False
|
|
215
|
+
continue
|
|
216
|
+
# in speech, and current frame is silence
|
|
217
|
+
if triggered and speech_prob < neg_threshold:
|
|
218
|
+
if temp_end == 0:
|
|
219
|
+
temp_end = frame_end
|
|
220
|
+
# record the last silence before reaching max speech duration
|
|
221
|
+
if frame_end - temp_end > min_silence_samples_at_max_speech:
|
|
222
|
+
prev_end = temp_end
|
|
223
|
+
if frame_end - temp_end >= min_silence_samples:
|
|
224
|
+
current_speech["end"] = temp_end
|
|
225
|
+
# keep the speech segment if it is longer than min_speech_samples
|
|
226
|
+
if (
|
|
227
|
+
current_speech["end"] - current_speech["start"]
|
|
228
|
+
> min_speech_samples
|
|
229
|
+
):
|
|
230
|
+
yield fn(idx, current_speech)
|
|
231
|
+
idx += 1
|
|
232
|
+
current_speech = {}
|
|
233
|
+
prev_end = 0
|
|
234
|
+
next_start = 0
|
|
235
|
+
temp_end = 0
|
|
236
|
+
triggered = False
|
|
237
|
+
|
|
238
|
+
# deal with the last speech segment
|
|
239
|
+
if current_speech and len(wav) - current_speech["start"] > min_speech_samples:
|
|
240
|
+
current_speech["end"] = len(wav)
|
|
241
|
+
yield fn(idx, current_speech)
|
|
242
|
+
idx += 1
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
class VADIterator:
|
|
246
|
+
def __init__(
|
|
247
|
+
self,
|
|
248
|
+
threshold: float = 0.5,
|
|
249
|
+
sampling_rate: int = 16000,
|
|
250
|
+
min_silence_duration_ms: int = 300,
|
|
251
|
+
speech_pad_ms: int = 100,
|
|
252
|
+
window_size_ms: int = 32,
|
|
253
|
+
):
|
|
254
|
+
"""
|
|
255
|
+
Class for stream imitation
|
|
256
|
+
|
|
257
|
+
Parameters
|
|
258
|
+
----------
|
|
259
|
+
threshold: float (default - 0.5)
|
|
260
|
+
Speech threshold. Silero VAD outputs speech probabilities for each
|
|
261
|
+
audio chunk, probabilities ABOVE this value are considered as SPEECH.
|
|
262
|
+
It is better to tune this parameter for each dataset separately, but
|
|
263
|
+
"lazy" 0.5 is pretty good for most datasets.
|
|
264
|
+
sampling_rate: int (default - 16000)
|
|
265
|
+
Currently silero VAD models support 8000 and 16000 sample rates
|
|
266
|
+
min_silence_duration_ms: int (default - 300 milliseconds)
|
|
267
|
+
In the end of each speech chunk wait for min_silence_duration_ms
|
|
268
|
+
before separating it
|
|
269
|
+
speech_pad_ms: int (default - 100 milliseconds)
|
|
270
|
+
Final speech chunks are padded by speech_pad_ms each side
|
|
271
|
+
window_size_ms: int (default - 32 milliseconds)
|
|
272
|
+
Audio chunks of window_size_ms size are fed to the silero VAD model.
|
|
273
|
+
WARNING! Silero VAD models were trained using 32, 64, 96 milliseconds
|
|
274
|
+
for 8000 sample rate and 16000 sample rate.
|
|
275
|
+
Values other than these may affect model perfomance!!
|
|
276
|
+
"""
|
|
277
|
+
|
|
278
|
+
self.model = SileroVAD()
|
|
279
|
+
if window_size_ms not in [32, 64, 96]:
|
|
280
|
+
warnings.warn("Supported window_size_ms: [32, 64, 96]")
|
|
281
|
+
|
|
282
|
+
if sampling_rate in self.model.sample_rates:
|
|
283
|
+
self.vad_sr = sampling_rate
|
|
284
|
+
self.queue = FrameQueue(
|
|
285
|
+
window_size_ms, sampling_rate, speech_pad_ms=speech_pad_ms
|
|
286
|
+
)
|
|
287
|
+
else:
|
|
288
|
+
self.vad_sr = 16000
|
|
289
|
+
self.queue = FrameQueue(
|
|
290
|
+
window_size_ms,
|
|
291
|
+
src_sr=sampling_rate,
|
|
292
|
+
dst_sr=self.vad_sr,
|
|
293
|
+
speech_pad_ms=speech_pad_ms,
|
|
294
|
+
)
|
|
295
|
+
|
|
296
|
+
self.threshold = threshold
|
|
297
|
+
self.speech_pad_samples = speech_pad_ms * self.vad_sr // 1000
|
|
298
|
+
self.min_silence_samples = min_silence_duration_ms * self.vad_sr // 1000
|
|
299
|
+
self.reset_states()
|
|
300
|
+
|
|
301
|
+
def reset_states(self):
|
|
302
|
+
self.model.reset_states()
|
|
303
|
+
self.triggered = False
|
|
304
|
+
self.temp_end = 0
|
|
305
|
+
self.queue.clear()
|
|
306
|
+
|
|
307
|
+
def __call__(self, chunk, use_energy=False, return_seconds=False):
|
|
308
|
+
"""
|
|
309
|
+
chunk: audio chunk
|
|
310
|
+
|
|
311
|
+
use_energy: bool (default - False)
|
|
312
|
+
whether to use harmonic energy to suppress background vocals
|
|
313
|
+
return_seconds: bool (default - False)
|
|
314
|
+
whether return timestamps in seconds (default - samples)
|
|
315
|
+
"""
|
|
316
|
+
|
|
317
|
+
for frame_start, frame_end, frame in self.queue.add_chunk(chunk):
|
|
318
|
+
speech_prob = self.model(frame, self.vad_sr)
|
|
319
|
+
# Suppress background vocals by harmonic energy
|
|
320
|
+
if use_energy:
|
|
321
|
+
energy = get_energy(frame, self.vad_sr, from_harmonic=4)
|
|
322
|
+
if speech_prob < 0.9 and energy < 500 * (1 - speech_prob):
|
|
323
|
+
speech_prob = 0
|
|
324
|
+
|
|
325
|
+
if speech_prob >= self.threshold:
|
|
326
|
+
self.temp_end = 0
|
|
327
|
+
# triggered = True means the speech has been started
|
|
328
|
+
if not self.triggered:
|
|
329
|
+
self.triggered = True
|
|
330
|
+
speech_start = frame_start - self.speech_pad_samples
|
|
331
|
+
if return_seconds:
|
|
332
|
+
speech_start = round(speech_start / self.vad_sr, 3)
|
|
333
|
+
yield {"start": speech_start}, self.queue.get_original_frame(True)
|
|
334
|
+
else:
|
|
335
|
+
yield None, self.queue.get_original_frame()
|
|
336
|
+
elif speech_prob < self.threshold - 0.15 and self.triggered:
|
|
337
|
+
if not self.temp_end:
|
|
338
|
+
self.temp_end = frame_end
|
|
339
|
+
if frame_end - self.temp_end >= self.min_silence_samples:
|
|
340
|
+
speech_end = self.temp_end + self.speech_pad_samples
|
|
341
|
+
if return_seconds:
|
|
342
|
+
speech_end = round(speech_end / self.vad_sr, 3)
|
|
343
|
+
self.temp_end = 0
|
|
344
|
+
self.triggered = False
|
|
345
|
+
yield {"end": speech_end}, self.queue.get_original_frame()
|
|
346
|
+
else:
|
|
347
|
+
yield None, self.queue.get_original_frame()
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# Copyright (c) 2024, Zhendong Peng (pzd17@tsinghua.org.cn)
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
import warnings
|
|
16
|
+
|
|
17
|
+
import librosa
|
|
18
|
+
import numpy as np
|
|
19
|
+
import parselmouth
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
warnings.filterwarnings("ignore")
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def get_energy(chunk, sr, from_harmonic=1, to_harmonic=5):
|
|
26
|
+
sound = parselmouth.Sound(chunk, sampling_frequency=sr)
|
|
27
|
+
# pitch
|
|
28
|
+
pitch = sound.to_pitch(pitch_floor=100, pitch_ceiling=350)
|
|
29
|
+
# pitch energy
|
|
30
|
+
# energy = np.mean(pitch.selected_array["strength"])
|
|
31
|
+
pitch = np.mean(pitch.selected_array["frequency"])
|
|
32
|
+
# frame log energy
|
|
33
|
+
# energy = np.mean(sound.to_mfcc().to_array(), axis=1)[0]
|
|
34
|
+
|
|
35
|
+
# energy form x-th harmonic to y-th harmonic
|
|
36
|
+
freqs = librosa.fft_frequencies(sr=sr)
|
|
37
|
+
freq_band_idx = np.where(
|
|
38
|
+
(freqs >= from_harmonic * pitch) & (freqs <= to_harmonic * pitch)
|
|
39
|
+
)[0]
|
|
40
|
+
energy = np.sum(np.abs(librosa.stft(chunk)[freq_band_idx, :]))
|
|
41
|
+
|
|
42
|
+
return energy
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: silero-vad
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Silero VAD
|
|
5
|
+
Home-page: https://github.com/pengzhendong/silero-vad
|
|
6
|
+
Author: Zhendong Peng
|
|
7
|
+
Author-email: pzd17@tsinghua.org.cn
|
|
8
|
+
License: UNKNOWN
|
|
9
|
+
Platform: UNKNOWN
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: Topic :: Scientific/Engineering
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
License-File: LICENSE
|
|
15
|
+
|
|
16
|
+
## Silero VAD
|
|
17
|
+
|
|
18
|
+
See [Silero VAD](https://github.com/snakers4/silero-vad).
|
|
19
|
+
|
|
20
|
+
### Denoiser
|
|
21
|
+
|
|
22
|
+
See [RnNoise](https://github.com/werman/noise-suppression-for-voice).
|
|
23
|
+
|
|
24
|
+
### Python Usage
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
$ pip install https://github.com/pengzhendong/silero-vad/archive/refs/heads/master.zip
|
|
28
|
+
$ silero_vad --wav_path audio.wav
|
|
29
|
+
$ python
|
|
30
|
+
>>> from silero_vad import SileroVAD
|
|
31
|
+
>>> vad = SileroVAD()
|
|
32
|
+
>>> vad.get_speech_timestamps("audio.wav")
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
setup.py
|
|
4
|
+
silero_vad/__init__.py
|
|
5
|
+
silero_vad/cli.py
|
|
6
|
+
silero_vad/frame_queue.py
|
|
7
|
+
silero_vad/inference_session.py
|
|
8
|
+
silero_vad/silero_vad.onnx
|
|
9
|
+
silero_vad/silero_vad.py
|
|
10
|
+
silero_vad/utils.py
|
|
11
|
+
silero_vad.egg-info/PKG-INFO
|
|
12
|
+
silero_vad.egg-info/SOURCES.txt
|
|
13
|
+
silero_vad.egg-info/dependency_links.txt
|
|
14
|
+
silero_vad.egg-info/entry_points.txt
|
|
15
|
+
silero_vad.egg-info/requires.txt
|
|
16
|
+
silero_vad.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
silero_vad
|