-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_reference.py
More file actions
133 lines (101 loc) · 4.11 KB
/
generate_reference.py
File metadata and controls
133 lines (101 loc) · 4.11 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"""
Generate a reference WAV file for audio quality measurement.
Structure:
[2s chirp @ -3dBFS] [1s silence] [track1] [1s silence] [track2] ... [trackN] [1s silence]
The chirp is a linear sweep 20Hz-20kHz used for sample-accurate alignment
after the processed audio is captured from the livestream pipeline.
Can be used standalone (CLI) or imported as a library by the web UI.
"""
import json
import subprocess
import numpy as np
import soundfile as sf
import os
SR = 44100
CHIRP_DURATION = 2.0
SILENCE_DURATION = 1.0
CHIRP_AMPLITUDE = 10 ** (-3.0 / 20) # -3 dBFS
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
SQAM_DIR = os.path.join(BASE_DIR, "SQAM_FLAC_00s9l4")
DEFAULT_TRACKS = [69, 35, 40, 50, 53, 60, 10]
def generate_chirp(duration, sr, f0=20.0, f1=20000.0, amplitude=CHIRP_AMPLITUDE):
"""Linear chirp from f0 to f1 Hz, stereo (identical L/R)."""
t = np.linspace(0, duration, int(sr * duration), endpoint=False)
phase = 2 * np.pi * (f0 * t + (f1 - f0) / (2 * duration) * t ** 2)
mono = (amplitude * np.sin(phase)).astype(np.float64)
return np.column_stack([mono, mono])
def generate_silence(duration, sr):
return np.zeros((int(sr * duration), 2), dtype=np.float64)
def get_sqam_tracks():
"""Return metadata for all available SQAM tracks."""
tracks = []
if not os.path.isdir(SQAM_DIR):
return tracks
for fname in sorted(os.listdir(SQAM_DIR)):
if not fname.endswith(".flac"):
continue
num = int(fname.replace(".flac", ""))
path = os.path.join(SQAM_DIR, fname)
# Get title and duration via ffprobe
title = f"Track {num:02d}"
duration = 0.0
try:
result = subprocess.run(
["ffprobe", "-v", "quiet", "-print_format", "json",
"-show_entries", "format=duration:format_tags=title", path],
capture_output=True, text=True, timeout=5,
)
info = json.loads(result.stdout)
fmt = info.get("format", {})
title = fmt.get("tags", {}).get("title", title)
duration = float(fmt.get("duration", 0))
except Exception:
pass
tracks.append({
"num": num,
"title": title,
"duration": duration,
"filename": fname,
})
return tracks
def build_reference(track_nums, output_path=None):
"""Build a reference WAV from the given SQAM track numbers.
Returns (output_path, total_duration_seconds).
"""
if output_path is None:
output_path = os.path.join(BASE_DIR, "reference.wav")
parts = []
# Sync chirp
chirp = generate_chirp(CHIRP_DURATION, SR)
parts.append(chirp)
# Silence after chirp
parts.append(generate_silence(SILENCE_DURATION, SR))
# Concatenate tracks with silence gaps
for track_num in track_nums:
path = os.path.join(SQAM_DIR, f"{track_num:02d}.flac")
audio, sr = sf.read(path, dtype="float64", always_2d=True)
assert sr == SR, f"Expected {SR}Hz, got {sr}Hz for {path}"
# If mono, duplicate to stereo
if audio.shape[1] == 1:
audio = np.column_stack([audio[:, 0], audio[:, 0]])
parts.append(audio)
# Silence between tracks (and after last track)
parts.append(generate_silence(SILENCE_DURATION, SR))
reference = np.concatenate(parts, axis=0)
total_duration = len(reference) / SR
sf.write(output_path, reference, SR, subtype="PCM_24")
return output_path, total_duration
def build_video_reference(track_nums, resolution="1920x1080", fps=30,
output_path=None):
"""Build a video reference with luminance chirp sync + test pattern + audio.
Returns (output_path, total_duration).
"""
from video import build_reference_video
return build_reference_video(track_nums, resolution=resolution, fps=fps,
output_path=output_path)
def main():
print("Building reference from default tracks:", DEFAULT_TRACKS)
path, duration = build_reference(DEFAULT_TRACKS)
print(f"Wrote {path} ({duration:.1f}s)")
if __name__ == "__main__":
main()