@@ -37,26 +37,68 @@ def __init__(self, mic_source, speaker_source, model):
37
37
}
38
38
}
39
39
40
- def transcribe_audio_queue (self , audio_queue ):
40
+ def transcribe_audio_queue (self , speaker_queue , mic_queue ):
41
+ import queue
42
+
41
43
while True :
42
- who_spoke , data , time_spoken = audio_queue .get ()
43
- self .update_last_sample_and_phrase_status (who_spoke , data , time_spoken )
44
- source_info = self .audio_sources [who_spoke ]
45
-
46
- text = ''
47
- try :
48
- fd , path = tempfile .mkstemp (suffix = ".wav" )
49
- os .close (fd )
50
- source_info ["process_data_func" ](source_info ["last_sample" ], path )
51
- text = self .audio_model .get_transcription (path )
52
- except Exception as e :
53
- print (e )
54
- finally :
55
- os .unlink (path )
56
-
57
- if text != '' and text .lower () != 'you' :
58
- self .update_transcript (who_spoke , text , time_spoken )
44
+ pending_transcriptions = []
45
+
46
+ mic_data = []
47
+ while True :
48
+ try :
49
+ data , time_spoken = mic_queue .get_nowait ()
50
+ self .update_last_sample_and_phrase_status ("You" , data , time_spoken )
51
+ mic_data .append ((data , time_spoken ))
52
+ except queue .Empty :
53
+ break
54
+
55
+ speaker_data = []
56
+ while True :
57
+ try :
58
+ data , time_spoken = speaker_queue .get_nowait ()
59
+ self .update_last_sample_and_phrase_status ("Speaker" , data , time_spoken )
60
+ speaker_data .append ((data , time_spoken ))
61
+ except queue .Empty :
62
+ break
63
+
64
+ if mic_data :
65
+ source_info = self .audio_sources ["You" ]
66
+ try :
67
+ fd , path = tempfile .mkstemp (suffix = ".wav" )
68
+ os .close (fd )
69
+ source_info ["process_data_func" ](source_info ["last_sample" ], path )
70
+ text = self .audio_model .get_transcription (path )
71
+ if text != '' and text .lower () != 'you' :
72
+ latest_time = max (time for _ , time in mic_data )
73
+ pending_transcriptions .append (("You" , text , latest_time ))
74
+ except Exception as e :
75
+ print (f"Transcription error for You: { e } " )
76
+ finally :
77
+ os .unlink (path )
78
+
79
+ if speaker_data :
80
+ source_info = self .audio_sources ["Speaker" ]
81
+ try :
82
+ fd , path = tempfile .mkstemp (suffix = ".wav" )
83
+ os .close (fd )
84
+ source_info ["process_data_func" ](source_info ["last_sample" ], path )
85
+ text = self .audio_model .get_transcription (path )
86
+ if text != '' and text .lower () != 'you' :
87
+ latest_time = max (time for _ , time in speaker_data )
88
+ pending_transcriptions .append (("Speaker" , text , latest_time ))
89
+ except Exception as e :
90
+ print (f"Transcription error for Speaker: { e } " )
91
+ finally :
92
+ os .unlink (path )
93
+
94
+ if pending_transcriptions :
95
+ pending_transcriptions .sort (key = lambda x : x [2 ])
96
+ for who_spoke , text , time_spoken in pending_transcriptions :
97
+ self .update_transcript (who_spoke , text , time_spoken )
98
+
59
99
self .transcript_changed_event .set ()
100
+
101
+ threading .Event ().wait (0.1 )
60
102
61
103
def update_last_sample_and_phrase_status (self , who_spoke , data , time_spoken ):
62
104
source_info = self .audio_sources [who_spoke ]
0 commit comments