Files
JIANT/firmware/src/sing.c
T
Claude 329aff53fa 0.9.12 draft: 12 voices with the second core, sound types + PRESETS by type, MIDI LEARN (MENU > MIDI), FM6 bank sector guard, copyright lines
- voice.c voice_cap: VOICES_2CORE 12 while the second core takes parts (fx.c mc_two: CORES 2, not REC at
  power-on, not given up); one core 8, LOAD ECO 6; a track still NVOICE 8 (no RAM). After Skiff by illuminus5.
  cpu_vg: mix/v12 (both cores 3924 instr/sample), mix/v12.c0 (core 0 1949), mix/v12.one (one core, 8 voices 3241);
  regress.c chk_budget_2core; the dual-core equivalence test at one budget (mc_host_v12).
- Sound types (PT_BASS..PT_FX) on all factory presets in preset_t.mono bits 1..4 (no format change); PRESETS LIST
  ALL / FAV / BASS LEAD PAD KEYS PLUCK BELL VOX DRUM FX across engines, saved in favorites.factory[15][18];
  gen.c keeps PT_BASS sounds off the other roles (was a name match). After Skiff by illuminus5.
- MIDI LEARN ported from Felucca 1.5 (midi_learn.c): 10 CCs, 3 bytes each in DIGITAL's favourites row, reserved
  CCs and the standard map refused, any channel, USB and TRS; MENU > MIDI tab (MIDI OUT, LEARN, LEARN CLEAR);
  state in the pool.
- fm6_vbank.c: 0xE7000 holding foreign data (Skiff's pattern bank) reads as no bank and is never erased at
  power-on; only a bank the user sends replaces it. fm6bank_test foreign_test.
- Copyright (C) 2026 Juan Ignacio Antonucci (Juan Jiant) in JIANT's files ("Portions" in mostly-Felucca ones),
  README / LICENSING.md / MENU > ABOUT credits (Felucca, Skiff).
- Docs: MANUAL.md / MANUAL.es.md, README v0.9.12 draft, EDITOR_PROTOCOL.md (tabs), cheat sheets, nav map.

Flash 576192 -> 577712 B (+1520), .data+.bss unchanged, pool +8 B (spare 10528 B).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KMHk6yH4XbP1xxHYKCo9Mh
2026-10-18 18:50:07 +00:00

1301 lines
58 KiB
C

/* SPDX-License-Identifier: GPL-3.0-only
* Copyright (C) 2026 Leo Kuroshita (@kurogedelic), Hügelton Instruments
* Copyright (C) 2026 Juan Ignacio Antonucci (Juan Jiant) */
/* VOICE words (JIANT 0.8.5): the VOICE engine sings syllables, words and lyrics (included by eng_formant.c).
*
* LANG (EDIT > WORDS, P_VLANG: OFF ES EN JA; OFF is VOICE as before, its vowels) turns a VOICE track into a singer: each
* note it starts sings the next syllable of its lyric (a held note holds the vowel; a TIE holds it on; the release
* sings the syllable's last consonants). The lyric (LYRIC, P_VLYR): TEXT, the track's own (typed on the device, sent
* by the editor, saved with the project: lyr_txt), or W1..W16, words the dice puts together from the language's bank
* (tools/lyrics/: themes LOVE NIGHT SEA SKY ROAD DREAM FIRE, or ANY), a phrase as long as the pattern's notes, or
* S1..S8, scat (pseudo-words of the language's syllables). An empty TEXT sings W1.
*
* How the words are read, a syllable at a time (sy_word: the word's phonemes, a byte each, 0x40 the first of a
* syllable): Spanish by rules (sy_es: the letters as Spanish reads them, seseo; sy_syl: the syllables, diphthongs and
* the onset clusters pl br tr ..), Japanese as romaji (sy_ja: Hepburn and kunrei, a mora a syllable: "n" before a
* consonant, a doubled consonant a longer closure, "-" the vowel again), English through the dictionary
* (felucca_lyrics.h, ~2100 words from the CMU Pronouncing Dictionary, syllabified by tools/gen_lyrics.py) and rough
* letter rules for a word it lacks. In the text: words are letters (case and apostrophes ignored, "_" joins two:
* "mi_amor" is two syllables), "-" alone or between words holds the vowel one more note (a melisma), the rest
* separates.
*
* How a syllable sounds (sing_block, once a block, 0.73 ms; formant_render reads it): its phonemes in turn, each a
* target the formants F1..F3 glide to (one-pole, ~6 .. 23 ms), with the source's three gains: voicing (the glottal
* pulse), aspiration (noise into the cascade: h, the aspiration after p t k) and frication (first-difference noise
* into the fourth resonator, tuned to the consonant: s 5.8 kHz, sh 2.9 kHz, f 7 kHz, the bursts). Plosives: a closure
* (silent, or a voice bar for b d g; the formants at the place's locus), a burst, the aspiration (EN ~45 ms, JA 20, ES
* 6), the formants gliding to the vowel; affricates a closure into their fricative; nasals and liquids voiced with wide
* bandwidths and less level; the Spanish tap a 22 ms dip, the trill three. The vowel holds while a key holds; at the
* release the coda (an English diphthong's second vowel too) sounds, voiced ones held. A next note before the release
* (LEGATO, or the next step) sings the coda quickly first.
*
* All per track (sing[]: ~100 bytes a track), none per voice: the voices of a track sing the same syllable (a choir).
* The UI reads sing[] (sing_view): the word, the syllable count, the phoneme sounding, the formants, the gains: the
* VOICE page shows them (and a later sagittal CT head can move with them). */
#include "felucca_lyrics.h" /* generated by build.py (tools/gen_lyrics.py) */
_Static_assert(PH_COUNT == 46, "sing.c PHON[]: one row a phoneme, in gen_lyrics.py's order");
#define LYR_LEN 64u /* a track's lyric: 63 characters and the 0 */
enum { LG_OFF, LG_ES, LG_EN, LG_JA, LG_N };
static const char *const N_LANG[] = {"OFF", "ES", "EN", "JA"};
#define LYR_NW 16u /* W1..W16: words */
#define LYR_NS 8u /* S1..S8: scat */
#define LYR_N (1u + LYR_NW + LYR_NS)
static const char *const N_LYRIC[LYR_N] = {"TEXT", "W1", "W2", "W3", "W4", "W5", "W6", "W7", "W8", "W9", "W10", "W11",
"W12", "W13", "W14", "W15", "W16", "S1", "S2", "S3", "S4", "S5", "S6", "S7",
"S8"};
static const char *const N_THEME[] = {"LOVE", "NIGHT", "SEA", "SKY", "ANY", "ROAD", "DREAM", "FIRE", 0}; /* 16 a name */
#define TH_ANY 4u
#define TH_FN 8u
static const char *const N_LOOP[] = {"FREE", "1", "2", "4", "8", 0};
static const uint8_t LOOP_N[5] = {0, 1, 2, 4, 8};
/* the track's lyric text (TEXT): ASCII, 0-ended. Main loop writes (lyr_set), the audio (either core) reads at syllable
* starts; lyr_gen moves on at each write (the audio starts the lyric over). In the pool (zeroed at boot, as the FX
* lines the audio reads there; 0.8.5: LOWBUF is the main loop's and full) */
static char lyr_txt[NTRK][LYR_LEN] __attribute__((section(".pool")));
static uint8_t lyr_gen[NTRK];
/* ---------------------------------------------------------------------------------------------- phonemes --- */
enum { PC_SIL, PC_VOW, PC_GLI, PC_LIQ, PC_TAP, PC_TRL, PC_NAS, PC_PLO, PC_FRI, PC_ASP, PC_AFF };
/* the place of articulation (a sagittal head: the lips, the tongue's tip, blade, body, the velum, the glottis); bit 7
* rounded lips */
enum { PL_NONE, PL_LAB, PL_LABDEN, PL_DEN, PL_ALV, PL_POST, PL_PAL, PL_VEL, PL_UVU, PL_GLO };
#define PL_RND 0x80u
typedef struct {
uint16_t f1, f2, f3; /* formant targets, Hz (adult male; SHIFT moves them); f1 0: the next vowel's (h) */
uint8_t cls; /* PC_* */
uint8_t av; /* voicing, /255 (a plosive's: its voice bar) */
uint8_t af; /* frication (a plosive's: its burst; h: aspiration), /255 */
uint8_t ff, fbw; /* the frication's resonance and bandwidth, /50 Hz */
uint8_t dur; /* ms: a consonant's steady part (a plosive's closure) */
uint8_t bwm; /* the formants' bandwidths x /16 (nasals, laterals: wider) */
uint8_t place; /* PL_* | PL_RND */
} phon_t;
/* vowels: Spanish / Japanese (Quilis; Japanese u unrounded) and English (Peterson & Barney 1952, Hillenbrand et al.
* 1995, adult male); consonants after Klatt (1980) and Stevens, Acoustic Phonetics (1998): the loci of F2 (labial
* ~900, alveolar ~1750, velar ~2000 Hz), the bursts' and fricatives' spectral peaks */
static const phon_t PHON[PH_COUNT] = {
[PH_SIL] = {0, 0, 0, PC_SIL, 0, 0, 0, 0, 50, 16, PL_NONE},
[PH_A] = {750, 1300, 2500, PC_VOW, 255, 0, 0, 0, 0, 16, PL_NONE},
[PH_E] = {480, 1850, 2580, PC_VOW, 255, 0, 0, 0, 0, 16, PL_NONE},
[PH_I] = {300, 2250, 3000, PC_VOW, 255, 0, 0, 0, 0, 16, PL_NONE},
[PH_O] = {500, 900, 2450, PC_VOW, 255, 0, 0, 0, 0, 16, PL_RND},
[PH_U] = {330, 800, 2300, PC_VOW, 255, 0, 0, 0, 0, 16, PL_RND},
[PH_UJ] = {350, 1250, 2350, PC_VOW, 255, 0, 0, 0, 0, 16, PL_NONE},
[PH_IH] = {420, 2000, 2600, PC_VOW, 255, 0, 0, 0, 0, 16, PL_NONE},
[PH_EH] = {580, 1800, 2600, PC_VOW, 255, 0, 0, 0, 0, 16, PL_NONE},
[PH_AE] = {660, 1720, 2450, PC_VOW, 255, 0, 0, 0, 0, 16, PL_NONE},
[PH_AA] = {730, 1100, 2450, PC_VOW, 255, 0, 0, 0, 0, 16, PL_NONE},
[PH_AO] = {580, 860, 2450, PC_VOW, 255, 0, 0, 0, 0, 16, PL_RND},
[PH_UH] = {450, 1050, 2250, PC_VOW, 255, 0, 0, 0, 0, 16, PL_RND},
[PH_AH] = {640, 1200, 2450, PC_VOW, 255, 0, 0, 0, 0, 16, PL_NONE},
[PH_ER] = {480, 1350, 1650, PC_VOW, 255, 0, 0, 0, 0, 16, PL_RND},
[PH_AX] = {500, 1450, 2450, PC_VOW, 240, 0, 0, 0, 0, 16, PL_NONE},
[PH_Y] = {270, 2200, 3000, PC_GLI, 230, 0, 0, 0, 40, 16, PL_PAL},
[PH_W] = {300, 650, 2250, PC_GLI, 230, 0, 0, 0, 40, 16, PL_LAB | PL_RND},
[PH_L] = {360, 1100, 2700, PC_LIQ, 190, 0, 0, 0, 55, 24, PL_ALV},
[PH_RT] = {450, 1500, 2450, PC_TAP, 255, 0, 0, 0, 22, 16, PL_ALV},
[PH_RR] = {450, 1500, 2450, PC_TRL, 255, 0, 0, 0, 80, 16, PL_ALV},
[PH_RA] = {400, 1100, 1550, PC_LIQ, 220, 0, 0, 0, 60, 20, PL_POST | PL_RND},
[PH_M] = {260, 1100, 2200, PC_NAS, 150, 0, 0, 0, 65, 40, PL_LAB},
[PH_N] = {260, 1650, 2600, PC_NAS, 150, 0, 0, 0, 60, 40, PL_ALV},
[PH_NY] = {260, 2050, 2900, PC_NAS, 150, 0, 0, 0, 65, 40, PL_PAL},
[PH_NG] = {270, 2200, 2700, PC_NAS, 150, 0, 0, 0, 60, 40, PL_VEL},
[PH_NM] = {280, 1400, 2500, PC_NAS, 160, 0, 0, 0, 60, 40, PL_UVU},
[PH_P] = {250, 900, 2200, PC_PLO, 0, 150, 30, 60, 55, 16, PL_LAB},
[PH_T] = {250, 1750, 2700, PC_PLO, 0, 230, 86, 32, 50, 16, PL_ALV},
[PH_K] = {250, 1950, 2500, PC_PLO, 0, 230, 46, 16, 55, 16, PL_VEL},
[PH_B] = {250, 900, 2200, PC_PLO, 50, 80, 30, 60, 40, 16, PL_LAB},
[PH_D] = {250, 1750, 2700, PC_PLO, 50, 140, 86, 32, 35, 16, PL_ALV},
[PH_G] = {250, 1950, 2500, PC_PLO, 50, 140, 46, 16, 40, 16, PL_VEL},
[PH_F] = {300, 1000, 2300, PC_FRI, 0, 50, 140, 80, 80, 16, PL_LABDEN},
[PH_S] = {300, 1650, 2700, PC_FRI, 0, 78, 116, 32, 85, 16, PL_ALV},
[PH_SH] = {300, 1900, 2500, PC_FRI, 0, 217, 54, 12, 90, 16, PL_POST | PL_RND},
[PH_H] = {0, 0, 0, PC_ASP, 0, 150, 0, 0, 60, 16, PL_GLO},
[PH_X] = {300, 1300, 2400, PC_FRI, 0, 84, 32, 16, 75, 16, PL_VEL},
[PH_TH] = {300, 1400, 2600, PC_FRI, 0, 34, 130, 90, 70, 16, PL_DEN},
[PH_V] = {300, 1000, 2300, PC_FRI, 140, 34, 140, 80, 60, 16, PL_LABDEN},
[PH_Z] = {300, 1650, 2700, PC_FRI, 140, 60, 116, 32, 70, 16, PL_ALV},
[PH_ZH] = {300, 1900, 2500, PC_FRI, 140, 170, 54, 12, 70, 16, PL_POST | PL_RND},
[PH_DH] = {300, 1400, 2600, PC_FRI, 160, 20, 130, 90, 50, 16, PL_DEN},
[PH_CH] = {250, 1900, 2500, PC_AFF, 0, 0, 0, 0, 45, 16, PL_POST | PL_RND},
[PH_TS] = {250, 1750, 2700, PC_AFF, 0, 0, 0, 0, 40, 16, PL_ALV},
[PH_J] = {250, 1900, 2500, PC_AFF, 50, 0, 0, 0, 35, 16, PL_POST | PL_RND},
};
/* (0.9.8) the fricatives' noise band-passed (formant_word_render: 1 - z^-2 into F4, ~+5 dB at their peaks) and their gains
* halved (sy_tick), then af set for softer, more natural sibilants: s z ~5 dB under 0.9.7's (s ~-16 .. -20 dB of the vowel, ~6
* kHz), sh zh ~2 dB; f th v dh and the jota as before against s (f, th weak: ~5 dB under s) */
static int ph_vowel(uint32_t c) { return c < PH_COUNT && PHON[c].cls == PC_VOW; }
/* an affricate's fricative */
static uint32_t ph_fric(uint32_t c) { return c == PH_CH ? PH_SH : c == PH_TS ? PH_S : PH_ZH; }
static int ph_sonorant(uint32_t c)
{
uint32_t k = c < PH_COUNT ? PHON[c].cls : PC_SIL;
return k == PC_VOW || k == PC_GLI || k == PC_LIQ || k == PC_NAS;
}
#define SYB 0x40u /* a phoneme byte: the first of a syllable */
/* ----------------------------------------------------------------------------------- reading the words --- */
#define SW_MAX 32u /* a word's phonemes, at most */
enum { TK_C, TK_S, TK_W }; /* a token: a consonant, a strong vowel (a e o), a weak one (i u) */
static int sy_isv(int c) { return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; }
/* Spanish letters -> tokens (tools/gen_lyrics.py es_tokens: the same) */
static uint32_t sy_es_tok(const char *w, uint32_t n, uint8_t *tc, uint8_t *tk)
{
uint32_t i, m = 0;
for (i = 0; i < n && m + 2u <= SW_MAX; i++) {
int c = w[i], nx = i + 1u < n ? w[i + 1u] : 0, nx2 = i + 2u < n ? w[i + 2u] : 0;
uint32_t code = 0xFFu, kind = TK_C;
switch (c) {
case 'a': code = PH_A; kind = TK_S; break;
case 'e': code = PH_E; kind = TK_S; break;
case 'o': code = PH_O; kind = TK_S; break;
case 'i': code = PH_I; kind = TK_W; break;
case 'u': code = PH_U; kind = TK_W; break;
case 'y':
if (sy_isv(nx)) code = PH_Y;
else { code = PH_I; kind = TK_W; }
break;
case 'c':
if (nx == 'h') { code = PH_CH; i++; }
else code = nx == 'e' || nx == 'i' ? PH_S : PH_K;
break;
case 'q': code = PH_K; if (nx == 'u') i++; break;
case 'g':
if (nx == 'e' || nx == 'i') code = PH_X;
else { code = PH_G; if (nx == 'u' && (nx2 == 'e' || nx2 == 'i')) i++; }
break;
case 'j': code = PH_X; break;
case 'x': tc[m] = PH_K; tk[m++] = TK_C; code = PH_S; break;
case 'z': case 's': code = PH_S; break;
case 'v': case 'b': code = PH_B; break;
case 'd': code = PH_D; break;
case 'f': code = PH_F; break;
case 'k': code = PH_K; break;
case 'p': code = PH_P; break;
case 't': code = PH_T; break;
case 'm': code = PH_M; break;
case 'w': code = PH_W; break;
case 'l':
if (nx == 'l') { code = PH_Y; i++; }
else code = PH_L;
break;
case 'n':
if (nx == 'y' && sy_isv(nx2)) { code = PH_NY; i++; }
else code = PH_N;
break;
case 'r':
if (nx == 'r') { code = PH_RR; i++; }
else code = !m || tc[m - 1u] == PH_N || tc[m - 1u] == PH_L || tc[m - 1u] == PH_S ? PH_RR : PH_RT;
break;
default: break; /* h, the rest: silent */
}
if (code != 0xFFu) {
tc[m] = (uint8_t)code;
tk[m++] = (uint8_t)kind;
}
}
return m;
}
static int sy_es_cl(uint32_t a, uint32_t b) /* an onset cluster: p b f k g t d + l r (not dl) */
{
return (a == PH_P || a == PH_B || a == PH_F || a == PH_K || a == PH_G || a == PH_T || a == PH_D) &&
(b == PH_L || b == PH_RT) && !(a == PH_D && b == PH_L);
}
/* tokens -> the word's phonemes with their syllables (gen_lyrics.py syllabify): the vowel groups (split between two
* strong vowels) are the syllables; a group's nucleus its first strong vowel, else its last; the weak ones before it
* glides (y w), after it offglides; between two groups one consonant goes to the next, two too when a cluster, else
* they split (more: the last two or one to the next). Returns the phonemes, *ns the syllables (0: no vowel) */
static uint32_t sy_syl(uint8_t *tc, const uint8_t *tk, uint32_t m, uint8_t *out, uint32_t *ns)
{
uint8_t st[SW_MAX];
uint32_t i = 0, g = 0, pa = 0, k;
for (k = 0; k < m; k++)
st[k] = 0;
while (i < m) {
uint32_t j, nu;
if (tk[i] == TK_C) {
i++;
continue;
}
for (j = i + 1u; j < m && tk[j] != TK_C && !(tk[j] == TK_S && tk[j - 1u] == TK_S); j++)
;
for (nu = i; nu < j && tk[nu] != TK_S; nu++)
;
if (nu == j)
nu = j - 1u;
for (k = i; k < nu; k++)
tc[k] = tc[k] == PH_I ? PH_Y : PH_W;
if (g) {
uint32_t c = i - pa;
if (!c) st[i] = 1;
else if (c == 1u) st[pa] = 1;
else if (c == 2u) st[sy_es_cl(tc[pa], tc[pa + 1u]) ? pa : pa + 1u] = 1;
else st[sy_es_cl(tc[i - 2u], tc[i - 1u]) ? i - 2u : i - 1u] = 1;
}
g++;
pa = j;
i = j;
}
*ns = g;
if (!g)
return 0;
st[0] = 1;
for (k = 0; k < m; k++)
out[k] = (uint8_t)(tc[k] | (st[k] ? SYB : 0u));
return m;
}
/* Japanese romaji -> morae (gen_lyrics.py ja_nmora counts the same) */
static uint32_t sy_ja(const char *w, uint32_t n, uint8_t *out, uint32_t *ns)
{
uint32_t i = 0, m = 0, k = 0, gem = 0, lastv = PH_A;
static const uint8_t JV[5] = {PH_A, PH_I, PH_UJ, PH_E, PH_O}; /* a i u e o */
#define JA_V(c) ((c) == 'a' ? 0 : (c) == 'i' ? 1 : (c) == 'u' ? 2 : (c) == 'e' ? 3 : (c) == 'o' ? 4 : -1)
#define JA_PUT(c, s) do { if (m < SW_MAX) out[m++] = (uint8_t)((c) | ((s) ? SYB : 0u)); } while (0)
while (i < n) {
int c = w[i], nx = i + 1u < n ? w[i + 1u] : 0, nx2 = i + 2u < n ? w[i + 2u] : 0;
if (JA_V(c) >= 0) { /* a vowel */
if (gem) JA_PUT(PH_SIL, 1);
lastv = c == 'u' && lastv == PH_O && k ? PH_O : JV[JA_V(c)]; /* (ou: a long o, sung o-o) */
JA_PUT(lastv, !gem);
gem = 0;
k++;
i++;
} else if (c == '-') { /* the vowel again */
JA_PUT(lastv, 1);
k++;
i++;
} else if (c == 'n' && JA_V(nx) < 0 && nx != 'y') { /* the moraic n */
JA_PUT(PH_NM, 1);
k++;
i += (nx == 'n' || nx == '\'') && JA_V(nx2) < 0 ? 2u : 1u;
} else if ((c == nx && c >= 'a' && c <= 'z') || (c == 't' && nx == 'c')) { /* a doubled consonant */
gem = 1;
i++;
} else if (c < 'a' || c > 'z') { /* (an apostrophe, ..) */
i++;
} else { /* a consonant (+ y) + a vowel */
uint32_t j = i, on = 0xFFu, pal = 0, v;
int vv;
switch (c) {
case 's': on = nx == 'h' ? PH_SH : PH_S; break;
case 'c': on = nx == 'h' ? PH_CH : PH_K; break;
case 't': on = nx == 's' ? PH_TS : PH_T; break;
case 'k': on = PH_K; break;
case 'n': on = PH_N; break;
case 'h': on = PH_H; break;
case 'f': on = PH_F; break;
case 'm': on = PH_M; break;
case 'y': on = PH_Y; break;
case 'r': case 'l': on = PH_RT; break;
case 'w': on = PH_W; break;
case 'g': on = PH_G; break;
case 'z': on = PH_Z; break;
case 'j': on = PH_J; break;
case 'd': on = PH_D; break;
case 'b': on = PH_B; break;
case 'p': on = PH_P; break;
case 'v': on = PH_V; break;
default: on = PH_K; break;
}
while (j < n && JA_V(w[j]) < 0 && w[j] != '-') {
if (j > i && w[j] == 'y')
pal = 1;
j++;
}
vv = j < n ? JA_V(w[j]) : -1;
v = vv >= 0 ? JV[vv] : PH_UJ; /* (a consonant alone: sung with u) */
if (on == PH_S && vv == 1) on = PH_SH; /* si -> shi, ti -> chi, tu -> tsu, hu -> fu, zi di -> ji, */
else if (on == PH_T && vv == 1) on = PH_CH; /* du -> zu */
else if (on == PH_T && vv == 2) on = PH_TS;
else if (on == PH_H && vv == 2) on = PH_F;
else if ((on == PH_Z || on == PH_D) && vv == 1) on = PH_J;
else if (on == PH_D && vv == 2) on = PH_Z;
if (on == PH_SH || on == PH_CH || on == PH_J)
pal = 0; /* (sha cha ja: palatal already) */
{
uint8_t mo[4];
uint32_t nm = 0, q;
if (gem) mo[nm++] = PH_SIL;
if (!(on == PH_W && vv == 4)) /* (wo: o) */
mo[nm++] = (uint8_t)on;
if (pal) mo[nm++] = PH_Y;
mo[nm++] = (uint8_t)(lastv = v);
for (q = 0; q < nm; q++)
JA_PUT(mo[q], !q);
}
gem = 0;
k++;
i = vv >= 0 ? j + 1u : j;
}
}
#undef JA_V
#undef JA_PUT
*ns = k;
return m;
}
/* 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)
{
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, 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;
s = ly_sym(&r, &LY_CL);
b = s == LY_END ? 0 : 'a' + (int)s;
c = a - b;
if (!a || !b)
break;
i++;
}
if (c >= 0) lo = mid;
else hi = mid;
}
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++;
}
if (wl == kn && wl <= sizeof w) {
uint32_t i;
for (i = 0; i < kn && w[i] == key[i]; i++)
;
if (i == kn) {
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;
}
/* a word the dictionary lacks: rough letter rules, then the syllables as Spanish's (vowel groups) */
static uint32_t sy_en_rules(const char *w, uint32_t n, uint8_t *tc, uint8_t *tk)
{
uint32_t i, m = 0, magic = 0xFFu;
#define EN_T(c, k) do { if (m < SW_MAX) { tc[m] = (uint8_t)(c); tk[m++] = (uint8_t)(k); } } while (0)
if (n >= 3u && w[n - 1u] == 'e' && !sy_isv(w[n - 2u]) && sy_isv(w[n - 3u]))
magic = n - 3u; /* a silent e: the vowel before says its name */
for (i = 0; i < n; i++) {
int c = w[i], nx = i + 1u < n ? w[i + 1u] : 0, nx2 = i + 2u < n ? w[i + 2u] : 0;
if (i == n - 1u && c == 'e' && n > 2u) break; /* (a final e: silent) */
if (sy_isv(c) || c == 'y') {
if (c == 'y' && (i == 0u || sy_isv(nx))) { EN_T(PH_Y, TK_C); continue; }
if (c == 'y') { EN_T(i == n - 1u ? PH_I : PH_IH, TK_S); continue; }
if (i == magic) {
if (c == 'a') { EN_T(PH_E, TK_S); EN_T(PH_IH, TK_W); }
else if (c == 'i') { EN_T(PH_AA, TK_S); EN_T(PH_IH, TK_W); }
else if (c == 'o') { EN_T(PH_O, TK_S); EN_T(PH_UH, TK_W); }
else if (c == 'u') EN_T(PH_U, TK_S);
else EN_T(PH_I, TK_S);
continue;
}
if ((c == 'e' && (nx == 'e' || nx == 'a')) ) { EN_T(PH_I, TK_S); i++; continue; }
if (c == 'o' && nx == 'o') { EN_T(PH_U, TK_S); i++; continue; }
if (c == 'o' && (nx == 'u' || nx == 'w')) { EN_T(PH_AA, TK_S); EN_T(PH_UH, TK_W); i++; continue; }
if (c == 'a' && (nx == 'i' || nx == 'y')) { EN_T(PH_E, TK_S); EN_T(PH_IH, TK_W); i++; continue; }
if (c == 'o' && nx == 'a') { EN_T(PH_O, TK_S); EN_T(PH_UH, TK_W); i++; continue; }
if (c == 'o' && (nx == 'i' || nx == 'y')) { EN_T(PH_AO, TK_S); EN_T(PH_IH, TK_W); i++; continue; }
if (c == 'a' && (nx == 'u' || nx == 'w')) { EN_T(PH_AO, TK_S); i++; continue; }
if (c == 'i' && nx == 'g' && nx2 == 'h') { EN_T(PH_AA, TK_S); EN_T(PH_IH, TK_W); i += 2u; continue; }
EN_T(c == 'a' ? PH_AE : c == 'e' ? PH_EH : c == 'i' ? PH_IH : c == 'o' ? PH_AA : PH_AH, TK_S);
continue;
}
if (c == 't' && nx == 'h') { EN_T(PH_TH, TK_C); i++; continue; }
if (c == 's' && nx == 'h') { EN_T(PH_SH, TK_C); i++; continue; }
if (c == 'c' && nx == 'h') { EN_T(PH_CH, TK_C); i++; continue; }
if (c == 'p' && nx == 'h') { EN_T(PH_F, TK_C); i++; continue; }
if (c == 'w' && nx == 'h') { EN_T(PH_W, TK_C); i++; continue; }
if (c == 'c' && nx == 'k') { EN_T(PH_K, TK_C); i++; continue; }
if (c == 'q' && nx == 'u') { EN_T(PH_K, TK_C); EN_T(PH_W, TK_C); i++; continue; }
if (c == 'k' && nx == 'n' && !i) continue;
if (c == 'w' && nx == 'r') continue;
if (c == 'g' && nx == 'h') { i++; continue; }
if (c == 'n' && nx == 'g' && !sy_isv(nx2)) { EN_T(PH_NG, TK_C); i++; continue; }
switch (c) {
case 'b': EN_T(PH_B, TK_C); break;
case 'c': EN_T(nx == 'e' || nx == 'i' || nx == 'y' ? PH_S : PH_K, TK_C); break;
case 'd': EN_T(PH_D, TK_C); break;
case 'f': EN_T(PH_F, TK_C); break;
case 'g': EN_T(PH_G, TK_C); break;
case 'h': if (sy_isv(nx)) EN_T(PH_H, TK_C); break;
case 'j': EN_T(PH_J, TK_C); break;
case 'k': EN_T(PH_K, TK_C); break;
case 'l': if (nx != 'l') EN_T(PH_L, TK_C); break;
case 'm': EN_T(PH_M, TK_C); break;
case 'n': EN_T(PH_N, TK_C); break;
case 'p': EN_T(PH_P, TK_C); break;
case 'r': EN_T(PH_RA, TK_C); break;
case 's': EN_T(i && sy_isv(w[i - 1u]) && sy_isv(nx) ? PH_Z : PH_S, TK_C); break;
case 't': EN_T(PH_T, TK_C); break;
case 'v': EN_T(PH_V, TK_C); break;
case 'w': EN_T(PH_W, TK_C); break;
case 'x': EN_T(PH_K, TK_C); EN_T(PH_S, TK_C); break;
case 'z': EN_T(PH_Z, TK_C); break;
default: break;
}
}
#undef EN_T
return m;
}
/* word w (lower case letters, n) in lang -> out (SW_MAX), its phonemes; *ns its syllables (0: none, a word to skip) */
static uint32_t sy_word(uint32_t lang, const char *w, uint32_t n, uint8_t *out, uint32_t *ns)
{
uint8_t tc[SW_MAX], tk[SW_MAX];
uint32_t m;
*ns = 0;
if (!n)
return 0;
if (lang == LG_JA)
return sy_ja(w, n, out, ns);
if (lang == LG_EN) {
uint32_t len = sy_en_find(w, n, out), i;
if (len) {
for (i = 0; i < len; i++)
*ns += (out[i] & SYB) != 0u;
return len;
}
m = sy_en_rules(w, n, tc, tk);
} else {
m = sy_es_tok(w, n, tc, tk);
}
return sy_syl(tc, tk, m, out, ns);
}
/* --------------------------------------------------------------------------------------- the generator --- */
/* a phrase: templates of categories (N V A D P C: the banks' rows), filled from the theme with words that fit the
* syllables left; a phrase as long as the pattern's notes; the last word fits exactly (or a vocable) */
static const char *const TPL_ES[] = {"DNA", "VDN", "DNV", "PDN", "CDNV", "DNPDN", "VPDN", "NV", "PDNA"};
static const char *const TPL_EN[] = {"CVDN", "DAN", "PDAN", "DNPDN", "CV", "VDN", "PDN", "DNPAN"};
static const char *const TPL_JA[] = {"NPV", "ANPV", "NPN", "NPNPV", "AN", "NPAN"};
static const char *const SCAT_ES[] = {"la", "le", "li", "lo", "lu", "na", "ne", "no", "ma", "me", "mo", "pa", "pe", "po",
"ta", "te", "to", "ba", "bo", "da", "do", "ra", "ri", "sa", "so", "ya", "yo", "cha",
"fa", "ga", "go", "lan", "son", "tin", "bal", "mar", "sol", "dum", "pam"};
static const char *const SCAT_EN[] = {"doo", "bee", "dah", "bop", "wah", "dum", "dat", "dot", "day", "shoo", "la", "na",
"yeah", "oh"};
static const char *const SCAT_JA[] = {"ra", "ri", "ru", "re", "ro", "na", "ni", "nu", "ne", "no", "ta", "te", "to", "ka",
"ki", "ku", "ke", "ko", "sa", "shi", "su", "se", "so", "ma", "mi", "mu", "me", "mo",
"ha", "hi", "fu", "he", "ho", "ya", "yu", "yo", "wa", "pa", "pi", "pu", "pe", "po",
"n"};
static uint32_t sy_rnd(uint32_t *r)
{
uint32_t x = *r ? *r : 0x9E3779B9u;
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
*r = x;
return x;
}
typedef struct {
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},
{LY_JA_TXT, LY_JA_OFF, LY_JA_META, LY_JA_GRP},
};
/* a word of category cat in the theme with at most maxs syllables (exact: just that many): its index, -1 none */
static int32_t ly_pick(uint32_t lang, uint32_t theme, uint32_t cat, uint32_t maxs, int exact, uint32_t *r)
{
const ly_bank_t *b = &LY_BANK[(lang - 1u) % 3u];
uint32_t st = b->grp[theme][cat][0], n = b->grp[theme][cat][1], k, o;
if (!n)
return -1;
o = sy_rnd(r) % n;
for (k = 0; k < n; k++) {
uint32_t j = st + (o + k) % n, s = b->meta[j] & 15u;
if (exact ? s == maxs : s <= maxs)
return (int32_t)j;
}
return -1;
}
/* ------------------------------------------------------------------------------------- the singer state --- */
#define SY_N 16u /* a syllable's phonemes with the last one's coda carried */
enum { SS_HOLD, SS_ON }; /* (sing_t.st, a plosive's: closure 0, burst 1, aspiration 2) */
typedef struct {
uint8_t lang, lyr, gen, playing; /* what it saw last: a change starts the lyric over */
uint8_t pend, rst, gated, mono; /* a note began; back to the start; a key held; the mono note */
uint16_t loops; /* the pattern's loops (LOOP) */
int16_t posv; /* POS as applied */
uint8_t to, tl; /* TEXT: the word's place and length in the text */
uint8_t wsy, wns, mel; /* the word's next syllable, its syllables; a "-" (hold the vowel) */
uint8_t prem, tpl, tps, fem; /* GEN: syllables left in the phrase, the template, its place; a feminine noun */
uint16_t phr; /* GEN: the phrase */
uint32_t rng; /* GEN: the dice */
uint16_t nsy; /* syllables sung since the start */
uint8_t ph[SY_N], np, nu, cd; /* the syllable: [the last one's coda] onset, its nucleus, its coda */
uint8_t i, st, cdon, fast; /* the phoneme sounding, its stage; the coda asked (released); the carried coda's end */
uint16_t tb; /* blocks left of the phoneme (0: held) */
int16_t f[3], ff, fb, bm; /* as it sounds: F1 F2 F3, the frication's resonance and bandwidth (Hz), bandwidths x/16 */
int16_t gv, ga, gf; /* voicing, aspiration, frication, Q15: the block's end .. */
int16_t pv, pa, pf; /* .. its start (formant_render ramps across) */
uint8_t live; /* the articulation has started (from rest: the formants at the first target) */
char w[20]; /* the word (shown; the generator's, read from here) */
} sing_t;
static sing_t sing[NTRK];
/* (0.9.8) more of a track's singer, in the pool (zeroed at boot; sing[] and the RAM region keep their layout, as sing_w):
* the formants' first smoothing stage (f follows it: an S-curve), the blocks since the syllable began (the vibrato's
* onset) and the vibrato's phase (eng_formant.c formant_word_render) */
typedef struct {
int16_t fq[3];
uint16_t vb, vph;
} sing_x_t;
static sing_x_t sing_x[NTRK] __attribute__((section(".pool")));
/* sing_view: what the UI reads (the VOICE page; the CT head) */
static const sing_t *sing_view(const track_t *t) { return &sing[(uint32_t)(t - trk) % NTRK]; }
static uint32_t sing_phon(const sing_t *s) { return s->i < s->np ? s->ph[s->i] : PH_SIL; }
static uint32_t sy_ms(uint32_t ms) { return (ms * 1411u + 512u) >> 10; } /* ms -> blocks (32 samples) */
/* the phrase's length: the NOTE steps of the pattern (1..64; none: 8) */
static uint32_t sy_phrase_len(const track_t *t)
{
uint32_t k, n = 0, len = (uint32_t)clamp(t->p[P_SLEN], 1, NSTEP);
for (k = 0; k < len; k++)
n += t->step[k].time == ST_NOTE && t->step[k].n;
return n ? n : 8u;
}
/* the word w (text) to lower case letters for sy_word: '_' and apostrophes dropped */
static uint32_t sy_norm(const char *s, uint32_t n, char *d, uint32_t max)
{
uint32_t i, m = 0;
for (i = 0; i < n && s[i] && m + 1u < max; i++) {
int c = s[i];
if (c >= 'A' && c <= 'Z') c += 32;
if (c >= 'a' && c <= 'z') d[m++] = (char)c;
}
d[m] = 0;
return m;
}
/* (0.9.0) English words resolved in the main loop, so the audio ISR does not search the dictionary (Huffman-coded:
* ~3x the old lookup's instructions) when a word starts. sy_word's result (n phonemes, ns syllables, the bytes) for:
* lyb_* every word the generator can sing in English (the bank's LY_EN_NW, SCAT_EN, the vocable), built once at
* boot (sing_init) and never written again (lyb_ok published last);
* lyc[] each track's TEXT lyric's words, built with it (lyr_set, the main loop): ph[at[p] - 1] the entry of the
* word at p, [its end, n, ns, the bytes]. seq is odd while it is written (a seqlock: core 1 sings too).
* A word not there (a cache not ready, a lyric past LYC_N bytes, its text changing) is looked up as before: the same
* result, the cost of 0.9.0's search (only then). sy_take reads the word's phonemes kept in sing_w (a track's: in the
* pool, so sing[] and the RAM region keep their layout, and the audio code its code). */
#define LYB_NONE 0xFFFFu
#define LYB_SCAT LY_EN_NW /* SCAT_EN's from here, then the vocable */
#define LYB_N (LY_EN_NW + NELEM(SCAT_EN) + 1u)
#define LYB_PH 1856u /* (2 + n a word: tests/voice_test.c checks they all fit) */
#define LYC_N 192u
static const char *const VOCABLE[3] = {"la", "oh", "ra"}; /* ES EN JA: what is sung when no word fits */
static uint16_t lyb_at[LYB_N] __attribute__((section(".pool"))); /* 1 + the word's entry in lyb_ph, 0 none */
static uint8_t lyb_ph[LYB_PH] __attribute__((section(".pool")));
static uint8_t lyb_ok __attribute__((section(".pool"))); /* (the pool: the RAM region's small globals as they were) */
typedef struct {
uint32_t seq;
uint8_t at[LYR_LEN];
uint8_t ph[LYC_N];
} lyc_t;
static lyc_t lyc[NTRK] __attribute__((section(".pool")));
typedef struct {
uint8_t wok, wlang, wn, wnsy; /* wph holds the word's (sing_t.w's) phonemes in wlang: n, ns, then them */
uint8_t wph[SW_MAX];
} sw_t;
_Static_assert(__builtin_offsetof(sw_t, wph) == __builtin_offsetof(sw_t, wn) + 2u, "sw_t wn wnsy wph: an entry");
static sw_t sing_w[NTRK] __attribute__((section(".pool")));
/* text (at most n characters) as s->w takes it (19 and the 0), then for sy_word: its letters, lower case */
static uint32_t sy_wkey(const char *txt, uint32_t n, char *w, char *nw)
{
uint32_t i, m = 0;
for (i = 0; i < n && txt[i] && m + 1u < 20u; i++)
w[m++] = txt[i];
w[m] = 0;
return sy_norm(w, 20u, nw, 24u);
}
/* the entry (n, ns, the bytes) of English word w into e (room: max): its size, 0 = it does not fit */
static uint32_t sy_entry(const char *txt, uint32_t len, uint8_t *e, uint32_t max)
{
uint8_t buf[SW_MAX];
char w[20], nw[24];
uint32_t ns = 0, n = sy_word(LG_EN, nw, sy_wkey(txt, len, w, nw), buf, &ns), i;
if (2u + n > max)
return 0;
e[0] = (uint8_t)n;
e[1] = (uint8_t)ns;
for (i = 0; i < n; i++)
e[2u + i] = buf[i];
return 2u + n;
}
/* (the main loop, at boot) the generator's English words */
static void sing_init(void)
{
uint32_t k, o = 0;
lyb_ok = 0;
for (k = 0; k < LYB_N; k++) {
const char *q = k < LYB_SCAT ? ly_word(&LY_BANK[1], k) : k < LYB_N - 1u ? SCAT_EN[k - LYB_SCAT] : VOCABLE[1];
uint32_t m = sy_entry(q, 64u, &lyb_ph[o], LYB_PH - o);
lyb_at[k] = (uint16_t)(m ? o + 1u : 0u);
o += m;
}
RING_PUBLISH();
lyb_ok = 1;
}
/* the next TEXT token at or after p in txt: its start (*a, *b its end, *mel "-"), 0 = none at or after p */
static __attribute__((noinline)) int sy_tok(const char *txt, uint32_t p, uint32_t *a, uint32_t *b, uint32_t *mel)
{
uint32_t q;
while (p < LYR_LEN && txt[p] && !(txt[p] == '-' || txt[p] == '_' || txt[p] == '\'' ||
(txt[p] >= 'a' && txt[p] <= 'z') || (txt[p] >= 'A' && txt[p] <= 'Z')))
p++;
if (!(p < LYR_LEN && txt[p]))
return 0;
q = p;
*mel = txt[p] == '-';
if (*mel)
q = p + 1u;
else
while (q < LYR_LEN && (txt[q] == '_' || txt[q] == '\'' || (txt[q] >= 'a' && txt[q] <= 'z') ||
(txt[q] >= 'A' && txt[q] <= 'Z')))
q++;
*a = p;
*b = q;
return 1;
}
/* (the main loop: lyr_set) track k's lyric's English words, from its text */
static void lyc_build(uint32_t k)
{
lyc_t *c = &lyc[k];
const char *txt = lyr_txt[k];
uint32_t p = 0, a, b, mel, o = 0;
for (a = 0; a < LYR_LEN; a++)
c->at[a] = 0;
while (p < LYR_LEN && sy_tok(txt, p, &a, &b, &mel)) {
if (!mel && o + 1u < LYC_N) {
uint32_t m = sy_entry(&txt[a], b - a, &c->ph[o + 1u], LYC_N - o - 1u);
if (m) {
c->ph[o] = (uint8_t)b;
c->at[a] = (uint8_t)(o + 1u);
o += 1u + m;
}
}
p = b;
}
}
/* entry e (n, ns, the bytes) into out: n */
static __attribute__((noinline)) uint32_t sy_take_entry(const uint8_t *e, uint8_t *out, uint32_t *ns)
{
uint32_t n = e[0], i;
*ns = e[1];
for (i = 0; i < n; i++)
out[i] = e[2u + i];
return n;
}
/* GEN: the next word into s->w (W: the bank; S: scat); (0.9.0) returns its place in the English cache (lyb_*), else
* LYB_NONE */
static uint32_t sy_gen_word(const track_t *t, sing_t *s, uint32_t lang, uint32_t lyr)
{
uint32_t theme = (uint32_t)clamp(t->p[P_E1], 0, 127) >> 4, tries, pcat = 0;
int32_t j = -1;
const ly_bank_t *b = &LY_BANK[(lang - 1u) % 3u];
uint32_t src = LYB_NONE;
if (!s->prem) { /* a new phrase */
s->prem = (uint8_t)sy_phrase_len(t);
s->rng = ((lyr * 0x9E3779B1u) ^ (lang * 0x85EBCA77u) ^ (theme * 0xC2B2AE3Du) ^ (s->phr * 0x27D4EB2Fu)) | 1u;
(void)sy_rnd(&s->rng);
s->phr++;
s->tpl = 0xFFu;
s->tps = 0;
}
if (lyr > LYR_NW) { /* scat: 1 .. 3 syllables (English: one) */
const char *const *L = lang == LG_ES ? SCAT_ES : lang == LG_EN ? SCAT_EN : SCAT_JA;
uint32_t nl = lang == LG_ES ? NELEM(SCAT_ES) : lang == LG_EN ? NELEM(SCAT_EN) : NELEM(SCAT_JA);
uint32_t ns = lang == LG_EN ? 1u : 1u + sy_rnd(&s->rng) % 3u, k, m = 0;
if (ns > s->prem) ns = s->prem;
for (k = 0; k < ns; k++) {
uint32_t r = sy_rnd(&s->rng) % nl;
const char *q = L[r];
if (!k && q[0] == 'n' && !q[1]) q = "na"; /* (no word starts with the moraic n) */
if (lang == LG_EN && ns == 1u)
src = LYB_SCAT + r;
while (*q && m + 1u < sizeof s->w)
s->w[m++] = *q++;
}
s->w[m] = 0;
s->prem = (uint8_t)(s->prem - ns);
return src;
}
for (tries = 0; tries < 10u && j < 0; tries++) {
const char *const *T = lang == LG_ES ? TPL_ES : lang == LG_EN ? TPL_EN : TPL_JA;
uint32_t nt = lang == LG_ES ? NELEM(TPL_ES) : lang == LG_EN ? NELEM(TPL_EN) : NELEM(TPL_JA), cat, th;
const char *q;
if (s->tpl == 0xFFu || !T[s->tpl % nt][s->tps]) {
s->tpl = (uint8_t)(sy_rnd(&s->rng) % nt);
s->tps = 0;
}
q = T[s->tpl % nt];
switch (q[s->tps++]) {
case 'N': cat = 0; break;
case 'V': cat = 1; break;
case 'A': cat = 2; break;
case 'D': cat = 3; break;
case 'P': cat = 4; break;
default: cat = 5; break;
}
th = cat >= 3u ? TH_FN : theme == TH_ANY ? (sy_rnd(&s->rng) % 7u + 5u) % 8u : theme; /* (ANY: 5 6 7 0 1 2 3) */
if (s->prem <= 2u && cat < 3u) /* the phrase's end: a word that fits it */
j = ly_pick(lang, th, cat, s->prem, 1, &s->rng);
if (j < 0)
j = ly_pick(lang, th, cat, s->prem, 0, &s->rng);
if (j >= 0 && cat == 0u)
s->fem = (b->meta[j] >> 4) & 1u;
pcat = cat;
}
if (j < 0) /* nothing fits: a noun or verb of exactly what is left */
for (tries = 0; tries < 6u && j < 0; tries++)
j = ly_pick(lang, (sy_rnd(&s->rng) % 7u + 5u) % 8u, tries & 1u, s->prem, 1, &s->rng);
if (j < 0) { /* a vocable */
const char *v = lang == LG_ES ? VOCABLE[0] : lang == LG_EN ? VOCABLE[1] : VOCABLE[2];
src = LYB_N - 1u;
s->w[0] = v[0];
s->w[1] = v[1];
s->w[2] = 0;
s->prem--;
return src;
}
{
const char *q = ly_word(b, j);
uint32_t m = 0;
while (q[m] && m + 1u < sizeof s->w) {
s->w[m] = q[m];
m++;
}
s->w[m] = 0;
if (lang == LG_ES && s->fem && m && s->w[m - 1u] == 'o' && pcat == 2u)
s->w[m - 1u] = 'a'; /* the adjective after a feminine noun */
s->prem = (uint8_t)(s->prem - clamp(b->meta[j] & 15u, 1, s->prem));
}
return (uint32_t)j; /* (English: the bank's word j, as it is) */
}
/* the next word (or "-") of the lyric into s: its text, its syllables; 0 = none */
static int sy_word_next(const track_t *t, sing_t *s, uint32_t lang, uint32_t lyr)
{
uint32_t ns = 0, n = 0, tries, ti = (uint32_t)(t - trk) % NTRK;
char nw[24];
const char *txt = lyr_txt[ti];
const lyc_t *c = &lyc[ti];
sw_t *sw = &sing_w[ti];
sw->wok = 0;
for (tries = 0; tries < 48u; tries++) {
const uint8_t *e = 0;
uint32_t seq = *(const volatile uint32_t *)&c->seq, p = 0, q = 0, mel = 0;
RING_PUBLISH();
s->mel = 0;
if (!lyr && txt[0]) { /* TEXT: its next token */
if (!sy_tok(txt, (uint32_t)s->to + s->tl, &p, &q, &mel) && !sy_tok(txt, 0, &p, &q, &mel))
return 0; /* (no word in the whole text) */
s->mel = (uint8_t)mel;
s->to = (uint8_t)p;
s->tl = (uint8_t)(q - p);
if (s->mel) {
sw->wok = 0;
s->wns = 1;
s->wsy = 0;
s->w[0] = '-';
s->w[1] = 0;
return 1;
}
{
uint32_t i, m = 0;
for (i = p; i < q && m + 1u < sizeof s->w; i++)
s->w[m++] = txt[i];
s->w[m] = 0;
}
if (lang == LG_EN && !(seq & 1u) && c->at[p] && c->ph[c->at[p] - 1u] == q)
e = &c->ph[c->at[p]]; /* (its words resolved with the text: lyr_set) */
} else {
uint32_t src = sy_gen_word(t, s, lang, lyr ? lyr : 1u);
if (lang == LG_EN && src < LYB_N && lyb_ok && lyb_at[src])
e = &lyb_ph[lyb_at[src] - 1u]; /* (the generator's: sing_init) */
}
if (e) {
n = sy_take_entry(e, sw->wph, &ns);
RING_PUBLISH();
if (!lyr && txt[0] && *(const volatile uint32_t *)&c->seq != seq)
e = 0; /* (the text changed meanwhile: as before) */
}
if (!e)
n = sy_word(lang, nw, sy_norm(s->w, sizeof s->w, nw, sizeof nw), sw->wph, &ns);
sw->wn = (uint8_t)n;
sw->wnsy = (uint8_t)ns;
sw->wlang = (uint8_t)lang;
sw->wok = 1;
if (ns) {
s->wns = (uint8_t)ns;
s->wsy = 0;
return 1;
}
}
return 0;
}
/* syllable k of word s->w -> ph (its phonemes), *nu its nucleus, *cd its coda's start; the count */
static uint32_t sy_take(uint32_t lang, const sing_t *s, const sw_t *sw, uint32_t k, uint8_t *ph, uint32_t *nu, uint32_t *cd)
{
uint8_t buf[SW_MAX];
char nw[24];
uint32_t ns, n, i = 0, a, m = 0, v;
if (sw->wok && sw->wlang == lang) /* (0.9.0) the word's, as sy_word_next resolved it */
n = sy_take_entry(&sw->wn, buf, &ns); /* (wn, wnsy, wph: an entry's layout) */
else
n = sy_word(lang, nw, sy_norm(s->w, sizeof s->w, nw, sizeof nw), buf, &ns);
for (a = 0; a < n; a++) /* the k-th syllable's first phoneme */
if ((buf[a] & SYB) && !k--)
break;
for (i = a; i < n && m < 10u; i++) {
if (i > a && (buf[i] & SYB))
break;
ph[m++] = buf[i] & 0x3Fu;
}
for (v = 0; v < m && !ph_vowel(ph[v]); v++) /* the nucleus: its vowel, else a sonorant (the moraic n) */
;
if (v == m)
for (v = 0; v < m && !ph_sonorant(ph[v]); v++)
;
if (v == m)
v = m ? m - 1u : 0u;
*nu = v;
*cd = v + 1u;
return m;
}
static void sy_reset(sing_t *s)
{
s->to = s->tl = 0;
s->wsy = s->wns = s->mel = 0;
s->prem = 0;
s->phr = 0;
s->nsy = 0;
}
/* POS: syllable k of the lyric (from its start; GEN: its phrases again) */
static void sy_seek(const track_t *t, sing_t *s, uint32_t lang, uint32_t lyr, uint32_t k)
{
uint32_t w;
sy_reset(s);
for (w = 0; w < 64u && k; w++) {
if (s->wsy >= s->wns && !sy_word_next(t, s, lang, lyr))
return;
{
uint32_t d = (uint32_t)(s->wns - s->wsy) < k ? (uint32_t)(s->wns - s->wsy) : k;
s->wsy = (uint8_t)(s->wsy + d);
k -= d;
s->nsy = (uint16_t)(s->nsy + d);
}
}
}
/* the phoneme i begins (its stage st): its time */
static void sy_enter(sing_t *s, uint32_t lang)
{
uint32_t c, ms;
const phon_t *P;
if (s->i >= s->np) {
s->tb = 0;
return;
}
c = s->ph[s->i];
P = &PHON[c];
if (s->i == s->nu) { /* the nucleus: held while the note is (the coda waits) */
s->tb = (uint16_t)(s->cdon && s->cd < s->np ? sy_ms(70) : 0u);
return;
}
if (P->cls == PC_PLO)
ms = s->st == 0u ? P->dur : s->st == 1u ? 8u : lang == LG_EN ? 45u : lang == LG_JA ? 20u : 6u;
else if (P->cls == PC_AFF)
ms = s->st == 0u ? P->dur : PHON[ph_fric(c)].dur * 3u / 4u;
else if (P->cls == PC_VOW)
ms = 70u; /* an offglide */
else
ms = P->dur;
if (lang != LG_EN)
ms = ms * 7u / 8u; /* (Spanish, Japanese: a little quicker) */
if (s->i < s->fast)
ms = ms * 5u / 8u; /* the last syllable's coda, carried: quick */
if (s->i + 1u == s->np && s->i > s->nu && ph_sonorant(c))
ms = 0; /* the last of a coda, voiced: held */
s->tb = (uint16_t)(ms ? sy_ms(ms) | 1u : 0u);
}
/* the next stage or phoneme */
static void sy_adv(sing_t *s, uint32_t lang)
{
uint32_t c = s->i < s->np ? s->ph[s->i] : PH_SIL, k = PHON[c].cls;
if (k == PC_PLO && s->st < 2u) {
s->st++;
if (s->st == 2u && (PHON[c].av || lang == LG_ES)) /* voiced (and Spanish p t k): no aspiration */
s->st = 3;
if (s->st < 3u) {
sy_enter(s, lang);
return;
}
} else if (k == PC_AFF && s->st < 1u) {
s->st = 1;
sy_enter(s, lang);
return;
}
s->i++;
s->st = 0;
if (s->i > s->nu && !s->cdon && s->i >= s->cd) { /* (not released: the nucleus holds) */
s->i = s->nu;
s->tb = 0;
return;
}
sy_enter(s, lang);
}
/* a note: the next syllable (the coda not sung yet first, quickly) */
static void sy_next(const track_t *t, sing_t *s, uint32_t lang, uint32_t lyr)
{
uint8_t ph[SY_N];
uint32_t nc = 0, k, n, nu, cd;
if (s->i <= s->nu && s->cd < s->np) /* the coda not begun (a legato / quick next note) */
for (k = s->cd; k < s->np && nc < 4u; k++)
ph[nc++] = s->ph[k];
if (s->wsy >= s->wns && !sy_word_next(t, s, lang, lyr)) {
s->np = s->nu = s->cd = s->i = 0;
return;
}
if (s->mel) { /* "-": the vowel on (re-sung, the coda kept for later) */
s->wsy++;
s->nsy++;
s->cdon = 0;
if (s->np) {
s->i = s->nu;
s->st = 0;
s->tb = 0;
}
return;
}
n = sy_take(lang, s, &sing_w[(uint32_t)(t - trk) % NTRK], s->wsy, ph + nc, &nu, &cd);
s->wsy++;
s->nsy++;
for (k = 0; k < nc + n && k < SY_N; k++)
s->ph[k] = ph[k];
s->np = (uint8_t)k;
s->nu = (uint8_t)(nc + nu);
s->cd = (uint8_t)(nc + cd);
s->fast = (uint8_t)nc;
s->i = 0;
s->st = 0;
s->cdon = 0;
sy_enter(s, lang);
}
/* the targets of the phoneme sounding, and the articulation one block on */
static __attribute__((noinline)) void sy_tick(const track_t *t, sing_t *s, uint32_t lang)
{
uint32_t c, k, j, nxt = PH_A, sh = 4u;
int32_t a15 = (127 - clamp(t->p[P_E2], 0, 127)) * 258, tv = 0, ta = 0, tf = 0, tff = 3300, tfb = 250, tbm;
int32_t tF[3];
const phon_t *P, *Q;
sing_x_t *x = &sing_x[(uint32_t)(s - sing) % NTRK];
if (s->tb && !--s->tb)
sy_adv(s, lang);
c = sing_phon(s);
P = &PHON[c];
k = P->cls;
for (j = s->i; j < s->np && !ph_vowel(s->ph[j]); j++) /* the vowel ahead (h, a release glides to it) */
;
if (j < s->np)
nxt = s->ph[j];
else if (s->nu < s->np)
nxt = s->ph[s->nu];
Q = &PHON[nxt];
tF[0] = P->f1 ? P->f1 : Q->f1;
tF[1] = P->f1 ? P->f2 : Q->f2;
tF[2] = P->f1 ? P->f3 : Q->f3;
tbm = P->bwm;
switch (k) {
case PC_SIL:
tF[0] = s->f[0];
tF[1] = s->f[1];
tF[2] = s->f[2];
break;
case PC_PLO:
if (s->st == 0u) { /* closure: silent (a voice bar), F1 down, the locus */
tv = P->av << 7;
tv += ((32767 - tv) * (32767 - a15)) >> 16; /* (ARTIC: less, the closure only half shut) */
} else { /* burst, aspiration: towards the vowel */
tF[0] = Q->f1;
tF[1] = (P->f2 + Q->f2) / 2;
tF[2] = Q->f3;
sh = 3;
if (s->st == 1u) {
tf = (P->af << 6) * a15 >> 15;
tff = P->ff * 50;
tfb = P->fbw * 50;
tv = P->av ? 16000 : 0;
} else {
ta = (14000 * a15) >> 15;
tf = (P->af << 4) * a15 >> 15;
tff = P->ff * 50;
tfb = P->fbw * 50;
}
}
break;
case PC_AFF:
if (s->st == 0u) {
tv = P->av << 7;
tv += ((32767 - tv) * (32767 - a15)) >> 16;
} else {
const phon_t *F = &PHON[ph_fric(c)];
tv = F->av << 7;
tf = (F->af << 6) * a15 >> 15;
tff = F->ff * 50;
tfb = F->fbw * 50;
sh = 3;
}
break;
case PC_FRI:
tv = P->av << 7;
tf = (P->af << 6) * a15 >> 15; /* (0.9.8: x 1/2, the band-pass's gain) */
tff = P->ff * 50;
tfb = P->fbw * 50;
if (c == PH_X)
ta = (6000 * a15) >> 15;
if (!P->av)
tv = ((32767 - a15) * 3) >> 3; /* (ARTIC down: some voice through it) */
break;
case PC_ASP:
ta = (P->af << 7) * a15 >> 15;
tv = ((32767 - a15) * 3) >> 3;
break;
case PC_TAP:
tv = 32767 - ((24000 * a15) >> 15);
sh = 3;
break;
case PC_TRL:
tv = (s->tb / sy_ms(20)) & 1u ? 32767 - ((24000 * a15) >> 15) : 32767;
sh = 3;
break;
default: /* vowels, glides, liquids, nasals */
tv = P->av << 7;
if (k == PC_VOW && s->i > 0u && s->i <= s->np && ph_vowel(s->ph[s->i - 1u]))
sh = 5; /* a diphthong's glide: slower */
break;
}
if (!s->live) { /* from rest: at the first target at once */
x->fq[0] = s->f[0] = (int16_t)(k == PC_SIL ? Q->f1 : tF[0]);
x->fq[1] = s->f[1] = (int16_t)(k == PC_SIL ? Q->f2 : tF[1]);
x->fq[2] = s->f[2] = (int16_t)(k == PC_SIL ? Q->f3 : tF[2]);
x->vph = 0; /* (the vibrato from its start: a phrase renders the same) */
tF[0] = s->f[0];
tF[1] = s->f[1];
tF[2] = s->f[2];
s->ff = (int16_t)tff;
s->fb = (int16_t)tfb;
s->bm = (int16_t)tbm;
s->live = 1;
}
/* (0.9.8) coarticulation: two one-poles in a row (each twice as quick: about the time one took), so the formants
* ease out of a target and into the next, an S-curve with no corner at the phoneme's change */
sh--;
for (j = 0; j < 6u; j++) {
int16_t *a = j < 3u ? &x->fq[j] : &s->f[j - 3u];
int32_t d = (j < 3u ? tF[j] : x->fq[j - 3u]) - *a;
*a = (int16_t)(*a + (d >> sh) + (d > 0 && (d >> sh) == 0));
}
s->ff = (int16_t)(s->ff + ((tff - s->ff) >> 1));
s->fb = (int16_t)(s->fb + ((tfb - s->fb) >> 1));
s->bm = (int16_t)(s->bm + ((tbm - s->bm) >> 2));
s->pv = s->gv;
s->pa = s->ga;
s->pf = s->gf;
/* (0.9.8) the gains: the voicing fades over ~6 ms into a fricative (the vowel's tail under the hiss), ~3 ms into a
* closure (a tap's, a trill's dips: at once), comes in over ~3 ms; the frication comes in over ~10 ms (a plosive's
* burst, an affricate's release: at once) and goes over ~5 ms: no hiss switched on and off */
s->gv = (int16_t)(s->gv + ((tv - s->gv) >> (tv >= s->gv ? 2 : k == PC_FRI ? 3 : k == PC_TAP || k == PC_TRL ? 1 : 2)));
s->ga = (int16_t)(s->ga + ((ta - s->ga) >> 1));
s->gf = (int16_t)(tf > s->gf && k != PC_FRI ? tf : s->gf + ((tf - s->gf) >> (tf > s->gf ? 3 : 2)));
}
/* the engine's block hook (eng_formant.c formant_block): the lyric, the syllable, the articulation */
static void sing_block(track_t *t)
{
uint32_t ti = (uint32_t)(t - trk) % NTRK, lang = (uint32_t)clamp(t->p[P_VLANG], 0, LG_N - 1), i, gated = 0;
uint32_t lyr = (uint32_t)clamp(t->p[P_VLYR], 0, LYR_N - 1);
int32_t pos = clamp(t->p[P_E0], 0, 127);
sing_t *s = &sing[ti];
sing_x_t *x = &sing_x[ti];
if (!lang) {
if (s->lang) {
memset(s, 0, sizeof *s);
memset(x, 0, sizeof *x);
}
return;
}
if (lang != s->lang || lyr != s->lyr || lyr_gen[ti] != s->gen) {
s->lang = (uint8_t)lang;
s->lyr = (uint8_t)lyr;
s->gen = lyr_gen[ti];
s->rst = 1;
}
if (song.playing != s->playing) { /* PLAY: from the start */
s->playing = song.playing;
s->loops = t->loops;
if (song.playing)
s->rst = 1;
} else if (song.playing && t->loops != s->loops) { /* LOOP: back to the start every so many loops */
uint32_t L = LOOP_N[((uint32_t)clamp(t->p[P_E7], 0, 127) * 5u) >> 7];
s->loops = t->loops;
if (L && t->loops % L == 0u)
s->rst = 1;
}
for (i = 0; i < NVOICE; i++)
gated |= t->v[i].gate & t->v[i].active;
if (gated && s->gated && t->mono_note && t->mono_note != s->mono && t->p[P_VOICE] != V_POLY)
s->pend = 1; /* a legato note: the next syllable too */
s->mono = t->mono_note;
if (s->pend) {
s->pend = 0;
if (s->rst) {
sy_reset(s);
s->rst = 0;
}
if (pos && pos != s->posv)
sy_seek(t, s, lang, lyr, (uint32_t)pos - 1u);
s->posv = (int16_t)pos;
x->vb = 0; /* (a syllable: the vibrato waits again) */
sy_next(t, s, lang, lyr);
} else if (!pos) {
s->posv = 0;
}
if (!gated && s->gated && !s->cdon) { /* released: the coda */
s->cdon = 1;
if (s->i == s->nu && s->cd < s->np)
s->tb = 1;
}
s->gated = (uint8_t)gated;
sy_tick(t, s, lang);
x->vb = (uint16_t)(x->vb + (x->vb < 0xFFFFu)); /* (0.9.8) the vibrato (formant_word_render): ~5.5 Hz */
x->vph = (uint16_t)(x->vph + 262u);
}
static void sing_note(const track_t *t) { sing[(uint32_t)(t - trk) % NTRK].pend = 1; }
/* ------------------------------------------------------------------------------------ the main loop's --- */
/* the track's lyric text (the NAME screen, the editor, a project load): the audio starts it over */
static void lyr_set(uint32_t k, const char *s)
{
uint32_t i;
char b[LYR_LEN];
for (i = 0; s && s[i] && i + 1u < LYR_LEN; i++)
b[i] = (char)(s[i] >= 32 && s[i] <= 126 ? s[i] : ' ');
b[i] = 0;
for (; i < LYR_LEN; i++)
b[i] = 0;
k %= NTRK;
lyc[k].seq++; /* (odd: the audio does not read the cache meanwhile) */
RING_PUBLISH();
for (i = 0; i < LYR_LEN; i++)
lyr_txt[k][i] = b[i];
lyc_build(k); /* (0.9.0) its English words resolved */
RING_PUBLISH();
lyc[k].seq++;
lyr_gen[k]++;
}
static const char *lyr_get(uint32_t k) { return lyr_txt[k % NTRK]; }
/* a phrase of the generator as text (the NAME screen's dice: KNOB 3), syllables in the language's words */
static void lyr_dice(const track_t *t, uint32_t lang, uint32_t seed, char *out, uint32_t max)
{
static sing_t g; /* (main loop only) */
uint32_t m = 0, nw;
memset(&g, 0, sizeof g);
g.phr = (uint16_t)seed;
lang = lang ? lang : LG_ES;
for (nw = 0; nw < 16u; nw++) {
uint32_t i;
sy_gen_word(t, &g, lang, 1u + seed % LYR_NW);
for (i = 0; g.w[i] && m + 2u < max; i++)
out[m++] = (char)(g.w[i] >= 'a' && g.w[i] <= 'z' ? g.w[i] - 32 : g.w[i]);
if (!g.prem)
break;
if (m + 2u < max)
out[m++] = ' ';
}
out[m] = 0;
}