manus-skills/.kortix/opencode/skills/media/clipify/scripts/audio_align.py
zhukang 1c7abd81d1 All 310 Manus skill-hub community skills as a Kortix marketplace registry
- 310 registry:skill items, 1528 files, 7 categories (hub tags)
- registry.json generated by @kortix/registry buildRegistry
- nested SKILL.md in sn-da-* renamed SUBSKILL.md (no phantom skills)
- provenance + licenses: SOURCES.md
2026-09-15 13:37:20 +08:00

28 lines
951 B
Python

#!/usr/bin/env python3
"""Find time offset of a short clip within a longer source via FFT cross-correlation.
Usage: audio_align.py CLIP_PCM SRC_PCM SRC_WINDOW_OFFSET_S
Both files must be int16 mono PCM at 8000 Hz.
SRC_WINDOW_OFFSET_S is the absolute time of the start of SRC_PCM in the original source.
Stdout: absolute offset (seconds) of CLIP_PCM start within the original source.
"""
import numpy as np, sys
SR = 8000
clip = np.fromfile(sys.argv[1], dtype=np.int16).astype(np.float32)
src = np.fromfile(sys.argv[2], dtype=np.int16).astype(np.float32)
window_off = float(sys.argv[3])
clip = (clip - clip.mean()) / (clip.std() + 1e-9)
src = (src - src.mean()) / (src.std() + 1e-9)
n = len(src) + len(clip)
p = 1
while p < n: p *= 2
S = np.fft.rfft(src, p)
C = np.fft.rfft(clip[::-1], p)
corr = np.fft.irfft(S * C, p)[:n-1]
peak = int(np.argmax(corr))
offset_in_window = (peak - (len(clip) - 1)) / SR
print(f"{window_off + offset_in_window:.3f}")