quran-word-align 0.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,8 @@
1
+ .venv/
2
+ __pycache__/
3
+ aligned/
4
+ *.egg-info/
5
+ .claude/
6
+ eval/RESULTS.json
7
+ eval/DETECT.json
8
+ dist/
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Voltaro Studio
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,277 @@
1
+ Metadata-Version: 2.5
2
+ Name: quran-word-align
3
+ Version: 0.3.0
4
+ Summary: Word-by-word timings for Quran recitations, by CTC forced alignment on quran-asr-tiny
5
+ Project-URL: Homepage, https://github.com/voltarostudio/quran-word-align
6
+ Project-URL: Demo, https://voltarostudio.github.io/quran-word-align/
7
+ Project-URL: Dataset, https://huggingface.co/datasets/voltarostudio/quran-word-timings-mp3quran
8
+ Project-URL: Changelog, https://github.com/voltarostudio/quran-word-align/blob/main/CHANGELOG.md
9
+ Author-email: Voltaro Studio <support@voltarostudio.com>
10
+ License-Expression: MIT
11
+ License-File: LICENSE
12
+ Keywords: arabic,ctc,forced-alignment,quran,recitation,word-timings
13
+ Classifier: License :: OSI Approved :: MIT License
14
+ Classifier: Natural Language :: Arabic
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Topic :: Multimedia :: Sound/Audio :: Speech
17
+ Classifier: Topic :: Religion
18
+ Requires-Python: >=3.10
19
+ Requires-Dist: kaldi-native-fbank
20
+ Requires-Dist: numpy
21
+ Requires-Dist: onnxruntime
22
+ Provides-Extra: test
23
+ Requires-Dist: pytest; extra == 'test'
24
+ Description-Content-Type: text/markdown
25
+
26
+ # quran-word-align
27
+
28
+ [![tests](https://github.com/voltarostudio/quran-word-align/actions/workflows/test.yml/badge.svg)](https://github.com/voltarostudio/quran-word-align/actions/workflows/test.yml)
29
+ [![dataset on Hugging Face](https://img.shields.io/badge/dataset-Hugging%20Face-yellow)](https://huggingface.co/datasets/voltarostudio/quran-word-timings-mp3quran)
30
+
31
+ Word-by-word timings for Quran recitations: when each word of the mushaf starts and ends in a
32
+ recording. It runs on a laptop without a GPU, or in the browser. [العربية أدناه](#بالعربية)
33
+
34
+ - **[Try it in your browser](https://voltarostudio.github.io/quran-word-align/)**: pick a reciter from
35
+ mp3quran.net or a recording of your own, watch the words light up, download the timings. Nothing is
36
+ uploaded.
37
+ - **[The dataset](https://github.com/voltarostudio/quran-word-align/releases/tag/dataset-v3.4)**: 111
38
+ recitations from mp3quran.net (103 Hafs, 5 Warsh, 3 Qalun), already timed word by word, with each
39
+ reciter's own basmala and isti'adha, the basmala's words in each surah's file, whose voice says each file's basmala and isti'adha, and how each file opens, CC-BY 4.0.
40
+ [What is in it](dataset/README.md). Also on
41
+ [Hugging Face](https://huggingface.co/datasets/voltarostudio/quran-word-timings-mp3quran).
42
+
43
+ It is the aligner [Midad](https://voltarostudio.com) builds its word timings with: they highlight each
44
+ word of the mushaf as the reciter reads it.
45
+
46
+ ## What it does
47
+
48
+ - **A surah, part of one, several in a row (a juz), or one file per ayah.** It finds the ayahs
49
+ itself; ayah timings are optional.
50
+ - **It knows where a recording begins**: leave the surah out and it is found from what is heard (the
51
+ surah was right for all 224 recordings we tried, [eval/DETECT.md](eval/DETECT.md)).
52
+ - **Hafs, Warsh and Qalun**, each on the King Fahd Complex's own text of that riwayah. The riwayah you
53
+ say: it cannot be told reliably from the sound, since the riwayat share the mushaf's letters and the
54
+ model leans to Hafs where they differ ([eval/DETECT.md](eval/DETECT.md)).
55
+ - **A confidence for every word**, so you know which timings to trust and which to check.
56
+ - **Repeats**: a reciter who goes back and reads words again is followed, and the words are listed
57
+ again in the order heard.
58
+ - **Pauses**: a word after a pause starts where the voice comes back; a word before one ends where the
59
+ voice stops.
60
+ - **JSON, CSV, SRT, WebVTT** (with word-by-word timestamps) and **Quran.com-style segments**.
61
+
62
+ ## Use
63
+
64
+ Needs Python 3.10+ and [ffmpeg](https://ffmpeg.org).
65
+
66
+ ```sh
67
+ pip install git+https://github.com/voltarostudio/quran-word-align
68
+
69
+ quran-word-align file yasin.mp3 36 # a recording of your own (Hafs)
70
+ quran-word-align file recitation.mp3 # where it begins, found from the recording
71
+ quran-word-align file juz30.mp3 78 # several surahs in a row, from an-Naba'
72
+ quran-word-align file part.mp3 36 --from-ayah 13 # part of a surah
73
+ quran-word-align ayahs everyayah/Alafasy_128kbps/ # one file per ayah: 001001.mp3, 001002.mp3, ...
74
+ quran-word-align file mulk.mp3 67 --riwayah warsh # Warsh, or qalun
75
+ quran-word-align file fatiha.mp3 1 --format json,srt,vtt
76
+ quran-word-align reads # mp3quran.net recordings, with their ids
77
+ quran-word-align mp3quran 118 1 112 # al-Fatiha and al-Ikhlas of recording 118
78
+ ```
79
+
80
+ The model (19 MB) is fetched once from Hugging Face and checked against its SHA-256. What comes
81
+ before the first ayah (the isti'adha, the basmala) is skipped. A recording of several surahs is
82
+ written as one file a surah; with `ayahs`, each ayah's times are from the start of its own file.
83
+
84
+ If your recording has ayah timings already, `--timings ayahs.json` aligns each ayah in its window
85
+ (`[{"ayah": 1, "start_ms": 0, "end_ms": 6100}, ...]`). For mp3quran.net, `--ayah-timings` uses
86
+ theirs; by default the ayahs are found in the recording, which is right also where their timings are
87
+ not (they are off by an ayah or more in some surahs).
88
+
89
+ ### Output
90
+
91
+ Each surah is written to `aligned/<surah>.json`:
92
+
93
+ ```json
94
+ {"1": [[1, 6480, 6960, 0.81], [2, 6960, 7640, 0.79], [3, 7640, 8800, 0.68], [4, 8800, 11080, 0.70]],
95
+ "2": [[1, 12400, 13480, 1.0], ...]}
96
+ ```
97
+
98
+ Each ayah lists its words as heard: `[word, start_ms, end_ms, confidence]`. Words are numbered from 1
99
+ in each ayah, as the King Fahd Complex's mushaf numbers them (`quran_word_align/data/<riwayah>-words.json`).
100
+
101
+ | `--format` | File | What it is |
102
+ |---|---|---|
103
+ | `json` | `001.json` | as above |
104
+ | `csv` | `001.csv` | `surah, ayah, word, start_ms, end_ms, confidence, text` |
105
+ | `srt` | `001.srt` | one subtitle an ayah |
106
+ | `vtt` | `001.vtt` | one cue an ayah, with a timestamp before each word, for karaoke-style highlighting |
107
+ | `segments` | `001.segments.json` | Quran.com's shape: `verse_timings` with `segments: [[word, start, end], ...]` |
108
+
109
+ The segments keep the King Fahd mushaf's word numbers, which Quran.com's match in most ayahs but not
110
+ in every one; check before mixing the two.
111
+
112
+ ### From Python
113
+
114
+ ```python
115
+ from pathlib import Path
116
+ from quran_word_align.align import Model, align_recording, mushaf_words
117
+
118
+ words = mushaf_words("hafs")
119
+ result = align_recording(36, Path("yasin.mp3"), words, Model())
120
+ ```
121
+
122
+ ## How good it is
123
+
124
+ There is no exact reference for word timings, so these are checks, not an error rate. All but the
125
+ last are made by [`eval/evaluate.py`](eval/evaluate.py) on mp3quran.net's recordings, with public data
126
+ only; the numbers are in [eval/RESULTS.md](eval/RESULTS.md).
127
+
128
+ - **Finding the ayahs.** On 203 surahs (28 reciters, three riwayat, 40 hours), every one of the
129
+ 10,871 ayahs was found. Where mp3quran's own ayah timings fit the recording, the ayah starts found
130
+ are within one second of theirs for 98.6% of ayahs in Hafs, 100% in Warsh and 99.6% in Qalun. Where they do not (al-Husary's al-Fatiha, al-Shuraim's Yusuf, Idris Abkar's Ya-Sin), the
131
+ model hears the ayah's first words at our start and other words at theirs.
132
+ - **Words after a pause** start within 80 ms of where the voice comes back: 99.3% of them in Hafs,
133
+ 97.5% in Warsh, 97.9% in Qalun.
134
+ - **Against Quran.com's word timings**, where Quran.com's recording is the same performance, word
135
+ starts within each ayah agree within a quarter of a second for 90% to 96% of words, slow reciters
136
+ included (Abdul Basit 92%, al-Husary 90%, al-Shatri 96%). Earlier we reported Abdul Basit at 45%:
137
+ that measure counted Quran.com's convention of starting a word in the silence before it as our
138
+ error, and leaned on mp3quran's ayah timings where they are off. Quran.com's data was used only to
139
+ measure, in Midad's own pipeline; none of it is in this repository or the dataset.
140
+ - **Confidence tracks accuracy**: of the words above 0.85 (about half), 90% agree with Quran.com
141
+ within a quarter second; of those under 0.3 (under 1%), 15%.
142
+
143
+ In flowing speech, our word starts run on average 0.1 s later than Quran.com's (0.2 s for Abdul
144
+ Basit). In one blind listening test (Abdul Basit's al-Fatiha cut word by word, once at our starts and
145
+ once 0.1 s earlier, one listener), our cut sounded better: the words began whole, without a piece of
146
+ the word before. A second blind test, al-Fatiha of five more reciters, agreed: ours was preferred for
147
+ Husary, Minshawi, Shuraim and Qatami, and Sudais could not be told apart. A third, on words joined with no
148
+ pause between them (five reciters, ours against 0.2 s earlier), chose ours all five times. One listener is not proof,
149
+ so treat this as a strong hint, not a settled answer. Recordings of part of a surah, of several surahs, and one file per ayah are supported, but
150
+ so far tried on a few recordings only, not measured like whole surahs. A surah opening with disjoined letters (الر، حم, …) after an
151
+ isti'adha and a basmala can still have its first word placed too early, inside the basmala; ayah 1 is
152
+ now aligned with them as context, which fixes some of these, not all.
153
+
154
+ ## How it works
155
+
156
+ 1. **Listening.** [quran-asr-tiny](https://huggingface.co/Abdou/quran-asr-tiny) (MIT, by Abdou), a
157
+ 16-million-parameter Conformer-CTC model trained on Quran recitations, gives each 40 ms of audio
158
+ the probability of each Arabic letter. Each moment is heard in several overlapping six-second
159
+ pieces and their probabilities averaged, since the model alone is unsteady on long stretches.
160
+ 2. **Finding the ayahs.** The surah is followed a minute at a time: CTC forced alignment fits the
161
+ words of the ayahs not yet placed to that minute, letter by letter, with the path free to stop
162
+ after any word. Every ayah but the last one reached is placed, and the next minute starts at that
163
+ last ayah.
164
+ 3. **Placing the words.** Each ayah is aligned again in its own window, with a few words of the ayahs
165
+ either side so they are not taken for its own. Silence and sounds that are not the text are left
166
+ out. A reciter who goes back is followed: the path may return to an earlier word, at a cost.
167
+ 4. **Confidence** is the mean, over the word's letters, of the highest probability the model gave the
168
+ letter while the path was on it.
169
+
170
+ The words are the King Fahd Glorious Quran Printing Complex's text of each riwayah, from its developer
171
+ packages (version 3.0), split into words by [`tools/mushaf_words.py`](tools/mushaf_words.py) and never
172
+ typed by hand. A test pins each words file's SHA-256.
173
+
174
+ ## Development
175
+
176
+ ```sh
177
+ pip install -e ".[test]"
178
+ pytest # the aligner on synthetic letter probabilities; no model, no audio
179
+ python eval/evaluate.py # the accuracy numbers above (downloads ~5 GB of recordings)
180
+ sh dataset/align.sh OUT # align the whole dataset (hours)
181
+ ```
182
+
183
+ The tests run on GitHub Actions on every push ([`.github/workflows/test.yml`](.github/workflows/test.yml)).
184
+
185
+ `demo/` is the browser version: `aligner.js` is `align.py` ported line by line, and gives the same
186
+ timings (346 of 348 words within 40 ms on a test surah).
187
+
188
+ ## Licence
189
+
190
+ The code: MIT. The timings in the dataset: CC-BY 4.0. The model: MIT
191
+ ([Abdou/quran-asr-tiny](https://huggingface.co/Abdou/quran-asr-tiny)). The Quran text and the mushaf
192
+ fonts in `demo/fonts`: the King Fahd Glorious Quran Printing Complex's, unchanged, which allows their
193
+ free use and distribution as long as they are not sold or modified. Recordings and their ayah timings
194
+ belong to their sources (mp3quran.net and others); this repository holds none.
195
+
196
+ ---
197
+
198
+ ## بالعربية
199
+
200
+ **quran-word-align** أداةٌ تعرف متى تبدأ كلُّ كلمةٍ من كلمات المصحف ومتى تنتهي في تسجيل التلاوة. تعمل
201
+ على حاسوبٍ عادي دون معالجٍ رسومي، وتعمل في المتصفح أيضًا. وهي التي يبني بها تطبيق
202
+ [مِداد](https://voltarostudio.com) توقيت الكلمات، فيُعلِّم الكلمة في المصحف حين يقرؤها القارئ.
203
+
204
+ - **[جرّبها في متصفحك](https://voltarostudio.github.io/quran-word-align/):** اختر قارئًا من mp3quran.net
205
+ أو تلاوةً من جهازك، فترى الكلمات تُعلَّم مع القراءة، ثم نزّل توقيتها. لا يُرفع تسجيلك إلى أي مكان.
206
+ - **[مجموعة البيانات](https://github.com/voltarostudio/quran-word-align/releases/tag/dataset-v3.4):** ١١١ ختمةً من
207
+ mp3quran.net بروايات حفص وورش وقالون، موقّتةٌ كلمةً كلمة، ومعها بسملة كل قارئ واستعاذته بصوته، وكلمات البسملة في ملف كل سورة، وهل بسملة كل ملف وتعوّذه بصوت قارئه، وكيف يبدأ كل ملف، ومباحةٌ برخصة CC-BY 4.0.
208
+ وهي على [Hugging Face](https://huggingface.co/datasets/voltarostudio/quran-word-timings-mp3quran) أيضًا.
209
+
210
+ ### ما تفعله
211
+
212
+ - **تقبل سورةً كاملة، أو جزءًا من سورة، أو سورًا متتابعة كجزء عمّ، أو ملفًّا لكل آية،** وتجد مواضع
213
+ الآيات بنفسها، فلا تحتاج إلى توقيتٍ للآيات.
214
+ - **تعرف من أين يبدأ التسجيل** إن لم تذكر السورة. وأصابت السورة في التسجيلات الـ٢٢٤ التي جرّبناها كلها.
215
+ - **تدعم روايات حفص وورش وقالون،** على نصّ مجمع الملك فهد لكل رواية. والرواية تذكرها أنت: لا تُعرف من
216
+ الصوت معرفةً يُوثق بها، لأن الروايات تشترك في حروف المصحف، والنموذج يميل إلى حفص حيث تختلف.
217
+ - **تعطي كلَّ كلمةٍ درجةَ ثقة** من صفرٍ إلى واحد، فتعرف أيَّ توقيتٍ تطمئن إليه وأيَّه تراجعه.
218
+ - **تتابع القارئ إن رجع** فأعاد كلمات، فتذكر الكلمات المعادة مرةً ثانية بترتيب سماعها.
219
+ - **تراعي الوقف:** تبدأ الكلمة بعد الوقف حين يعود الصوت، وتنتهي الكلمة قبله حين يسكت القارئ.
220
+ - **تكتب التوقيت** بصيغ JSON وCSV وSRT وWebVTT، وبصيغة المقاطع التي يستعملها Quran.com.
221
+
222
+ ### الاستعمال
223
+
224
+ تحتاج إلى بايثون ٣٫١٠ أو أحدث، وإلى ffmpeg.
225
+
226
+ ```sh
227
+ pip install git+https://github.com/voltarostudio/quran-word-align
228
+ quran-word-align file yasin.mp3 36 # تلاوة سورة يس من جهازك
229
+ quran-word-align file recitation.mp3 # تعرف الأداة من أين يبدأ التسجيل
230
+ quran-word-align file juz30.mp3 78 # سورٌ متتابعة، أولها النبأ
231
+ quran-word-align file part.mp3 36 --from-ayah 13 # جزءٌ من سورة
232
+ quran-word-align ayahs everyayah/Alafasy_128kbps/ # ملفٌّ لكل آية: 001001.mp3 وهكذا
233
+ quran-word-align file mulk.mp3 67 --riwayah warsh # برواية ورش، أو qalun لقالون
234
+ quran-word-align mp3quran 118 1 112 # الفاتحة والإخلاص من تسجيلات mp3quran
235
+ ```
236
+
237
+ يُنزَّل النموذج (١٩ م.ب) مرةً واحدة، ويُتحقَّق من بصمته. وتُتخطّى الاستعاذة والبسملة قبل أول آية. ويُكتب
238
+ تسجيل السور المتتابعة ملفًّا لكل سورة، وفي وضع `ayahs` يُحسب وقت كل آية من أول ملفّها.
239
+
240
+ ### ما مدى دقتها
241
+
242
+ ليس لتوقيت الكلمات مرجعٌ دقيق يُقاس عليه، فهذه فحوصٌ لا نسبةُ خطأ. وأكثرها يُعاد بسكربت
243
+ `eval/evaluate.py` على تسجيلات mp3quran ببياناتٍ عامة.
244
+
245
+ - **مواضع الآيات:** وجدنا الآيات كلَّها، ١٠٨٧١ آية في ٢٠٣ سورة لثمانيةٍ وعشرين قارئًا. وحيث يوافق توقيتُ
246
+ الآيات في mp3quran التسجيلَ، تقع بداياتنا على ثانيةٍ أو أقل من بداياتهم في ٩٨٫٦٪ من الآيات برواية حفص،
247
+ و١٠٠٪ برواية ورش، و٩٩٫٦٪ برواية قالون. وحيث لا يوافقه، كفاتحة
248
+ الحصري ويوسف الشريم، يسمع النموذجُ أولَ كلمات الآية عند بدايتنا، وكلماتٍ أخرى عند بدايتهم.
249
+ - **الكلمة بعد الوقف** تبدأ على بُعد ٨٠ جزءًا من ألف من الثانية أو أقل من عودة الصوت في ٩٧٫٥٪ إلى ٩٩٫٣٪ منها.
250
+ - **موازنةً بتوقيت Quran.com** حيث يكون تسجيلُه التلاوةَ نفسها: تتفق بدايات الكلمات داخل الآية على ربع
251
+ ثانيةٍ أو أقل في ٩٠٪ إلى ٩٦٪ منها، ومنها التلاوات البطيئة (عبد الباسط ٩٢٪، الحصري ٩٠٪، الشاطري ٩٦٪).
252
+ واستعملنا بيانات Quran.com للقياس وحده، وليس منها شيءٌ في هذا المستودع ولا في مجموعة البيانات.
253
+ - **درجة الثقة تدلّ على الدقة:** ٩٠٪ من الكلمات التي تزيد ثقتها على ٠٫٨٥ توافق Quran.com، و١٥٪ فقط مما
254
+ تقلّ ثقته عن ٠٫٣.
255
+
256
+ وما لم نحسمه بعد: في الكلام المتصل تتأخر بداياتنا عن بدايات Quran.com عُشرَ ثانيةٍ في المتوسط (وخُمسَها عند
257
+ عبد الباسط)، وفي اختبار سماعٍ أعمى واحد (فاتحة عبد الباسط مقطَّعةً كلمةً كلمة، مرةً عند بداياتنا ومرةً قبلها بعُشر ثانية،
258
+ وسامعٌ واحد) كان تقطيعنا أحسن: بدأت الكلمات تامّةً بلا بقيةٍ من الكلمة التي قبلها. ووافقه اختبارٌ أعمى ثانٍ
259
+ على فاتحة خمسة قرّاء آخرين: فُضِّل تقطيعنا عند الحصري والمنشاوي والشريم والقطامي، ولم يُفرَّق عند السديس. وثالثٌ على كلماتٍ موصولةٍ بلا وقفة
260
+ (خمسة قرّاء، تقطيعنا مقابل خُمس ثانيةٍ قبله) اختار تقطيعنا في الخمسة.
261
+ وسامعٌ واحدٌ لا يحسم الأمر، فهو قرينةٌ قويةٌ لا برهان. أما جزء السورة، والسور المتتابعة، والملف لكل آية، فقد جرّبناها
262
+ على تسجيلاتٍ قليلة، ولم نقسها كما قسنا السور الكاملة. والسورة التي تبدأ بحروفٍ مقطّعة (الر، حم، …) بعد تعوّذٍ وبسملة قد تُوضع كلمتها الأولى
263
+ مبكّرةً داخل البسملة؛ وصرنا نوقّت الآية الأولى والتعوّذ والبسملة قبلها، فصلح بعض ذلك لا كلّه.
264
+
265
+ ### كيف تعمل
266
+
267
+ ١. **تسمع:** يسمع نموذج [quran-asr-tiny](https://huggingface.co/Abdou/quran-asr-tiny) التلاوةَ حرفًا
268
+ حرفًا، كلَّ جزءٍ من خمسةٍ وعشرين من الثانية.
269
+ ٢. **تجد الآيات:** تتابع السورةَ دقيقةً دقيقة، فتطابق كلماتِ الآيات على ما سمعه النموذج، وتثبّت كلَّ آيةٍ
270
+ اكتملت قبل أن تنتقل.
271
+ ٣. **تضع الكلمات:** تطابق كلَّ آيةٍ وحدها في نافذتها، ومعها كلماتٌ من الآيتين قبلها وبعدها، فلا تختلط بها.
272
+
273
+ **النص:** نصُّ مجمع الملك فهد لطباعة المصحف الشريف لكل رواية، من حزم المطوّرين التي نشرها، مقسومًا إلى
274
+ كلماتٍ بسكربت، كما هو دون تعديل، ولم تُكتب منه كلمةٌ باليد.
275
+
276
+ **الرخصة:** الشيفرة بترخيص MIT، والنموذج كذلك، والتوقيت في مجموعة البيانات بترخيص CC-BY 4.0. ونصّ المصحف
277
+ وخطوطه من مجمع الملك فهد كما هي. والتسجيلات وتوقيت آياتها لأصحابها، ولا يحمل المستودع شيئًا منها.
@@ -0,0 +1,252 @@
1
+ # quran-word-align
2
+
3
+ [![tests](https://github.com/voltarostudio/quran-word-align/actions/workflows/test.yml/badge.svg)](https://github.com/voltarostudio/quran-word-align/actions/workflows/test.yml)
4
+ [![dataset on Hugging Face](https://img.shields.io/badge/dataset-Hugging%20Face-yellow)](https://huggingface.co/datasets/voltarostudio/quran-word-timings-mp3quran)
5
+
6
+ Word-by-word timings for Quran recitations: when each word of the mushaf starts and ends in a
7
+ recording. It runs on a laptop without a GPU, or in the browser. [العربية أدناه](#بالعربية)
8
+
9
+ - **[Try it in your browser](https://voltarostudio.github.io/quran-word-align/)**: pick a reciter from
10
+ mp3quran.net or a recording of your own, watch the words light up, download the timings. Nothing is
11
+ uploaded.
12
+ - **[The dataset](https://github.com/voltarostudio/quran-word-align/releases/tag/dataset-v3.4)**: 111
13
+ recitations from mp3quran.net (103 Hafs, 5 Warsh, 3 Qalun), already timed word by word, with each
14
+ reciter's own basmala and isti'adha, the basmala's words in each surah's file, whose voice says each file's basmala and isti'adha, and how each file opens, CC-BY 4.0.
15
+ [What is in it](dataset/README.md). Also on
16
+ [Hugging Face](https://huggingface.co/datasets/voltarostudio/quran-word-timings-mp3quran).
17
+
18
+ It is the aligner [Midad](https://voltarostudio.com) builds its word timings with: they highlight each
19
+ word of the mushaf as the reciter reads it.
20
+
21
+ ## What it does
22
+
23
+ - **A surah, part of one, several in a row (a juz), or one file per ayah.** It finds the ayahs
24
+ itself; ayah timings are optional.
25
+ - **It knows where a recording begins**: leave the surah out and it is found from what is heard (the
26
+ surah was right for all 224 recordings we tried, [eval/DETECT.md](eval/DETECT.md)).
27
+ - **Hafs, Warsh and Qalun**, each on the King Fahd Complex's own text of that riwayah. The riwayah you
28
+ say: it cannot be told reliably from the sound, since the riwayat share the mushaf's letters and the
29
+ model leans to Hafs where they differ ([eval/DETECT.md](eval/DETECT.md)).
30
+ - **A confidence for every word**, so you know which timings to trust and which to check.
31
+ - **Repeats**: a reciter who goes back and reads words again is followed, and the words are listed
32
+ again in the order heard.
33
+ - **Pauses**: a word after a pause starts where the voice comes back; a word before one ends where the
34
+ voice stops.
35
+ - **JSON, CSV, SRT, WebVTT** (with word-by-word timestamps) and **Quran.com-style segments**.
36
+
37
+ ## Use
38
+
39
+ Needs Python 3.10+ and [ffmpeg](https://ffmpeg.org).
40
+
41
+ ```sh
42
+ pip install git+https://github.com/voltarostudio/quran-word-align
43
+
44
+ quran-word-align file yasin.mp3 36 # a recording of your own (Hafs)
45
+ quran-word-align file recitation.mp3 # where it begins, found from the recording
46
+ quran-word-align file juz30.mp3 78 # several surahs in a row, from an-Naba'
47
+ quran-word-align file part.mp3 36 --from-ayah 13 # part of a surah
48
+ quran-word-align ayahs everyayah/Alafasy_128kbps/ # one file per ayah: 001001.mp3, 001002.mp3, ...
49
+ quran-word-align file mulk.mp3 67 --riwayah warsh # Warsh, or qalun
50
+ quran-word-align file fatiha.mp3 1 --format json,srt,vtt
51
+ quran-word-align reads # mp3quran.net recordings, with their ids
52
+ quran-word-align mp3quran 118 1 112 # al-Fatiha and al-Ikhlas of recording 118
53
+ ```
54
+
55
+ The model (19 MB) is fetched once from Hugging Face and checked against its SHA-256. What comes
56
+ before the first ayah (the isti'adha, the basmala) is skipped. A recording of several surahs is
57
+ written as one file a surah; with `ayahs`, each ayah's times are from the start of its own file.
58
+
59
+ If your recording has ayah timings already, `--timings ayahs.json` aligns each ayah in its window
60
+ (`[{"ayah": 1, "start_ms": 0, "end_ms": 6100}, ...]`). For mp3quran.net, `--ayah-timings` uses
61
+ theirs; by default the ayahs are found in the recording, which is right also where their timings are
62
+ not (they are off by an ayah or more in some surahs).
63
+
64
+ ### Output
65
+
66
+ Each surah is written to `aligned/<surah>.json`:
67
+
68
+ ```json
69
+ {"1": [[1, 6480, 6960, 0.81], [2, 6960, 7640, 0.79], [3, 7640, 8800, 0.68], [4, 8800, 11080, 0.70]],
70
+ "2": [[1, 12400, 13480, 1.0], ...]}
71
+ ```
72
+
73
+ Each ayah lists its words as heard: `[word, start_ms, end_ms, confidence]`. Words are numbered from 1
74
+ in each ayah, as the King Fahd Complex's mushaf numbers them (`quran_word_align/data/<riwayah>-words.json`).
75
+
76
+ | `--format` | File | What it is |
77
+ |---|---|---|
78
+ | `json` | `001.json` | as above |
79
+ | `csv` | `001.csv` | `surah, ayah, word, start_ms, end_ms, confidence, text` |
80
+ | `srt` | `001.srt` | one subtitle an ayah |
81
+ | `vtt` | `001.vtt` | one cue an ayah, with a timestamp before each word, for karaoke-style highlighting |
82
+ | `segments` | `001.segments.json` | Quran.com's shape: `verse_timings` with `segments: [[word, start, end], ...]` |
83
+
84
+ The segments keep the King Fahd mushaf's word numbers, which Quran.com's match in most ayahs but not
85
+ in every one; check before mixing the two.
86
+
87
+ ### From Python
88
+
89
+ ```python
90
+ from pathlib import Path
91
+ from quran_word_align.align import Model, align_recording, mushaf_words
92
+
93
+ words = mushaf_words("hafs")
94
+ result = align_recording(36, Path("yasin.mp3"), words, Model())
95
+ ```
96
+
97
+ ## How good it is
98
+
99
+ There is no exact reference for word timings, so these are checks, not an error rate. All but the
100
+ last are made by [`eval/evaluate.py`](eval/evaluate.py) on mp3quran.net's recordings, with public data
101
+ only; the numbers are in [eval/RESULTS.md](eval/RESULTS.md).
102
+
103
+ - **Finding the ayahs.** On 203 surahs (28 reciters, three riwayat, 40 hours), every one of the
104
+ 10,871 ayahs was found. Where mp3quran's own ayah timings fit the recording, the ayah starts found
105
+ are within one second of theirs for 98.6% of ayahs in Hafs, 100% in Warsh and 99.6% in Qalun. Where they do not (al-Husary's al-Fatiha, al-Shuraim's Yusuf, Idris Abkar's Ya-Sin), the
106
+ model hears the ayah's first words at our start and other words at theirs.
107
+ - **Words after a pause** start within 80 ms of where the voice comes back: 99.3% of them in Hafs,
108
+ 97.5% in Warsh, 97.9% in Qalun.
109
+ - **Against Quran.com's word timings**, where Quran.com's recording is the same performance, word
110
+ starts within each ayah agree within a quarter of a second for 90% to 96% of words, slow reciters
111
+ included (Abdul Basit 92%, al-Husary 90%, al-Shatri 96%). Earlier we reported Abdul Basit at 45%:
112
+ that measure counted Quran.com's convention of starting a word in the silence before it as our
113
+ error, and leaned on mp3quran's ayah timings where they are off. Quran.com's data was used only to
114
+ measure, in Midad's own pipeline; none of it is in this repository or the dataset.
115
+ - **Confidence tracks accuracy**: of the words above 0.85 (about half), 90% agree with Quran.com
116
+ within a quarter second; of those under 0.3 (under 1%), 15%.
117
+
118
+ In flowing speech, our word starts run on average 0.1 s later than Quran.com's (0.2 s for Abdul
119
+ Basit). In one blind listening test (Abdul Basit's al-Fatiha cut word by word, once at our starts and
120
+ once 0.1 s earlier, one listener), our cut sounded better: the words began whole, without a piece of
121
+ the word before. A second blind test, al-Fatiha of five more reciters, agreed: ours was preferred for
122
+ Husary, Minshawi, Shuraim and Qatami, and Sudais could not be told apart. A third, on words joined with no
123
+ pause between them (five reciters, ours against 0.2 s earlier), chose ours all five times. One listener is not proof,
124
+ so treat this as a strong hint, not a settled answer. Recordings of part of a surah, of several surahs, and one file per ayah are supported, but
125
+ so far tried on a few recordings only, not measured like whole surahs. A surah opening with disjoined letters (الر، حم, …) after an
126
+ isti'adha and a basmala can still have its first word placed too early, inside the basmala; ayah 1 is
127
+ now aligned with them as context, which fixes some of these, not all.
128
+
129
+ ## How it works
130
+
131
+ 1. **Listening.** [quran-asr-tiny](https://huggingface.co/Abdou/quran-asr-tiny) (MIT, by Abdou), a
132
+ 16-million-parameter Conformer-CTC model trained on Quran recitations, gives each 40 ms of audio
133
+ the probability of each Arabic letter. Each moment is heard in several overlapping six-second
134
+ pieces and their probabilities averaged, since the model alone is unsteady on long stretches.
135
+ 2. **Finding the ayahs.** The surah is followed a minute at a time: CTC forced alignment fits the
136
+ words of the ayahs not yet placed to that minute, letter by letter, with the path free to stop
137
+ after any word. Every ayah but the last one reached is placed, and the next minute starts at that
138
+ last ayah.
139
+ 3. **Placing the words.** Each ayah is aligned again in its own window, with a few words of the ayahs
140
+ either side so they are not taken for its own. Silence and sounds that are not the text are left
141
+ out. A reciter who goes back is followed: the path may return to an earlier word, at a cost.
142
+ 4. **Confidence** is the mean, over the word's letters, of the highest probability the model gave the
143
+ letter while the path was on it.
144
+
145
+ The words are the King Fahd Glorious Quran Printing Complex's text of each riwayah, from its developer
146
+ packages (version 3.0), split into words by [`tools/mushaf_words.py`](tools/mushaf_words.py) and never
147
+ typed by hand. A test pins each words file's SHA-256.
148
+
149
+ ## Development
150
+
151
+ ```sh
152
+ pip install -e ".[test]"
153
+ pytest # the aligner on synthetic letter probabilities; no model, no audio
154
+ python eval/evaluate.py # the accuracy numbers above (downloads ~5 GB of recordings)
155
+ sh dataset/align.sh OUT # align the whole dataset (hours)
156
+ ```
157
+
158
+ The tests run on GitHub Actions on every push ([`.github/workflows/test.yml`](.github/workflows/test.yml)).
159
+
160
+ `demo/` is the browser version: `aligner.js` is `align.py` ported line by line, and gives the same
161
+ timings (346 of 348 words within 40 ms on a test surah).
162
+
163
+ ## Licence
164
+
165
+ The code: MIT. The timings in the dataset: CC-BY 4.0. The model: MIT
166
+ ([Abdou/quran-asr-tiny](https://huggingface.co/Abdou/quran-asr-tiny)). The Quran text and the mushaf
167
+ fonts in `demo/fonts`: the King Fahd Glorious Quran Printing Complex's, unchanged, which allows their
168
+ free use and distribution as long as they are not sold or modified. Recordings and their ayah timings
169
+ belong to their sources (mp3quran.net and others); this repository holds none.
170
+
171
+ ---
172
+
173
+ ## بالعربية
174
+
175
+ **quran-word-align** أداةٌ تعرف متى تبدأ كلُّ كلمةٍ من كلمات المصحف ومتى تنتهي في تسجيل التلاوة. تعمل
176
+ على حاسوبٍ عادي دون معالجٍ رسومي، وتعمل في المتصفح أيضًا. وهي التي يبني بها تطبيق
177
+ [مِداد](https://voltarostudio.com) توقيت الكلمات، فيُعلِّم الكلمة في المصحف حين يقرؤها القارئ.
178
+
179
+ - **[جرّبها في متصفحك](https://voltarostudio.github.io/quran-word-align/):** اختر قارئًا من mp3quran.net
180
+ أو تلاوةً من جهازك، فترى الكلمات تُعلَّم مع القراءة، ثم نزّل توقيتها. لا يُرفع تسجيلك إلى أي مكان.
181
+ - **[مجموعة البيانات](https://github.com/voltarostudio/quran-word-align/releases/tag/dataset-v3.4):** ١١١ ختمةً من
182
+ mp3quran.net بروايات حفص وورش وقالون، موقّتةٌ كلمةً كلمة، ومعها بسملة كل قارئ واستعاذته بصوته، وكلمات البسملة في ملف كل سورة، وهل بسملة كل ملف وتعوّذه بصوت قارئه، وكيف يبدأ كل ملف، ومباحةٌ برخصة CC-BY 4.0.
183
+ وهي على [Hugging Face](https://huggingface.co/datasets/voltarostudio/quran-word-timings-mp3quran) أيضًا.
184
+
185
+ ### ما تفعله
186
+
187
+ - **تقبل سورةً كاملة، أو جزءًا من سورة، أو سورًا متتابعة كجزء عمّ، أو ملفًّا لكل آية،** وتجد مواضع
188
+ الآيات بنفسها، فلا تحتاج إلى توقيتٍ للآيات.
189
+ - **تعرف من أين يبدأ التسجيل** إن لم تذكر السورة. وأصابت السورة في التسجيلات الـ٢٢٤ التي جرّبناها كلها.
190
+ - **تدعم روايات حفص وورش وقالون،** على نصّ مجمع الملك فهد لكل رواية. والرواية تذكرها أنت: لا تُعرف من
191
+ الصوت معرفةً يُوثق بها، لأن الروايات تشترك في حروف المصحف، والنموذج يميل إلى حفص حيث تختلف.
192
+ - **تعطي كلَّ كلمةٍ درجةَ ثقة** من صفرٍ إلى واحد، فتعرف أيَّ توقيتٍ تطمئن إليه وأيَّه تراجعه.
193
+ - **تتابع القارئ إن رجع** فأعاد كلمات، فتذكر الكلمات المعادة مرةً ثانية بترتيب سماعها.
194
+ - **تراعي الوقف:** تبدأ الكلمة بعد الوقف حين يعود الصوت، وتنتهي الكلمة قبله حين يسكت القارئ.
195
+ - **تكتب التوقيت** بصيغ JSON وCSV وSRT وWebVTT، وبصيغة المقاطع التي يستعملها Quran.com.
196
+
197
+ ### الاستعمال
198
+
199
+ تحتاج إلى بايثون ٣٫١٠ أو أحدث، وإلى ffmpeg.
200
+
201
+ ```sh
202
+ pip install git+https://github.com/voltarostudio/quran-word-align
203
+ quran-word-align file yasin.mp3 36 # تلاوة سورة يس من جهازك
204
+ quran-word-align file recitation.mp3 # تعرف الأداة من أين يبدأ التسجيل
205
+ quran-word-align file juz30.mp3 78 # سورٌ متتابعة، أولها النبأ
206
+ quran-word-align file part.mp3 36 --from-ayah 13 # جزءٌ من سورة
207
+ quran-word-align ayahs everyayah/Alafasy_128kbps/ # ملفٌّ لكل آية: 001001.mp3 وهكذا
208
+ quran-word-align file mulk.mp3 67 --riwayah warsh # برواية ورش، أو qalun لقالون
209
+ quran-word-align mp3quran 118 1 112 # الفاتحة والإخلاص من تسجيلات mp3quran
210
+ ```
211
+
212
+ يُنزَّل النموذج (١٩ م.ب) مرةً واحدة، ويُتحقَّق من بصمته. وتُتخطّى الاستعاذة والبسملة قبل أول آية. ويُكتب
213
+ تسجيل السور المتتابعة ملفًّا لكل سورة، وفي وضع `ayahs` يُحسب وقت كل آية من أول ملفّها.
214
+
215
+ ### ما مدى دقتها
216
+
217
+ ليس لتوقيت الكلمات مرجعٌ دقيق يُقاس عليه، فهذه فحوصٌ لا نسبةُ خطأ. وأكثرها يُعاد بسكربت
218
+ `eval/evaluate.py` على تسجيلات mp3quran ببياناتٍ عامة.
219
+
220
+ - **مواضع الآيات:** وجدنا الآيات كلَّها، ١٠٨٧١ آية في ٢٠٣ سورة لثمانيةٍ وعشرين قارئًا. وحيث يوافق توقيتُ
221
+ الآيات في mp3quran التسجيلَ، تقع بداياتنا على ثانيةٍ أو أقل من بداياتهم في ٩٨٫٦٪ من الآيات برواية حفص،
222
+ و١٠٠٪ برواية ورش، و٩٩٫٦٪ برواية قالون. وحيث لا يوافقه، كفاتحة
223
+ الحصري ويوسف الشريم، يسمع النموذجُ أولَ كلمات الآية عند بدايتنا، وكلماتٍ أخرى عند بدايتهم.
224
+ - **الكلمة بعد الوقف** تبدأ على بُعد ٨٠ جزءًا من ألف من الثانية أو أقل من عودة الصوت في ٩٧٫٥٪ إلى ٩٩٫٣٪ منها.
225
+ - **موازنةً بتوقيت Quran.com** حيث يكون تسجيلُه التلاوةَ نفسها: تتفق بدايات الكلمات داخل الآية على ربع
226
+ ثانيةٍ أو أقل في ٩٠٪ إلى ٩٦٪ منها، ومنها التلاوات البطيئة (عبد الباسط ٩٢٪، الحصري ٩٠٪، الشاطري ٩٦٪).
227
+ واستعملنا بيانات Quran.com للقياس وحده، وليس منها شيءٌ في هذا المستودع ولا في مجموعة البيانات.
228
+ - **درجة الثقة تدلّ على الدقة:** ٩٠٪ من الكلمات التي تزيد ثقتها على ٠٫٨٥ توافق Quran.com، و١٥٪ فقط مما
229
+ تقلّ ثقته عن ٠٫٣.
230
+
231
+ وما لم نحسمه بعد: في الكلام المتصل تتأخر بداياتنا عن بدايات Quran.com عُشرَ ثانيةٍ في المتوسط (وخُمسَها عند
232
+ عبد الباسط)، وفي اختبار سماعٍ أعمى واحد (فاتحة عبد الباسط مقطَّعةً كلمةً كلمة، مرةً عند بداياتنا ومرةً قبلها بعُشر ثانية،
233
+ وسامعٌ واحد) كان تقطيعنا أحسن: بدأت الكلمات تامّةً بلا بقيةٍ من الكلمة التي قبلها. ووافقه اختبارٌ أعمى ثانٍ
234
+ على فاتحة خمسة قرّاء آخرين: فُضِّل تقطيعنا عند الحصري والمنشاوي والشريم والقطامي، ولم يُفرَّق عند السديس. وثالثٌ على كلماتٍ موصولةٍ بلا وقفة
235
+ (خمسة قرّاء، تقطيعنا مقابل خُمس ثانيةٍ قبله) اختار تقطيعنا في الخمسة.
236
+ وسامعٌ واحدٌ لا يحسم الأمر، فهو قرينةٌ قويةٌ لا برهان. أما جزء السورة، والسور المتتابعة، والملف لكل آية، فقد جرّبناها
237
+ على تسجيلاتٍ قليلة، ولم نقسها كما قسنا السور الكاملة. والسورة التي تبدأ بحروفٍ مقطّعة (الر، حم، …) بعد تعوّذٍ وبسملة قد تُوضع كلمتها الأولى
238
+ مبكّرةً داخل البسملة؛ وصرنا نوقّت الآية الأولى والتعوّذ والبسملة قبلها، فصلح بعض ذلك لا كلّه.
239
+
240
+ ### كيف تعمل
241
+
242
+ ١. **تسمع:** يسمع نموذج [quran-asr-tiny](https://huggingface.co/Abdou/quran-asr-tiny) التلاوةَ حرفًا
243
+ حرفًا، كلَّ جزءٍ من خمسةٍ وعشرين من الثانية.
244
+ ٢. **تجد الآيات:** تتابع السورةَ دقيقةً دقيقة، فتطابق كلماتِ الآيات على ما سمعه النموذج، وتثبّت كلَّ آيةٍ
245
+ اكتملت قبل أن تنتقل.
246
+ ٣. **تضع الكلمات:** تطابق كلَّ آيةٍ وحدها في نافذتها، ومعها كلماتٌ من الآيتين قبلها وبعدها، فلا تختلط بها.
247
+
248
+ **النص:** نصُّ مجمع الملك فهد لطباعة المصحف الشريف لكل رواية، من حزم المطوّرين التي نشرها، مقسومًا إلى
249
+ كلماتٍ بسكربت، كما هو دون تعديل، ولم تُكتب منه كلمةٌ باليد.
250
+
251
+ **الرخصة:** الشيفرة بترخيص MIT، والنموذج كذلك، والتوقيت في مجموعة البيانات بترخيص CC-BY 4.0. ونصّ المصحف
252
+ وخطوطه من مجمع الملك فهد كما هي. والتسجيلات وتوقيت آياتها لأصحابها، ولا يحمل المستودع شيئًا منها.