Download code/audio_sync.py from laion/tyger-speaker-predictor: direct link, hf CLI and curl.
- Browser
- Download file 1.12 kB
-
https://huggingface.co/laion/tyger-speaker-predictor/resolve/main/code/audio_sync.py
- Command line
-
hf download hf://laion/tyger-speaker-predictor/code/audio_sync.py
-
curl -L -o audio_sync.py https://huggingface.co/laion/tyger-speaker-predictor/resolve/main/code/audio_sync.py
1.12 kB
| """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<sr//2 or np.linalg.norm(mic)<1e-5 or np.linalg.norm(reference)<1e-5: | |
| return {'available':False,'reason':'No measurable reference/echo','confidence':0.} | |
| corr=correlate(mic,reference,mode='full',method='fft');lags=correlation_lags(n,n) | |
| keep=(lags>=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'} | |