-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjarvis.py
More file actions
221 lines (162 loc) · 5.19 KB
/
Copy pathjarvis.py
File metadata and controls
221 lines (162 loc) · 5.19 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
"""
Jarvis Audio Controller
Wake word: "Jarvis"
Commands triggered by clap count.
1 clap -> Coding environment
2 claps -> Gaming environment
Technologies used:
- Picovoice Porcupine (wake word detection)
- PyAudio (microphone streaming)
- SoundDevice + NumPy (clap detection)
- Subprocess (system automation)
"""
import os
import struct
import subprocess
import numpy as np
import pyaudio
import pvporcupine
import sounddevice as sd
# =================================================================
# CONFIGURATION
# =================================================================
# Wake word access key (must be set as environment variable)
ACCESS_KEY = os.getenv("PORCUPINE_ACCESS_KEY")
if not ACCESS_KEY:
raise ValueError(
"Porcupine access key not found. "
"Set PORCUPINE_ACCESS_KEY as an environment variable."
)
# Application paths (edit if needed for your system)
CHROME_PATH = "chrome"
VSCODE_PATH = "code"
GAME_LAUNCHER = "TLauncher.exe"
# Optional audio file (set to None if you don't want music)
MUSIC_FILE = None
# Clap detection settings
CLAP_THRESHOLD_LOW = 0.05
CLAP_THRESHOLD_HIGH = 0.15
CLAP_LISTEN_DURATION = 2
SAMPLE_RATE = 44100
# =================================================================
# AUDIO PROCESSING
# =================================================================
def detect_claps():
"""
Records a short burst of audio and checks peak amplitude
to detect clap-like spikes.
"""
print("Listening for claps...")
recording = sd.rec(
int(CLAP_LISTEN_DURATION * SAMPLE_RATE),
samplerate=SAMPLE_RATE,
channels=1
)
sd.wait()
audio = np.abs(recording.flatten())
peak = np.max(audio)
print(f"Peak volume detected: {peak:.4f}")
if peak > CLAP_THRESHOLD_HIGH:
return 2
elif peak > CLAP_THRESHOLD_LOW:
return 1
else:
return 0
# =================================================================
# AUTOMATION COMMANDS
# =================================================================
def coding_setup():
"""Launch coding environment"""
print("Initializing Coding Environment...")
try:
subprocess.Popen([
CHROME_PATH,
"https://chat.openai.com",
"https://claude.ai",
"https://gemini.google.com"
])
except Exception:
print("Could not open Chrome.")
try:
subprocess.Popen(VSCODE_PATH)
except Exception:
print("Could not open VS Code.")
# Optional music playback
if MUSIC_FILE:
try:
subprocess.Popen([
"powershell",
"-c",
f"Start-Process '{MUSIC_FILE}' -WindowStyle Minimized"
])
except Exception:
print("Could not play audio file.")
def gaming_setup():
"""Launch gaming setup"""
print("Initializing Gaming Setup...")
try:
subprocess.Popen([
CHROME_PATH,
"https://youtube.com"
])
except Exception:
print("Could not open Chrome.")
try:
subprocess.Popen(GAME_LAUNCHER)
except Exception:
print("Could not launch game launcher.")
# =================================================================
# INITIALIZATION
# =================================================================
print("Initializing Jarvis...")
porcupine = pvporcupine.create(
access_key=ACCESS_KEY,
keywords=["jarvis"]
)
pa = pyaudio.PyAudio()
try:
stream = pa.open(
rate=porcupine.sample_rate,
channels=1,
format=pyaudio.paInt16,
input=True,
frames_per_buffer=porcupine.frame_length
)
except Exception as e:
print("Microphone initialization failed:", e)
exit(1)
# =================================================================
# MAIN LOOP
# =================================================================
def main():
print("\n--- Jarvis is now online ---")
print("Wake word: Jarvis")
print("Clap once -> Coding setup")
print("Clap twice -> Gaming setup\n")
try:
while True:
pcm = stream.read(porcupine.frame_length)
pcm = struct.unpack_from("h" * porcupine.frame_length, pcm)
result = porcupine.process(pcm)
if result >= 0:
print("\n[!] Jarvis wake-word detected!")
claps = detect_claps()
print(f"Action triggered by {claps} clap(s)")
if claps == 1:
coding_setup()
elif claps == 2:
gaming_setup()
else:
print("No action taken.")
except KeyboardInterrupt:
print("\nStopping Jarvis...")
finally:
stream.stop_stream()
stream.close()
pa.terminate()
porcupine.delete()
# =================================================================
# ENTRY POINT
# =================================================================
if __name__ == "__main__":
main()