-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreal_time_processor.py
More file actions
431 lines (337 loc) · 16.9 KB
/
real_time_processor.py
File metadata and controls
431 lines (337 loc) · 16.9 KB
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#!/usr/bin/env python3
"""
Real-Time Processor - Unique Feature for Live Audio Transcription
Handles real-time audio processing, live transcription, and streaming capabilities
"""
import threading
import queue
import time
import numpy as np
from typing import Dict, Any, Optional, Callable
import logging
# Audio libraries
import pyaudio
import whisper
import webrtcvad
from scipy import signal
logger = logging.getLogger(__name__)
class RealTimeProcessor:
"""Real-time audio processing and transcription system"""
def __init__(self, config: Dict[str, Any]):
self.config = config
self.sample_rate = config['processing']['audio_sample_rate']
self.whisper_model_name = config['models']['whisper_model']
# Audio streaming parameters
self.chunk_duration = 1.0 # seconds
self.chunk_size = int(self.sample_rate * self.chunk_duration)
self.overlap_duration = 0.3 # seconds overlap for continuity
self.overlap_size = int(self.sample_rate * self.overlap_duration)
# Initialize components
self.whisper_model = whisper.load_model(self.whisper_model_name)
self.vad = webrtcvad.Vad(2) # Aggressiveness level 0-3
# Streaming state
self.is_streaming = False
self.audio_queue = queue.Queue()
self.transcription_queue = queue.Queue()
self.audio_buffer = np.array([], dtype=np.float32)
# Callbacks
self.transcription_callback: Optional[Callable] = None
self.voice_activity_callback: Optional[Callable] = None
def start_streaming(self,
transcription_callback: Callable[[str, Dict], None] = None,
voice_activity_callback: Callable[[bool], None] = None):
"""Start real-time audio streaming and transcription"""
self.transcription_callback = transcription_callback
self.voice_activity_callback = voice_activity_callback
if self.is_streaming:
logger.warning("Streaming already active")
return
self.is_streaming = True
# Start audio capture thread
self.audio_thread = threading.Thread(target=self._audio_capture_loop)
self.audio_thread.daemon = True
self.audio_thread.start()
# Start transcription thread
self.transcription_thread = threading.Thread(target=self._transcription_loop)
self.transcription_thread.daemon = True
self.transcription_thread.start()
logger.info("Real-time processing started")
def stop_streaming(self):
"""Stop real-time processing"""
self.is_streaming = False
if hasattr(self, 'audio_thread'):
self.audio_thread.join(timeout=2.0)
if hasattr(self, 'transcription_thread'):
self.transcription_thread.join(timeout=2.0)
logger.info("Real-time processing stopped")
def _audio_capture_loop(self):
"""Capture audio from microphone in real-time"""
try:
# Initialize PyAudio
audio = pyaudio.PyAudio()
# Open audio stream
stream = audio.open(
format=pyaudio.paFloat32,
channels=1,
rate=self.sample_rate,
input=True,
frames_per_buffer=self.chunk_size,
stream_callback=None
)
logger.info("Audio capture started")
while self.is_streaming:
# Read audio chunk
try:
audio_data = stream.read(
self.chunk_size,
exception_on_overflow=False
)
# Convert to numpy array
audio_chunk = np.frombuffer(audio_data, dtype=np.float32)
# Add to processing queue
self.audio_queue.put(audio_chunk)
except Exception as e:
logger.error(f"Audio capture error: {e}")
continue
except Exception as e:
logger.error(f"Audio stream initialization failed: {e}")
finally:
try:
stream.stop_stream()
stream.close()
audio.terminate()
except:
pass
def _transcription_loop(self):
"""Process audio chunks for transcription"""
while self.is_streaming:
try:
# Get audio chunk with timeout
try:
audio_chunk = self.audio_queue.get(timeout=1.0)
except queue.Empty:
continue
# Add to buffer with overlap handling
self.audio_buffer = np.concatenate([self.audio_buffer, audio_chunk])
# Process if buffer is large enough
if len(self.audio_buffer) >= self.chunk_size + self.overlap_size:
# Extract processing chunk
process_chunk = self.audio_buffer[:self.chunk_size + self.overlap_size]
# Keep overlap for next iteration
self.audio_buffer = self.audio_buffer[self.chunk_size:]
# Voice Activity Detection
has_voice = self._detect_voice_activity(process_chunk)
if self.voice_activity_callback:
self.voice_activity_callback(has_voice)
# Transcribe if voice is detected
if has_voice:
transcription = self._transcribe_chunk(process_chunk)
if transcription['text'].strip() and self.transcription_callback:
# Add timing information
transcription['timestamp'] = time.time()
transcription['chunk_duration'] = len(process_chunk) / self.sample_rate
self.transcription_callback(transcription['text'], transcription)
except Exception as e:
logger.error(f"Transcription loop error: {e}")
continue
def _detect_voice_activity(self, audio_chunk: np.ndarray) -> bool:
"""Detect voice activity in audio chunk"""
try:
# Convert to 16-bit PCM for WebRTC VAD
audio_pcm = (audio_chunk * 32767).astype(np.int16)
# WebRTC VAD requires specific frame sizes (10, 20, or 30 ms)
frame_duration = 30 # ms
frame_size = int(self.sample_rate * frame_duration / 1000)
voice_frames = 0
total_frames = 0
# Process in frames
for i in range(0, len(audio_pcm) - frame_size, frame_size):
frame = audio_pcm[i:i + frame_size].tobytes()
try:
is_speech = self.vad.is_speech(frame, self.sample_rate)
if is_speech:
voice_frames += 1
total_frames += 1
except:
continue
# Return True if significant portion contains voice
if total_frames > 0:
voice_ratio = voice_frames / total_frames
return voice_ratio > 0.3
except Exception as e:
logger.error(f"Voice activity detection failed: {e}")
# Fallback to energy-based detection
return self._energy_based_vad(audio_chunk)
def _energy_based_vad(self, audio_chunk: np.ndarray) -> bool:
"""Simple energy-based voice activity detection"""
try:
# Calculate RMS energy
rms_energy = np.sqrt(np.mean(audio_chunk**2))
# Dynamic threshold based on recent history
# This is a simplified implementation
energy_threshold = 0.01 # Adjust based on testing
return rms_energy > energy_threshold
except Exception as e:
logger.error(f"Energy-based VAD failed: {e}")
return False
def _transcribe_chunk(self, audio_chunk: np.ndarray) -> Dict[str, Any]:
"""Transcribe audio chunk using Whisper"""
try:
# Ensure audio is normalized
if np.max(np.abs(audio_chunk)) > 1.0:
audio_chunk = audio_chunk / np.max(np.abs(audio_chunk))
# Transcribe with Whisper
result = self.whisper_model.transcribe(
audio_chunk,
language=None, # Auto-detect
task="transcribe",
fp16=False # Use fp32 for better compatibility
)
return result
except Exception as e:
logger.error(f"Chunk transcription failed: {e}")
return {"text": "", "language": "unknown"}
def get_stream_statistics(self) -> Dict[str, Any]:
"""Get real-time processing statistics"""
return {
'is_streaming': self.is_streaming,
'audio_queue_size': self.audio_queue.qsize(),
'transcription_queue_size': self.transcription_queue.qsize(),
'buffer_length_seconds': len(self.audio_buffer) / self.sample_rate,
'sample_rate': self.sample_rate,
'chunk_duration': self.chunk_duration,
'overlap_duration': self.overlap_duration
}
def configure_sensitivity(self,
vad_aggressiveness: int = 2,
energy_threshold: float = 0.01):
"""Configure voice activity detection sensitivity"""
try:
# Update VAD aggressiveness (0-3, higher = more aggressive)
self.vad = webrtcvad.Vad(vad_aggressiveness)
# Store energy threshold for fallback VAD
self.energy_threshold = energy_threshold
logger.info(f"VAD sensitivity updated: aggressiveness={vad_aggressiveness}")
except Exception as e:
logger.error(f"Failed to configure sensitivity: {e}")
def process_audio_file_realtime(self,
file_path: str,
playback_speed: float = 1.0):
"""Simulate real-time processing of an audio file"""
try:
import librosa
# Load audio file
audio_data, sr = librosa.load(file_path, sr=self.sample_rate)
# Calculate chunk timing
chunk_samples = int(self.sample_rate * self.chunk_duration)
delay_between_chunks = self.chunk_duration / playback_speed
logger.info(f"Starting real-time simulation of {file_path}")
# Process in chunks with realistic timing
for i in range(0, len(audio_data), chunk_samples):
if not self.is_streaming:
break
chunk = audio_data[i:i + chunk_samples]
# Pad if chunk is too short
if len(chunk) < chunk_samples:
chunk = np.pad(chunk, (0, chunk_samples - len(chunk)))
# Voice activity detection
has_voice = self._detect_voice_activity(chunk)
if self.voice_activity_callback:
self.voice_activity_callback(has_voice)
# Transcribe if voice detected
if has_voice:
transcription = self._transcribe_chunk(chunk)
if transcription['text'].strip() and self.transcription_callback:
transcription['timestamp'] = time.time()
transcription['file_position'] = i / sr
transcription['chunk_duration'] = len(chunk) / self.sample_rate
self.transcription_callback(transcription['text'], transcription)
# Wait for next chunk (simulate real-time)
time.sleep(delay_between_chunks)
except Exception as e:
logger.error(f"Real-time file processing failed: {e}")
class LiveTranscriptionManager:
"""Manages live transcription sessions with text smoothing and correction"""
def __init__(self, config: Dict[str, Any]):
self.config = config
self.current_session = None
self.session_history = []
def start_session(self, session_name: str = None) -> str:
"""Start a new live transcription session"""
session_id = session_name or f"session_{int(time.time())}"
self.current_session = {
'id': session_id,
'start_time': time.time(),
'transcripts': [],
'raw_chunks': [],
'processed_text': "",
'confidence_scores': [],
'language_detected': None
}
logger.info(f"Started transcription session: {session_id}")
return session_id
def add_transcription_chunk(self, text: str, metadata: Dict[str, Any]):
"""Add transcription chunk to current session"""
if not self.current_session:
self.start_session()
chunk = {
'text': text,
'timestamp': time.time(),
'metadata': metadata
}
self.current_session['transcripts'].append(chunk)
self.current_session['raw_chunks'].append(text)
# Update processed text with smoothing
self._update_processed_text(text)
def _update_processed_text(self, new_text: str):
"""Update processed text with smoothing and correction"""
# Simple text smoothing - can be enhanced with more sophisticated NLP
# Remove duplicate consecutive words/phrases
words = new_text.split()
if len(words) > 0:
# Check for overlap with previous text
current_words = self.current_session['processed_text'].split()
# Find overlap and append only new words
overlap_found = False
for i in range(min(len(current_words), len(words))):
if current_words[-i-1:] == words[:i+1]:
# Found overlap, append remaining words
new_words = words[i+1:]
if new_words:
self.current_session['processed_text'] += " " + " ".join(new_words)
overlap_found = True
break
if not overlap_found:
# No overlap found, append with separator
if self.current_session['processed_text']:
self.current_session['processed_text'] += " " + new_text
else:
self.current_session['processed_text'] = new_text
def get_current_transcript(self) -> str:
"""Get current processed transcript"""
if self.current_session:
return self.current_session['processed_text']
return ""
def end_session(self) -> Dict[str, Any]:
"""End current transcription session and return summary"""
if not self.current_session:
return {}
self.current_session['end_time'] = time.time()
self.current_session['duration'] = self.current_session['end_time'] - self.current_session['start_time']
# Calculate session statistics
total_chunks = len(self.current_session['transcripts'])
total_words = len(self.current_session['processed_text'].split())
session_summary = {
'session_id': self.current_session['id'],
'duration': self.current_session['duration'],
'total_chunks': total_chunks,
'total_words': total_words,
'words_per_minute': total_words / (self.current_session['duration'] / 60) if self.current_session['duration'] > 0 else 0,
'final_transcript': self.current_session['processed_text'],
'language_detected': self.current_session.get('language_detected', 'unknown')
}
# Archive session
self.session_history.append(self.current_session.copy())
self.current_session = None
logger.info(f"Ended transcription session: {session_summary['session_id']}")
return session_summary