Skip to content

Instantly share code, notes, and snippets.

@fakerybakery
Created December 15, 2025 00:59
Show Gist options
  • Select an option

  • Save fakerybakery/ed9136eb042764877f7025152c6f4d08 to your computer and use it in GitHub Desktop.

Select an option

Save fakerybakery/ed9136eb042764877f7025152c6f4d08 to your computer and use it in GitHub Desktop.
import numpy as np
import soundfile as sf
import librosa
file_a = "a.wav"
file_b = "b.wav"
# config
target_sr = 16000
chunk_ms = 50
threshold = 0.02
min_gap_ms = 100
# load & normalize
a, sr_a = sf.read(file_a, dtype='float32')
b, sr_b = sf.read(file_b, dtype='float32')
if a.ndim > 1: a = a.mean(axis=1)
if b.ndim > 1: b = b.mean(axis=1)
if sr_a != target_sr: a = librosa.resample(a, orig_sr=sr_a, target_sr=target_sr)
if sr_b != target_sr: b = librosa.resample(b, orig_sr=sr_b, target_sr=target_sr)
# pad to same length
max_len = max(len(a), len(b))
a = np.pad(a, (0, max_len - len(a)))
b = np.pad(b, (0, max_len - len(b)))
# chunk & compare
chunk_samples = int(target_sr * chunk_ms / 1000)
n_chunks = max_len // chunk_samples
trim = n_chunks * chunk_samples
a = a[:trim].reshape(n_chunks, chunk_samples)
b = b[:trim].reshape(n_chunks, chunk_samples)
diff_rms = np.sqrt(np.mean((a - b) ** 2, axis=1))
max_rms = np.maximum(np.sqrt(np.mean(a**2, axis=1)), np.sqrt(np.mean(b**2, axis=1))) + 1e-10
diff_mask = (diff_rms > threshold) | (diff_rms / max_rms > 0.5)
# merge nearby diffs into regions
diff_idx = np.where(diff_mask)[0]
min_gap = int(min_gap_ms / chunk_ms)
regions = []
if len(diff_idx):
start = end = diff_idx[0]
for i in diff_idx[1:]:
if i - end <= min_gap:
end = i
else:
regions.append((start * chunk_ms / 1000, (end + 1) * chunk_ms / 1000))
start = end = i
regions.append((start * chunk_ms / 1000, (end + 1) * chunk_ms / 1000))
for s, e in regions:
print(f"{s:.3f} - {e:.3f} ({e-s:.3f}s)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment