Documentation
¶
Overview ¶
Package quality holds the objective audio quality metrics, alignment, WAV I/O, and deterministic test programs behind the tools/quality harness, which compares this project's MP3 encoder against LAME (used strictly as a black-box binary; see PROVENANCE.md).
Nothing here runs on any encode or decode path, so the package is free to use libm transcendentals and carries no cross-architecture bit-exactness requirement: it measures, it does not produce output that is golden-pinned. It is a normal (non-_test) package so both tools/quality and its tests can import it.
Index ¶
- Constants
- func AlignLag(ref, deg []float64, minLag, maxLag int) int
- func Bandwidth(x []float64, sampleRate int) float64
- func PreEcho(ref, deg []float64, sampleRate int) (meanDB float64, events int)
- func Quantize16(x []float64) []float64
- func ReadWAV(r io.Reader) (sampleRate int, ch [][]float64, err error)
- func SNR(ref, deg []float64) float64
- func SegmentalSNR(ref, deg []float64, seg int) float64
- func SpectralMetrics(ref, deg []float64, sampleRate int) (bandSNR, lsd float64)
- func WriteWAV16(w io.Writer, sampleRate int, ch [][]float64) error
- type Metrics
- type Program
Constants ¶
const ( // SNRCap is the SNR reported when the error energy is zero or below // epsilon times the signal energy (identical or numerically identical // signals). SNRCap = 120.0 // BandLimitHz bounds the STFT bins BandSNR and LSD are computed over, so // LAME's deliberate high-frequency lowpass at lower bitrates is not // counted as noise there (full-band SNR still sees it). BandLimitHz = 16000.0 // SegSNRMin and SegSNRMax clamp per-segment SNR (the segmental-SNR // convention) so one silent or one destroyed segment cannot dominate // the mean. SegSNRMin = -10.0 SegSNRMax = 35.0 // SegSNRSegment is the segmental-SNR segment length in samples: one // MPEG-1 Layer III frame. SegSNRSegment = 1152 // BandwidthFloorDB is how far below the long-term PSD peak a bin may sit // and still count as "in band". BandwidthFloorDB = 50.0 )
Tunables of the Go-native metrics. The exported ones appear in reports so a reader knows which definitions the numbers were computed under.
Variables ¶
This section is empty.
Functions ¶
func AlignLag ¶
AlignLag returns the lag, in [minLag, maxLag], that maximizes the normalized cross-correlation between ref and deg, where a positive lag means deg is delayed relative to ref (deg[i+lag] tracks ref[i]). It returns 0 when nothing correlates positively, so a silent or unrelated pair is left unshifted rather than trimmed at an arbitrary lag.
The correlation is normalized by the energy of BOTH windows, not just by their length. That is what makes the result trustworthy on a periodic program: a bare sum, or a mean product, is not bounded by the aligned value, so a lag one signal period away whose own window happens to carry more energy can outscore the true delay. The harness measured a 440 Hz program at 3262 samples instead of 1057, exactly 22 periods late, and then scored every metric on misaligned audio and reported it as valid. Cauchy and Schwarz bound the normalized form by 1, reached only at true alignment.
The correlation runs over the first alignSamples reference samples (or fewer when the inputs are shorter). Lags whose overlap with deg is under half that window are skipped: a short overlap leaves too few samples for the ratio to mean anything, and it can reach 1 by coincidence.
func Bandwidth ¶
Bandwidth returns the frequency in Hz of the highest bin whose long-term average power spectrum lies within BandwidthFloorDB of the spectrum's peak. It reports 0 for an all-silent or too-short signal.
func PreEcho ¶
PreEcho detects attacks in ref (a preEchoWindowMs window whose energy exceeds preEchoRatio times the previous window's and preEchoFloor in mean-square, at least preEchoHoldoffMs after the previous counted attack and at least preEchoPreMs into the signal) and, for each, measures the error energy in the preEchoPreMs before the attack window relative to the attack window's reference energy, in dB. It returns the mean over attacks and the attack count; the mean is NaN when no attack is found. Lower is better: a clean encoder sits far below 0 dB, audible pre-echo approaches it.
func Quantize16 ¶
Quantize16 rounds every sample to the nearest 16-bit PCM code (clamping to the int16 rails) and returns the dequantized values in [-1, 32767/32768], exactly the signal a reader of WriteWAV16's output sees. The harness feeds this quantized signal to both encoders so they start from identical input.
func ReadWAV ¶
ReadWAV parses a RIFF/WAVE file with PCM 16/24/32-bit or IEEE float32 samples (plain or WAVE_FORMAT_EXTENSIBLE) into planar float64 channels in [-1, 1]. Unknown chunks are skipped. It reads the whole input into memory.
func SNR ¶
SNR is the full-band signal-to-noise ratio of deg against ref in dB over their common length.
func SegmentalSNR ¶
SegmentalSNR is the mean over active seg-sample segments (reference mean-square above the active floor) of the per-segment SNR clamped to [SegSNRMin, SegSNRMax]. It returns NaN when no segment is active.
func SpectralMetrics ¶
SpectralMetrics returns the band-limited SNR (bins at or below BandLimitHz, all frames pooled) and the mean log-spectral distance over active frames (in-band reference power above the active floor), both in dB. LSD is NaN when no frame is active.
Types ¶
type Metrics ¶
type Metrics struct {
SNR float64 // dB, full band, capped at SNRCap
BandSNR float64 // dB, STFT bins at or below BandLimitHz
SegSNR float64 // dB, mean per-segment SNR clamped to [SegSNRMin, SegSNRMax]; NaN when no active segment
LSD float64 // dB, mean log-spectral distance over active frames; NaN when no active frame
PreEcho float64 // dB, pre-attack error energy relative to attack energy; NaN when no attack
PreEchoN int // attacks PreEcho averaged over
Bandwidth float64 // Hz, highest long-term PSD bin within BandwidthFloorDB of the peak
}
Metrics is one channel-averaged measurement of a decoded signal against its reference. Directions: SNR, BandSNR, SegSNR higher is better; LSD and PreEcho lower is better; Bandwidth is informational.
func Compare ¶
Compare measures deg against ref (planar channels of equal length, already aligned) and averages every metric over channels, except Bandwidth, which is the maximum over channels, and PreEchoN, which is the total. NaN channel values propagate: a metric undefined on one channel is undefined for the pair.
type Program ¶
type Program struct {
Name string
Channels int
// SampleRate pins a program to one rate, and is 0 for the synthetic
// programs, which generate at any rate. A corpus WAV is only meaningful
// at its own rate, and resampling it would change what is measured, so
// the caller skips the rates it does not serve rather than failing them.
SampleRate int
// Gen returns Channels planar float64 channels of nSamples samples each,
// every sample in [-1, 1], identical on every call. A program with a
// pinned SampleRate ignores both arguments and returns its own samples.
Gen func(sampleRate, nSamples int) [][]float64
}
Program is one deterministic test signal of the synthetic corpus. Names are stable identifiers used in reports and -programs filters.
func ProgramByName ¶
ProgramByName looks a program up by its report name.