Files
uj-mastering-master/master_core.py
T
Mikkeli Matlock d7782bb9d9 Speed up analysis: drop BPM, vectorise loudness/true-peak, share short-term
Profiled hot spots on a 4-min track and cut the worst offenders:

- Remove BPM: librosa.beat.beat_track ran on every load (~3.7s) for a number no
  better than tapping by hand. Dropped from AudioFile + the metadata panel.
- LUFS short-term: replace 474 per-window pyloudnorm.integrated_loudness calls
  with one K-weighting pass (reusing pyloudnorm's own filter coefficients) + a
  vectorised sliding mean-square. This is true *ungated* EBU R128 short-term
  (the old loop wrongly gated each 3s window). Integrated + LRA still use
  pyloudnorm's gated calls. ~3.8s -> ~1.9s.
- PSR: reuse LUFS's short-term series (memoised on the AudioFile) + vectorised
  sample-peak. ~3.0s -> ~0.2s.
- True Peak: oversample the whole signal once, then an O(N) running max over
  windows instead of per-window resample_poly. Bit-identical to the old loop
  (max|diff| 0.0000 dB). ~2.1s -> ~1.1s.
- Crest Factor: peaks via the same O(N) running max (last per-window loop gone).

lufs+psr+true_peak: ~9.2s -> ~3.2s, plus ~3.7s of BPM removed from every load.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-14 01:36:08 +09:00

63 lines
2.1 KiB
Python

import os
import librosa
import numpy as np
from mutagen.mp3 import MP3
from mutagen.easyid3 import EasyID3
from font_manager import safe_title
def _try_mp3_tags(file_path):
try:
return MP3(file_path, ID3=EasyID3)
except Exception:
return None
class AudioFile:
def __init__(self, file_path):
self.file_path = file_path
audio = _try_mp3_tags(self.file_path)
artist = audio.get('artist', [None])[0] if audio is not None else None
title = audio.get('title', [None])[0] if audio is not None else None
if artist and title:
self.song_name = safe_title(f"{artist} - {title}")
else:
self.song_name = safe_title(os.path.basename(self.file_path))
# librosa.load normalises to [-1.0, 1.0]. sr=None preserves the file's
# native sample rate; without it librosa resamples to 22050 Hz, which would
# discard everything above ~11 kHz (the entire top octave) before analysis.
self.y, self.sr = librosa.load(file_path, sr=None)
self.y_mono = librosa.to_mono(self.y)
self.max_amplitude = np.max(np.abs(self.y_mono))
self.avg_amplitude = np.mean(np.abs(self.y_mono))
# BPM intentionally not computed: librosa.beat.beat_track cost ~3.7s on a
# 4-min track for a number that's no better than tapping it by hand.
def get_energy_levels_over_time(self, window=10, hop=2):
"""Compute rolling RMS power.
Args:
window: Rolling window length in seconds.
hop: Hop length in seconds.
"""
if (not hasattr(self, 'window')) or (self.window != window) or (self.hop != hop):
self.window, self.hop = window, hop
if not hasattr(self, 'rms_array'):
window_samples = window * self.sr
hop_samples = hop * self.sr
self.rms_array = librosa.feature.rms(
y=self.y, frame_length=window_samples, hop_length=hop_samples
)
def get_times(self):
"""Time-axis values matching the RMS frames."""
if not hasattr(self, 'rms_array'):
self.get_energy_levels_over_time()
return librosa.frames_to_time(
np.arange(self.rms_array.shape[1]), sr=self.sr, hop_length=self.hop * self.sr
)