#!/usr/bin/env python3 """writes src/tables/dstar_tables.c and dstar_tables.h, the D-STAR (AMBE 2400) tables, from the fitted values in scripts/data/dstar_tables.json""" import json import math import os import struct ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..') OUT_C = os.path.join(ROOT, 'src', 'tables', 'dstar_tables.c') OUT_H = os.path.join(ROOT, 'src', 'tables', 'dstar_tables.h') DATA = os.path.join(ROOT, 'scripts', 'data', 'dstar_tables.json') B0_COUNT = 128 LMAX = 56 BANDS_PER_UNIT = 16.0 # 8000 / 500, harmonic l at f0 lies in 500 Hz band floor(l*f0*16) TONE_NONE = 255 def fl32(x): return struct.unpack('f', struct.pack('f', float(x)))[0] def f32(x): """x rounded to the nearest float, printed to read back exactly""" v = fl32(x) s = '%.9g' % v if 'e' not in s and '.' not in s: s += '.0' return s + 'f' def rows(values, per_line=6, indent=' '): return '\n'.join(indent + ', '.join(values[i:i + per_line]) + ',' for i in range(0, len(values), per_line)) def c_matrix(name, ctype, dims, data, fmt): out = ['const %s %s%s = {' % (ctype, name, ''.join('[%s]' % d for d in dims))] if len(dims) == 1: out.append(rows([fmt(v) for v in data], 6 if fmt is f32 else 16)) else: for r in data: out.append(' {' + ', '.join(fmt(v) for v in r) + '},') out.append('};') return out def main(): with open(DATA) as f: T = json.load(f) p0, p1, rho, grho = float(T['p0']), float(T['p1']), float(T['rho']), float(T['grho']) w0 = [2.0 ** (p0 + p1 * (b + 0.5)) for b in range(B0_COUNT)] lt = [int(min(max(math.floor(0.4627 / f), 9), LMAX)) for f in w0] # decoder's per-harmonic band in float as params.c computes it, ((float)l*f0)*16 with f0 the float table # value, must equal the double floor(l*f0*8000/500) for b in range(B0_COUNT): for l in range(1, lt[b] + 1): c = int(fl32(fl32(l * fl32(w0[b])) * BANDS_PER_UNIT)) assert c == int(l * w0[b] * 8000.0 / 500.0), (b, l) dg, p24, p58, hoc = T['DG'], T['PRBA24'], T['PRBA58'], T['HOC'] assert (len(dg), len(p24), len(p58), [len(h) for h in hoc]) == (64, 512, 128, [16, 16, 16, 8]) assert all(len(r) == 4 for h in hoc for r in h) ok_dg, ok24, ok58, ok_hoc = T['ok_DG'], T['ok_PRBA24'], T['ok_PRBA58'], T['ok_HOC'] # b1 -> flags of 500 Hz bands 0..7 as the decoder plays them (code 4 plays as 12) vuv = [[int(ch) for ch in s] for s in T['VUV0']] # D-STAR tone index T -> AMBE+2 tone ID: T 0..15 DTMF (T = 4*column + row), 16..19 call progress, # 37..154 single tones at 31.25*(T - 32) Hz, every other T invalid tone = [TONE_NONE if t is None else t for t in T['tone_id']] # b0 of the D-STAR voice frame carrying Knox tone ID 144 + i (observed) knox_b0 = T['knox_b0'] assert (len(vuv), len(tone), len(knox_b0)) == (16, 256, 16) c = ['/* generated by scripts/gen_dstar_tables.py, do not edit */', '#include "dstar_tables.h"', ''] c += c_matrix('xombe__ds_w0', 'float', ['XOMBE_DS_B0_COUNT'], w0, f32) + [''] c += c_matrix('xombe__ds_lt', 'unsigned char', ['XOMBE_DS_B0_COUNT'], lt, str) + [''] c += c_matrix('xombe__ds_dg', 'float', ['64'], dg, f32) + [''] c += c_matrix('xombe__ds_prba24', 'float', ['512', '3'], p24, f32) + [''] c += c_matrix('xombe__ds_prba58', 'float', ['128', '4'], p58, f32) + [''] for k, n in enumerate([16, 16, 16, 8]): c += c_matrix('xombe__ds_hoc%d' % (k + 5), 'float', [str(n), '4'], hoc[k], f32) + [''] c += c_matrix('xombe__ds_ok_dg', 'unsigned char', ['64'], ok_dg, str) + [''] c += c_matrix('xombe__ds_ok_prba24', 'unsigned char', ['512'], ok24, str) + [''] c += c_matrix('xombe__ds_ok_prba58', 'unsigned char', ['128'], ok58, str) + [''] for k, n in enumerate([16, 16, 16, 8]): c += c_matrix('xombe__ds_ok_hoc%d' % (k + 5), 'unsigned char', [str(n)], ok_hoc[k], str) + [''] c += c_matrix('xombe__ds_vuv', 'unsigned char', ['16', '8'], vuv, str) + [''] c += c_matrix('xombe__ds_tone_id', 'unsigned char', ['256'], tone, str) + [''] c += c_matrix('xombe__ds_knox_b0', 'unsigned char', ['16'], knox_b0, str) h = '''#ifndef XOMBE_DSTAR_TABLES_H #define XOMBE_DSTAR_TABLES_H /* D-STAR (AMBE 2400) tables, generated by scripts/gen_dstar_tables.py, do not edit each value is a fitted double rounded once to float, spectral block lengths are AMBE+2's xombe__blk */ #define XOMBE_DS_B0_COUNT %(nb0)d /* pitch grid f0 = 2^(P0 + P1*(b0 + 1/2)) cycles/sample, from doubles %(p0)r and %(p1)r */ #define XOMBE_DS_P0 %(p0f)s #define XOMBE_DS_P1 %(p1f)s /* amplitude prediction weight and gain recursion (0: b2 is an absolute gain index) */ #define XOMBE_DS_RHO %(rho)s #define XOMBE_DS_GRHO %(grho)s #define XOMBE_DS_TONE_NONE %(none)d /* f0 in cycles/sample and L = floor(0.4627/f0) clamped to 9..56, both from the double f0 (voice b0 0..123) */ extern const float xombe__ds_w0[XOMBE_DS_B0_COUNT]; extern const unsigned char xombe__ds_lt[XOMBE_DS_B0_COUNT]; /* gain (b2), PRBA (b3, b4), and HOC (b5..b8, rows of 4, first min(J, 6) - 2 used) codebooks */ extern const float xombe__ds_dg[64]; extern const float xombe__ds_prba24[512][3]; extern const float xombe__ds_prba58[128][4]; extern const float xombe__ds_hoc5[16][4]; extern const float xombe__ds_hoc6[16][4]; extern const float xombe__ds_hoc7[16][4]; extern const float xombe__ds_hoc8[8][4]; /* 1 where the fit had data for the entry, the closed-loop quantizer skips the others */ extern const unsigned char xombe__ds_ok_dg[64]; extern const unsigned char xombe__ds_ok_prba24[512]; extern const unsigned char xombe__ds_ok_prba58[128]; extern const unsigned char xombe__ds_ok_hoc5[16]; extern const unsigned char xombe__ds_ok_hoc6[16]; extern const unsigned char xombe__ds_ok_hoc7[16]; extern const unsigned char xombe__ds_ok_hoc8[8]; /* b1 -> eight 500 Hz band flags as the decoder plays them (code 4 plays as 12) */ extern const unsigned char xombe__ds_vuv[16][8]; /* tone index T (8 bits) -> AMBE+2 tone ID, or XOMBE_DS_TONE_NONE */ extern const unsigned char xombe__ds_tone_id[256]; /* encoder sends Knox tones (AMBE+2 IDs 144..159, no D-STAR tone code) as voice frames at this b0 per ID */ extern const unsigned char xombe__ds_knox_b0[16]; #endif ''' % dict(nb0=B0_COUNT, p0=p0, p1=p1, p0f=f32(p0), p1f=f32(p1), rho=f32(rho), grho=f32(grho), none=TONE_NONE) with open(OUT_C, 'w') as f: f.write('\n'.join(c) + '\n') with open(OUT_H, 'w') as f: f.write(h) print('wrote', OUT_C, OUT_H) if __name__ == '__main__': main()