visqol-audio: ViSQOL v3 audio mode in pure NumPy/SciPy
A small, dependency-light port of the audio mode of Google's ViSQOL v3 (Virtual Speech Quality Objective Listener). It compares a degraded music signal against its reference and predicts a MOS-LQO score from 1 (bad) to 5 (excellent).
- Needs only
numpyandscipy. There is no C++ build, Bazel, protobuf, torch or transformers. - Follows the C++ pipeline step by step: global alignment, 32-band gammatone
spectrogram, 30-frame patches with DTW-style patch matching, per-patch fine
realignment, NSIM, and the default libSVM nu-SVR model
(
libsvm_nu_svr_model.txt, unchanged from the original repository). - Checked against the official C++ build (see Parity).
- It covers audio mode only (48 kHz, music). Speech mode is not implemented.
Usage
Option A: pip install (recommended)
pip install "git+https://huggingface.co/puar-playground/visqol"
# or, from a local checkout: pip install ./visqol
import soundfile as sf # any loader works; soundfile is not required
from visqol_audio import visqol
ref, sr = sf.read("reference.wav")
deg, _ = sf.read("degraded.wav")
mos = visqol(ref, deg, sr) # float in [1, 5]
mos, details = visqol(ref, deg, sr, return_details=True)
print(mos, details["vnsim"], details["fvnsim"].shape) # 32 per-band NSIMs
Option B: no install, using huggingface_hub only
pip install numpy scipy huggingface_hub
import sys
from huggingface_hub import snapshot_download
sys.path.insert(0, snapshot_download("puar-playground/visqol"))
from visqol_audio import visqol
mos = visqol(ref, deg, sr)
PyTorch tensors
Torch tensors (CPU or GPU, with or without grad) are accepted through duck
typing (.detach().cpu().numpy()). The package never imports torch.
import torch, torchaudio
from visqol_audio import visqol
ref, sr = torchaudio.load("reference.wav") # [channels, n]
deg, _ = torchaudio.load("degraded.wav")
mos = visqol(ref, deg, sr)
Inputs
reference,degraded: float arrays, either mono[n]or multichannel[channels, n]/[n, channels]. Multichannel input is averaged to mono, as the C++ wav loader does. Use float values in [-1, 1], as the C++ loader produces for 16-bit wav. The degraded signal is rescaled to the reference's level, so absolute gain has little effect.sr: the sample rate of both signals. ViSQOL audio mode is defined at 48 kHz, so other rates are resampled withscipy.signal.resample_poly. By default the FIR is the Hann-windowed sinc thattorchaudio.functional.resampleuses by default (width 6, rolloff 0.99), and the output matches torchaudio to about 1e-13 (resampler="sinc_hann"). Other choices areresampler="kaiser"(the scipy default filter) or your owncallable(x, sr_in, sr_out). The resampler matters for 44.1 kHz input. After upsampling, ViSQOL's top gammatone bands (about 16 to 24 kHz) see the resampler's transition band. When the degraded signal has content there that the reference lacks (for example noise added on top of an MP3-sourced reference), different resamplers can change MOS by up to about 0.1 to 0.3. Use the same resampler for every score you compare, or pass 48 kHz audio.- Signals of at least about 1 s are needed. ViSQOL was designed for clips of about 5 to 10 s.
- Options with the same defaults as the C++ API:
search_window=60,disable_global_alignment=False,disable_realignment=False,model_path=None(a different libSVM model file can be given here). VISQOL_THREADS=1turns off the thread pool used for the gammatone filterbank (default: up to 8 threads).
Parity with the C++ implementation
Parity was measured against the official C++ ViSQOL (pyvisqol bindings, audio
mode, default SVR model, search_window_radius=60) on 166 pairs of 48 kHz
inputs. The pairs use 10 s and 5 s crops of commercial songs and FMA clips with these
degradations: identical, white noise (SNR 0 to 40 dB), low-pass at 1, 3.5 and
8 kHz, MP3 at 8, 32 and 128 kbps, gain changes of 0.8x and 1.1x, time shifts
(+37 samples, +10 ms, -50 ms, +162 ms), random 100 ms dropouts, a 13 ms jump in
the middle of the clip, degraded clips 1 s shorter or zero-padded longer, quiet
signals with DC offset, and a different song as the degraded signal.
| metric | value |
|---|---|
| MOS range covered | 1.000 to 4.732 |
| Pearson r | 1.000000 |
| Spearman rho | 1.000000 (0.9972 before rounding, because exact ties such as 4.7321 and 1.0 are ordered by 1e-14 noise) |
| mean abs MOS difference | 3e-14 |
| max abs MOS difference | 3e-13 |
| max abs per-band FVNSIM difference | 5e-12 |
| alignment lag, number of patches | identical in all 166 pairs |
With 44.1 kHz inputs, visqol(x, y, 44100) matches the C++ API fed with
torchaudio.functional.resample(x, 44100, 48000) to within 7e-6 MOS on 12
noise, low-pass and MP3 pairs (the remaining difference comes from float32
versus float64 resampling).
Runtime for a 10 s clip on an Apple-silicon laptop (16 cores): this port takes
about 1.5 s with the default 8 filterbank threads and about 3 to 5 s with one
thread. The C++ build in the pyvisqol wheel takes about 7 s. Most of the time
is spent in the gammatone filterbank (scipy.signal.sosfilt).
Faithfulness notes
The port keeps the reference implementation's behaviour, including its quirks, so that scores match:
- The "Hilbert" envelope used for alignment is not analytic. The C++ code applies the Hilbert weights and then uses a real inverse FFT, and the port does the same.
Sliceadds end-of-signal padding at the front of a patch.- Patches with no match get NSIM 0 but keep their standard deviation and energy.
- MOS = clip(SVR(FVNSIM), 1, 5), and it is set to 1 when mean NSIM < 0.15.
The other features (
fvnsim10,fstdnsim,fvdegenergy) are returned indetailsbut, as in the C++ code, are not used by the SVR.
The C++ code runs its FFTs in single precision (pffft) and the port uses double precision. This only matters when two alignment lags have almost the same correlation.
Citation
If you use this metric, please cite the original ViSQOL papers:
@inproceedings{chinen2020visqol,
title = {ViSQOL v3: An Open Source Production Ready Objective Speech and Audio Metric},
author = {Chinen, Michael and Lim, Felicia S. C. and Skoglund, Jan and Gureev, Nikita and O'Gorman, Feargus and Hines, Andrew},
booktitle = {2020 Twelfth International Conference on Quality of Multimedia Experience (QoMEX)},
year = {2020}
}
@article{hines2015visqolaudio,
title = {ViSQOLAudio: An objective audio quality metric for low bitrate codecs},
author = {Hines, Andrew and Gillen, Eoin and Kelly, Damien and Skoglund, Jan and Kokaram, Anil and Harte, Naomi},
journal = {The Journal of the Acoustical Society of America},
volume = {137}, number = {6}, pages = {EL449--EL455}, year = {2015}
}
License and attribution
Apache License 2.0 (see LICENSE and NOTICE). This is an unofficial port and
is not affiliated with or endorsed by Google. The algorithm and the bundled SVR
model visqol_audio/libsvm_nu_svr_model.txt come from
google/visqol, Copyright 2019 Google LLC and
Andrew Hines, licensed under Apache-2.0. The C++ code was translated into Python
and modified, and the model file is unchanged.