02-ago: [proyecto] vigilancia - fix sirena en Pi: pitido con math en vez de numpy (numpy roto en ARM). Desplegado en la Pi.

This commit is contained in:
minguezsanzjuanjose
2026-08-02 20:38:41 +02:00
parent 4006113663
commit 78950cdd07

View File

@@ -156,37 +156,37 @@ def _esperar_fin(cast, url, timeout=120):
def _generar_pitido(): def _generar_pitido():
"""Genera una sirena estilo Proteccion Civil (wail) si no existe.""" """Genera sirena estilo Proteccion Civil (wail). Usa solo math (sin numpy)."""
import math, struct
ruta = os.path.join(MEDIA_DIR, "pitido_emergencia.wav") ruta = os.path.join(MEDIA_DIR, "pitido_emergencia.wav")
if os.path.exists(ruta): if os.path.exists(ruta):
return ruta return ruta
try: try:
import numpy as np
import wave as _wave
os.makedirs(MEDIA_DIR, exist_ok=True) os.makedirs(MEDIA_DIR, exist_ok=True)
sr = 44100 sr = 44100
dur = config.GOOGLE_TV_PITIDO_SG dur = config.GOOGLE_TV_PITIDO_SG
t = np.linspace(0, dur, int(sr * dur), endpoint=False) n = int(sr * dur)
if config.GOOGLE_TV_PITIDO_ESTILO == "wail": periodo = 4.0
# wail defensa civil: sube 400->1000 y baja en ciclos de ~4s nfade = min(int(0.3 * sr), n // 4)
periodo = 4.0 phase = 0.0
fase = (t % periodo) / periodo samples = []
freq = 400 + 600 * (1 - abs(2 * fase - 1)) for i in range(n):
else: t = i / sr
freq = 500 + 400 * np.abs(np.sin(2 * np.pi * 0.5 * t)) if config.GOOGLE_TV_PITIDO_ESTILO == "wail":
fase = 2 * np.pi * np.cumsum(freq) / sr f = (t % periodo) / periodo
y = 0.8 * np.sin(fase) freq = 400 + 600 * (1 - abs(2 * f - 1))
fade = min(0.3, dur / 4) else:
n_fade = int(fade * sr) freq = 500 + 400 * abs(math.sin(2 * math.pi * 0.5 * t))
y[:n_fade] *= np.linspace(0, 1, n_fade) phase += 2 * math.pi * freq / sr
y[-n_fade:] *= np.linspace(1, 0, n_fade) y = 0.8 * math.sin(phase)
data = (y * 32767).astype(np.int16) if i < nfade: y *= i / nfade
with _wave.open(ruta, "w") as w: elif i > n - nfade: y *= (n - i) / nfade
w.setnchannels(1) samples.append(struct.pack("<h", max(-32768, min(32767, int(y * 32767)))))
w.setsampwidth(2) import wave as _wave
w.setframerate(sr) with open(ruta, "wb") as f:
w.writeframes(data.tobytes()) with _wave.open(f, "w") as w:
w.setnchannels(1); w.setsampwidth(2); w.setframerate(sr)
w.writeframes(b"".join(samples))
except Exception as e: except Exception as e:
print(f"[Voz] No se pudo generar el pitido: {e}") print(f"[Voz] No se pudo generar el pitido: {e}")
return ruta return ruta