-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathear.py
78 lines (65 loc) · 2.31 KB
/
ear.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
import time
import os
import sounddevice as sd
import numpy as np
import threading
SAMPLE_RATE = 16000
recording = False
audio_data = []
TRIGGER_FILE = "./trigger"
def record_audio():
global recording, audio_data
audio_data = []
def callback(indata, frames, time, status):
if recording:
audio_data.append(indata.copy())
with sd.InputStream(samplerate=SAMPLE_RATE, channels=1, callback=callback):
while recording:
sd.sleep(100)
def save_audio(filename):
if audio_data:
audio = np.concatenate(audio_data, axis=0)
with open(filename, 'wb') as f:
f.write(audio.tobytes())
return True
return False
def read_trigger_file():
try:
with open(TRIGGER_FILE, "r") as f:
content = f.read().strip()
print(f"Read trigger file. Content: '{content}'")
return content
except FileNotFoundError:
print(f"Trigger file not found: {TRIGGER_FILE}")
return None
except Exception as e:
print(f"Error reading trigger file: {e}")
return None
def main():
global recording
last_trigger_state = None
record_thread = None
while True:
trigger_state = read_trigger_file()
if trigger_state and trigger_state != last_trigger_state:
print(f"Trigger state changed from '{last_trigger_state}' to '{trigger_state}'")
if trigger_state == "START" and not recording:
print("Recording started...")
recording = True
record_thread = threading.Thread(target=record_audio)
record_thread.start()
elif trigger_state == "STOP" and recording:
print("Recording stopped.")
recording = False
if record_thread:
record_thread.join()
if save_audio("./audio.raw"):
with open("./whisper_input", "w") as f:
f.write("./audio.raw")
print("Audio saved and whisper_input created.")
else:
print("No audio data recorded.")
last_trigger_state = trigger_state
time.sleep(0.1) # Check for trigger changes every 100ms
if __name__ == "__main__":
main()