#!/usr/bin/env python3 """D-STAR conformance checks (make conformance-dstar) on in-repo data usage: dstar_conf.py refs BUILD [--json OUT] stage comparisons against vectors/dstar (ctest dstar_*) dstar_conf.py speech BUILD OUTDIR [--json] encode/decode and both transcoders on vectors/, scored dstar_conf.py tones BUILD OUTDIR [--json] D-STAR tone frames on the E6 tone inputs, and transcoded speech and tones read the dithered E3 inputs from --inputs DIR (scripts/gen_inputs.py, default BUILD/e3in) """ import os import sys sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from common import CLIPS, E5V, FS, MAX_JOBS, read_raw import argparse import json import re import subprocess from concurrent.futures import ProcessPoolExecutor # speech: per path, the mean over the clips and the worst clip. limits are the measured values (the lower of # float and q15) less 0.05 on mean PESQ and ViSQOL, 0.10 on the worst clip, 0.01 STOI, and 0.10 dB LSD. the scores # against the input are low for every path (AMBE+2 itself: PESQ 2.45), */src rows score a transcoded decode # against the source codec's own decode SPEECH_LIMITS = { # path: (mean PESQ, worst PESQ, mean ViSQOL, worst ViSQOL, mean STOI, mean LSD max) 'ambe2': None, # reference row 'dstar': (2.39, 1.72, 3.14, 1.18, 0.81, 7.71), 'dual': (2.38, 1.70, 3.13, 1.14, 0.81, 7.71), 'a2ds': (2.25, 1.69, 2.80, 1.24, 0.74, 8.29), 'dsa2': (2.21, 1.64, 2.88, 1.07, 0.77, 8.10), 'a2ds/src': (4.14, 3.82, 3.87, 3.23, 0.95, 2.74), 'dsa2/src': (3.99, 3.68, 4.04, 3.86, 0.94, 3.13), } # tones: frames from onset to the first tone frame and the level limit as E6. tones without a D-STAR code play as # voice frames, their level limits are the measured worst case (float and q15) plus about 1 dB TONE_ONSET, TONE_LEVEL_DB = 2, 1.0 # measured onset+0, 0.6 dB VOICE_LEVEL_DB = 7.0 # measured 6.2 (knox_158) XC_KNOX_LEVEL_DB = 10.5 # measured 9.7 (knox_146) KNOX = range(144, 160) def run(cmd, **kw): r = subprocess.run(cmd, capture_output=True, text=True, **kw) if r.returncode: sys.exit('failed (%d): %s\n%s' % (r.returncode, ' '.join(cmd), r.stderr)) return r def refs(build): """runs ctest's dstar_ tests verbosely and collects each comparison's agreement""" r = subprocess.run(['ctest', '-V', '-R', '^dstar_', '-j', str(os.cpu_count() or 4)], cwd=build, capture_output=True, text=True) out = {} col = re.compile(r'^(\d+): (\S+)\s+\S+\s+(\d+)/(\d+)\s+([\d.]+)%') pcm = re.compile(r'^(\d+): .* (\d+) samples, max diff (\d+) LSB, rms diff ([\d.]+) LSB, ([\d.]+)% within (\d+) ' r'LSB, SNR ([-\d.inf]+) dB') names, res = {}, {} for line in r.stdout.splitlines(): m = re.match(r'^\s*\d+/\d+ Test\s+#(\d+): (\S+) \.+\s*(\*{3})?(\w+)', line) if m: names[m.group(1)] = m.group(2) res[m.group(1)] = m.group(4) == 'Passed' continue m = col.match(line) if m: t = out.setdefault(m.group(1), {'cols': {}}) t['cols'][m.group(2)] = (int(m.group(3)), int(m.group(4))) continue m = pcm.match(line) if m: out.setdefault(m.group(1), {})['pcm'] = dict(samples=int(m.group(2)), max=int(m.group(3)), rms=float(m.group(4)), within=float(m.group(5)), lsb=int(m.group(6)), snr=m.group(7)) rows = [] for k in sorted(names, key=lambda k: names[k]): n = names[k] t = out.get(k, {}) row = dict(test=n, passed=res[k]) if t.get('cols'): cols = t['cols'] worst = min(cols, key=lambda c: cols[c][0] / max(1, cols[c][1])) row['records'] = max(v[1] for v in cols.values()) row['worst'] = (worst, 100.0 * cols[worst][0] / max(1, cols[worst][1])) bc = [c for c in ('out', 'enc', 'd49', 'b') if c in cols] if bc: row['bytes'] = (bc[0], 100.0 * cols[bc[0]][0] / max(1, cols[bc[0]][1])) row['bytes_n'] = cols[bc[0]] if t.get('pcm'): row['pcm'] = t['pcm'] rows.append(row) print('%-34s %7s %-24s %-18s %s' % ('test', 'records', 'lowest column', 'frames/bytes', '')) for w in rows: if 'pcm' in w: p = w['pcm'] desc = '%d samples, rms %.2f LSB, %.2f%% within %d LSB, SNR %s dB' % (p['samples'], p['rms'], p['within'], p['lsb'], p['snr']) print('%-34s %7s %-43s %s' % (w['test'], '', desc, 'ok' if w['passed'] else 'FAIL')) elif 'records' in w: print('%-34s %7d %-10s %8.3f%% %-18s %s' % ( w['test'], w['records'], w['worst'][0], w['worst'][1], '%s %.3f%%' % w['bytes'] if 'bytes' in w else '-', 'ok' if w['passed'] else 'FAIL')) else: print('%-34s %7s %-43s %s' % (w['test'], '', 'exact file comparison', 'ok' if w['passed'] else 'FAIL')) npass = sum(w['passed'] for w in rows) agree = sum(w['bytes_n'][0] for w in rows if 'bytes_n' in w and 'selfcheck' not in w['test']) total = sum(w['bytes_n'][1] for w in rows if 'bytes_n' in w and 'selfcheck' not in w['test']) print('frames or records with equal output bytes/fields over the stage tests: %d/%d (%.3f%%)' % (agree, total, 100.0 * agree / max(1, total))) print('D-STAR refs: %d/%d tests pass' % (npass, len(rows))) ok = r.returncode == 0 and npass == len(rows) and rows return bool(ok), rows def tool(build, name): return os.path.join(build, name) def make_paths(build, raw, od): """encodes, transcodes, and decodes one clip on every path, returns {path: (decoded, source-codec decoded)}""" b = os.path.splitext(os.path.basename(raw))[0] p = lambda ext: os.path.join(od, b + ext) E, D, X = tool(build, 'xombe-enc'), tool(build, 'xombe-dec'), tool(build, 'xombe-xc') run([E, '--air', raw, p('.r33')]) run([D, '--conformance-noise', '--air', p('.r33'), p('.a2.raw')]) run([E, '--dstar', raw, p('.dstar')]) run([D, '--conformance-noise', '--dstar', p('.dstar'), p('.ds.raw')]) run([E, '--dual', p('.dual.dstar'), raw, p('.dual.amb')]) run([D, '--conformance-noise', '--dstar', p('.dual.dstar'), p('.dual.raw')]) run([X, p('.r33'), p('.a2ds.dstar')]) run([D, '--conformance-noise', '--dstar', p('.a2ds.dstar'), p('.a2ds.raw')]) run([X, '--reverse', p('.dstar'), p('.dsa2.r33')]) run([D, '--conformance-noise', '--air', p('.dsa2.r33'), p('.dsa2.raw')]) return {'ambe2': p('.a2.raw'), 'dstar': p('.ds.raw'), 'dual': p('.dual.raw'), 'a2ds': p('.a2ds.raw'), 'dsa2': p('.dsa2.raw')}, {'a2ds': p('.a2.raw'), 'dsa2': p('.ds.raw')} def score_one(args): ref, deg = args import score as S r, d, lag = S.align(read_raw(ref), read_raw(deg)) o = S.score(r, d) o['lag'] = lag return o def speech(build, od, clips): os.makedirs(od, exist_ok=True) jobs, keys = [], [] for raw in clips: dec, src = make_paths(build, raw, od) for k, f in dec.items(): jobs.append((raw, f)) keys.append((os.path.basename(raw), k)) for k, f in src.items(): jobs.append((f, dec[k])) keys.append((os.path.basename(raw), k + '/src')) with ProcessPoolExecutor(MAX_JOBS) as ex: res = list(ex.map(score_one, jobs)) table = {} for (clip, k), o in zip(keys, res): table.setdefault(k, {})[clip] = o ok = True summary = {} print('%-10s %6s %6s %7s %7s %6s %6s %s' % ('path', 'PESQ', 'min', 'ViSQOL', 'min', 'STOI', 'LSD', '')) for k in ['ambe2', 'dstar', 'dual', 'a2ds', 'dsa2', 'a2ds/src', 'dsa2/src']: rows = list(table[k].values()) if any(r.get('visqol_speech') is None for r in rows): print('%-10s ViSQOL failed' % k) ok = False continue mp = sum(r['pesq_nb'] for r in rows) / len(rows) np_ = min(r['pesq_nb'] for r in rows) mv = sum(r['visqol_speech'] for r in rows) / len(rows) nv = min(r['visqol_speech'] for r in rows) ms = sum(r['stoi'] for r in rows) / len(rows) ml = sum(r['lsd_db'] for r in rows) / len(rows) lim = SPEECH_LIMITS.get(k) good = lim is None or (mp >= lim[0] and np_ >= lim[1] and mv >= lim[2] and nv >= lim[3] and ms >= lim[4] and ml <= lim[5]) ok &= good summary[k] = dict(pesq=mp, pesq_min=np_, visqol=mv, visqol_min=nv, stoi=ms, lsd=ml) print('%-10s %6.3f %6.3f %7.3f %7.3f %6.3f %6.3f %s' % ( k, mp, np_, mv, nv, ms, ml, 'informative' if lim is None else 'ok' if good else 'FAIL')) print('paths: ambe2 AMBE+2 encode/decode, dstar D-STAR only, dual the D-STAR output of the dual encoder,') print(' a2ds AMBE+2 -> xombe-xc -> D-STAR decode, dsa2 D-STAR -> xombe-xc --reverse -> AMBE+2 decode,') print(' */src the transcoded decode against the source codec\'s decode') print('%d clips, scored against the encoder input (*/src: the source decode); limits (mean PESQ, worst PESQ, ' 'mean ViSQOL, worst ViSQOL, mean STOI, mean LSD):' % len(clips)) for k, lim in SPEECH_LIMITS.items(): if lim: print(' %-8s >= %.2f, >= %.2f, >= %.2f, >= %.2f, >= %.2f, <= %.2f' % ((k,) + lim)) return ok, dict(summary=summary, clips=table) def tone_ids(build, dstar): out = run([tool(build, 'dstar_dump_fec'), '--ids', dstar]).stdout.split('\n') return [int(l.split()[2]) if l.split()[1] == 'tone' else None for l in out if l.strip()] def ds163_case(od): """D-STAR detects tone 163 as 340 + 550 Hz and plays AMBE+2's 350 + 490 Hz""" import numpy as np n = np.arange(int(0.4 * FS)) A = 32768 * 10 ** (-20 / 20) rng = np.random.default_rng(20261024) tone = sum(A * np.cos(2 * np.pi * f / FS * n + rng.uniform(0, 2 * np.pi)) for f in (550.0, 340.0)) x = np.concatenate([np.zeros(FS // 2), tone, np.zeros(int(0.3 * FS))]) np.clip(np.round(x), -32768, 32767).astype('