RealTimeSTT 0.1.5__tar.gz → 0.1.7__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.
- {RealTimeSTT-0.1.5 → RealTimeSTT-0.1.7}/PKG-INFO +29 -6
- {RealTimeSTT-0.1.5 → RealTimeSTT-0.1.7}/README.md +27 -4
- {RealTimeSTT-0.1.5 → RealTimeSTT-0.1.7}/RealTimeSTT.egg-info/PKG-INFO +29 -6
- {RealTimeSTT-0.1.5 → RealTimeSTT-0.1.7}/RealTimeSTT.egg-info/requires.txt +2 -2
- {RealTimeSTT-0.1.5 → RealTimeSTT-0.1.7}/RealtimeSTT/audio_recorder.py +460 -283
- {RealTimeSTT-0.1.5 → RealTimeSTT-0.1.7}/setup.py +1 -1
- {RealTimeSTT-0.1.5 → RealTimeSTT-0.1.7}/RealTimeSTT.egg-info/SOURCES.txt +0 -0
- {RealTimeSTT-0.1.5 → RealTimeSTT-0.1.7}/RealTimeSTT.egg-info/dependency_links.txt +0 -0
- {RealTimeSTT-0.1.5 → RealTimeSTT-0.1.7}/RealTimeSTT.egg-info/top_level.txt +0 -0
- {RealTimeSTT-0.1.5 → RealTimeSTT-0.1.7}/RealtimeSTT/__init__.py +0 -0
- {RealTimeSTT-0.1.5 → RealTimeSTT-0.1.7}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: RealTimeSTT
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.7
|
|
4
4
|
Summary: A fast Voice Activity Detection and Transcription System
|
|
5
5
|
Home-page: https://github.com/KoljaB/RealTimeSTT
|
|
6
6
|
Author: Kolja Beigel
|
|
@@ -16,8 +16,8 @@ Requires-Dist: faster-whisper>=0.7.1
|
|
|
16
16
|
Requires-Dist: pvporcupine==1.9.5
|
|
17
17
|
Requires-Dist: webrtcvad>=2.0.10
|
|
18
18
|
Requires-Dist: halo>=0.0.31
|
|
19
|
-
Requires-Dist: torch
|
|
20
|
-
Requires-Dist: torchaudio
|
|
19
|
+
Requires-Dist: torch>=2.0.1
|
|
20
|
+
Requires-Dist: torchaudio>=2.0.2
|
|
21
21
|
|
|
22
22
|
|
|
23
23
|
# RealtimeSTT
|
|
@@ -37,6 +37,15 @@ https://github.com/KoljaB/RealtimeSTT/assets/7604638/207cb9a2-4482-48e7-9d2b-072
|
|
|
37
37
|
|
|
38
38
|
### Updates
|
|
39
39
|
|
|
40
|
+
#### v0.1.7
|
|
41
|
+
- Bugfix for Mac OS Installation (multiprocessing / queue.size())
|
|
42
|
+
- KeyboardInterrupt handling (now abortable with CTRL+C)
|
|
43
|
+
- Bugfix for spinner handling (could lead to exception in some cases)
|
|
44
|
+
|
|
45
|
+
#### v0.1.6
|
|
46
|
+
- Implements context manager protocol (recorder can be used in a `with` statement)
|
|
47
|
+
- Bugfix for resource management in shutdown method
|
|
48
|
+
|
|
40
49
|
#### v0.1.5
|
|
41
50
|
|
|
42
51
|
- Bugfix for detection of short speech right after sentence detection (the problem mentioned in the video)
|
|
@@ -151,8 +160,8 @@ print(recorder.text())
|
|
|
151
160
|
Recording based on voice activity detection.
|
|
152
161
|
|
|
153
162
|
```python
|
|
154
|
-
|
|
155
|
-
print(recorder.text())
|
|
163
|
+
with AudioToTextRecorder() as recorder:
|
|
164
|
+
print(recorder.text())
|
|
156
165
|
```
|
|
157
166
|
|
|
158
167
|
When running recorder.text in a loop it is recommended to use a callback, allowing the transcription to be run asynchronously:
|
|
@@ -191,6 +200,20 @@ recorder = AudioToTextRecorder(on_recording_start=my_start_callback,
|
|
|
191
200
|
on_recording_stop=my_stop_callback)
|
|
192
201
|
```
|
|
193
202
|
|
|
203
|
+
### Shutdown
|
|
204
|
+
|
|
205
|
+
You can shutdown the recorder safely by using the context manager protocol:
|
|
206
|
+
|
|
207
|
+
```python
|
|
208
|
+
with AudioToTextRecorder() as recorder:
|
|
209
|
+
[...]
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
Or you can call the shutdown method manually (if using "with" is not feasible):
|
|
213
|
+
|
|
214
|
+
```python
|
|
215
|
+
recorder.shutdown()
|
|
216
|
+
```
|
|
194
217
|
|
|
195
218
|
## Testing the Library
|
|
196
219
|
|
|
@@ -275,7 +298,7 @@ When you initialize the `AudioToTextRecorder` class, you have various options to
|
|
|
275
298
|
|
|
276
299
|
- **silero_sensitivity** (float, default=0.6): Sensitivity for Silero's voice activity detection ranging from 0 (least sensitive) to 1 (most sensitive). Default is 0.6.
|
|
277
300
|
|
|
278
|
-
- **silero_use_onnx** (bool, default=
|
|
301
|
+
- **silero_use_onnx** (bool, default=False): Enables usage of the pre-trained model from Silero in the ONNX (Open Neural Network Exchange) format instead of the PyTorch format. Default is False. Recommended for faster performance.
|
|
279
302
|
|
|
280
303
|
- **post_speech_silence_duration** (float, default=0.2): Duration in seconds of silence that must follow speech before the recording is considered to be completed. This ensures that any brief pauses during speech don't prematurely end the recording.
|
|
281
304
|
|
|
@@ -16,6 +16,15 @@ https://github.com/KoljaB/RealtimeSTT/assets/7604638/207cb9a2-4482-48e7-9d2b-072
|
|
|
16
16
|
|
|
17
17
|
### Updates
|
|
18
18
|
|
|
19
|
+
#### v0.1.7
|
|
20
|
+
- Bugfix for Mac OS Installation (multiprocessing / queue.size())
|
|
21
|
+
- KeyboardInterrupt handling (now abortable with CTRL+C)
|
|
22
|
+
- Bugfix for spinner handling (could lead to exception in some cases)
|
|
23
|
+
|
|
24
|
+
#### v0.1.6
|
|
25
|
+
- Implements context manager protocol (recorder can be used in a `with` statement)
|
|
26
|
+
- Bugfix for resource management in shutdown method
|
|
27
|
+
|
|
19
28
|
#### v0.1.5
|
|
20
29
|
|
|
21
30
|
- Bugfix for detection of short speech right after sentence detection (the problem mentioned in the video)
|
|
@@ -130,8 +139,8 @@ print(recorder.text())
|
|
|
130
139
|
Recording based on voice activity detection.
|
|
131
140
|
|
|
132
141
|
```python
|
|
133
|
-
|
|
134
|
-
print(recorder.text())
|
|
142
|
+
with AudioToTextRecorder() as recorder:
|
|
143
|
+
print(recorder.text())
|
|
135
144
|
```
|
|
136
145
|
|
|
137
146
|
When running recorder.text in a loop it is recommended to use a callback, allowing the transcription to be run asynchronously:
|
|
@@ -170,6 +179,20 @@ recorder = AudioToTextRecorder(on_recording_start=my_start_callback,
|
|
|
170
179
|
on_recording_stop=my_stop_callback)
|
|
171
180
|
```
|
|
172
181
|
|
|
182
|
+
### Shutdown
|
|
183
|
+
|
|
184
|
+
You can shutdown the recorder safely by using the context manager protocol:
|
|
185
|
+
|
|
186
|
+
```python
|
|
187
|
+
with AudioToTextRecorder() as recorder:
|
|
188
|
+
[...]
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
Or you can call the shutdown method manually (if using "with" is not feasible):
|
|
192
|
+
|
|
193
|
+
```python
|
|
194
|
+
recorder.shutdown()
|
|
195
|
+
```
|
|
173
196
|
|
|
174
197
|
## Testing the Library
|
|
175
198
|
|
|
@@ -254,7 +277,7 @@ When you initialize the `AudioToTextRecorder` class, you have various options to
|
|
|
254
277
|
|
|
255
278
|
- **silero_sensitivity** (float, default=0.6): Sensitivity for Silero's voice activity detection ranging from 0 (least sensitive) to 1 (most sensitive). Default is 0.6.
|
|
256
279
|
|
|
257
|
-
- **silero_use_onnx** (bool, default=
|
|
280
|
+
- **silero_use_onnx** (bool, default=False): Enables usage of the pre-trained model from Silero in the ONNX (Open Neural Network Exchange) format instead of the PyTorch format. Default is False. Recommended for faster performance.
|
|
258
281
|
|
|
259
282
|
- **post_speech_silence_duration** (float, default=0.2): Duration in seconds of silence that must follow speech before the recording is considered to be completed. This ensures that any brief pauses during speech don't prematurely end the recording.
|
|
260
283
|
|
|
@@ -299,4 +322,4 @@ MIT
|
|
|
299
322
|
|
|
300
323
|
Kolja Beigel
|
|
301
324
|
Email: kolja.beigel@web.de
|
|
302
|
-
[GitHub](https://github.com/KoljaB/RealtimeSTT)
|
|
325
|
+
[GitHub](https://github.com/KoljaB/RealtimeSTT)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: RealTimeSTT
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.7
|
|
4
4
|
Summary: A fast Voice Activity Detection and Transcription System
|
|
5
5
|
Home-page: https://github.com/KoljaB/RealTimeSTT
|
|
6
6
|
Author: Kolja Beigel
|
|
@@ -16,8 +16,8 @@ Requires-Dist: faster-whisper>=0.7.1
|
|
|
16
16
|
Requires-Dist: pvporcupine==1.9.5
|
|
17
17
|
Requires-Dist: webrtcvad>=2.0.10
|
|
18
18
|
Requires-Dist: halo>=0.0.31
|
|
19
|
-
Requires-Dist: torch
|
|
20
|
-
Requires-Dist: torchaudio
|
|
19
|
+
Requires-Dist: torch>=2.0.1
|
|
20
|
+
Requires-Dist: torchaudio>=2.0.2
|
|
21
21
|
|
|
22
22
|
|
|
23
23
|
# RealtimeSTT
|
|
@@ -37,6 +37,15 @@ https://github.com/KoljaB/RealtimeSTT/assets/7604638/207cb9a2-4482-48e7-9d2b-072
|
|
|
37
37
|
|
|
38
38
|
### Updates
|
|
39
39
|
|
|
40
|
+
#### v0.1.7
|
|
41
|
+
- Bugfix for Mac OS Installation (multiprocessing / queue.size())
|
|
42
|
+
- KeyboardInterrupt handling (now abortable with CTRL+C)
|
|
43
|
+
- Bugfix for spinner handling (could lead to exception in some cases)
|
|
44
|
+
|
|
45
|
+
#### v0.1.6
|
|
46
|
+
- Implements context manager protocol (recorder can be used in a `with` statement)
|
|
47
|
+
- Bugfix for resource management in shutdown method
|
|
48
|
+
|
|
40
49
|
#### v0.1.5
|
|
41
50
|
|
|
42
51
|
- Bugfix for detection of short speech right after sentence detection (the problem mentioned in the video)
|
|
@@ -151,8 +160,8 @@ print(recorder.text())
|
|
|
151
160
|
Recording based on voice activity detection.
|
|
152
161
|
|
|
153
162
|
```python
|
|
154
|
-
|
|
155
|
-
print(recorder.text())
|
|
163
|
+
with AudioToTextRecorder() as recorder:
|
|
164
|
+
print(recorder.text())
|
|
156
165
|
```
|
|
157
166
|
|
|
158
167
|
When running recorder.text in a loop it is recommended to use a callback, allowing the transcription to be run asynchronously:
|
|
@@ -191,6 +200,20 @@ recorder = AudioToTextRecorder(on_recording_start=my_start_callback,
|
|
|
191
200
|
on_recording_stop=my_stop_callback)
|
|
192
201
|
```
|
|
193
202
|
|
|
203
|
+
### Shutdown
|
|
204
|
+
|
|
205
|
+
You can shutdown the recorder safely by using the context manager protocol:
|
|
206
|
+
|
|
207
|
+
```python
|
|
208
|
+
with AudioToTextRecorder() as recorder:
|
|
209
|
+
[...]
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
Or you can call the shutdown method manually (if using "with" is not feasible):
|
|
213
|
+
|
|
214
|
+
```python
|
|
215
|
+
recorder.shutdown()
|
|
216
|
+
```
|
|
194
217
|
|
|
195
218
|
## Testing the Library
|
|
196
219
|
|
|
@@ -275,7 +298,7 @@ When you initialize the `AudioToTextRecorder` class, you have various options to
|
|
|
275
298
|
|
|
276
299
|
- **silero_sensitivity** (float, default=0.6): Sensitivity for Silero's voice activity detection ranging from 0 (least sensitive) to 1 (most sensitive). Default is 0.6.
|
|
277
300
|
|
|
278
|
-
- **silero_use_onnx** (bool, default=
|
|
301
|
+
- **silero_use_onnx** (bool, default=False): Enables usage of the pre-trained model from Silero in the ONNX (Open Neural Network Exchange) format instead of the PyTorch format. Default is False. Recommended for faster performance.
|
|
279
302
|
|
|
280
303
|
- **post_speech_silence_duration** (float, default=0.2): Duration in seconds of silence that must follow speech before the recording is considered to be completed. This ensures that any brief pauses during speech don't prematurely end the recording.
|
|
281
304
|
|
|
@@ -20,7 +20,7 @@ Author: Kolja Beigel
|
|
|
20
20
|
|
|
21
21
|
"""
|
|
22
22
|
|
|
23
|
-
from multiprocessing import Process, Pipe,
|
|
23
|
+
from multiprocessing import Process, Pipe, Event, Manager
|
|
24
24
|
import faster_whisper
|
|
25
25
|
import collections
|
|
26
26
|
import numpy as np
|
|
@@ -51,6 +51,7 @@ INIT_WAKE_WORDS_SENSITIVITY = 0.6
|
|
|
51
51
|
INIT_PRE_RECORDING_BUFFER_DURATION = 1.0
|
|
52
52
|
INIT_WAKE_WORD_ACTIVATION_DELAY = 0.0
|
|
53
53
|
INIT_WAKE_WORD_TIMEOUT = 5.0
|
|
54
|
+
ALLOWED_LATENCY_LIMIT = 10
|
|
54
55
|
|
|
55
56
|
TIME_SLEEP = 0.02
|
|
56
57
|
SAMPLE_RATE = 16000
|
|
@@ -82,7 +83,7 @@ class AudioToTextRecorder:
|
|
|
82
83
|
|
|
83
84
|
# Voice activation parameters
|
|
84
85
|
silero_sensitivity: float = INIT_SILERO_SENSITIVITY,
|
|
85
|
-
silero_use_onnx: bool =
|
|
86
|
+
silero_use_onnx: bool = False,
|
|
86
87
|
webrtc_sensitivity: int = INIT_WEBRTC_SENSITIVITY,
|
|
87
88
|
post_speech_silence_duration: float = INIT_POST_SPEECH_SILENCE_DURATION,
|
|
88
89
|
min_length_of_recording: float = INIT_MIN_LENGTH_OF_RECORDING,
|
|
@@ -122,7 +123,7 @@ class AudioToTextRecorder:
|
|
|
122
123
|
- on_realtime_transcription_update = A callback function that is triggered whenever there's an update in the real-time transcription. The function is called with the newly transcribed text as its argument.
|
|
123
124
|
- on_realtime_transcription_stabilized = A callback function that is triggered when the transcribed text stabilizes in quality. The stabilized text is generally more accurate but may arrive with a slight delay compared to the regular real-time updates.
|
|
124
125
|
- silero_sensitivity (float, default=SILERO_SENSITIVITY): Sensitivity for the Silero Voice Activity Detection model ranging from 0 (least sensitive) to 1 (most sensitive). Default is 0.5.
|
|
125
|
-
- silero_use_onnx (bool, default=
|
|
126
|
+
- silero_use_onnx (bool, default=False): Enables usage of the pre-trained model from Silero in the ONNX (Open Neural Network Exchange) format instead of the PyTorch format. This is recommended for faster performance.
|
|
126
127
|
- webrtc_sensitivity (int, default=WEBRTC_SENSITIVITY): Sensitivity for the WebRTC Voice Activity Detection engine ranging from 0 (least aggressive / most sensitive) to 3 (most aggressive, least sensitive). Default is 3.
|
|
127
128
|
- post_speech_silence_duration (float, default=0.2): Duration in seconds of silence that must follow speech before the recording is considered to be completed. This ensures that any brief pauses during speech don't prematurely end the recording.
|
|
128
129
|
- min_gap_between_recordings (float, default=1.0): Specifies the minimum time interval in seconds that should exist between the end of one recording session and the beginning of another to prevent rapid consecutive recordings.
|
|
@@ -168,9 +169,11 @@ class AudioToTextRecorder:
|
|
|
168
169
|
self.realtime_processing_pause = realtime_processing_pause
|
|
169
170
|
self.on_realtime_transcription_update = on_realtime_transcription_update
|
|
170
171
|
self.on_realtime_transcription_stabilized = on_realtime_transcription_stabilized
|
|
172
|
+
self.allowed_latency_limit = ALLOWED_LATENCY_LIMIT
|
|
171
173
|
|
|
172
174
|
self.level = level
|
|
173
|
-
|
|
175
|
+
manager = Manager()
|
|
176
|
+
self.audio_queue = manager.Queue()
|
|
174
177
|
self.buffer_size = BUFFER_SIZE
|
|
175
178
|
self.sample_rate = SAMPLE_RATE
|
|
176
179
|
self.recording_start_time = 0
|
|
@@ -207,7 +210,7 @@ class AudioToTextRecorder:
|
|
|
207
210
|
logger.setLevel(level) # Set the root logger's level
|
|
208
211
|
|
|
209
212
|
# Create a file handler and set its level
|
|
210
|
-
file_handler = logging.FileHandler('
|
|
213
|
+
file_handler = logging.FileHandler('realtimesst.log')
|
|
211
214
|
file_handler.setLevel(logging.DEBUG)
|
|
212
215
|
file_handler.setFormatter(logging.Formatter(log_format))
|
|
213
216
|
|
|
@@ -220,16 +223,21 @@ class AudioToTextRecorder:
|
|
|
220
223
|
logger.addHandler(file_handler)
|
|
221
224
|
logger.addHandler(console_handler)
|
|
222
225
|
|
|
226
|
+
self.is_shut_down = False
|
|
227
|
+
self.shutdown_event = Event()
|
|
223
228
|
|
|
224
|
-
|
|
225
|
-
self.parent_transcription_pipe, child_transcription_pipe = Pipe()
|
|
226
|
-
self.process = Process(target=AudioToTextRecorder._transcription_worker, args=(child_transcription_pipe, model))
|
|
227
|
-
self.process.start()
|
|
229
|
+
logging.info(f"Starting RealTimeSTT")
|
|
228
230
|
|
|
229
|
-
#
|
|
230
|
-
|
|
231
|
-
|
|
231
|
+
# Start transcription process
|
|
232
|
+
self.interrupt_stop_event = Event()
|
|
233
|
+
self.main_transcription_ready_event = Event()
|
|
234
|
+
self.parent_transcription_pipe, child_transcription_pipe = Pipe()
|
|
235
|
+
self.transcript_process = Process(target=AudioToTextRecorder._transcription_worker, args=(child_transcription_pipe, model, self.main_transcription_ready_event, self.shutdown_event, self.interrupt_stop_event))
|
|
236
|
+
self.transcript_process.start()
|
|
232
237
|
|
|
238
|
+
# Start audio data reading process
|
|
239
|
+
self.reader_process = Process(target=AudioToTextRecorder._audio_data_worker, args=(self.audio_queue, self.sample_rate, self.buffer_size, self.shutdown_event, self.interrupt_stop_event))
|
|
240
|
+
self.reader_process.start()
|
|
233
241
|
|
|
234
242
|
# Initialize the realtime transcription model
|
|
235
243
|
if self.enable_realtime_transcription:
|
|
@@ -310,11 +318,34 @@ class AudioToTextRecorder:
|
|
|
310
318
|
self.realtime_thread.daemon = True
|
|
311
319
|
self.realtime_thread.start()
|
|
312
320
|
|
|
321
|
+
# Wait for transcription models to start
|
|
322
|
+
logging.debug('Waiting for main transcription model to start')
|
|
323
|
+
self.main_transcription_ready_event.wait()
|
|
324
|
+
logging.debug('Main transcription model ready')
|
|
325
|
+
|
|
313
326
|
logging.debug('RealtimeSTT initialization completed successfully')
|
|
314
327
|
|
|
315
328
|
|
|
316
329
|
@staticmethod
|
|
317
|
-
def _transcription_worker(conn, model_path):
|
|
330
|
+
def _transcription_worker(conn, model_path, ready_event, shutdown_event, interrupt_stop_event):
|
|
331
|
+
"""
|
|
332
|
+
Worker method that handles the continuous process of transcribing audio data.
|
|
333
|
+
|
|
334
|
+
This method runs in a separate process and is responsible for:
|
|
335
|
+
- Initializing the `faster_whisper` model used for transcription.
|
|
336
|
+
- Receiving audio data sent through a pipe and using the model to transcribe it.
|
|
337
|
+
- Sending transcription results back through the pipe.
|
|
338
|
+
- Continuously checking for a shutdown event to gracefully terminate the transcription process.
|
|
339
|
+
|
|
340
|
+
Args:
|
|
341
|
+
conn (multiprocessing.Connection): The connection endpoint used for receiving audio data and sending transcription results.
|
|
342
|
+
model_path (str): The path to the pre-trained faster_whisper model for transcription.
|
|
343
|
+
ready_event (threading.Event): An event that is set when the transcription model is successfully initialized and ready.
|
|
344
|
+
shutdown_event (threading.Event): An event that, when set, signals this worker method to terminate.
|
|
345
|
+
|
|
346
|
+
Raises:
|
|
347
|
+
Exception: If there is an error while initializing the transcription model.
|
|
348
|
+
"""
|
|
318
349
|
|
|
319
350
|
logging.info(f"Initializing faster_whisper main transcription model {model_path}")
|
|
320
351
|
|
|
@@ -328,25 +359,53 @@ class AudioToTextRecorder:
|
|
|
328
359
|
logging.exception(f"Error initializing main faster_whisper transcription model: {e}")
|
|
329
360
|
raise
|
|
330
361
|
|
|
362
|
+
ready_event.set()
|
|
363
|
+
|
|
331
364
|
logging.debug('Faster_whisper main speech to text transcription model initialized successfully')
|
|
332
365
|
|
|
333
|
-
while
|
|
334
|
-
audio, language = conn.recv()
|
|
366
|
+
while not shutdown_event.is_set():
|
|
335
367
|
try:
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
368
|
+
if conn.poll(0.5):
|
|
369
|
+
audio, language = conn.recv()
|
|
370
|
+
try:
|
|
371
|
+
segments = model.transcribe(audio, language=language if language else None)[0]
|
|
372
|
+
transcription = " ".join(seg.text for seg in segments).strip()
|
|
373
|
+
conn.send(('success', transcription))
|
|
374
|
+
except faster_whisper.WhisperError as e:
|
|
375
|
+
logging.error(f"Whisper transcription error: {e}")
|
|
376
|
+
conn.send(('error', str(e)))
|
|
377
|
+
except Exception as e:
|
|
378
|
+
logging.error(f"General transcription error: {e}")
|
|
379
|
+
conn.send(('error', str(e)))
|
|
380
|
+
else:
|
|
381
|
+
# If there's no data, sleep for a short while to prevent busy waiting
|
|
382
|
+
time.sleep(0.02)
|
|
383
|
+
except KeyboardInterrupt:
|
|
384
|
+
interrupt_stop_event.set()
|
|
385
|
+
logging.debug('Transcription worker process finished due to KeyboardInterrupt')
|
|
386
|
+
break
|
|
345
387
|
|
|
346
388
|
|
|
347
389
|
@staticmethod
|
|
348
|
-
def _audio_data_worker(audio_queue, sample_rate, buffer_size):
|
|
390
|
+
def _audio_data_worker(audio_queue, sample_rate, buffer_size, shutdown_event, interrupt_stop_event):
|
|
391
|
+
"""
|
|
392
|
+
Worker method that handles the audio recording process.
|
|
393
|
+
|
|
394
|
+
This method runs in a separate process and is responsible for:
|
|
395
|
+
- Setting up the audio input stream for recording.
|
|
396
|
+
- Continuously reading audio data from the input stream and placing it in a queue.
|
|
397
|
+
- Handling errors during the recording process, including input overflow.
|
|
398
|
+
- Gracefully terminating the recording process when a shutdown event is set.
|
|
399
|
+
|
|
400
|
+
Args:
|
|
401
|
+
audio_queue (queue.Queue): A queue where recorded audio data is placed.
|
|
402
|
+
sample_rate (int): The sample rate of the audio input stream.
|
|
403
|
+
buffer_size (int): The size of the buffer used in the audio input stream.
|
|
404
|
+
shutdown_event (threading.Event): An event that, when set, signals this worker method to terminate.
|
|
349
405
|
|
|
406
|
+
Raises:
|
|
407
|
+
Exception: If there is an error while initializing the audio recording.
|
|
408
|
+
"""
|
|
350
409
|
logging.info("Initializing audio recording (creating pyAudio input stream)")
|
|
351
410
|
|
|
352
411
|
try:
|
|
@@ -359,29 +418,37 @@ class AudioToTextRecorder:
|
|
|
359
418
|
|
|
360
419
|
logging.debug('Audio recording (pyAudio input stream) initialized successfully')
|
|
361
420
|
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
421
|
+
try:
|
|
422
|
+
while not shutdown_event.is_set():
|
|
423
|
+
try:
|
|
424
|
+
data = stream.read(buffer_size)
|
|
425
|
+
|
|
426
|
+
except OSError as e:
|
|
427
|
+
if e.errno == pyaudio.paInputOverflowed:
|
|
428
|
+
logging.warning("Input overflowed. Frame dropped.")
|
|
429
|
+
else:
|
|
430
|
+
logging.error(f"Error during recording: {e}")
|
|
431
|
+
tb_str = traceback.format_exc()
|
|
432
|
+
print (f"Traceback: {tb_str}")
|
|
433
|
+
print (f"Error: {e}")
|
|
434
|
+
continue
|
|
435
|
+
|
|
436
|
+
except Exception as e:
|
|
370
437
|
logging.error(f"Error during recording: {e}")
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
438
|
+
tb_str = traceback.format_exc()
|
|
439
|
+
print (f"Traceback: {tb_str}")
|
|
440
|
+
print (f"Error: {e}")
|
|
441
|
+
continue
|
|
375
442
|
|
|
376
|
-
|
|
377
|
-
logging.error(f"Error during recording: {e}")
|
|
378
|
-
time.sleep(1)
|
|
379
|
-
tb_str = traceback.format_exc()
|
|
380
|
-
print (f"Traceback: {tb_str}")
|
|
381
|
-
print (f"Error: {e}")
|
|
382
|
-
continue
|
|
443
|
+
audio_queue.put(data)
|
|
383
444
|
|
|
384
|
-
|
|
445
|
+
except KeyboardInterrupt:
|
|
446
|
+
interrupt_stop_event.set()
|
|
447
|
+
logging.debug('Audio data worker process finished due to KeyboardInterrupt')
|
|
448
|
+
finally:
|
|
449
|
+
stream.stop_stream()
|
|
450
|
+
stream.close()
|
|
451
|
+
audio_interface.terminate()
|
|
385
452
|
|
|
386
453
|
|
|
387
454
|
def wait_audio(self):
|
|
@@ -406,15 +473,17 @@ class AudioToTextRecorder:
|
|
|
406
473
|
self._set_state("listening")
|
|
407
474
|
self.start_recording_on_voice_activity = True
|
|
408
475
|
|
|
409
|
-
#
|
|
410
|
-
self.
|
|
476
|
+
# Wait until recording starts
|
|
477
|
+
while not self.interrupt_stop_event.is_set():
|
|
478
|
+
if (self.start_recording_event.wait(timeout=0.5)): break
|
|
411
479
|
|
|
412
480
|
# If recording is ongoing, wait for voice inactivity to finish recording.
|
|
413
481
|
if self.is_recording:
|
|
414
482
|
self.stop_recording_on_voice_deactivity = True
|
|
415
483
|
|
|
416
|
-
#
|
|
417
|
-
self.
|
|
484
|
+
# Wait until recording stops
|
|
485
|
+
while not self.interrupt_stop_event.is_set():
|
|
486
|
+
if (self.stop_recording_event.wait(timeout=0.5)): break
|
|
418
487
|
|
|
419
488
|
# Convert recorded frames to the appropriate audio format.
|
|
420
489
|
audio_array = np.frombuffer(b''.join(self.frames), dtype=np.int16)
|
|
@@ -428,8 +497,25 @@ class AudioToTextRecorder:
|
|
|
428
497
|
self._set_state("inactive")
|
|
429
498
|
|
|
430
499
|
|
|
431
|
-
|
|
432
500
|
def transcribe(self):
|
|
501
|
+
"""
|
|
502
|
+
Transcribes audio captured by this class instance using the `faster_whisper` model.
|
|
503
|
+
|
|
504
|
+
Automatically starts recording upon voice activity if not manually started using `recorder.start()`.
|
|
505
|
+
Automatically stops recording upon voice deactivity if not manually stopped with `recorder.stop()`.
|
|
506
|
+
Processes the recorded audio to generate transcription.
|
|
507
|
+
|
|
508
|
+
Args:
|
|
509
|
+
on_transcription_finished (callable, optional): Callback function to be executed when transcription is ready.
|
|
510
|
+
If provided, transcription will be performed asynchronously, and the callback will receive the transcription
|
|
511
|
+
as its argument. If omitted, the transcription will be performed synchronously, and the result will be returned.
|
|
512
|
+
|
|
513
|
+
Returns (if no callback is set):
|
|
514
|
+
str: The transcription of the recorded audio.
|
|
515
|
+
|
|
516
|
+
Raises:
|
|
517
|
+
Exception: If there is an error during the transcription process.
|
|
518
|
+
"""
|
|
433
519
|
self._set_state("transcribing")
|
|
434
520
|
self.parent_transcription_pipe.send((self.audio, self.language))
|
|
435
521
|
status, result = self.parent_transcription_pipe.recv()
|
|
@@ -463,6 +549,9 @@ class AudioToTextRecorder:
|
|
|
463
549
|
|
|
464
550
|
self.wait_audio()
|
|
465
551
|
|
|
552
|
+
if self.is_shut_down or self.interrupt_stop_event.is_set():
|
|
553
|
+
return ""
|
|
554
|
+
|
|
466
555
|
if on_transcription_finished:
|
|
467
556
|
threading.Thread(target=on_transcription_finished, args=(self.transcribe(),)).start()
|
|
468
557
|
else:
|
|
@@ -530,172 +619,44 @@ class AudioToTextRecorder:
|
|
|
530
619
|
Safely shuts down the audio recording by stopping the recording worker and closing the audio stream.
|
|
531
620
|
"""
|
|
532
621
|
|
|
533
|
-
self.parent_transcription_pipe.close()
|
|
534
|
-
self.process.terminate()
|
|
535
622
|
|
|
623
|
+
# Force wait_audio() and text() to exit
|
|
624
|
+
self.is_shut_down = True
|
|
625
|
+
self.start_recording_event.set()
|
|
626
|
+
self.stop_recording_event.set()
|
|
627
|
+
|
|
628
|
+
self.shutdown_event.set()
|
|
536
629
|
self.is_recording = False
|
|
537
630
|
self.is_running = False
|
|
538
631
|
|
|
632
|
+
logging.debug('Finishing recording thread')
|
|
539
633
|
if self.recording_thread:
|
|
540
634
|
self.recording_thread.join()
|
|
541
|
-
if self.realtime_thread:
|
|
542
|
-
self.realtime_thread.join()
|
|
543
|
-
|
|
544
|
-
try:
|
|
545
|
-
if self.stream:
|
|
546
|
-
self.stream.stop_stream()
|
|
547
|
-
self.stream.close()
|
|
548
|
-
if self.audio_interface:
|
|
549
|
-
self.audio_interface.terminate()
|
|
550
|
-
|
|
551
|
-
except Exception as e:
|
|
552
|
-
logging.error(f"Error closing the audio stream: {e}")
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
def _is_silero_speech(self, data):
|
|
556
|
-
"""
|
|
557
|
-
Returns true if speech is detected in the provided audio data
|
|
558
|
-
|
|
559
|
-
Args:
|
|
560
|
-
data (bytes): raw bytes of audio data (1024 raw bytes with 16000 sample rate and 16 bits per sample)
|
|
561
|
-
"""
|
|
562
|
-
|
|
563
|
-
self.silero_working = True
|
|
564
|
-
audio_chunk = np.frombuffer(data, dtype=np.int16)
|
|
565
|
-
audio_chunk = audio_chunk.astype(np.float32) / INT16_MAX_ABS_VALUE # Convert to float and normalize
|
|
566
|
-
vad_prob = self.silero_vad_model(torch.from_numpy(audio_chunk), SAMPLE_RATE).item()
|
|
567
|
-
is_silero_speech_active = vad_prob > (1 - self.silero_sensitivity)
|
|
568
|
-
if is_silero_speech_active:
|
|
569
|
-
self.is_silero_speech_active = True
|
|
570
|
-
self.silero_working = False
|
|
571
|
-
return is_silero_speech_active
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
def _is_webrtc_speech(self, data, all_frames_must_be_true=False):
|
|
575
|
-
"""
|
|
576
|
-
Returns true if speech is detected in the provided audio data
|
|
577
635
|
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
# Number of audio frames per millisecond
|
|
582
|
-
frame_length = int(self.sample_rate * 0.01) # for 10ms frame
|
|
583
|
-
num_frames = int(len(data) / (2 * frame_length))
|
|
584
|
-
speech_frames = 0
|
|
636
|
+
logging.debug('Terminating reader process')
|
|
637
|
+
# Give it some time to finish the loop and cleanup.
|
|
638
|
+
self.reader_process.join(timeout=10)
|
|
585
639
|
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
frame = data[start_byte:end_byte]
|
|
590
|
-
if self.webrtc_vad_model.is_speech(frame, self.sample_rate):
|
|
591
|
-
speech_frames += 1
|
|
592
|
-
if not all_frames_must_be_true:
|
|
593
|
-
return True
|
|
594
|
-
if all_frames_must_be_true:
|
|
595
|
-
return speech_frames == num_frames
|
|
596
|
-
else:
|
|
597
|
-
return False
|
|
598
|
-
|
|
640
|
+
if self.reader_process.is_alive():
|
|
641
|
+
logging.warning("Reader process did not terminate in time. Terminating forcefully.")
|
|
642
|
+
self.reader_process.terminate()
|
|
599
643
|
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
Initiate check if voice is active based on the provided data.
|
|
644
|
+
logging.debug('Terminating transcription process')
|
|
645
|
+
self.transcript_process.join(timeout=10)
|
|
603
646
|
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
self.is_webrtc_speech_active = self._is_webrtc_speech(data)
|
|
608
|
-
|
|
609
|
-
# First quick performing check for voice activity using WebRTC
|
|
610
|
-
if self.is_webrtc_speech_active:
|
|
611
|
-
|
|
612
|
-
if not self.silero_working:
|
|
613
|
-
self.silero_working = True
|
|
614
|
-
|
|
615
|
-
# Run the intensive check in a separate thread
|
|
616
|
-
threading.Thread(target=self._is_silero_speech, args=(data,)).start()
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
def _is_voice_active(self):
|
|
620
|
-
"""
|
|
621
|
-
Determine if voice is active.
|
|
647
|
+
if self.transcript_process.is_alive():
|
|
648
|
+
logging.warning("Transcript process did not terminate in time. Terminating forcefully.")
|
|
649
|
+
self.transcript_process.terminate()
|
|
622
650
|
|
|
623
|
-
|
|
624
|
-
bool: True if voice is active, False otherwise.
|
|
625
|
-
"""
|
|
626
|
-
return self.is_webrtc_speech_active and self.is_silero_speech_active
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
def _set_state(self, new_state):
|
|
630
|
-
"""
|
|
631
|
-
Update the current state of the recorder and execute corresponding state-change callbacks.
|
|
632
|
-
|
|
633
|
-
Args:
|
|
634
|
-
new_state (str): The new state to set.
|
|
635
|
-
|
|
636
|
-
"""
|
|
637
|
-
# Check if the state has actually changed
|
|
638
|
-
if new_state == self.state:
|
|
639
|
-
return
|
|
640
|
-
|
|
641
|
-
# Store the current state for later comparison
|
|
642
|
-
old_state = self.state
|
|
643
|
-
|
|
644
|
-
# Update to the new state
|
|
645
|
-
self.state = new_state
|
|
651
|
+
self.parent_transcription_pipe.close()
|
|
646
652
|
|
|
647
|
-
|
|
648
|
-
if
|
|
649
|
-
|
|
650
|
-
self.on_vad_detect_stop()
|
|
651
|
-
elif old_state == "wakeword":
|
|
652
|
-
if self.on_wakeword_detection_end:
|
|
653
|
-
self.on_wakeword_detection_end()
|
|
653
|
+
logging.debug('Finishing realtime thread')
|
|
654
|
+
if self.realtime_thread:
|
|
655
|
+
self.realtime_thread.join()
|
|
654
656
|
|
|
655
|
-
# Execute callbacks based on transitioning TO a particular state
|
|
656
|
-
if new_state == "listening":
|
|
657
|
-
if self.on_vad_detect_start:
|
|
658
|
-
self.on_vad_detect_start()
|
|
659
|
-
self._set_spinner("speak now")
|
|
660
|
-
if self.spinner:
|
|
661
|
-
self.halo._interval = 250
|
|
662
|
-
elif new_state == "wakeword":
|
|
663
|
-
if self.on_wakeword_detection_start:
|
|
664
|
-
self.on_wakeword_detection_start()
|
|
665
|
-
self._set_spinner(f"say {self.wake_words}")
|
|
666
|
-
if self.spinner:
|
|
667
|
-
self.halo._interval = 500
|
|
668
|
-
elif new_state == "transcribing":
|
|
669
|
-
if self.on_transcription_start:
|
|
670
|
-
self.on_transcription_start()
|
|
671
|
-
self._set_spinner("transcribing")
|
|
672
|
-
if self.spinner:
|
|
673
|
-
self.halo._interval = 50
|
|
674
|
-
elif new_state == "recording":
|
|
675
|
-
self._set_spinner("recording")
|
|
676
|
-
if self.spinner:
|
|
677
|
-
self.halo._interval = 100
|
|
678
|
-
elif new_state == "inactive":
|
|
679
|
-
if self.spinner and self.halo:
|
|
680
|
-
self.halo.stop()
|
|
681
|
-
self.halo = None
|
|
682
657
|
|
|
683
658
|
|
|
684
|
-
def _set_spinner(self, text):
|
|
685
|
-
"""
|
|
686
|
-
Update the spinner's text or create a new spinner with the provided text.
|
|
687
659
|
|
|
688
|
-
Args:
|
|
689
|
-
text (str): The text to be displayed alongside the spinner.
|
|
690
|
-
"""
|
|
691
|
-
if self.spinner:
|
|
692
|
-
# If the Halo spinner doesn't exist, create and start it
|
|
693
|
-
if self.halo is None:
|
|
694
|
-
self.halo = halo.Halo(text=text)
|
|
695
|
-
self.halo.start()
|
|
696
|
-
# If the Halo spinner already exists, just update the text
|
|
697
|
-
else:
|
|
698
|
-
self.halo.text = text
|
|
699
660
|
|
|
700
661
|
|
|
701
662
|
def _recording_worker(self):
|
|
@@ -712,15 +673,30 @@ class AudioToTextRecorder:
|
|
|
712
673
|
# Continuously monitor audio for voice activity
|
|
713
674
|
while self.is_running:
|
|
714
675
|
|
|
715
|
-
|
|
676
|
+
try:
|
|
677
|
+
|
|
678
|
+
data = self.audio_queue.get()
|
|
679
|
+
|
|
680
|
+
# Handle queue overflow
|
|
681
|
+
queue_overflow_logged = False
|
|
682
|
+
while self.audio_queue.qsize() > self.allowed_latency_limit:
|
|
683
|
+
if not queue_overflow_logged:
|
|
684
|
+
logging.warning(f"Audio queue size exceeds latency limit. Current size: {self.audio_queue.qsize()}. Discarding old audio chunks.")
|
|
685
|
+
queue_overflow_logged = True
|
|
686
|
+
data = self.audio_queue.get()
|
|
687
|
+
|
|
688
|
+
except BrokenPipeError:
|
|
689
|
+
print ("BrokenPipeError _recording_worker")
|
|
690
|
+
self.is_running = False
|
|
691
|
+
break
|
|
716
692
|
|
|
717
693
|
if not self.is_recording:
|
|
718
|
-
#
|
|
694
|
+
# Handle not recording state
|
|
719
695
|
|
|
720
696
|
time_since_listen_start = time.time() - self.listen_start if self.listen_start else 0
|
|
721
697
|
wake_word_activation_delay_passed = (time_since_listen_start > self.wake_word_activation_delay)
|
|
722
698
|
|
|
723
|
-
#
|
|
699
|
+
# Handle wake-word timeout callback
|
|
724
700
|
if wake_word_activation_delay_passed and not delay_was_passed:
|
|
725
701
|
if self.wake_words and self.wake_word_activation_delay:
|
|
726
702
|
if self.on_wakeword_timeout:
|
|
@@ -808,6 +784,7 @@ class AudioToTextRecorder:
|
|
|
808
784
|
logging.info("voice deactivity detected")
|
|
809
785
|
self.stop()
|
|
810
786
|
|
|
787
|
+
|
|
811
788
|
if not self.is_recording and was_recording:
|
|
812
789
|
# Reset after stopping recording to ensure clean state
|
|
813
790
|
self.stop_recording_on_voice_deactivity = False
|
|
@@ -815,98 +792,28 @@ class AudioToTextRecorder:
|
|
|
815
792
|
if time.time() - self.silero_check_time > 0.1:
|
|
816
793
|
self.silero_check_time = 0
|
|
817
794
|
|
|
818
|
-
#
|
|
795
|
+
# Handle wake word timeout (waited to long initiating speech after wake word detection)
|
|
819
796
|
if self.wake_word_detect_time and time.time() - self.wake_word_detect_time > self.wake_word_timeout:
|
|
820
797
|
self.wake_word_detect_time = 0
|
|
821
798
|
if self.wakeword_detected and self.on_wakeword_timeout:
|
|
822
799
|
self.on_wakeword_timeout()
|
|
823
800
|
self.wakeword_detected = False
|
|
824
801
|
|
|
802
|
+
was_recording = self.is_recording
|
|
803
|
+
|
|
804
|
+
|
|
825
805
|
if self.is_recording:
|
|
826
806
|
self.frames.append(data)
|
|
827
807
|
|
|
828
808
|
if not self.is_recording or self.speech_end_silence_start:
|
|
829
809
|
self.audio_buffer.append(data)
|
|
830
810
|
|
|
831
|
-
was_recording = self.is_recording
|
|
832
811
|
|
|
833
812
|
except Exception as e:
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
def _preprocess_output(self, text, preview=False):
|
|
839
|
-
"""
|
|
840
|
-
Preprocesses the output text by removing any leading or trailing whitespace,
|
|
841
|
-
converting all whitespace sequences to a single space character, and capitalizing
|
|
842
|
-
the first character of the text.
|
|
843
|
-
|
|
844
|
-
Args:
|
|
845
|
-
text (str): The text to be preprocessed.
|
|
846
|
-
|
|
847
|
-
Returns:
|
|
848
|
-
str: The preprocessed text.
|
|
849
|
-
"""
|
|
850
|
-
text = re.sub(r'\s+', ' ', text.strip())
|
|
851
|
-
|
|
852
|
-
if self.ensure_sentence_starting_uppercase:
|
|
853
|
-
if text:
|
|
854
|
-
text = text[0].upper() + text[1:]
|
|
855
|
-
|
|
856
|
-
# Ensure the text ends with a proper punctuation if it ends with an alphanumeric character
|
|
857
|
-
if not preview:
|
|
858
|
-
if self.ensure_sentence_ends_with_period:
|
|
859
|
-
if text and text[-1].isalnum():
|
|
860
|
-
text += '.'
|
|
861
|
-
|
|
862
|
-
return text
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
def find_tail_match_in_text(self, text1, text2, length_of_match=10):
|
|
866
|
-
"""
|
|
867
|
-
Find the position where the last 'n' characters of text1 match with a substring in text2.
|
|
868
|
-
|
|
869
|
-
This method takes two texts, extracts the last 'n' characters from text1 (where 'n' is determined
|
|
870
|
-
by the variable 'length_of_match'), and searches for an occurrence of this substring in text2,
|
|
871
|
-
starting from the end of text2 and moving towards the beginning.
|
|
872
|
-
|
|
873
|
-
Parameters:
|
|
874
|
-
- text1 (str): The text containing the substring that we want to find in text2.
|
|
875
|
-
- text2 (str): The text in which we want to find the matching substring.
|
|
876
|
-
- length_of_match(int): The length of the matching string that we are looking for
|
|
877
|
-
|
|
878
|
-
Returns:
|
|
879
|
-
int: The position (0-based index) in text2 where the matching substring starts.
|
|
880
|
-
If no match is found or either of the texts is too short, returns -1.
|
|
881
|
-
"""
|
|
882
|
-
|
|
883
|
-
# Check if either of the texts is too short
|
|
884
|
-
if len(text1) < length_of_match or len(text2) < length_of_match:
|
|
885
|
-
return -1
|
|
886
|
-
|
|
887
|
-
# The end portion of the first text that we want to compare
|
|
888
|
-
target_substring = text1[-length_of_match:]
|
|
889
|
-
|
|
890
|
-
# Loop through text2 from right to left
|
|
891
|
-
for i in range(len(text2) - length_of_match + 1):
|
|
892
|
-
# Extract the substring from text2 to compare with the target_substring
|
|
893
|
-
current_substring = text2[len(text2) - i - length_of_match:len(text2) - i]
|
|
894
|
-
|
|
895
|
-
# Compare the current_substring with the target_substring
|
|
896
|
-
if current_substring == target_substring:
|
|
897
|
-
return len(text2) - i # Position in text2 where the match starts
|
|
898
|
-
|
|
899
|
-
return -1
|
|
900
|
-
|
|
901
|
-
def _on_realtime_transcription_stabilized(self, text):
|
|
902
|
-
if self.on_realtime_transcription_stabilized:
|
|
903
|
-
if self.is_recording:
|
|
904
|
-
self.on_realtime_transcription_stabilized(text)
|
|
813
|
+
if not self.interrupt_stop_event.is_set():
|
|
814
|
+
logging.error(f"Unhandled exeption in _recording_worker: {e}")
|
|
815
|
+
raise
|
|
905
816
|
|
|
906
|
-
def _on_realtime_transcription_update(self, text):
|
|
907
|
-
if self.on_realtime_transcription_update:
|
|
908
|
-
if self.is_recording:
|
|
909
|
-
self.on_realtime_transcription_update(text)
|
|
910
817
|
|
|
911
818
|
def _realtime_worker(self):
|
|
912
819
|
"""
|
|
@@ -969,7 +876,7 @@ class AudioToTextRecorder:
|
|
|
969
876
|
self.realtime_stabilized_safetext = prefix
|
|
970
877
|
|
|
971
878
|
# Find parts of the stabilized text in the freshly transscripted text
|
|
972
|
-
matching_position = self.
|
|
879
|
+
matching_position = self._find_tail_match_in_text(self.realtime_stabilized_safetext, self.realtime_transcription_text)
|
|
973
880
|
if matching_position < 0:
|
|
974
881
|
if self.realtime_stabilized_safetext:
|
|
975
882
|
self._on_realtime_transcription_stabilized(self._preprocess_output(self.realtime_stabilized_safetext, True))
|
|
@@ -996,8 +903,278 @@ class AudioToTextRecorder:
|
|
|
996
903
|
logging.error(f"Unhandled exeption in _realtime_worker: {e}")
|
|
997
904
|
raise
|
|
998
905
|
|
|
999
|
-
|
|
906
|
+
|
|
907
|
+
def _is_silero_speech(self, data):
|
|
908
|
+
"""
|
|
909
|
+
Returns true if speech is detected in the provided audio data
|
|
910
|
+
|
|
911
|
+
Args:
|
|
912
|
+
data (bytes): raw bytes of audio data (1024 raw bytes with 16000 sample rate and 16 bits per sample)
|
|
913
|
+
"""
|
|
914
|
+
|
|
915
|
+
self.silero_working = True
|
|
916
|
+
audio_chunk = np.frombuffer(data, dtype=np.int16)
|
|
917
|
+
audio_chunk = audio_chunk.astype(np.float32) / INT16_MAX_ABS_VALUE # Convert to float and normalize
|
|
918
|
+
vad_prob = self.silero_vad_model(torch.from_numpy(audio_chunk), SAMPLE_RATE).item()
|
|
919
|
+
is_silero_speech_active = vad_prob > (1 - self.silero_sensitivity)
|
|
920
|
+
if is_silero_speech_active:
|
|
921
|
+
self.is_silero_speech_active = True
|
|
922
|
+
self.silero_working = False
|
|
923
|
+
return is_silero_speech_active
|
|
924
|
+
|
|
925
|
+
|
|
926
|
+
def _is_webrtc_speech(self, data, all_frames_must_be_true=False):
|
|
927
|
+
"""
|
|
928
|
+
Returns true if speech is detected in the provided audio data
|
|
929
|
+
|
|
930
|
+
Args:
|
|
931
|
+
data (bytes): raw bytes of audio data (1024 raw bytes with 16000 sample rate and 16 bits per sample)
|
|
932
|
+
"""
|
|
933
|
+
# Number of audio frames per millisecond
|
|
934
|
+
frame_length = int(self.sample_rate * 0.01) # for 10ms frame
|
|
935
|
+
num_frames = int(len(data) / (2 * frame_length))
|
|
936
|
+
speech_frames = 0
|
|
937
|
+
|
|
938
|
+
for i in range(num_frames):
|
|
939
|
+
start_byte = i * frame_length * 2
|
|
940
|
+
end_byte = start_byte + frame_length * 2
|
|
941
|
+
frame = data[start_byte:end_byte]
|
|
942
|
+
if self.webrtc_vad_model.is_speech(frame, self.sample_rate):
|
|
943
|
+
speech_frames += 1
|
|
944
|
+
if not all_frames_must_be_true:
|
|
945
|
+
return True
|
|
946
|
+
if all_frames_must_be_true:
|
|
947
|
+
return speech_frames == num_frames
|
|
948
|
+
else:
|
|
949
|
+
return False
|
|
950
|
+
|
|
951
|
+
|
|
952
|
+
def _check_voice_activity(self, data):
|
|
953
|
+
"""
|
|
954
|
+
Initiate check if voice is active based on the provided data.
|
|
955
|
+
|
|
956
|
+
Args:
|
|
957
|
+
data: The audio data to be checked for voice activity.
|
|
958
|
+
"""
|
|
959
|
+
self.is_webrtc_speech_active = self._is_webrtc_speech(data)
|
|
960
|
+
|
|
961
|
+
# First quick performing check for voice activity using WebRTC
|
|
962
|
+
if self.is_webrtc_speech_active:
|
|
963
|
+
|
|
964
|
+
if not self.silero_working:
|
|
965
|
+
self.silero_working = True
|
|
966
|
+
|
|
967
|
+
# Run the intensive check in a separate thread
|
|
968
|
+
threading.Thread(target=self._is_silero_speech, args=(data,)).start()
|
|
969
|
+
|
|
970
|
+
|
|
971
|
+
def _is_voice_active(self):
|
|
972
|
+
"""
|
|
973
|
+
Determine if voice is active.
|
|
974
|
+
|
|
975
|
+
Returns:
|
|
976
|
+
bool: True if voice is active, False otherwise.
|
|
977
|
+
"""
|
|
978
|
+
return self.is_webrtc_speech_active and self.is_silero_speech_active
|
|
979
|
+
|
|
980
|
+
|
|
981
|
+
def _set_state(self, new_state):
|
|
982
|
+
"""
|
|
983
|
+
Update the current state of the recorder and execute corresponding state-change callbacks.
|
|
984
|
+
|
|
985
|
+
Args:
|
|
986
|
+
new_state (str): The new state to set.
|
|
987
|
+
|
|
988
|
+
"""
|
|
989
|
+
# Check if the state has actually changed
|
|
990
|
+
if new_state == self.state:
|
|
991
|
+
return
|
|
992
|
+
|
|
993
|
+
# Store the current state for later comparison
|
|
994
|
+
old_state = self.state
|
|
995
|
+
|
|
996
|
+
# Update to the new state
|
|
997
|
+
self.state = new_state
|
|
998
|
+
|
|
999
|
+
# Execute callbacks based on transitioning FROM a particular state
|
|
1000
|
+
if old_state == "listening":
|
|
1001
|
+
if self.on_vad_detect_stop:
|
|
1002
|
+
self.on_vad_detect_stop()
|
|
1003
|
+
elif old_state == "wakeword":
|
|
1004
|
+
if self.on_wakeword_detection_end:
|
|
1005
|
+
self.on_wakeword_detection_end()
|
|
1006
|
+
|
|
1007
|
+
# Execute callbacks based on transitioning TO a particular state
|
|
1008
|
+
if new_state == "listening":
|
|
1009
|
+
if self.on_vad_detect_start:
|
|
1010
|
+
self.on_vad_detect_start()
|
|
1011
|
+
self._set_spinner("speak now")
|
|
1012
|
+
if self.spinner and self.halo:
|
|
1013
|
+
self.halo._interval = 250
|
|
1014
|
+
elif new_state == "wakeword":
|
|
1015
|
+
if self.on_wakeword_detection_start:
|
|
1016
|
+
self.on_wakeword_detection_start()
|
|
1017
|
+
self._set_spinner(f"say {self.wake_words}")
|
|
1018
|
+
if self.spinner and self.halo:
|
|
1019
|
+
self.halo._interval = 500
|
|
1020
|
+
elif new_state == "transcribing":
|
|
1021
|
+
if self.on_transcription_start:
|
|
1022
|
+
self.on_transcription_start()
|
|
1023
|
+
self._set_spinner("transcribing")
|
|
1024
|
+
if self.spinner and self.halo:
|
|
1025
|
+
self.halo._interval = 50
|
|
1026
|
+
elif new_state == "recording":
|
|
1027
|
+
self._set_spinner("recording")
|
|
1028
|
+
if self.spinner and self.halo:
|
|
1029
|
+
self.halo._interval = 100
|
|
1030
|
+
elif new_state == "inactive":
|
|
1031
|
+
if self.spinner and self.halo:
|
|
1032
|
+
self.halo.stop()
|
|
1033
|
+
self.halo = None
|
|
1034
|
+
|
|
1035
|
+
|
|
1036
|
+
def _set_spinner(self, text):
|
|
1037
|
+
"""
|
|
1038
|
+
Update the spinner's text or create a new spinner with the provided text.
|
|
1039
|
+
|
|
1040
|
+
Args:
|
|
1041
|
+
text (str): The text to be displayed alongside the spinner.
|
|
1042
|
+
"""
|
|
1043
|
+
if self.spinner:
|
|
1044
|
+
# If the Halo spinner doesn't exist, create and start it
|
|
1045
|
+
if self.halo is None:
|
|
1046
|
+
self.halo = halo.Halo(text=text)
|
|
1047
|
+
self.halo.start()
|
|
1048
|
+
# If the Halo spinner already exists, just update the text
|
|
1049
|
+
else:
|
|
1050
|
+
self.halo.text = text
|
|
1051
|
+
|
|
1052
|
+
|
|
1053
|
+
def _preprocess_output(self, text, preview=False):
|
|
1054
|
+
"""
|
|
1055
|
+
Preprocesses the output text by removing any leading or trailing whitespace,
|
|
1056
|
+
converting all whitespace sequences to a single space character, and capitalizing
|
|
1057
|
+
the first character of the text.
|
|
1058
|
+
|
|
1059
|
+
Args:
|
|
1060
|
+
text (str): The text to be preprocessed.
|
|
1061
|
+
|
|
1062
|
+
Returns:
|
|
1063
|
+
str: The preprocessed text.
|
|
1064
|
+
"""
|
|
1065
|
+
text = re.sub(r'\s+', ' ', text.strip())
|
|
1066
|
+
|
|
1067
|
+
if self.ensure_sentence_starting_uppercase:
|
|
1068
|
+
if text:
|
|
1069
|
+
text = text[0].upper() + text[1:]
|
|
1070
|
+
|
|
1071
|
+
# Ensure the text ends with a proper punctuation if it ends with an alphanumeric character
|
|
1072
|
+
if not preview:
|
|
1073
|
+
if self.ensure_sentence_ends_with_period:
|
|
1074
|
+
if text and text[-1].isalnum():
|
|
1075
|
+
text += '.'
|
|
1076
|
+
|
|
1077
|
+
return text
|
|
1078
|
+
|
|
1079
|
+
|
|
1080
|
+
def _find_tail_match_in_text(self, text1, text2, length_of_match=10):
|
|
1081
|
+
"""
|
|
1082
|
+
Find the position where the last 'n' characters of text1 match with a substring in text2.
|
|
1083
|
+
|
|
1084
|
+
This method takes two texts, extracts the last 'n' characters from text1 (where 'n' is determined
|
|
1085
|
+
by the variable 'length_of_match'), and searches for an occurrence of this substring in text2,
|
|
1086
|
+
starting from the end of text2 and moving towards the beginning.
|
|
1087
|
+
|
|
1088
|
+
Parameters:
|
|
1089
|
+
- text1 (str): The text containing the substring that we want to find in text2.
|
|
1090
|
+
- text2 (str): The text in which we want to find the matching substring.
|
|
1091
|
+
- length_of_match(int): The length of the matching string that we are looking for
|
|
1092
|
+
|
|
1093
|
+
Returns:
|
|
1094
|
+
int: The position (0-based index) in text2 where the matching substring starts.
|
|
1095
|
+
If no match is found or either of the texts is too short, returns -1.
|
|
1000
1096
|
"""
|
|
1001
|
-
|
|
1097
|
+
|
|
1098
|
+
# Check if either of the texts is too short
|
|
1099
|
+
if len(text1) < length_of_match or len(text2) < length_of_match:
|
|
1100
|
+
return -1
|
|
1101
|
+
|
|
1102
|
+
# The end portion of the first text that we want to compare
|
|
1103
|
+
target_substring = text1[-length_of_match:]
|
|
1104
|
+
|
|
1105
|
+
# Loop through text2 from right to left
|
|
1106
|
+
for i in range(len(text2) - length_of_match + 1):
|
|
1107
|
+
# Extract the substring from text2 to compare with the target_substring
|
|
1108
|
+
current_substring = text2[len(text2) - i - length_of_match:len(text2) - i]
|
|
1109
|
+
|
|
1110
|
+
# Compare the current_substring with the target_substring
|
|
1111
|
+
if current_substring == target_substring:
|
|
1112
|
+
return len(text2) - i # Position in text2 where the match starts
|
|
1113
|
+
|
|
1114
|
+
return -1
|
|
1115
|
+
|
|
1116
|
+
|
|
1117
|
+
def _on_realtime_transcription_stabilized(self, text):
|
|
1118
|
+
"""
|
|
1119
|
+
Callback method invoked when the real-time transcription stabilizes.
|
|
1120
|
+
|
|
1121
|
+
This method is called internally when the transcription text is considered "stable"
|
|
1122
|
+
meaning it's less likely to change significantly with additional audio input. It
|
|
1123
|
+
notifies any registered external listener about the stabilized text if recording is
|
|
1124
|
+
still ongoing. This is particularly useful for applications that need to display
|
|
1125
|
+
live transcription results to users and want to highlight parts of the transcription
|
|
1126
|
+
that are less likely to change.
|
|
1127
|
+
|
|
1128
|
+
Args:
|
|
1129
|
+
text (str): The stabilized transcription text.
|
|
1130
|
+
"""
|
|
1131
|
+
if self.on_realtime_transcription_stabilized:
|
|
1132
|
+
if self.is_recording:
|
|
1133
|
+
self.on_realtime_transcription_stabilized(text)
|
|
1134
|
+
|
|
1135
|
+
|
|
1136
|
+
def _on_realtime_transcription_update(self, text):
|
|
1137
|
+
"""
|
|
1138
|
+
Callback method invoked when there's an update in the real-time transcription.
|
|
1139
|
+
|
|
1140
|
+
This method is called internally whenever there's a change in the transcription text,
|
|
1141
|
+
notifying any registered external listener about the update if recording is still
|
|
1142
|
+
ongoing. This provides a mechanism for applications to receive and possibly display
|
|
1143
|
+
live transcription updates, which could be partial and still subject to change.
|
|
1144
|
+
|
|
1145
|
+
Args:
|
|
1146
|
+
text (str): The updated transcription text.
|
|
1147
|
+
"""
|
|
1148
|
+
if self.on_realtime_transcription_update:
|
|
1149
|
+
if self.is_recording:
|
|
1150
|
+
self.on_realtime_transcription_update(text)
|
|
1151
|
+
|
|
1152
|
+
|
|
1153
|
+
def __enter__(self):
|
|
1154
|
+
"""
|
|
1155
|
+
Method to setup the context manager protocol.
|
|
1156
|
+
|
|
1157
|
+
This enables the instance to be used in a `with` statement, ensuring proper
|
|
1158
|
+
resource management. When the `with` block is entered, this method is
|
|
1159
|
+
automatically called.
|
|
1160
|
+
|
|
1161
|
+
Returns:
|
|
1162
|
+
self: The current instance of the class.
|
|
1163
|
+
"""
|
|
1164
|
+
return self
|
|
1165
|
+
|
|
1166
|
+
|
|
1167
|
+
def __exit__(self, exc_type, exc_value, traceback):
|
|
1168
|
+
"""
|
|
1169
|
+
Method to define behavior when the context manager protocol exits.
|
|
1170
|
+
|
|
1171
|
+
This is called when exiting the `with` block and ensures that any necessary
|
|
1172
|
+
cleanup or resource release processes are executed, such as shutting down
|
|
1173
|
+
the system properly.
|
|
1174
|
+
|
|
1175
|
+
Args:
|
|
1176
|
+
exc_type (Exception or None): The type of the exception that caused the context to be exited, if any.
|
|
1177
|
+
exc_value (Exception or None): The exception instance that caused the context to be exited, if any.
|
|
1178
|
+
traceback (Traceback or None): The traceback corresponding to the exception, if any.
|
|
1002
1179
|
"""
|
|
1003
1180
|
self.shutdown()
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|