video-vec2wav2-tokenizer / tests /test_features.py
k9cli's picture
Flatten package contents to repo root
de01f6a verified
Raw History Blame Contribute Delete
1.3 kB
"""Tests for feature extraction and the binary store."""
from __future__ import annotations
import json
from pathlib import Path
import numpy as np
from video_vec2wav2_tokenizer.features.extractor import (
FeatureExtractor,
compute_mel_spectrogram,
read_feature_record,
)
def test_compute_mel_shape(sine_wav: Path) -> None:
from video_vec2wav2_tokenizer.audio.extractor import load_wav
samples, sr = load_wav(sine_wav)
mel = compute_mel_spectrogram(samples, sr, n_mels=80)
assert mel.shape[0] == 80
assert mel.dtype == np.float32
assert mel.shape[1] > 0
def test_feature_store_roundtrip(sine_wav: Path, tmp_path: Path) -> None:
fx = FeatureExtractor(sample_rate=16000, n_mels=40)
features_dir = tmp_path / "features"
dat_path = fx.extract_dataset([sine_wav, sine_wav], features_dir)
bin_path = features_dir / "train.bin"
assert bin_path.exists()
assert dat_path.exists()
records = [json.loads(line) for line in dat_path.read_text().splitlines() if line]
assert len(records) == 2
audio, mel = read_feature_record(bin_path, records[0])
assert audio.dtype == np.float32
assert mel.shape == tuple(records[0]["mel_shape"])
assert records[0]["sample_rate"] == 16000
assert records[0]["duration"] > 0