-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathwhisper-typer-tool.py
More file actions
163 lines (138 loc) · 4.25 KB
/
whisper-typer-tool.py
File metadata and controls
163 lines (138 loc) · 4.25 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
from pynput import keyboard
import codecs
import whisper
import time
import subprocess
import threading
import pyaudio
import wave
import os
from datetime import datetime
#load model
#model selection -> (tiny base small medium large)
print("loading model...")
model_name = "tiny"
model = whisper.load_model(model_name)
# Replace playsound with pyaudio for sound playback
def play_audio_file(filename):
# Create a PyAudio object
audio = pyaudio.PyAudio()
# Open the wave file
wf = wave.open(filename, 'rb')
# Get the audio format
stream = audio.open(
format=audio.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True
)
# Read data and play
data = wf.readframes(1024)
while data:
stream.write(data)
data = wf.readframes(1024)
# Cleanup
stream.stop_stream()
stream.close()
audio.terminate()
# Play the sound when model is loaded
play_audio_file("model_loaded.wav")
print(f"{model_name} model loaded")
file_ready_counter=0
stop_recording=False
is_recording=False
pykeyboard= keyboard.Controller()
def transcribe_speech():
global file_ready_counter
i=1
print("ready - start transcribing with F2 ...\n")
while True:
while file_ready_counter<i:
time.sleep(0.01)
result = model.transcribe("test"+str(i)+".wav")
print(result["text"]+"\n")
now = str(datetime.now()).split(".")[0]
with codecs.open('transcribe.log', 'a', encoding='utf-8') as f:
f.write(now+" : "+result["text"]+"\n")
for element in result["text"]:
try:
pykeyboard.type(element)
time.sleep(0.0025)
except:
print("empty or unknown symbol")
os.remove("test"+str(i)+".wav")
i=i+1
#keyboard events
pressed = set()
COMBINATIONS = [
{
"keys": [
#{keyboard.Key.ctrl ,keyboard.Key.shift, keyboard.KeyCode(char="r")},
#{keyboard.Key.ctrl ,keyboard.Key.shift, keyboard.KeyCode(char="R")},
{keyboard.Key.f2},
],
"command": "start record",
},
]
#------------
#record audio
def record_speech():
global file_ready_counter
global stop_recording
global is_recording
is_recording=True
chunk = 1024 # Record in chunks of 1024 samples
sample_format = pyaudio.paInt16 # 16 bits per sample
channels = 2
fs = 44100 # Record at 44100 samples per second
p = pyaudio.PyAudio() # Create an interface to PortAudio
stream = p.open(format=sample_format,
channels=channels,
rate=fs,
frames_per_buffer=chunk,
input=True)
frames = [] # Initialize array to store frames
print("Start recording...\n")
play_audio_file("on.wav")
while stop_recording==False:
data = stream.read(chunk)
frames.append(data)
# Stop and close the stream
stream.stop_stream()
stream.close()
p.terminate()
play_audio_file("off.wav")
print('Finish recording')
# Save the recorded data as a WAV file
wf = wave.open("test"+str(file_ready_counter+1)+".wav", 'wb')
wf.setnchannels(channels)
wf.setsampwidth(p.get_sample_size(sample_format))
wf.setframerate(fs)
wf.writeframes(b''.join(frames))
wf.close()
stop_recording=False
is_recording=False
file_ready_counter=file_ready_counter+1
#------------
#transcribe speech in infinte loop
t2 = threading.Thread(target=transcribe_speech)
t2.start()
#hot key events
def on_press(key):
pressed.add(key)
def on_release(key):
global pressed
global stop_recording
global is_recording
for c in COMBINATIONS:
for keys in c["keys"]:
if keys.issubset(pressed):
if c["command"]=="start record" and stop_recording==False and is_recording==False:
t1 = threading.Thread(target=record_speech)
t1.start()
else:
if c["command"]=="start record" and is_recording==True:
stop_recording=True
pressed = set()
with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()