-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvoice_enhanced.py
More file actions
executable file
·417 lines (344 loc) · 15.7 KB
/
voice_enhanced.py
File metadata and controls
executable file
·417 lines (344 loc) · 15.7 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#!/usr/bin/env python3
"""
Advanced Mini Bash - Enhanced Voice Control Module (Phase 3)
AI-powered voice recognition with Hindi & English support using Google Cloud APIs
Enhanced with better shell integration and command processing
"""
import os
import sys
import json
import threading
import time
from typing import Optional, Dict, List
# Google Cloud imports
try:
from google.cloud import speech
from google.cloud import translate_v2 as translate
from google.cloud import texttospeech
except ImportError:
print("❌ Google Cloud libraries not installed!")
print("Run: pip install google-cloud-speech google-cloud-translate google-cloud-texttospeech")
sys.exit(1)
# Audio recording imports
try:
import pyaudio
import wave
except ImportError:
print("❌ Audio libraries not installed!")
print("Run: pip install pyaudio wave")
sys.exit(1)
# Import our shell bridge
from shell_bridge import ShellBridge, VoiceCommandProcessor
class EnhancedVoiceShell:
def __init__(self, config_file: str = "voice_config.json"):
"""Initialize the enhanced voice-controlled shell"""
self.config = self.load_config(config_file)
self.speech_client = speech.SpeechClient()
self.translate_client = translate.Client()
self.tts_client = texttospeech.TextToSpeechClient()
# Initialize shell bridge
self.shell_bridge = ShellBridge()
self.command_processor = None
# Audio settings
self.CHUNK = 1024
self.FORMAT = pyaudio.paInt16
self.CHANNELS = 1
self.RATE = 16000
self.RECORD_SECONDS = 5
# Voice control state
self.is_listening = False
self.is_processing = False
print("🎤 Enhanced Voice-Controlled Mini Bash initialized!")
print("💬 Supported languages: Hindi (हिंदी) & English")
print("🔊 Say 'exit' or 'बाहर निकलो' to quit")
print("=" * 60)
def load_config(self, config_file: str) -> Dict:
"""Load configuration from JSON file"""
default_config = {
"google_cloud_credentials": "credentials.json",
"language_codes": ["hi-IN", "en-US"],
"voice_feedback": True,
"auto_translate": True,
"recording_timeout": 5,
"confidence_threshold": 0.7,
"continuous_listening": False
}
if os.path.exists(config_file):
with open(config_file, 'r', encoding='utf-8') as f:
return {**default_config, **json.load(f)}
else:
# Create default config file
with open(config_file, 'w', encoding='utf-8') as f:
json.dump(default_config, f, indent=2, ensure_ascii=False)
return default_config
def start_shell(self) -> bool:
"""Start the Mini Bash shell"""
if self.shell_bridge.start_shell():
self.command_processor = VoiceCommandProcessor(self.shell_bridge)
print("✅ Mini Bash shell started")
return True
else:
print("❌ Failed to start Mini Bash shell")
return False
def stop_shell(self) -> None:
"""Stop the Mini Bash shell"""
self.shell_bridge.stop_shell()
print("✅ Mini Bash shell stopped")
def record_audio(self) -> Optional[str]:
"""Record audio from microphone with enhanced error handling"""
try:
audio = pyaudio.PyAudio()
# Check for available audio devices
device_count = audio.get_device_count()
if device_count == 0:
print("❌ No audio devices found!")
return None
print("🎤 Listening... (Speak now!)")
self.is_listening = True
stream = audio.open(
format=self.FORMAT,
channels=self.CHANNELS,
rate=self.RATE,
input=True,
frames_per_buffer=self.CHUNK
)
frames = []
for _ in range(0, int(self.RATE / self.CHUNK * self.RECORD_SECONDS)):
data = stream.read(self.CHUNK)
frames.append(data)
print("⏹️ Recording finished!")
self.is_listening = False
stream.stop_stream()
stream.close()
audio.terminate()
# Save audio to temporary file
temp_file = f"temp_audio_{int(time.time())}.wav"
with wave.open(temp_file, 'wb') as wf:
wf.setnchannels(self.CHANNELS)
wf.setsampwidth(audio.get_sample_size(self.FORMAT))
wf.setframerate(self.RATE)
wf.writeframes(b''.join(frames))
return temp_file
except Exception as e:
print(f"❌ Audio recording error: {e}")
self.is_listening = False
return None
def speech_to_text(self, audio_file: str) -> tuple[Optional[str], Optional[str]]:
"""Convert speech to text using Google Cloud Speech-to-Text with enhanced config"""
try:
with open(audio_file, 'rb') as audio_file_content:
content = audio_file_content.read()
audio = speech.RecognitionAudio(content=content)
config = speech.RecognitionConfig(
encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=self.RATE,
language_code="hi-IN", # Start with Hindi
alternative_language_codes=["en-US"],
enable_automatic_punctuation=True,
enable_word_time_offsets=True,
model="latest_long",
use_enhanced=True
)
response = self.speech_client.recognize(config=config, audio=audio)
if response.results:
result = response.results[0]
transcript = result.alternatives[0].transcript
confidence = result.alternatives[0].confidence
detected_language = result.language_code
print(f"🎯 Detected Language: {detected_language}")
print(f"📝 Transcript: {transcript}")
print(f"🎯 Confidence: {confidence:.2%}")
# Check confidence threshold
if confidence < self.config.get("confidence_threshold", 0.7):
print("⚠️ Low confidence, please try again")
return None, None
return transcript, detected_language
return None, None
except Exception as e:
print(f"❌ Speech-to-text error: {e}")
return None, None
def translate_text(self, text: str, source_lang: str = "hi", target_lang: str = "en") -> str:
"""Translate text using Google Cloud Translation API"""
try:
if source_lang == target_lang:
return text
result = self.translate_client.translate(
text,
source_language=source_lang,
target_language=target_lang
)
translated = result['translatedText']
print(f"🌐 Translated: {text} → {translated}")
return translated
except Exception as e:
print(f"❌ Translation error: {e}")
return text
def text_to_speech(self, text: str, language: str = "en") -> None:
"""Convert text to speech for voice feedback with enhanced voice selection"""
if not self.config.get("voice_feedback", True):
return
try:
synthesis_input = texttospeech.SynthesisInput(text=text)
# Select appropriate voice based on language
if language.startswith("hi"):
voice = texttospeech.VoiceSelectionParams(
language_code="hi-IN",
name="hi-IN-Wavenet-A", # Hindi voice
ssml_gender=texttospeech.SsmlVoiceGender.FEMALE
)
else:
voice = texttospeech.VoiceSelectionParams(
language_code="en-US",
name="en-US-Wavenet-D", # English voice
ssml_gender=texttospeech.SsmlVoiceGender.MALE
)
audio_config = texttospeech.AudioConfig(
audio_encoding=texttospeech.AudioEncoding.MP3
)
response = self.tts_client.synthesize_speech(
input=synthesis_input,
voice=voice,
audio_config=audio_config
)
# Save and play audio
feedback_file = f"feedback_{int(time.time())}.mp3"
with open(feedback_file, "wb") as out:
out.write(response.audio_content)
# Play feedback (platform specific)
self.play_audio(feedback_file)
# Clean up
os.remove(feedback_file)
except Exception as e:
print(f"❌ Text-to-speech error: {e}")
def play_audio(self, audio_file: str) -> None:
"""Play audio file (platform specific)"""
try:
if sys.platform == "darwin": # macOS
subprocess.run(["afplay", audio_file], check=False, capture_output=True)
elif sys.platform == "linux": # Linux
subprocess.run(["mpg123", audio_file], check=False, capture_output=True)
elif sys.platform == "win32": # Windows
subprocess.run(["start", audio_file], check=False, capture_output=True)
except:
pass # Silently fail if audio player not available
def process_voice_command(self) -> None:
"""Main voice command processing loop with enhanced features"""
print("🎤 Voice control ready! Start speaking...")
while True:
try:
# Record audio
audio_file = self.record_audio()
if not audio_file:
continue
# Convert speech to text
transcript, detected_lang = self.speech_to_text(audio_file)
if not transcript:
print("❌ No speech detected or low confidence")
continue
# Clean up audio file
os.remove(audio_file)
# Check for exit commands
exit_commands = ["exit", "quit", "stop", "बाहर निकलो", "रुको", "बंद करो"]
if any(exit_cmd in transcript.lower() for exit_cmd in exit_commands):
print("👋 Goodbye!")
self.text_to_speech("Goodbye! See you later!", "en")
break
# Process command
self.is_processing = True
print("🔄 Processing command...")
# Determine language
if detected_lang and detected_lang.startswith("hi"):
language = "hindi"
# Translate if needed
english_text = self.translate_text(transcript, "hi", "en")
print(f"📝 Original (Hindi): {transcript}")
print(f"🌐 Translated (English): {english_text}")
else:
language = "english"
english_text = transcript
print(f"📝 Command (English): {english_text}")
# Execute command using shell bridge
print(f"🎯 Language detected: {language}")
result = self.command_processor.process_voice_command(transcript, language)
print(f"🔧 Mapped command: {result.get('mapped_command', 'N/A')}")
# Display results
if result.get("success", False):
print("✅ Command executed successfully!")
if result.get("output"):
print("📤 Output:")
print(result["output"])
# Voice feedback
if language == "hindi":
self.text_to_speech("कमांड सफलतापूर्वक चलाया गया", "hi")
else:
self.text_to_speech("Command executed successfully", "en")
else:
print("❌ Command failed!")
if result.get("error"):
print("⚠️ Error:")
print(result["error"])
# Voice feedback
if language == "hindi":
self.text_to_speech("कमांड असफल", "hi")
else:
self.text_to_speech("Command failed", "en")
self.is_processing = False
print("\n" + "="*60)
print("🎤 Ready for next command...")
except KeyboardInterrupt:
print("\n👋 Goodbye!")
break
except Exception as e:
print(f"❌ Error: {e}")
self.is_processing = False
continue
def show_status(self) -> None:
"""Show current status of the voice shell"""
print("\n📊 Voice Shell Status:")
print(f" Listening: {'Yes' if self.is_listening else 'No'}")
print(f" Processing: {'Yes' if self.is_processing else 'No'}")
print(f" Shell Running: {'Yes' if self.shell_bridge.is_running else 'No'}")
if self.command_processor:
history = self.command_processor.get_command_history()
print(f" Commands Executed: {len(history)}")
if history:
print(" Recent Commands:")
for cmd in history[-3:]: # Show last 3 commands
print(f" - {cmd['voice_text']} → {cmd['mapped_command']}")
def run(self) -> None:
"""Start the enhanced voice-controlled shell"""
print("🎤 Starting Enhanced Voice-Controlled Mini Bash...")
print("Press Ctrl+C to exit")
try:
# Start shell
if not self.start_shell():
return
# Start voice processing
self.process_voice_command()
except KeyboardInterrupt:
print("\n👋 Voice control stopped!")
finally:
# Cleanup
self.stop_shell()
def main():
"""Main entry point"""
print("🔥 Advanced Mini Bash - Enhanced Voice Control (Phase 3)")
print("=" * 70)
# Check if Mini Bash exists
if not os.path.exists("./mini-bash"):
print("❌ Mini Bash executable not found!")
print("Please run 'make' first to build the shell")
sys.exit(1)
# Check Google Cloud credentials
if not os.path.exists("credentials.json"):
print("❌ Google Cloud credentials not found!")
print("Please place your credentials.json file in the current directory")
print("Get it from: https://console.cloud.google.com/apis/credentials")
sys.exit(1)
# Set environment variable for credentials
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "credentials.json"
# Start enhanced voice control
voice_shell = EnhancedVoiceShell()
voice_shell.run()
if __name__ == "__main__":
main()