-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
164 lines (143 loc) · 5.29 KB
/
main.py
File metadata and controls
164 lines (143 loc) · 5.29 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
import whisper
import torch
import sounddevice as sd
from scipy.io.wavfile import write
import os
import queue
import threading
import time
import datetime
import re
import numpy as np
from google.cloud import translate_v2 as translate
from google.cloud import texttospeech
import pygame
# === KONFIGURATION ===
SAMPLE_RATE = 16000
BLOCK_SECONDS = 25
ROLLING_SECONDS = 30
LANGUAGE = "ar"
MODEL_NAME = "medium"
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "translate_key.json"
# === INITIALISIERUNG ===
model = whisper.load_model(MODEL_NAME).to("cuda")
translate_client = translate.Client()
audio_queue = queue.Queue()
tts_queue = queue.Queue()
letzter_text = ""
# === TEXT-TO-SPEECH WORKER ===
def tts_worker():
while True:
text = tts_queue.get()
if text is None:
break
speak_google(text)
# === TEXT-TO-SPEECH FUNKTION ===
def speak_google(text):
client = texttospeech.TextToSpeechClient()
synthesis_input = texttospeech.SynthesisInput(ssml=f"""
<speak><prosody rate=\"0.94\" pitch=\"-1st\">{text}</prosody></speak>
""")
voice = texttospeech.VoiceSelectionParams(
language_code="de-DE",
name="de-DE-Wavenet-B",
ssml_gender=texttospeech.SsmlVoiceGender.MALE
)
audio_config = texttospeech.AudioConfig(
audio_encoding=texttospeech.AudioEncoding.MP3,
speaking_rate=0.94,
pitch=-1.0
)
t0 = time.time()
response = client.synthesize_speech(input=synthesis_input, voice=voice, audio_config=audio_config)
print(f"🕒 Google TTS Dauer: {time.time() - t0:.2f}s")
filename = f"tts_{datetime.datetime.now().strftime('%H%M%S%f')}.mp3"
with open(filename, "wb") as out:
out.write(response.audio_content)
if not os.path.exists(filename) or os.path.getsize(filename) == 0:
print("[Warnung] Leere MP3-Datei.")
return
try:
if pygame.mixer.get_init() is None:
pygame.mixer.init()
while pygame.mixer.music.get_busy():
pygame.time.Clock().tick(10)
pygame.mixer.music.load(filename)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
pygame.time.Clock().tick(10)
except pygame.error as e:
print(f"[TTS Fehler] {e}")
finally:
pygame.mixer.quit()
# === VERARBEITUNGS-THREAD ===
def verarbeitung():
global letzter_text
while True:
audio_data = audio_queue.get()
if audio_data is None:
break
filename = f"aufnahme_{datetime.datetime.now().strftime('%H%M%S%f')}.wav"
write(filename, SAMPLE_RATE, audio_data)
try:
t0 = time.time()
result = model.transcribe(filename, language=LANGUAGE, temperature=0, initial_prompt="هذا خطاب رسمي")
whisper_time = time.time() - t0
print(f"🕒 Whisper Dauer: {whisper_time:.2f}s")
arabic_text = result["text"]
print("\n🗣️ Erkanntes Arabisch:\n", arabic_text)
saetze = [s.strip() for s in re.split(r"[.!؟؛،\n]", arabic_text) if s.strip()]
blocktext = ""
t0 = time.time()
for satz in saetze:
if satz:
t = translate_client.translate(satz, source_language=LANGUAGE, target_language='de')
blocktext += t["translatedText"] + " "
translate_time = time.time() - t0
print(f"🕒 Google Translate Dauer: {translate_time:.2f}s")
print(f"🔹 Deutsch (zusammen): {blocktext}")
if blocktext.strip() and blocktext != letzter_text:
tts_queue.put(blocktext)
letzter_text = blocktext
except Exception as e:
print("[Verarbeitungsfehler]", e)
# === ROLLING AUDIO-AUFNAHME ===
def rolling_record():
buffer = np.zeros((ROLLING_SECONDS * SAMPLE_RATE, 1), dtype='int16')
index = 0
def callback(indata, frames, time_info, status):
nonlocal buffer, index
if status:
print(f"[InputStream Status] {status}")
if index + frames < len(buffer):
buffer[index:index + frames] = indata
index += frames
else:
rest = len(buffer) - index
buffer[index:] = indata[:rest]
buffer[:frames - rest] = indata[rest:]
index = (frames - rest)
with sd.InputStream(samplerate=SAMPLE_RATE, channels=1, callback=callback, dtype='int16'):
print("⏳ Initialisiere Aufnahme...")
time.sleep(ROLLING_SECONDS)
print(f"✅ Aufnahme aktiv – Blöcke alle {BLOCK_SECONDS}s")
try:
while True:
time.sleep(BLOCK_SECONDS)
end = index
start = (index - BLOCK_SECONDS * SAMPLE_RATE) % len(buffer)
if start < end:
block = buffer[start:end]
else:
block = np.concatenate((buffer[start:], buffer[:end]), axis=0)
audio_queue.put(block.copy())
except KeyboardInterrupt:
print("\n🛑 Aufnahme gestoppt.")
audio_queue.put(None)
tts_queue.put(None)
# === START ===
if __name__ == "__main__":
for _ in range(2):
threading.Thread(target=verarbeitung, daemon=True).start()
threading.Thread(target=tts_worker, daemon=True).start()
rolling_record()