-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
62 lines (54 loc) · 2.09 KB
/
Copy pathutils.py
File metadata and controls
62 lines (54 loc) · 2.09 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
import re
import os
try:
from elevenlabs.client import ElevenLabs
from elevenlabs import Voice, VoiceSettings
except ImportError:
print("ElevenLabs not installed or import failed.")
def text_to_speech(text: str):
"""
Converts text to speech using ElevenLabs API (v2+).
Returns audio bytes or None if API key is missing.
"""
api_key = os.getenv("ELEVENLABS_API_KEY")
if not api_key:
print("Warning: ELEVENLABS_API_KEY not set.")
return None
try:
print(f"🔊 Generating speech for: '{text[:20]}...'")
client = ElevenLabs(api_key=api_key)
# Use the correct method for v2+ client
audio_generator = client.text_to_speech.convert(
text=text,
voice_id="EXAVITQu4vr4xnSDxMaL", # Bella voice ID
model_id="eleven_monolingual_v1"
)
# Consume generator to get full bytes
audio_bytes = b"".join(list(audio_generator))
print(f"✅ Speech generated: {len(audio_bytes)} bytes")
return audio_bytes
except Exception as e:
print(f"❌ Error generating speech: {e}")
import traceback
traceback.print_exc()
return None
def map_sound_to_vibration(sound_label: str):
"""
Maps a sound classification label to a vibration pattern.
Returns a description of the pattern (e.g., 'short-short', 'long').
"""
mapping = {
"dog_bark": {"pattern": "short-short", "intensity": "medium"},
"car_horn": {"pattern": "strong-pulse", "intensity": "high"},
"alarm": {"pattern": "long-continuous", "intensity": "high"},
"doorbell": {"pattern": "double-pulse", "intensity": "medium"},
"siren": {"pattern": "rapid-pulse", "intensity": "high"},
"cat_meow": {"pattern": "soft-short", "intensity": "low"},
"glass_breaking": {"pattern": "sharp-pulse", "intensity": "high"}
}
return mapping.get(sound_label, {"pattern": "default", "intensity": "low"})
def clean_text(text: str):
"""
Basic text cleaning.
"""
return re.sub(r'\s+', ' ', text).strip()