Elffuss BraidHub

kikocis/online-translator public

main · 3.7 KiBRawPermalink
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3
4"""
5Continuous Buffer Module
6-----------------------
7Manages continuous audio buffer processing for real-time transcription.
8Handles audio data streaming and buffering for optimal performance.
9
10Copyright (c) 2025 Kiko Cisneros
11Licensed under the MIT License (see LICENSE file for details)
12"""
13
14import threading
15import numpy as np
16
17
18class ContinuousBuffer:
19 """Manages continuous audio buffer for real-time transcription"""
20
21 def __init__(self, sample_rate=16000, max_duration=30.0):
22 self.sample_rate = sample_rate
23 self.max_samples = int(max_duration * sample_rate)
24 self.buffer = np.array([], dtype=np.float32)
25 self.lock = threading.Lock()
26
27 # Transcription overlap management
28 self.overlap_duration = 0.4 # ✅ RELAXED: Balanced overlap
29 self.overlap_samples = int(self.overlap_duration * sample_rate)
30 self.last_transcribed_position = 0
31
32 # Word boundary context
33 self.word_boundary_context = 0.15 # ✅ RELAXED: Less context needed
34 self.word_boundary_samples = int(self.word_boundary_context * sample_rate)
35
36 print(f"📊 Continuous buffer initialized: {max_duration}s max, {self.overlap_duration}s overlap, {self.word_boundary_context}s word boundary context")
37
38 def add_audio(self, audio_chunk):
39 """Add new audio to the continuous buffer"""
40 with self.lock:
41 # Append new audio
42 self.buffer = np.concatenate([self.buffer, audio_chunk])
43
44 # Keep buffer within max size (sliding window)
45 if len(self.buffer) > self.max_samples:
46 excess = len(self.buffer) - self.max_samples
47 self.buffer = self.buffer[excess:]
48 # Adjust transcribed position
49 self.last_transcribed_position = max(0, self.last_transcribed_position - excess)
50
51 def get_new_audio_for_transcription(self):
52 """Get new audio that hasn't been transcribed yet (with overlap)"""
53 with self.lock:
54 if len(self.buffer) == 0:
55 return np.array([])
56
57 # Calculate start position with overlap
58 start_pos = max(0, self.last_transcribed_position - self.overlap_samples)
59
60 # Get new audio chunk
61 new_audio = self.buffer[start_pos:].copy()
62
63 # Update last transcribed position
64 self.last_transcribed_position = len(self.buffer)
65
66 return new_audio
67
68 def clear_transcribed_audio(self, keep_samples=None):
69 """Clear old transcribed audio, keeping recent samples for context"""
70 with self.lock:
71 if keep_samples is None:
72 keep_samples = self.overlap_samples * 2
73
74 if len(self.buffer) > keep_samples:
75 self.buffer = self.buffer[-keep_samples:]
76 self.last_transcribed_position = 0
77
78 def get_total_duration(self):
79 """Get total duration of audio in buffer"""
80 with self.lock:
81 return len(self.buffer) / self.sample_rate
82
83 def get_buffer_info(self):
84 """Get detailed buffer information for debugging"""
85 with self.lock:
86 return {
87 'total_samples': len(self.buffer),
88 'total_duration': len(self.buffer) / self.sample_rate,
89 'max_samples': self.max_samples,
90 'max_duration': self.max_samples / self.sample_rate,
91 'last_transcribed_position': self.last_transcribed_position,
92 'overlap_samples': self.overlap_samples,
93 'overlap_duration': self.overlap_duration
94 }