forked from lars-quaedvlieg/chronica
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shh.py
153 lines (134 loc) · 5.69 KB
/
shh.py
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
import os
import time
import shutil
import threading
from pathlib import Path
import wave
from newwhisper.client import Client, TranscriptionTeeClient
class ActualWhisperClient(TranscriptionTeeClient):
def __init__(
self,
host,
port,
lang=None,
translate=False,
model="small",
use_vad=True,
save_output_recording=False,
output_recording_filename="./output_recording.wav",
output_transcription_path="./output.srt",
log_transcription=True,
):
self.client = Client(host, port, lang, translate, model, srt_file_path=output_transcription_path, use_vad=use_vad, log_transcription=log_transcription)
if save_output_recording and not output_recording_filename.endswith(".wav"):
raise ValueError(f"Please provide a valid `output_recording_filename`: {output_recording_filename}")
if not output_transcription_path.endswith(".srt"):
raise ValueError(f"Please provide a valid `output_transcription_path`: {output_transcription_path}. The file extension should be `.srt`.")
TranscriptionTeeClient.__init__(
self,
[self.client],
save_output_recording=save_output_recording,
output_recording_filename=output_recording_filename
)
print("[INFO]: Waiting for server ready ...")
for client in self.clients:
while not client.recording:
if client.waiting or client.server_error:
self.close_all_clients()
return
print("[INFO]: Server ready")
self.cv = threading.Condition()
if self.save_output_recording:
if os.path.exists("chunks"):
shutil.rmtree("chunks")
os.makedirs("chunks")
self.n_audio_files = 0
def process_segments(self, segments: list[str]):
super().process_segments(segments)
#with self.cv:
# self.cv.notify()
def update(self, data: bytes):
self.frames += data
audio_array = self.bytes_to_float_array(data)
self.multicast_packet(audio_array.tobytes())
# save frames if more than a minute
if len(self.frames) > 60 * self.rate:
if self.save_output_recording:
self.save_chunk(self.n_audio_files)
self.n_audio_files += 1
self.frames = b""
self.write_all_clients_srt()
# Wait until we receive a response from the server (we get notified in the process_segments).
#with self.cv:
# print("[INFO] Waiting for server response")
# self.cv.wait()
#print("[INFO] Got server response :D")
data_root = Path("data/entry_new")
CLIENT: ActualWhisperClient = ActualWhisperClient(
"localhost",
9090,
lang="en",
translate=False,
model="small",
use_vad=False,
save_output_recording=True,
output_recording_filename=str(data_root/"recording.wav"),
output_transcription_path=str(data_root/"transcript.srt"),
)
assert len(CLIENT.clients) == 1
CLIENT.stream = CLIENT.p.open(
format=CLIENT.p.get_format_from_width(2), # TODO maybe not 2?
channels=CLIENT.channels,
rate=48000, # TODO maybe different?
input=True,
output=True,
frames_per_buffer=CLIENT.chunk,
)
def update_recording(new_entry_path: str) -> str:
print(f"[INFO] Whispering path {new_entry_path}")
with wave.open(new_entry_path, "rb") as wavfile:
# assert wavfile.getframerate() == 48000, wavfile.getframerate()
# assert wavfile.getsampwidth() == 2, wavfile.getsampwidth()
print("[INFO] This wav should be", wavfile.getnframes()/wavfile.getframerate(), "seconds")
while (data := wavfile.readframes(CLIENT.chunk)) != b"":
print("[INFO] Sending data with length", len(data))
assert len(CLIENT.clients) == 1
audio_array = CLIENT.bytes_to_float_array(data)
print("[INFO] Len array:", audio_array.size)
CLIENT.multicast_packet(audio_array.tobytes())
CLIENT.stream.write(data)
while CLIENT.clients[0].done_with_this_crap < 2:
time.sleep(0.05)
CLIENT.clients[0].done_with_this_crap = 0
return "".join(segment["text"] for segment in CLIENT.clients[0].transcript) + CLIENT.clients[0].last_segment["text"]
# Wait until transcription is done.
# We do this in (a not very nice way).
# We wait until we receive two consecutive responses where the text didn't change.
#prev_text = None
#current_text = None
#last_response_received = CLIENT.clients[0].last_response_received
#current_last_response_received = CLIENT.clients[0].last_response_received
#converged = False
#while not converged:
# time.sleep(1.0)
# prev_text = current_text
# last_response_received = current_last_response_received
# current_text = "".join(segment["text"] for segment in CLIENT.clients[0].transcript)
# current_last_response_received = CLIENT.clients[0].last_response_received
# if CLIENT.clients[0].last_segment is not None:
# current_text += CLIENT.clients[0].last_segment["text"]
# print(last_response_received, current_last_response_received, prev_text, current_text)
# if last_response_received < current_last_response_received and prev_text == current_text:
# assert CLIENT.clients[0].last_segment is not None
# converged = True
#return current_text
if __name__ == "__main__":
txt1 = update_recording("recordings/rec1.wav")
print("Output1:")
print(txt1)
txt2 = update_recording("recordings/rec2.wav")
print("Output2:")
print(txt2)
txt3 = update_recording("recordings/rec3.wav")
print("Output3:")
print(txt3)