File size: 10,682 Bytes
c0447ed
 
 
 
 
 
fca9809
c0447ed
 
 
fca9809
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c0447ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import gc
import time
import numpy as np
import onnxruntime
from datetime import timedelta
from pydub import AudioSegment
from silero_vad import load_silero_vad, get_speech_timestamps, VADIterator
import os
import logging

class FixedVADIterator(VADIterator):
    '''It fixes VADIterator by allowing to process any audio length, not only exactly 512 frames at once.
    If audio to be processed at once is long and multiple voiced segments detected, 
    then __call__ returns the start of the first segment, and end (or middle, which means no end) of the last segment. 
    '''

    def reset_states(self):
        super().reset_states()
        self.buffer = np.array([],dtype=np.float32)

    def __call__(self, x, return_seconds=False):
        self.buffer = np.append(self.buffer, x) 
        ret = None
        while len(self.buffer) >= 512:
            r = super().__call__(self.buffer[:512], return_seconds=return_seconds)
            self.buffer = self.buffer[512:]
            if ret is None:
                ret = r
            elif r is not None:
                if 'end' in r:
                    ret['end'] = r['end']  # the latter end
                if 'start' in r and 'end' in ret:  # there is an earlier start.
                    # Remove end, merging this segment with the previous one.
                    del ret['end']
        return ret if ret != {} else None

class SileroVADProcessor:
    """
    A class for processing audio files using Silero VAD to detect voice activity
    and extract voice segments from audio files.
    """

    def __init__(self,
                 activate_threshold=0.5,
                 fusion_threshold=0.3,
                 min_speech_duration=0.25,
                 max_speech_duration=20,
                 min_silence_duration=250,
                 sample_rate=16000,
                 ort_providers=None):
        """
        Initialize the SileroVADProcessor.

        Args:
            activate_threshold (float): Threshold for voice activity detection
            fusion_threshold (float): Threshold for merging close speech segments (seconds)
            min_speech_duration (float): Minimum duration of speech to be considered valid (seconds)
            max_speech_duration (float): Maximum duration of speech (seconds)
            min_silence_duration (int): Minimum silence duration (ms)
            sample_rate (int): Sample rate of the audio (8000 or 16000 Hz)
            ort_providers (list): ONNX Runtime providers for acceleration
        """
        # VAD parameters
        self.activate_threshold = activate_threshold
        self.fusion_threshold = fusion_threshold
        self.min_speech_duration = min_speech_duration
        self.max_speech_duration = max_speech_duration
        self.min_silence_duration = min_silence_duration
        self.sample_rate = sample_rate
        self.ort_providers = ort_providers if ort_providers else []

        # Initialize logger
        self.logger = logging.getLogger(__name__)

        # Load Silero VAD model
        self._init_onnx_session()
        self.silero_vad = load_silero_vad(onnx=True)

    def _init_onnx_session(self):
        """Initialize ONNX Runtime session with appropriate settings."""
        session_opts = onnxruntime.SessionOptions()
        session_opts.log_severity_level = 3
        session_opts.inter_op_num_threads = 0
        session_opts.intra_op_num_threads = 0
        session_opts.enable_cpu_mem_arena = True
        session_opts.execution_mode = onnxruntime.ExecutionMode.ORT_SEQUENTIAL
        session_opts.graph_optimization_level = onnxruntime.GraphOptimizationLevel.ORT_ENABLE_ALL

        session_opts.add_session_config_entry("session.intra_op.allow_spinning", "1")
        session_opts.add_session_config_entry("session.inter_op.allow_spinning", "1")
        session_opts.add_session_config_entry("session.set_denormal_as_zero", "1")

        # Set the session_opts to be used by silero_vad
        # onnxruntime.capi._pybind_state.get_default_session_options(session_opts)

    def load_audio(self, audio_path):
        """
        Load audio file and prepare it for VAD processing.

        Args:
            audio_path (str): Path to the audio file

        Returns:
            numpy.ndarray: Audio data as numpy array
        """
        self.logger.info(f"Loading audio from {audio_path}")
        audio_segment = AudioSegment.from_file(audio_path)
        audio_segment = audio_segment.set_channels(1).set_frame_rate(self.sample_rate)

        # Convert to numpy array and normalize
        dtype = np.float16 if self.use_gpu_fp16 else np.float32
        audio_array = np.array(audio_segment.get_array_of_samples(), dtype=dtype) * 0.000030517578  # 1/32768

        self.audio_segment = audio_segment  # Store for later use
        return audio_array
    
    @property
    def model(self):
        return self.silero_vad

    def process_timestamps(self, timestamps):
        """
        Process VAD timestamps: filter short segments and merge close segments.

        Args:
            timestamps (list): List of (start, end) tuples

        Returns:
            list: Processed list of (start, end) tuples
        """
        # Filter out short durations
        filtered_timestamps = [(start, end) for start, end in timestamps
                               if (end - start) >= self.min_speech_duration]

        # Fuse timestamps in two passes for better merging
        fused_timestamps_1st = []
        for start, end in filtered_timestamps:
            if fused_timestamps_1st and (start - fused_timestamps_1st[-1][1] <= self.fusion_threshold):
                fused_timestamps_1st[-1] = (fused_timestamps_1st[-1][0], end)
            else:
                fused_timestamps_1st.append((start, end))

        fused_timestamps_2nd = []
        for start, end in fused_timestamps_1st:
            if fused_timestamps_2nd and (start - fused_timestamps_2nd[-1][1] <= self.fusion_threshold):
                fused_timestamps_2nd[-1] = (fused_timestamps_2nd[-1][0], end)
            else:
                fused_timestamps_2nd.append((start, end))

        return fused_timestamps_2nd

    def format_time(self, seconds):
        """
        Convert seconds to VTT time format 'hh:mm:ss.mmm'.

        Args:
            seconds (float): Time in seconds

        Returns:
            str: Formatted time string
        """
        td = timedelta(seconds=seconds)
        td_sec = td.total_seconds()
        total_seconds = int(td_sec)
        milliseconds = int((td_sec - total_seconds) * 1000)
        hours = total_seconds // 3600
        minutes = (total_seconds % 3600) // 60
        seconds = total_seconds % 60
        return f"{hours:02}:{minutes:02}:{seconds:02}.{milliseconds:03}"

    def detect_speech(self, audio:np.array):
        """
        Run VAD on the audio file to detect speech segments.

        Args:
            audio_path (str): Path to the audio file

        Returns:
            list: List of processed timestamps as (start, end) tuples
        """
        self.logger.info("Starting VAD process")
        start_time = time.time()
        # Get speech timestamps
        raw_timestamps = get_speech_timestamps(
            audio,
            model=self.silero_vad,
            threshold=self.activate_threshold,
            max_speech_duration_s=self.max_speech_duration,
            min_speech_duration_ms=int(self.min_speech_duration * 1000),
            min_silence_duration_ms=self.min_silence_duration,
            return_seconds=True
        )

        # Convert to simple format and process
        timestamps = [(item['start'], item['end']) for item in raw_timestamps]
        processed_timestamps = self.process_timestamps(timestamps)

        # Clean up
        del audio
        gc.collect()

        self.logger.info(f"VAD completed in {time.time() - start_time:.3f} seconds")
        return processed_timestamps

        """
        Save timestamps in both second and sample indices formats.

        Args:
            timestamps (list): List of (start, end) tuples
            output_prefix (str): Prefix for output files
        """
        # Save timestamps in seconds (VTT format)
        seconds_path = f"{output_prefix}_timestamps_second.txt"
        with open(seconds_path, "w", encoding='UTF-8') as file:
            self.logger.info("Saving timestamps in seconds format")
            for start, end in timestamps:
                s_time = self.format_time(start)
                e_time = self.format_time(end)
                line = f"{s_time} --> {e_time}\n"
                file.write(line)

        # Save timestamps in sample indices
        indices_path = f"{output_prefix}_timestamps_indices.txt"
        with open(indices_path, "w", encoding='UTF-8') as file:
            self.logger.info("Saving timestamps in indices format")
            for start, end in timestamps:
                line = f"{int(start * self.sample_rate)} --> {int(end * self.sample_rate)}\n"
                file.write(line)

        self.logger.info(f"Timestamps saved to {seconds_path} and {indices_path}")

    def extract_speech_segments(self, audio_segment, timestamps):
        """
        Extract speech segments from the audio and combine them into a single audio file.

        Args:
            timestamps (list): List of (start, end) tuples indicating speech segments

        Returns:
            AudioSegment: The combined speech segments
        """
        audio_segment = audio_segment.numpy()
        combined_speech = np.array([], dtype=np.float32)

        # Extract and combine each speech segment
        for i, (start, end) in enumerate(timestamps):
            # Convert seconds to milliseconds for pydub
            start_ms = int(start * 1000)
            end_ms = int(end * 1000)

            # Ensure the end time does not exceed the length of the audio segment
            if end_ms > len(audio_segment):
                end_ms = len(audio_segment)

            # Extract the segment
            segment = audio_segment[start_ms:end_ms]

            # Add to combined audio
            combined_speech = np.append(combined_speech, segment)

        return combined_speech

    def process_audio(self, audio_array:np.array):
        """
        Complete processing pipeline: detect speech, save timestamps, and optionally extract speech.

        Returns:
            tuple: (timestamps, output_speech_path if extract_speech else None)
        """

        # Run VAD to detect speech
        timestamps = self.detect_speech(audio_array)

        combined_speech = self.extract_speech_segments(audio_array, timestamps)

        return timestamps, combined_speech