-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaudio_utils.py
More file actions
129 lines (108 loc) · 5.1 KB
/
audio_utils.py
File metadata and controls
129 lines (108 loc) · 5.1 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
import subprocess
import glob
import random
import os
import time
import numpy as np
import sounddevice as sd
import webrtcvad
import collections
import config
import hashlib
import traceback
# Diretório para guardar os ficheiros de áudio gerados
TTS_CACHE_DIR = "/opt/phantasma/cache/tts"
def clean_old_cache(days=30):
""" Remove ficheiros da cache que sejam mais antigos que 'days'. """
if not os.path.exists(TTS_CACHE_DIR): return
print(f"Manutenção: A verificar limpeza de cache TTS (> {days} dias)...")
now = time.time()
cutoff = days * 86400
try:
for f in os.listdir(TTS_CACHE_DIR):
f_path = os.path.join(TTS_CACHE_DIR, f)
if os.path.isfile(f_path) and (now - os.stat(f_path).st_mtime > cutoff):
os.remove(f_path)
except Exception as e: print(f"ERRO ao limpar cache: {e}")
def play_tts(text, use_cache=True):
""" Converte texto em voz (Lógica restaurada com Cache e SoX). """
if not text: return
text_cleaned = text.replace('**', '').replace('*', '').replace('#', '').replace('`', '').strip()
print(f"IA: {text_cleaned}")
if use_cache:
os.makedirs(TTS_CACHE_DIR, exist_ok=True)
file_hash = hashlib.md5(text_cleaned.encode('utf-8')).hexdigest()
cache_path = os.path.join(TTS_CACHE_DIR, f"{file_hash}.wav")
if os.path.exists(cache_path):
try:
subprocess.run(['aplay', '-D', config.ALSA_DEVICE_OUT, '-q', cache_path], check=False)
return
except: pass
try:
p1 = subprocess.Popen(['piper', '--model', config.TTS_MODEL_PATH, '--output-raw'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
p2 = subprocess.Popen(['sox', '-t', 'raw', '-r', '22050', '-e', 'signed-integer', '-b', '16', '-c', '1', '-', cache_path, 'flanger', '1', '1', '5', '50', '1', 'sin', 'tempo', '0.9'], stdin=p1.stdout)
p1.stdin.write(text_cleaned.encode('utf-8')); p1.stdin.close(); p2.wait()
if os.path.exists(cache_path):
subprocess.run(['aplay', '-D', config.ALSA_DEVICE_OUT, '-q', cache_path], check=False)
except: pass
else:
try:
p1 = subprocess.Popen(['piper', '--model', config.TTS_MODEL_PATH, '--output-raw'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
p2 = subprocess.Popen(['sox', '-t', 'raw', '-r', '22050', '-e', 'signed-integer', '-b', '16', '-c', '1', '-', '-t', 'wav', '-', 'flanger', '1', '1', '5', '50', '1', 'sin', 'tempo', '0.9'], stdin=p1.stdout, stdout=subprocess.PIPE)
p3 = subprocess.Popen(['aplay', '-D', config.ALSA_DEVICE_OUT, '-q'], stdin=p2.stdout)
p1.stdin.write(text_cleaned.encode('utf-8')); p1.stdin.close(); p3.wait()
except: pass
def play_random_music_snippet():
try:
mp3s = glob.glob(os.path.join('/home/media/music', '**/*.mp3'), recursive=True)
if mp3s: subprocess.Popen(['mpg123', '-q', '-n', '45', random.choice(mp3s)], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).wait()
except: pass
def play_random_song_full():
try:
mp3s = glob.glob(os.path.join('/home/media/music', '**/*.mp3'), recursive=True)
if not mp3s: return False
subprocess.Popen(['mpg123', '-q', random.choice(mp3s)], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
return True
except: return False
def record_audio():
"""
A Lógica VAD Original (Afinada).
Usa Vad(2) e espera 1.5s de silêncio.
Não aceita device_index (Usa Default do Sistema).
"""
print("A ouvir...")
vad = webrtcvad.Vad(2)
frame_duration_ms = 30
samples_per_frame = int(config.MIC_SAMPLERATE * frame_duration_ms / 1000)
silence_threshold_seconds = 1.5
max_duration_seconds = 10.0
frames = []
silence_counter = 0
speech_detected = False
chunks_per_second = 1000 // frame_duration_ms
silence_limit_chunks = int(silence_threshold_seconds * chunks_per_second)
max_chunks = int(max_duration_seconds * chunks_per_second)
try:
# Usa o DEFAULT do sistema (sem device=...)
with sd.InputStream(samplerate=config.MIC_SAMPLERATE, channels=1, dtype='int16') as stream:
for _ in range(max_chunks):
audio_chunk, overflowed = stream.read(samples_per_frame)
if overflowed: pass
audio_bytes = audio_chunk.tobytes()
is_speech = vad.is_speech(audio_bytes, config.MIC_SAMPLERATE)
if is_speech:
silence_counter = 0
speech_detected = True
else:
silence_counter += 1
frames.append(audio_chunk.flatten().astype(np.float32) / 32768.0)
if speech_detected and silence_counter > silence_limit_chunks:
print("Fim de fala detetado.")
break
print("Gravação terminada.")
if not speech_detected:
return np.array([], dtype='float32')
return np.concatenate(frames)
except Exception as e:
print(f"ERRO Gravação VAD: {e}")
return np.array([], dtype='float32')