The feedback engine, step by step
The one way to turn the model's output into feedback a learner sees: the calibrated engine, every step exact, and the test vectors that prove a build of it.
Feedback a learner can trust comes from one computation: the one the model's thresholds were fitted for. Quran Lab fits them for every model version on correct recitation, and a threshold is validated once it held on reciters the fit never heard. So a flag at the strict set means strong evidence that the learner did not recite what the ayah has, at a known rate of false flags: its target is 0.5 % of the places checked. This page is that computation, step by step, exact enough to build from, and the vectors that prove a build of it right. The engine is your app's code. The SDK gives it everything it reads.
Built any other way, the same output gives bad feedback. A diff of the decoded text against the ayah reads the model's best guess as the last word on the recitation: one unclear frame becomes a flag, and a sound the model was unsure of passes. One head alone, or thresholds of your own, have no measured rate of false flags at all. An LLM or another model listening to the audio or reading the text is not the model the thresholds belong to. Build feedback none of these ways.
What the engine reads
| Input | Where it comes from |
|---|---|
| The take's heads | session.finish(), once the take has ended: log_probs, inter1_log_probs, timing_log_probs, attr_qalqalah and gem over the whole take. Ask for these five in the session options (heads). Never the heads of single chunks while the take runs: the thresholds were fitted on whole takes, and a chunk read alone gives other numbers. |
| The thresholds | model.calibration of the loaded model, at the context the take ran in: name it in the session options, or read the context of the client's diagnosis (C: tf_client_diagnose) right before the take, since a new config can change it later. Never numbers copied into code: each model version has its own. |
| The expected units | model.reference: the units of the ayah from its start to its stop, with the word bar between words. |
| Lengthening and shadda | The dataset quran-tajweed-phonetics, joined to the units as the reference shows: each long vowel's class and the counts it may have, and each geminate letter. Make this table once, when you build the app. |
| The set | strict, unless the learner or you chose another. |
The calls, per platform:
| Android | Apple | Web | Windows and Linux (C) | Flutter | |
|---|---|---|---|---|---|
| The take's heads | session.finish(), then heads["log_probs"] |
try await session.finish(), then heads["log_probs"] |
await session.finish(), then heads.get('log_probs') |
tf_session_finish(session) (a take from tf_session_listen has already ended), tf_session_heads(session), then tf_heads_get(heads, "log_probs", &head) |
await session.finish(), then heads['log_probs'] |
| A head's numbers | frames, classes, values |
frames, classes, values |
frames, classes, values |
head.frames, head.classes, head.values |
frames, classes, values |
| The thresholds | model.calibration: json, sha256, threshold(context, siteClass, preset), status(context, siteClass), fusionWeight(context, siteClass), value(...) |
model.calibration: data, sha256, threshold(context:class:preset:), status(context:class:), fusionWeight(context:class:), number(...), string(...) |
model.calibration: json, sha256, threshold(context, cls, preset), context(name) |
tf_model_calibration_json(model), tf_model_calibration(model, &c) for c.sha256, tf_model_calibration_number(model, path, &x) |
model.calibration: json, sha256, threshold, status, fusionWeight, value([...]) |
| The expected units | model.reference: fetch(), then units(surah, ayah) |
model.reference: try await fetch(), then try units(surah:ayah:) |
model.reference: await fetch(), then await units(surah, ayah) |
tf_model_reference(model), tf_reference_fetch(reference), then tf_reference_units(reference, surah, ayah, &units) |
model.reference: await fetch(), then await units(surah, ayah) |
| A sample clip on the CPU engine | model.listen(from = clip, options = TajweedSessionOptions(heads = ..., backend = TajweedSessionOptions.Backend.CPU, pace = TajweedSessionOptions.Pace.FAST)) |
model.listen(from: clip, options: TajweedSessionOptions(heads: ..., backend: .cpu, pace: .fast)) |
model.listen({ from: clip, heads: [...], backend: 'cpu', pace: 'fast' }) |
tf_session_listen(model, &clip, &options) with heads, backend = "cpu" and pace = TF_PACE_FAST |
model.listenFrom(clip, options: TajweedSessionOptions(heads: [...], backend: TajweedSessionBackend.cpu, pace: TajweedPace.fast)) |
A head's values are frames rows of classes numbers, row by row. The CTC heads are log-probabilities over the 38 tokens; attr_qalqalah and gem are probabilities per class. Read the units in order: a word bar ends a word and is dropped, and every other unit keeps the number of its word.
When the engine cannot run
- No calibration. When
model.calibrationis null, nothing may be flagged: show what was heard, and no thresholds of your own in its place. In browsers the web SDK has given it since 2026-11-24; in Node,@quran-lab/tajweedformergives it from its release of 2026-11-25 on, and an earlier release gives null. - No reference. When
model.referenceis null, the units of the ayah come from the same dataset join, which gives exactly the unitsmodel.referencegives for this vocabulary, checked on all 6,236 ayat (the reference). - Another riwaya. The reference and the thresholds are for Hafs 'an 'Asim. A recitation in another riwaya is shown as heard, never flagged.
The steps
The numbers are token ids of the model's vocabulary (model.tokens, 38 tokens; the calibration's tokens_sha256 names it): blank 0, word bar 1, the 28 consonants 2 to 29, the ikhfa nasal 30, the short vowels a 31, i 32 and u 33, the long vowels aa 34, ii 35 and uu 36, and a foreign sound 37. Compute in double precision.
- The path. The ayah's units without the word bars: P, with the word of each unit. A unit is the last of its word when the next unit belongs to another word.
- The two heads. A is
log_probsas it comes. B isinter1_log_probswith the word bar folded into blank, as the calibration'sfusion.inter1_prepsays: in every frame, blank becomes log(exp(blank) + exp(word bar)), and the word bar becomes minus infinity. - The likelihood. LL(H, S) is the full-sequence CTC forward log-likelihood of the unit sequence S on head H: blank around and between the units, a unit may last several frames, the blank between two different units may be skipped, summed over every path in log space. Compute LL(A, P) and LL(B, P) once. Computing it fast: one full forward per alternative costs units times frames each time, minutes for a long ayah. Compute instead, once per head, the forward variables of P (every prefix, at every state and frame) and its backward variables (every suffix); an alternative changes only its window, so its likelihood joins the forward variables before the window, a short forward over the changed window, and the backward variables after it. Running that short forward over every frame is exact. To go faster, run it over a band of frames and bound what the band leaves out: the paths that leave the part before the window outside the band carry at most the forward mass there times the whole backward mass after the window, the paths that enter the part after it outside the band the same the other way round, and a window at the start of the ayah also counts paths that begin inside it. Keep the band only when that bound is more than 40 nats below the result, else use every frame. Measured with one desktop thread in C on takes made by repeating a sample clip: under a second for 75 units (the mean ayah), about 20 seconds for 672 units (the longest ayah has 802). Check it against a full forward on a few alternatives.
- The sites. A site starts at each unit of P that is one of the 28 consonants (2 to 29). Its window is that consonant and the vowel after it when one follows (31 to 36), else the consonant alone. The ikhfa nasal and the vowels start no site.
- The alternatives. For each position of the window, in order: first every substitution, by each other id in the same group in ascending order (a consonant or the ikhfa nasal: ids 2 to 30; a vowel: ids 31 to 36); then the deletion of that unit; then, for a consonant or the ikhfa nasal with no vowel after it inside the window, the insertion right after it of each vowel, 31 to 36 in order. An alternative is P with the window replaced by the changed window.
- The class of an alternative. A vowel replaced by the same vowel of the other length (a and aa, i and ii, u and uu) is
short_to_longfrom a short vowel andlong_to_shortfrom a long one. Any other vowel for a vowel isvowel_sub. Any other substitution iscons_sub. A deleted vowel isvowel_del, any other deletioncons_del. An insertion isvowel_ins. The classinsertionnever comes from this list. - The evidence. For an alternative S: llr_A = LL(A, S) minus LL(A, P), and llr_B the same on B. With a =
fusion.a.<class>, elsefusion.a.default: fused = a times llr_A plus (1 minus a) times llr_B. A result that is not a number becomes -30. Then clip it tofusion.clip, [-30, 30]. Clip after fusing, never each head before. - The best per class. At each site and for each class, keep the alternative with the largest fused value; on a tie, the first one in the order of step 5.
- The flags. Compute every value whatever the status; the status and the checks chosen decide only whether a flag shows. A class is flagged at a site when its status lets it show (below) and its fused value is greater than the context's
sites.<class>.thresholds.<set>. Strictly greater: equal is not flagged. The site's class is the flagged class with the largest fused value; on a tie, the one that comes first in the calibration'sclasses. Show it with its alternative: the model heard the changed window where the ayah has the window. - The spans. Align the path on
timing_log_probs(20 ms frames). The states are a blank, the units of the first word, a blank, the units of the next word, and so on, ending with a blank: one blank between words and at both ends, none inside a word. Find the best path (Viterbi, maximum of summed log-probabilities): it starts in the first blank or the first unit; at each frame it stays, moves to the next state, or jumps over a blank to the unit after it; it ends in the last blank or the last unit. On a tie, staying comes before moving and moving before jumping, and of the two ends the last blank wins. A unit's span is the frames its state holds, from its first frame to the frame after its last. - Lengthening (
madd). Each long vowel that the dataset join gives alength(a phone of kindmadd) has an entry: classlazimif one of its rule ids starts with R187, R188 or R011_MUQATTAAT, elsemuttasilif one starts with R185_MUTTASIL, elsefreeif one starts with R186_MUNFASIL, R189_AARED or R184_SILAH_KUBRA, elsefixed2; its counts are thescoringof itslength. The last word is read with its stop: there, a long vowel followed by exactly one more unit, one of the 28 consonants, isfreewith counts 2, 4 and 6 when its entry wasfixed2or it had none. The ratio r is the long vowel's span length over the median span length of the path's short vowels (31, 32 and 33; for an even number of them, the mean of the two in the middle); with fewer thanmadd.min_short_vowelsshort vowels there is no reading: no ratio, no count, no flag. The cuts aremadd.preset_cuts.<set>.<class>, elsemadd.cuts.<class>, and for a class with no cuts thefreeones: the count is 2 when r is below "2|4", 4 when r is below "4|6" (when "4|6" is not above "2|4", "2|4" is used for it), and 6 otherwise. Flag the vowel when its count is not one of its counts. The lazim class shows only where bothmadd.status(ormaddchosen) andmadd.cuts.lazim.status(orlazimchosen) let it. - Qalqalah. A site is each of the five qalqalah letters (ids 3, 6, 9, 17 and 22) with no vowel after it. With e the end of its span: P(burst) is the mean of
attr_qalqalahclasses 1 plus 2 over frames e + 2 to e + 13 (12 frames), or to the last frame of the take if it comes first; no reading, and no flag, when frame e + 2 does not exist. Flag "qalqalah missing" when P(burst) is belowqalqalah.preset_tau.<set>, elseqalqalah.tau. - Gemination. A site is each geminate letter of the dataset join. P(held) is the mean of
gemclass 1 over the letter's span (at least its first frame). Flag "shadda missing" when P(held) is belowgemination.preset_tau.<set>, elsegemination.tau. - The per-word readout, when the context has a
wordblock. For each word, the window is all its units, and its alternatives are those of step 5 over that window, leaving out one that empties it, whatever the status of their site class. The word's value is the largest fused value of step 7, the first on a tie. Flag the word when its value is at least the larger ofword.presets.<set>.tauandword.floor. The word's sets arestrict,generalandbeginner; usestrictunless the learner or you chose another.
What may show
Every check carries a status in the calibration: each site class (sites.<class>.status), madd.status (and madd.cuts.lazim.status for the lazim class), qalqalah.status, gemination.status and word.status.
validatedshows by default.experimentalandunverifiedshow only where you, or the learner in a setting your app offers, chose that check, and the screen says that it is not validated yet.offnever shows.
A check is named by its site class (vowel_sub, cons_sub and the others), or madd, lazim, qalqalah, gemination or word. (The app's declaration names what its screen shows in its own words, such as shadda for gemination.)
strict is the default set: the fewest false flags. Use it in place of the calibration's default_preset and word.default_preset, which name the middle of what was measured, not a learner's default. balanced, sensitive and very_sensitive catch more and flag more correct recitation; offer them as a choice, never as the default for a learner alone. Ghunna and tafkheem have no readout: never flag them.
Say what was heard against what the ayah has, never a ruling on the learner's recitation. A line such as "Automatic tajweed feedback can be wrong. It does not replace a qualified teacher." is encouraged where the feedback shows. Your app may count what it showed: client.analytics.reportChecks on Android and Apple, and an outcome with session.reportRecited (Build your own feedback), with the calibration's class ids as rule ids.
Where the books state the rule
Under a flag of lengthening, qalqalah or shadda, your app may show where the classical books state the rule that applies at that place. It comes from the dataset, like the lengthening entries: when you make the table the engine needs, keep each entry's rule id, and ship the rows of the dataset's rule_citations.jsonl for the rules you keep, with their names from rulings.jsonl. The rule ids are the dataset's, not names for reportChecks.
Which rule id. A unit often carries several. A flag shows one:
- Lengthening: the id that gave the long vowel its class in step 11. For
lazim, the one that starts with R187, R188 or R011_MUQATTAAT; formuttasil, the one that starts with R185_MUTTASIL; forfree, the one that starts with R186_MUNFASIL, R189_AARED or R184_SILAH_KUBRA. Forfixed2,R181_BADAL,R121_MADD_EWADorR180_PAUSAL_GLIDEwhere the phone carries one, elseR180_TABEEI; neverR134_LAM_JALALA_ALIF, whose citation is the line on the lam of the name Allah. A long vowel that step 11 makesfreeat the stop showsR189_AAREDwhere the phone carries it, and no citation otherwise. - Qalqalah: the one the letter carries of
R200_QALQALAH_SUGHRA,R201_QALQALAH_KUBRAandR202_QALQALAH_AKBAR. - Shadda: the first the letter carries of
R141_IDGHAM_GHUNNA,R141_IDGHAM_GHUNNA_NAQIS,R142_IDGHAM_BILA_GHUNNA,R160_MUTAMATHILAYN,R170_GHUNNA_MUSHADDADAHandR133_R160_IDGHAM_KAMIL, never a tafkheem rule (R210_ISTILA,R211_REH,R212_LAM_JALALA). A shadda of the word's own spelling carries none of them: no citation. Where a word that ends in a noon sakinah is followed by a word that starts with ra or lam (338 places, 2:5 among them), the merged letter carriesR133_R160_IDGHAM_KAMILwhereR142_IDGHAM_BILA_GHUNNAapplies: show no citation there. In the table, that is a geminate ra or lam that starts its word, after a written word that ends in noon with no noon among its phones. - The site classes and the per-word readout (a letter or a vowel heard in place of another) have no tajweed rule: no citation.
Which row. Of the rule's rows, in the order of n, show the first whose status is quoted, bayt, on_cited_page or located. A candidate, not_found or note row is never shown as a page. A rule with none of the four shows its name alone.
How it shows. Under the flag, the words "Where the books state this rule", then the rule's name (name_ar or name_en), the work, the edition, the volume and page (and the bayt) and the link (url), as the files give them, never translated or shortened, and the row's status in words:
status |
In words |
|---|---|
quoted |
The quoted words are on this page |
bayt |
The cited bayt is on this page |
on_cited_page |
This page holds the rule's terms |
located |
Found in this work by the rule's terms |
The passage (excerpt) may show too, except on a row with a remark, where the page and the link show without it. The citation explains the rule. It never says that the learner erred: the flag stays what was heard against what the ayah has.
Prove it: the vectors
Quran Lab publishes, for each model calibration, the numbers this engine gives on the model's own sample clips, made by the code the thresholds were fitted with. The index lists one file per calibration, at https://sdk.quranlab.ai/engine-vectors/<calibration sha256>.json: model_id, version, builds, contexts, calibration_sha256, url, sha256 and bytes. The file for the loaded model is the one whose calibration_sha256 equals the sha256 of model.calibration.
A file holds the calibration it belongs to (calibration, the text), the tolerance (llr, probability, ratio and spans), the sets it carries (presets: sites, lengthening, word), and cases. Each case has:
context,clip(a name inmodel.sampleClips),kind(clean,planted,mutatedorprobe) andayah. The model's own sample clips come first; then the three clips the Apple SDK carries (Abdul_112002,Husary_001003andSaood_112002,clip_set:apple-bundled), which are Apple'smodel.sampleClipsuntil the build's own clips are on the device;input:unitsandwordsasmodel.referencegives them, and from the dataset joinlengthening(unit,class,scoring) andgeminate, by position in the units without the word bars;mutation: on amutatedcase,inputalready carries the change: the unit atmutation.unit(counted without the word bars) ismutation.toinstead ofmutation.from, and its lengthening entry and geminate mark are gone, so the audio no longer says what the units say there;calibration_changes: paths of keys with a new value, to set in a copy of the calibration first, andopt_in: the checks chosen beyondvalidated. Aprobemay also carry aninputof its own (lengthening_muttasilgives a long vowel another class);heads(per headframes,classesandfloat32le_base64, its values as little-endian float32 in base64), orheads_from, the case whose heads it reads; only cases in the model's default context have heads, their own or throughheads_from; elsewhereheads_fromis null and the case is for the live check;- what the engine gives:
pathandpath_word(the units without bars and the word of each),sites(per sitei,window,word, and per classalternative,label,llr_log_probs,llr_inter1andfused, withflaggedper set:classes, in the calibration's order, andsite_class),words(per wordfused,label,classandflaggedper word set;labelnames the edit assub<position>:<token>,del<position>orins<position>:<token>, the position counted in the window and the token by its name inmodel.tokens, for reading: comparealternative),spans(per unit[i, first frame, frame after the last]),madd(short_vowel_median_frames, and initemsper voweli,class,scoring,aared_at_stop,ratio, andcountandflaggedper set),qalqalah(i,p_burst,flagged),gemination(i,p_held,flagged) andmust_flag(per set, each flagged site as[i, class]). Sites carry all four sets; lengthening, qalqalah and gemination carrystrictandbalanced. A value with no reading is null and never flagged.llr_log_probsandllr_inter1are each clipped to [-30, 30] on their own, for comparison only.
Two checks prove an engine:
- The unit test, offline, in the project's tests. For every case with heads, its own or through
heads_from, run the engine on them with the file's calibration (changed as the case says), itsinputand itsopt_in. Every number must match within the tolerance (log-likelihood ratios 0.001 nats, probabilities 0.0001, ratios 0.001, spans exactly), and every flag exactly, at every set the case carries. - The live check, where the app runs: when the index has a file for the loaded model, play each sample clip on the CPU engine in each context of the file, with the five heads, take
session.finish(), run the engine withmodel.calibrationandmodel.reference, and compare with that context's cases in the same way (on Apple, whichever of the two setsmodel.sampleClipsgives), themutatedandprobeones included (apply the mutation to the units ofmodel.reference, the changes to a copy ofmodel.calibration, and for aprobetakeinputfrom the case). This proves that the app hands the engine the whole take.
What the tolerance rests on, measured on TajweedFormer V4 4.0.0: the native and the WebAssembly CPU engines give the same bits on every head of every sample clip in every context; an engine computing in single precision stays within 0.00002 nats of one in double precision; and every flag in the file is decided by more than 0.017 nats. An accelerator's heads differ by tenths of a nat per frame, so the live check runs on the CPU engine. A learner's takes may run on any backend.
When the index has no file for the loaded model, as for a version on an early channel, the engine still runs for learners, the unit test still runs on a published file, and the live check is skipped with one line that says why: "No engine vectors for calibration
Limits
- One ayah, start to stop. The engine reads a take as one ayah recited from its start to its stop, the reading the units describe. The fit read the correct reading closest to what was heard (the calibration's
readout.path): for an ayah recited from its start to its stop that is the units ofmodel.reference, checked on every sample clip in every context. For several ayat in one take, use the units of each in turn only where the learner stopped at the end of each. Where two ayat are joined, the last word of the first changes (a correct reading, not a mistake): show no flag inside it. - After the take. Flags come from the whole take, once it has ended. While the learner recites, show what was heard.
- The weakest cases. Phone recordings and children's voices are the weakest case for every threshold (Limits you should design around).