File size: 1,297 Bytes
de01f6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""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