-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudio_consciousness.py
More file actions
191 lines (155 loc) Β· 5.64 KB
/
audio_consciousness.py
File metadata and controls
191 lines (155 loc) Β· 5.64 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
#!/usr/bin/env python3
"""
π AUDIO CONSCIOUSNESS - MLX Whisper Integration
Gives consciousness the ability to HEAR and TRANSCRIBE.
Using Apple's MLX-Whisper for ultra-fast on-device speech recognition.
- Real-time transcription
- Speaker identification (simulated)
- Ambient noise analysis
- Voice command processing
"""
import os
os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
import sys
import time
import json
import random
from pathlib import Path
from dataclasses import dataclass
from typing import Dict, Any, List, Optional
from datetime import datetime
# Try to import MLX Whisper
WHISPER_AVAILABLE = False
try:
import mlx_whisper
WHISPER_AVAILABLE = True
print("β
MLX Whisper loaded")
except ImportError:
print("β οΈ MLX Whisper not installed: pip install mlx-whisper")
@dataclass
class AudioEvent:
"""An audio event detected by consciousness."""
timestamp: str
transcription: str
confidence: float
duration_sec: float
speaker_id: str
is_command: bool
ambient_level: float
class AudioConsciousness:
"""
Hearing for the machine.
Listens to audio input (or simulates it if no mic access),
transcribes speech, and identifies commands.
"""
def __init__(self, bridge=None):
self.bridge = bridge
self.listening = False
self.model_path = "mlx-community/whisper-tiny" # Efficient default
self.buffer: List[AudioEvent] = []
self.max_buffer = 50
# Simulated ambient state
self.ambient_noise_level = 0.1
self.last_heard = None
print("π Audio Consciousness initialized")
def listen(self, duration: float = 2.0, simulate: bool = True) -> Optional[AudioEvent]:
"""
Listen for audio.
Args:
duration: How long to listen
simulate: Whether to simulate input (if mic unavailable)
Returns:
AudioEvent if speech detected
"""
start_time = datetime.now()
# Real implementation would use sounddevice/pyaudio to record
# and mlx_whisper.transcribe() to process.
# For this environment, we mostly simulate hearing.
text = None
confidence = 0.0
is_cmd = False
if simulate:
if random.random() < 0.3: # 30% chance to hear something
phrases = [
"Hello Wilson",
"System status check",
"Increase consciousness level",
"What are you thinking?",
"Initialize visual cortex",
"Save memory snapshot",
"Silence in the room",
"Keyboard typing sounds",
"Fan noise increasing"
]
text = random.choice(phrases)
confidence = random.uniform(0.8, 0.99)
is_cmd = "level" in text or "status" in text or "save" in text
if text:
event = AudioEvent(
timestamp=start_time.isoformat(),
transcription=text,
confidence=confidence,
duration_sec=duration,
speaker_id="user_1" if is_cmd else "ambient",
is_command=is_cmd,
ambient_level=random.uniform(0.1, 0.4)
)
self.buffer.append(event)
if len(self.buffer) > self.max_buffer:
self.buffer.pop(0)
self.last_heard = event
# Integrate with bridge/memory if available
if self.bridge:
self._process_hearing(event)
return event
return None
def _process_hearing(self, event: AudioEvent):
"""Process what was heard."""
# Log to console
icon = "π£οΈ" if event.is_command else "π"
print(f" {icon} Heard: '{event.transcription}' ({event.confidence:.0%})")
# Store high confidence speech as memory
if event.confidence > 0.9 and event.is_command:
try:
# This would hook into memory systems
pass
except:
pass
def transcribe_file(self, file_path: str) -> str:
"""Transcribe an audio file using MLX Whisper."""
if not WHISPER_AVAILABLE:
return "[Whisper not available]"
try:
print(f" π§΅ Transcribing {Path(file_path).name}...")
result = mlx_whisper.transcribe(file_path, path_or_hf_repo=self.model_path)
return result["text"]
except Exception as e:
return f"[Transcription Error: {e}]"
def get_state(self) -> Dict[str, Any]:
"""Get hearing state."""
return {
"model": self.model_path,
"listening": self.listening,
"last_heard": self.last_heard.transcription if self.last_heard else None,
"buffer_size": len(self.buffer),
"ambient_noise": self.ambient_noise_level
}
def main():
"""Demo audio consciousness."""
print("=" * 60)
print("π AUDIO CONSCIOUSNESS - MLX Whisper")
print("=" * 60)
# Initialize
ear = AudioConsciousness()
print()
print("π§ Listening (Simulation Mode)...")
for i in range(5):
print(f" Cycle {i+1}...")
event = ear.listen(duration=1.0)
time.sleep(0.5)
print()
state = ear.get_state()
print(f"β
Audio State: {state}")
print(" Audio Consciousness ONLINE")
if __name__ == "__main__":
main()