MCPcopy Create free account
hub / github.com/heygen-com/hyperframes / extract

Function extract

skills/hyperframes-creative/scripts/extract-audio-data.py:88–163  ·  view source on GitHub ↗

Extract per-frame audio data.

(path: str, fps: int, n_bands: int)

Source from the content-addressed store, hash-verified

86
87
88def extract(path: str, fps: int, n_bands: int) -> dict:
89 """Extract per-frame audio data."""
90 print(f"Decoding audio from {path}...", file=sys.stderr)
91 samples = decode_audio(path)
92 duration = len(samples) / SAMPLE_RATE
93 frame_step = SAMPLE_RATE // fps
94 total_frames = int(duration * fps)
95
96 print(f"Duration: {duration:.1f}s, {total_frames} frames at {fps}fps", file=sys.stderr)
97 print(f"FFT window: {FFT_SIZE} samples ({SAMPLE_RATE / FFT_SIZE:.1f} Hz/bin)", file=sys.stderr)
98 print(f"Frequency range: {MIN_FREQ:.0f}-{MAX_FREQ:.0f} Hz, {n_bands} bands", file=sys.stderr)
99
100 # Precompute constants
101 hann = np.hanning(FFT_SIZE)
102 band_edges = compute_band_edges(n_bands)
103 freq_per_bin = SAMPLE_RATE / FFT_SIZE
104 n_bins = FFT_SIZE // 2 + 1
105 half_fft = FFT_SIZE // 2
106
107 # Pass 1: extract raw values
108 rms_values = np.zeros(total_frames)
109 band_values = np.zeros((total_frames, n_bands))
110
111 for f in range(total_frames):
112 # RMS from the frame's audio slice
113 rms_start = f * frame_step
114 rms_end = rms_start + frame_step
115 frame_slice = samples[rms_start:min(rms_end, len(samples))]
116 if len(frame_slice) > 0:
117 rms_values[f] = np.sqrt(np.mean(frame_slice ** 2))
118
119 # FFT from a centered 4096-sample window
120 center = rms_start + frame_step // 2
121 win_start = center - half_fft
122 win_end = center + half_fft
123
124 if win_start >= 0 and win_end <= len(samples):
125 window = samples[win_start:win_end] * hann
126 else:
127 # Zero-pad at edges
128 padded = np.zeros(FFT_SIZE)
129 src_start = max(0, win_start)
130 src_end = min(len(samples), win_end)
131 dst_start = src_start - win_start
132 dst_end = dst_start + (src_end - src_start)
133 padded[dst_start:dst_end] = samples[src_start:src_end]
134 window = padded * hann
135
136 band_values[f] = compute_fft_bands(window, freq_per_bin, n_bins, band_edges, n_bands)
137
138 # Pass 2: normalize
139 peak_rms = rms_values.max() if total_frames > 0 else 1.0
140 if peak_rms > 0:
141 rms_values /= peak_rms
142
143 # Per-band normalization so treble is visible alongside louder bass
144 band_peaks = band_values.max(axis=0)
145 band_peaks[band_peaks == 0] = 1.0

Callers 1

mainFunction · 0.85

Calls 4

decode_audioFunction · 0.85
compute_band_edgesFunction · 0.85
compute_fft_bandsFunction · 0.85
roundFunction · 0.50

Tested by

no test coverage detected