"""Residual acoustic echo delay estimation; no recording or guessed delay.""" import numpy as np from scipy.signal import correlate,correlation_lags def estimate_echo_delay(mic,reference,sr=16000,max_delay=.45): mic=np.asarray(mic,np.float64);reference=np.asarray(reference,np.float64) n=min(len(mic),len(reference));mic=mic[:n];reference=reference[:n] if n=0)&(lags<=round(max_delay*sr));indexes=np.flatnonzero(keep) index=indexes[np.argmax(corr[keep])];lag=int(lags[index]) a=mic[lag:];b=reference[:n-lag] confidence=float(np.dot(a,b)/max(np.linalg.norm(a)*np.linalg.norm(b),1e-10)) available=confidence>=.45 return {'available':available,'delay_ms':lag/sr*1000 if available else None, 'confidence':confidence,'reason':'Measured acoustic residual' if available else 'Echo cancellation/noise makes delay uncertain'}