0.9.0: flash: VOICE's English dictionary Huffman-coded, the word banks' offsets sparse (-5.4 KB)

The CMU dictionary (2095 words, front-coded bytes: 18928 B) is now Huffman-coded (tools/gen_lyrics.py en_dict_pack):
each word its shared-letter count, its letters and its phonemes, each with its own canonical code (P, L, H), a block
of 8 words from a byte (LY_EN_IDX; was 16), LY_EN_LET the blocks of each first letter. src/sing.c sy_en_find reads
it with a 32-bit bit reader and a table for the codes up to 6 / 6 / 7 bits (99.6, 96, 94 % of the symbols; the rest
from the canonical counts), into the caller's buffer (no pointer into the table any more: sy_word passes its own,
SW_MAX as before). 14537 B with the tables and the index. The generator decodes it back and fails if any word differs.
The word banks' offsets (LY_ES/EN/JA_OFF, a uint16 a word) keep each 16th word's: ly_word skips the 0s from there.

image 551192 -> 545784 B (-5408): dictionary -4391 B, offsets -1390 B, code +373 B.

Same words, same phonemes, same sound: tests/voice_test.c looks every word up against gen_lyrics.py's own list
(LY_EN_REF_*, host only) - all 2095 found with exactly their phonemes - and 161312 near misses (each word one letter
shorter, longer, its first or last letter changed) found exactly when they are themselves words; the banks' 743 words
as before; every golden render, VOICE demo and test output bit for bit as before.

The lookup's cost (it runs in the audio ISR when a VOICE EN word starts): host callgrind, every word once, ~3550
instructions a lookup against ~1160 as bytes (ly_sym kept a call: inlined, 670 B more for ~15 % less), once a word;
tests/cpu_vg.c eng/VOICE.EN 1727 instructions a sample, as before (+0.0 %).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KMHk6yH4XbP1xxHYKCo9Mh
This commit is contained in:
Claude committed 2026-10-17 12:32:52 +00:00
1 parent e7a30ff085
commit ab603684b0
3 files changed
+311 -57

No files matched your search

+82 -28
View File
@@ -334,17 +334,68 @@ static uint32_t sy_ja(const char *w, uint32_t n, uint8_t *out, uint32_t *ns)
return m;
}
/* English: the dictionary (front-coded: a byte < 0x20 the letters shared with the word before, then the rest, then the
* phonemes, 0x80 | SYB | code; each 16th word whole, LY_EN_IDX its place). 0: not there; else its phoneme bytes */
static const uint8_t *sy_en_find(const char *key, uint32_t kn, uint32_t *len)
/* English: the dictionary (tools/gen_lyrics.py en_dict_pack; 0.9.0: Huffman-coded, 4.4 KB less than as bytes): a word
* is the count of letters shared with the word before (code P), the rest of its letters (code L: 0 .. 25 a .. z) and
* LY_END, its phonemes (code H: SYB | code) and LY_END; each LY_EN_BLK-th word whole, from a byte, LY_EN_IDX its place.
* A code: hc the codes of each length 1 .. 15, then the symbols in code order (canonical); lut the next k bits ->
* length << 8 | symbol (0: a longer code, read from hc). The reader keeps the next n bits in acc (MSB first), at least
* 25 after each symbol (a code is at most 15), reading up to 4 bytes ahead (the dictionary ends with 4 to spare) */
typedef struct { const uint8_t *hc; const uint16_t *lut; uint32_t k; } ly_code_t;
static const ly_code_t LY_CP = {LY_EN_HCP, LY_EN_LUTP, LY_EN_KP}, LY_CL = {LY_EN_HCL, LY_EN_LUTL, LY_EN_KL},
LY_CH = {LY_EN_HCH, LY_EN_LUTH, LY_EN_KH};
typedef struct { const uint8_t *p; uint32_t acc, n; } ly_rd_t;
static inline __attribute__((always_inline)) void ly_fill(ly_rd_t *r)
{
uint32_t lo = 0, hi = (uint32_t)NELEM(LY_EN_IDX), blk, e, n = 0;
while (r->n <= 24u) {
r->acc |= (uint32_t)*r->p++ << (24u - r->n);
r->n += 8u;
}
}
static void ly_at(ly_rd_t *r, uint32_t blk) /* at block blk's first word */
{
r->p = &LY_EN_DICT[LY_EN_IDX[blk]];
r->acc = r->n = 0;
ly_fill(r);
}
static __attribute__((noinline)) uint32_t ly_sym(ly_rd_t *r, const ly_code_t *c)
{
uint32_t e = c->lut[r->acc >> (32u - c->k)];
if (!e) { /* a code longer than k bits */
uint32_t first = 0, idx = 0, n;
for (n = 1; n < 15u; n++) {
uint32_t code = r->acc >> (32u - n), cnt = c->hc[n - 1u];
if (code - first < cnt)
break;
idx += cnt;
first = (first + cnt) << 1;
}
e = n << 8 | c->hc[15u + idx + (r->acc >> (32u - n)) - first];
}
r->acc <<= e >> 8;
r->n -= e >> 8;
ly_fill(r);
return e & 255u;
}
/* key's phonemes (SYB | code) into out (SW_MAX; the rest dropped): how many, 0 = not there */
static uint32_t sy_en_find(const char *key, uint32_t kn, uint8_t *out)
{
uint32_t lo = 0, hi = (uint32_t)NELEM(LY_EN_IDX), n;
ly_rd_t r;
char w[24];
if (kn && key[0] >= 'a' && key[0] <= 'z') { /* the blocks from the one before the key's letter's first */
lo = LY_EN_LET[key[0] - 'a'];
hi = LY_EN_LET[key[0] - 'a' + 1];
lo -= lo > 0u;
}
while (hi - lo > 1u) { /* the last block whose first word is <= the key */
uint32_t mid = (lo + hi) / 2u, p = LY_EN_IDX[mid] + 1u, i = 0;
uint32_t mid = (lo + hi) / 2u, i = 0, s;
int c = 0;
ly_at(&r, mid);
(void)ly_sym(&r, &LY_CP); /* (0: a block's first word is whole) */
while (!c) {
int a = i < kn ? key[i] : 0, b = LY_EN_DICT[p + i] >= 'a' && LY_EN_DICT[p + i] <= 'z' ? LY_EN_DICT[p + i] : 0;
int a = i < kn ? key[i] : 0, b;
s = ly_sym(&r, &LY_CL);
b = s == LY_END ? 0 : 'a' + (int)s;
c = a - b;
if (!a || !b)
break;
@@ -353,27 +404,25 @@ static const uint8_t *sy_en_find(const char *key, uint32_t kn, uint32_t *len)
if (c >= 0) lo = mid;
else hi = mid;
}
blk = lo;
e = LY_EN_IDX[blk];
for (n = 0; n < 16u && e < sizeof LY_EN_DICT; n++) {
uint32_t pre = LY_EN_DICT[e++], wl = pre, p0;
while (e < sizeof LY_EN_DICT && LY_EN_DICT[e] >= 'a' && LY_EN_DICT[e] <= 'z') {
if (wl < sizeof w) w[wl] = (char)LY_EN_DICT[e];
ly_at(&r, lo);
for (n = 0; n < LY_EN_BLK && lo * LY_EN_BLK + n < LY_EN_N; n++) {
uint32_t wl = ly_sym(&r, &LY_CP), s, m = 0;
while ((s = ly_sym(&r, &LY_CL)) != LY_END) {
if (wl < sizeof w) w[wl] = (char)('a' + s);
wl++;
e++;
}
p0 = e;
while (e < sizeof LY_EN_DICT && LY_EN_DICT[e] >= 0x80u)
e++;
if (wl == kn && wl <= sizeof w) {
uint32_t i;
for (i = 0; i < kn && w[i] == key[i]; i++)
;
if (i == kn) {
*len = e - p0;
return &LY_EN_DICT[p0];
while ((s = ly_sym(&r, &LY_CH)) != LY_END)
if (m < SW_MAX) out[m++] = (uint8_t)s;
return m;
}
}
while (ly_sym(&r, &LY_CH) != LY_END) /* (the phonemes of a word that is not it) */
;
}
return 0;
}
@@ -458,14 +507,11 @@ static uint32_t sy_word(uint32_t lang, const char *w, uint32_t n, uint8_t *out,
if (lang == LG_JA)
return sy_ja(w, n, out, ns);
if (lang == LG_EN) {
uint32_t len, i;
const uint8_t *d = sy_en_find(w, n, &len);
if (d) {
for (i = 0; i < len && i < SW_MAX; i++) {
out[i] = d[i] & 0x7Fu;
uint32_t len = sy_en_find(w, n, out), i;
if (len) {
for (i = 0; i < len; i++)
*ns += (out[i] & SYB) != 0u;
}
return i;
return len;
}
m = sy_en_rules(w, n, tc, tk);
} else {
@@ -499,11 +545,19 @@ static uint32_t sy_rnd(uint32_t *r)
return x;
}
typedef struct {
const char *txt;
const uint16_t *off;
const char *txt; /* the words, each ended by a 0 */
const uint16_t *off; /* (0.9.0) each 16th word's place in txt (ly_word) */
const uint8_t *meta;
const uint16_t (*grp)[6][2];
} ly_bank_t;
static const char *ly_word(const ly_bank_t *b, uint32_t j) /* word j of the bank */
{
const char *q = b->txt + b->off[j >> 4];
for (j &= 15u; j; j--)
while (*q++)
;
return q;
}
static const ly_bank_t LY_BANK[3] = {
{LY_ES_TXT, LY_ES_OFF, LY_ES_META, LY_ES_GRP},
{LY_EN_TXT, LY_EN_OFF, LY_EN_META, LY_EN_GRP},
@@ -645,7 +699,7 @@ static void sy_gen_word(const track_t *t, sing_t *s, uint32_t lang, uint32_t lyr
return;
}
{
const char *q = b->txt + b->off[j];
const char *q = ly_word(b, j);
uint32_t m = 0;
while (q[m] && m + 1u < sizeof s->w) {
s->w[m] = q[m];
+50 -18
View File
@@ -15,6 +15,7 @@
* 5. cost: host ns per sample and voice, vowels against words (the counts: tests/cpu_vg.c eng/VOICE.*).
* 6. demos into DEMODIR: Spanish, English, Japanese phrases on a melody, the dice's lyrics, every word preset. */
#define main hostsim_main
#define LY_EN_REF 1 /* (0.9.0) felucca_lyrics.h: the dictionary's words as text too (the lookup's check) */
#include "hostsim.c"
#undef main
@@ -289,25 +290,56 @@ static void check_reading(void)
if (strcmp(b, EN[k][1])) { printf("voice: EN %-12s %s (want %s)\n", EN[k][0], b, EN[k][1]); ok = 0; }
}
check("English through the dictionary (CMU, syllabified): love my time beautiful water strong the don't", ok);
{ /* every dictionary word found, with at least a syllable; an unknown word by the letter rules */
uint32_t e = 0, nw = 0, ns, len;
char w[24];
{ /* every dictionary word found, with at least a syllable; an unknown word by the letter rules.
* (0.9.0: the dictionary Huffman-coded) each word's phonemes exactly gen_lyrics.py's (LY_EN_REF_*: the words as
* text); every word cut short, one letter more and one changed at its end: not found unless itself a word */
uint32_t nw, len, bad = 0, miss = 0, probes = 0;
uint8_t ph[SW_MAX];
char w[32];
ok = 1;
while (e < sizeof LY_EN_DICT && nw < 3000u) {
uint32_t pre = LY_EN_DICT[e++], wl = pre;
while (e < sizeof LY_EN_DICT && LY_EN_DICT[e] >= 'a' && LY_EN_DICT[e] <= 'z')
w[wl++] = (char)LY_EN_DICT[e++];
while (e < sizeof LY_EN_DICT && LY_EN_DICT[e] >= 0x80u)
e++;
w[wl] = 0;
nw++;
ok &= sy_en_find(w, wl, &len) != 0 && len > 0;
for (nw = 0; nw < LY_EN_N; nw++) {
const char *rw = LY_EN_REF_W[nw], *rp = LY_EN_REF_PH[nw];
uint32_t wl = (uint32_t)strlen(rw), i, v;
len = sy_en_find(rw, wl, ph);
ok &= len > 0;
if (len != strlen(rp))
bad++;
else
for (i = 0; i < len; i++)
bad += ph[i] != ((uint8_t)rp[i] & 0x7Fu);
for (v = 0; v < 79u; v++) { /* the near misses: one less, one more, last / first changed */
uint32_t n2 = wl, lo = 0, hi = LY_EN_N, found = 0;
memcpy(w, rw, wl);
if (v == 0u) {
if (wl < 2u) continue;
n2 = wl - 1u;
} else if (v <= 26u) {
w[wl] = (char)('a' + v - 1u);
n2 = wl + 1u;
} else if (v <= 52u) {
if (w[wl - 1u] == (char)('a' + v - 27u)) continue;
w[wl - 1u] = (char)('a' + v - 27u);
} else {
if (w[0] == (char)('a' + v - 53u)) continue;
w[0] = (char)('a' + v - 53u);
}
w[n2] = 0;
while (lo < hi) { /* (the reference words are sorted) */
uint32_t mid = (lo + hi) / 2u;
int c = strcmp(LY_EN_REF_W[mid], w);
if (!c) { found = 1; break; }
if (c < 0) lo = mid + 1u; else hi = mid;
}
probes++;
miss += (sy_en_find(w, n2, ph) != 0) != found;
}
}
ok &= nw == LY_EN_N;
ok &= !bad && !miss;
g2p_str(LG_EN, "zorblat", b, sizeof b);
ok &= sy_en_find("zorblat", 7, &len) == 0 && !strcmp(b, "|z aa r |b l ae t");
(void)ns;
snprintf(what, sizeof what, "English dictionary: all %u words found; a word it lacks by letter rules (zorblat: %s)",
ok &= sy_en_find("zorblat", 7, ph) == 0 && !strcmp(b, "|z aa r |b l ae t");
printf("voice: dictionary: %u words' phonemes against gen_lyrics.py's, %u differ; %u near misses, %u wrong\n",
(unsigned)LY_EN_N, bad, probes, miss);
snprintf(what, sizeof what, "English dictionary: all %u words found, exact; a word it lacks by letter rules (zorblat: %s)",
(unsigned)LY_EN_N, b);
check(what, ok);
}
@@ -320,7 +352,7 @@ static void check_reading(void)
for (k = 0; k < nwds; k++) {
uint8_t ph[SW_MAX];
char nw2[24];
const char *w = bk->txt + bk->off[k];
const char *w = ly_word(bk, k);
(void)sy_word(l, nw2, sy_norm(w, (uint32_t)strlen(w), nw2, sizeof nw2), ph, &n);
tot++;
if (n != (bk->meta[k] & 15u)) {
@@ -414,7 +446,7 @@ static void check_generator(void)
n = (uint32_t)(q - p);
if (!n) break;
for (w = 0; w < nwds && !found; w++) {
const char *t = bk->txt + bk->off[w];
const char *t = ly_word(bk, w);
found = strlen(t) == n && (!strncmp(t, p, n) ||
(l == LG_ES && !strncmp(t, p, n - 1u) && t[n - 1u] == 'o' && p[n - 1u] == 'a'));
}
+179 -11
View File
@@ -4,8 +4,10 @@
"""VOICE words (JIANT 0.8.5): the phoneme codes, the English dictionary and the lyric generator's word banks as
constant tables (firmware/src/sing.c reads them), from tools/lyrics/:
en_dict.txt the English words the device knows (a subset of the CMU Pronouncing Dictionary, BSD-2-Clause): each
packed as its letters, then its phonemes (a byte each, 0x80 | syllable start 0x40 | code), sorted, with
an index every 16 words (binary search, then at most 16 compared)
its letters, then its phonemes (a byte each, 0x80 | syllable start 0x40 | code), sorted, front-coded
(the letters shared with the word before), with an index every EN_BLK words (binary search, then at
most EN_BLK compared); (0.9.0) Huffman-coded (en_dict_pack: the shared count, the letters, the
phonemes, each their own canonical code), a block of EN_BLK from a byte (8: was 16 as bytes)
es.txt en.txt ja.txt the generator's banks: words by theme and category, each with its syllables (Spanish by the
same rules sing.c reads it with, sy_es below; English from the dictionary; Japanese in morae)
Spanish and Japanese text is read by rules on the device; English only through the dictionary (and rough letter rules
@@ -80,6 +82,135 @@ def en_phones(arpa):
return [(PH[c], i in starts) for i, (c, _) in enumerate(seq)], len(nuc)
# (0.9.0) the dictionary Huffman-coded (-5.4 KB): three canonical codes (sing.c ly_sym), at most HC_MAX bits a code
HC_MAX = 15
LY_END = 0x7F # the end of a word's letters / phonemes (no phoneme byte & 0x7F)
def huff_code(count):
"""symbol -> (length, code), canonical (by length, then symbol), lengths at most HC_MAX; and the table sing.c
reads: the number of codes of each length 1..HC_MAX, then the symbols in code order"""
import heapq
if len(count) == 1:
length = {next(iter(count)): 1}
else:
heap = [(c, i, (s,)) for i, (s, c) in enumerate(sorted(count.items()))]
heapq.heapify(heap)
length = dict.fromkeys(count, 0)
n = len(heap)
while len(heap) > 1:
a, b = heapq.heappop(heap), heapq.heappop(heap)
for s in a[2] + b[2]:
length[s] += 1
n += 1
heapq.heappush(heap, (a[0] + b[0], n, a[2] + b[2]))
assert max(length.values()) <= HC_MAX, "a code over HC_MAX bits"
order = sorted(count, key=lambda s: (length[s], s))
code, c, prev = {}, 0, length[order[0]]
for s in order:
c <<= length[s] - prev
prev = length[s]
code[s] = (length[s], c)
c += 1
table = [sum(1 for s in order if length[s] == n) for n in range(1, HC_MAX + 1)] + order
return code, table
EN_BLK = 8 # (0.9.0) the dictionary's words a block (an index entry)
LUT_BITS = {"P": 6, "L": 6, "H": 7} # the codes' lookup tables: 99.6 %, 96 %, 94 % of the symbols
def huff_lut(code, k):
"""the 2^k entries of a code's lookup table (sing.c ly_sym): the next k bits -> length << 8 | symbol of the code
they start with, 0 when it is longer than k bits"""
lut = [0] * (1 << k)
for s, (n, c) in code.items():
if n <= k:
for v in range(c << (k - n), (c + 1) << (k - n)):
lut[v] = n << 8 | s
return lut
def huff_decode(table, bits, pos):
"""sing.c ly_sym: the symbol at bit pos of bits (a list of 0 / 1); (symbol, next pos)"""
code = first = idx = 0
for n in range(HC_MAX):
code |= bits[pos]
pos += 1
cnt = table[n]
if code - first < cnt:
return table[HC_MAX + idx + code - first], pos
idx += cnt
first = (first + cnt) << 1
code <<= 1
raise AssertionError("no code")
def en_dict_pack(recs):
"""recs: [(shared, rest, phoneme bytes)] in order -> (blob, idx, tables, codes): each EN_BLK-th word (pre 0) starts
a block at a
byte (idx: its offset); a word is its shared count (code P), its letters then LY_END (code L, 'a' .. 'z' as 0 ..
25), its phoneme bytes & 0x7F then LY_END (code H)"""
from collections import Counter
cp, cl, ch = Counter(), Counter(), Counter()
for pre, rest, ph in recs:
cp[pre] += 1
cl.update(ord(c) - 97 for c in rest)
cl[LY_END] += 1
ch.update(b & 0x7F for b in ph)
ch[LY_END] += 1
(kp, tp), (kl, tl), (kh, th) = huff_code(cp), huff_code(cl), huff_code(ch)
blob, idx, bits = bytearray(), [], []
def put(code, s):
n, c = code[s]
bits.extend((c >> (n - 1 - i)) & 1 for i in range(n))
def flush():
bits.extend([0] * (-len(bits) % 8))
for i in range(0, len(bits), 8):
blob.append(int("".join(map(str, bits[i:i + 8])), 2))
bits.clear()
for k, (pre, rest, ph) in enumerate(recs):
if k % EN_BLK == 0:
flush()
idx.append(len(blob))
put(kp, pre)
for c in rest:
put(kl, ord(c) - 97)
put(kl, LY_END)
for b in ph:
put(kh, b & 0x7F)
put(kh, LY_END)
flush()
blob += bytes(4) # (sing.c ly_fill reads up to 4 bytes ahead)
return blob, idx, (tp, tl, th), (kp, kl, kh)
def en_dict_unpack(blob, idx, tables, n):
"""the words back from en_dict_pack's blob, as sing.c sy_en_find reads them: [(word, phoneme bytes)]"""
bits = [(b >> (7 - i)) & 1 for b in blob for i in range(8)]
tp, tl, th = tables
out, w = [], ""
for k in range(n):
pos = idx[k // EN_BLK] * 8 if k % EN_BLK == 0 else pos
pre, pos = huff_decode(tp, bits, pos)
w = w[:pre]
while True:
s, pos = huff_decode(tl, bits, pos)
if s == LY_END:
break
w += chr(97 + s)
ph = []
while True:
s, pos = huff_decode(th, bits, pos)
if s == LY_END:
break
ph.append(0x80 | s)
out.append((w, bytes(ph)))
return out
def load_en_dict():
d = {}
for ln in (LY / "en_dict.txt").read_text().splitlines():
@@ -262,6 +393,17 @@ def c_bytes(data, per=24):
return "\n ".join(out)
def c_strs(items):
out, line = [], ""
for tok in items:
if line and len(line) + len(tok) + 2 > 116:
out.append(line)
line = ""
line += tok + ", "
out.append(line)
return "\n ".join(out)
def main():
out = Path(sys.argv[1]) if len(sys.argv) > 1 else Path("felucca_lyrics.h")
en = load_en_dict()
@@ -269,28 +411,52 @@ def main():
L.append("enum { " + ", ".join(f"PH_{c}" for c, _ in PHONEMES) + ", PH_COUNT };")
L.append("static const char PH_NAME[PH_COUNT][3] = {" + ", ".join(f'"{n}"' for _, n in PHONEMES) + "};")
# the English dictionary
blob, idx, nsyl_en = bytearray(), [], {}
recs, nsyl_en, plain = [], {}, 0
words = sorted(en)
prev = ""
for k, w in enumerate(words):
ph, ns = en_phones(en[w])
if not ns or not re.fullmatch(r"[a-z]{1,23}", w):
raise SystemExit(f"gen_lyrics: {w}: no vowel, or not a-z")
if len(ph) > 32: # (sing.c SW_MAX)
raise SystemExit(f"gen_lyrics: {w}: over 32 phonemes")
nsyl_en[w] = ns
pre = 0
if k % 16 == 0: # each 16th whole (the index's), the rest front-coded
idx.append(len(blob))
else:
if k % EN_BLK: # each EN_BLK-th whole (the index's), the rest front-coded
while pre < min(len(w), len(prev), 31) and w[pre] == prev[pre]:
pre += 1
blob.append(pre)
blob += w[pre:].encode()
blob += bytes(0x80 | (0x40 if st else 0) | c for c, st in ph)
recs.append((pre, w[pre:], bytes(0x80 | (0x40 if st else 0) | c for c, st in ph)))
p16 = 0 # (0.8.9's bytes, a whole word each 16th: the print's comparison)
while k % 16 and p16 < min(len(w), len(prev), 31) and w[p16] == prev[p16]:
p16 += 1
plain += 1 + len(w) - p16 + len(ph) + 2 * (k % 16 == 0)
prev = w
blob, idx, (tp, tl, th), codes = en_dict_pack(recs)
if en_dict_unpack(blob, idx, (tp, tl, th), len(words)) != [(w, r[2]) for w, r in zip(words, recs)]:
raise SystemExit("gen_lyrics: the dictionary does not decode back")
assert len(blob) < 65536
L.append(f"#define LY_EN_N {len(words)}u")
L.append(f"#define LY_END {LY_END}u")
L.append(f"static const uint8_t LY_EN_DICT[{len(blob)}] = {{\n {c_bytes(blob)}}};")
L.append(f"static const uint16_t LY_EN_IDX[{len(idx)}] = {{\n {c_bytes(idx)}}};")
L.append(f"#define LY_EN_BLK {EN_BLK}u")
heads = words[::EN_BLK] # LY_EN_LET[c]: the first block whose first word starts at or after c
let = [sum(1 for h in heads if h[0] < chr(97 + c)) for c in range(27)]
L.append(f"static const uint16_t LY_EN_LET[27] = {{\n {c_bytes(let)}}};")
nlut = 0
for nm, t, code in (("P", tp, codes[0]), ("L", tl, codes[1]), ("H", th, codes[2])):
lut = huff_lut(code, LUT_BITS[nm])
nlut += 2 * len(lut)
L.append(f"#define LY_EN_K{nm} {LUT_BITS[nm]}u")
L.append(f"static const uint8_t LY_EN_HC{nm}[{len(t)}] = {{\n {c_bytes(t)}}};")
L.append(f"static const uint16_t LY_EN_LUT{nm}[{len(lut)}] = {{\n {c_bytes(lut)}}};")
# the words and their phoneme bytes as text, for the host tests only (tests/voice_test.c: every word looked up)
L.append("#ifdef LY_EN_REF")
L.append("static const char *const LY_EN_REF_W[LY_EN_N] = {\n " +
c_strs(f'"{w}"' for w in words) + "};")
L.append("static const char *const LY_EN_REF_PH[LY_EN_N] = {\n " +
c_strs('"' + "".join(f"\\x{b:02x}" for b in r[2]) + '"' for r in recs) + "};")
L.append("#endif")
# the banks
nsyl = {"es": es_nsyl, "en": lambda w: nsyl_en[w] if w in nsyl_en else 0, "ja": ja_nmora}
counts = {}
@@ -311,12 +477,14 @@ def main():
U = lang.upper()
L.append(f"#define LY_{U}_NW {n}u")
L.append(f"static const char LY_{U}_TXT[{len(txt)}] = {{\n {c_bytes(txt)}}};")
L.append(f"static const uint16_t LY_{U}_OFF[{n}] = {{\n {c_bytes(offs)}}};")
offs = offs[::16] # (0.9.0) each 16th word's place (sing.c ly_word: then the 0s)
L.append(f"static const uint16_t LY_{U}_OFF[{len(offs)}] = {{\n {c_bytes(offs)}}};")
L.append(f"static const uint8_t LY_{U}_META[{n}] = {{\n {c_bytes(meta)}}};")
rows_c = ",\n ".join("{" + ", ".join(f"{{{a}, {b}}}" for a, b in t) + "}" for t in grp)
L.append(f"static const uint16_t LY_{U}_GRP[{len(THEMES)}][{len(CATS)}][2] = {{\n {rows_c}}};")
out.write_text("\n".join(L) + "\n")
print(f"lyrics: {len(PHONEMES)} phonemes; English dictionary {len(words)} words, {len(blob) + 2 * len(idx)} B; "
print(f"lyrics: {len(PHONEMES)} phonemes; English dictionary {len(words)} words, "
f"{len(blob) + 2 * len(idx) + len(tp) + len(tl) + len(th) + nlut} B (Huffman; {plain} B as bytes); "
f"banks ES {counts['es']} EN {counts['en']} JA {counts['ja']} words -> {out}")