File size: 11,239 Bytes
21f2aa3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | #!/usr/bin/env python3
"""
TajweedSST - Duration Model
Calibrates and validates letter durations based on Tajweed rules.
Works with harakat (beat) counts and reciter-specific speech rates.
Key Features:
- Per-reciter harakat calibration
- Madd type detection from Quranic context
- Duration validation against Tajweed expectations
- Speech rate normalization
"""
import json
import numpy as np
from dataclasses import dataclass, field
from typing import List, Dict, Optional, Tuple
from pathlib import Path
from enum import Enum
class MaddType(Enum):
NONE = "none"
ASLI = "asli" # 2 harakat
WAJIB = "wajib" # 4-5 harakat
JAIZ = "jaiz" # 2-4-6 harakat (flexible)
LAZIM = "lazim" # 6 harakat
LEEN = "leen" # 2-4-6 harakat (soft)
ARID = "arid" # 2-4-6 harakat (for pause)
BADAL = "badal" # 2 harakat (substitution)
SILAH = "silah" # 2 harakat (connection)
@dataclass
class HarakatCalibration:
"""Per-reciter timing calibration"""
reciter_name: str
harakat_base_ms: float = 100.0 # Base beat duration
speech_rate_wpm: float = 60.0 # Words per minute
pitch_range_hz: Tuple[float, float] = (80.0, 300.0)
sample_size: int = 0 # How many samples used for calibration
@dataclass
class DurationExpectation:
"""Expected duration for a Tajweed rule"""
rule_name: str
min_harakat: int
max_harakat: int
expected_ms_range: Tuple[float, float]
tolerance: float = 0.25 # 25% tolerance
@dataclass
class DurationResult:
"""Result of duration validation"""
is_valid: bool
actual_ms: float
expected_ms: float
harakat_count: float
deviation_percent: float
rule_applied: str
class DurationModel:
"""
Duration model for Tajweed-based timing validation
"""
# Default expectations (will be calibrated per reciter)
DEFAULT_HARAKAT_MS = 100.0
# Tajweed duration rules (in harakat counts)
TAJWEED_DURATIONS = {
MaddType.ASLI: DurationExpectation("Madd Asli", 2, 2, (150, 280), 0.30),
MaddType.WAJIB: DurationExpectation("Madd Wajib", 4, 5, (350, 550), 0.25),
MaddType.LAZIM: DurationExpectation("Madd Lazim", 6, 6, (500, 800), 0.20),
MaddType.JAIZ: DurationExpectation("Madd Jaiz", 2, 6, (150, 700), 0.30),
MaddType.ARID: DurationExpectation("Madd Arid", 2, 6, (150, 700), 0.30),
MaddType.LEEN: DurationExpectation("Madd Leen", 2, 6, (150, 700), 0.30),
}
# Ghunnah duration
GHUNNAH_DURATION = DurationExpectation("Ghunnah", 2, 2, (80, 250), 0.30)
def __init__(self, lisan_path: Optional[str] = None):
"""Initialize with optional path to lisan_phonemes.json"""
self.calibration: Optional[HarakatCalibration] = None
self.lisan_data: Dict = {}
if lisan_path and Path(lisan_path).exists():
with open(lisan_path, 'r', encoding='utf-8') as f:
self.lisan_data = json.load(f)
def calibrate_from_samples(self,
reciter_name: str,
vowel_durations: List[float],
words_per_minute: float = 60.0) -> HarakatCalibration:
"""
Calibrate harakat duration from sample vowel measurements
Args:
reciter_name: Name of reciter for identification
vowel_durations: List of short vowel durations in seconds
words_per_minute: Estimated speech rate
Returns:
HarakatCalibration object
"""
if not vowel_durations:
# Use defaults
self.calibration = HarakatCalibration(
reciter_name=reciter_name,
harakat_base_ms=self.DEFAULT_HARAKAT_MS,
speech_rate_wpm=words_per_minute,
sample_size=0
)
return self.calibration
# Convert to milliseconds and compute median (robust to outliers)
durations_ms = [d * 1000 for d in vowel_durations]
harakat_base = np.median(durations_ms)
self.calibration = HarakatCalibration(
reciter_name=reciter_name,
harakat_base_ms=harakat_base,
speech_rate_wpm=words_per_minute,
sample_size=len(vowel_durations)
)
return self.calibration
def get_expected_duration(self,
madd_type: MaddType,
harakat_count: Optional[int] = None) -> Tuple[float, float]:
"""
Get expected duration range for a Madd type
Returns:
Tuple of (min_ms, max_ms)
"""
if not self.calibration:
base_ms = self.DEFAULT_HARAKAT_MS
else:
base_ms = self.calibration.harakat_base_ms
if madd_type in self.TAJWEED_DURATIONS:
expectation = self.TAJWEED_DURATIONS[madd_type]
if harakat_count:
# Use specific harakat count
center = harakat_count * base_ms
tolerance = expectation.tolerance
return (center * (1 - tolerance), center * (1 + tolerance))
else:
# Use range from Tajweed rule
min_ms = expectation.min_harakat * base_ms * (1 - expectation.tolerance)
max_ms = expectation.max_harakat * base_ms * (1 + expectation.tolerance)
return (min_ms, max_ms)
# Default: 1 harakat
return (base_ms * 0.7, base_ms * 1.3)
def validate_duration(self,
actual_duration_s: float,
madd_type: MaddType,
expected_harakat: int = 2) -> DurationResult:
"""
Validate if actual duration matches Tajweed expectation
Args:
actual_duration_s: Actual duration in seconds
madd_type: Type of Madd rule
expected_harakat: Expected harakat count
Returns:
DurationResult with validation details
"""
actual_ms = actual_duration_s * 1000
min_ms, max_ms = self.get_expected_duration(madd_type, expected_harakat)
expected_ms = (min_ms + max_ms) / 2
is_valid = min_ms <= actual_ms <= max_ms
deviation = abs(actual_ms - expected_ms) / expected_ms * 100 if expected_ms > 0 else 0
# Calculate actual harakat count
base_ms = self.calibration.harakat_base_ms if self.calibration else self.DEFAULT_HARAKAT_MS
harakat_count = actual_ms / base_ms if base_ms > 0 else 0
return DurationResult(
is_valid=is_valid,
actual_ms=actual_ms,
expected_ms=expected_ms,
harakat_count=harakat_count,
deviation_percent=deviation,
rule_applied=madd_type.value
)
def validate_ghunnah_duration(self, actual_duration_s: float) -> DurationResult:
"""Validate Ghunnah duration (2 harakat)"""
return self.validate_duration(actual_duration_s, MaddType.ASLI, 2)
def suggest_correction(self,
actual_duration_s: float,
madd_type: MaddType,
expected_harakat: int = 2) -> Tuple[float, float]:
"""
Suggest corrected start/end times based on Tajweed expectations
Returns:
Tuple of (suggested_duration_s, adjustment_s)
"""
min_ms, max_ms = self.get_expected_duration(madd_type, expected_harakat)
actual_ms = actual_duration_s * 1000
if actual_ms < min_ms:
# Too short - suggest minimum
suggested_ms = min_ms
elif actual_ms > max_ms:
# Too long - suggest maximum
suggested_ms = max_ms
else:
# Already valid
suggested_ms = actual_ms
adjustment_ms = suggested_ms - actual_ms
return (suggested_ms / 1000, adjustment_ms / 1000)
def detect_madd_type_from_context(self,
current_letter: str,
next_letter: Optional[str],
next_harakat: Optional[str],
is_word_end: bool,
is_waqf: bool = False) -> MaddType:
"""
Auto-detect Madd type from Quranic text context
Args:
current_letter: The Madd letter (ا و ي)
next_letter: Following letter (if any)
next_harakat: Harakat on next letter
is_word_end: Whether this is at word boundary
is_waqf: Whether reciter is pausing here
Returns:
Detected MaddType
"""
SUKUN = '\u0652'
SHADDA = '\u0651'
# If at end with pause
if is_waqf and is_word_end:
return MaddType.ARID # Flexible 2-4-6
# Check for Madd Lazim (Sukun or Shadda follows)
if next_harakat:
if SHADDA in next_harakat or SUKUN in next_harakat:
return MaddType.LAZIM
# Check for Madd Wajib (Hamza in same word follows)
if next_letter and next_letter in 'ءأإؤئ':
return MaddType.WAJIB
# Default: Madd Asli (natural 2 harakat)
return MaddType.ASLI
def main():
"""Test duration model"""
print("=" * 50)
print("TajweedSST Duration Model Test")
print("=" * 50)
model = DurationModel()
# Calibrate with sample data (simulated short vowels ~100ms each)
sample_vowels = [0.095, 0.105, 0.098, 0.102, 0.100, 0.103, 0.097]
calibration = model.calibrate_from_samples("Abdul_Basit", sample_vowels)
print(f"\nCalibration for {calibration.reciter_name}:")
print(f" Harakat base: {calibration.harakat_base_ms:.1f} ms")
print(f" Sample size: {calibration.sample_size}")
# Test duration validation
print("\nDuration Validation Tests:")
# Madd Asli (2 harakat ~ 200ms)
result = model.validate_duration(0.195, MaddType.ASLI, 2)
print(f"\n Madd Asli (0.195s):")
print(f" Valid: {result.is_valid}")
print(f" Harakat: {result.harakat_count:.1f}")
print(f" Deviation: {result.deviation_percent:.1f}%")
# Madd Lazim (6 harakat ~ 600ms)
result = model.validate_duration(0.580, MaddType.LAZIM, 6)
print(f"\n Madd Lazim (0.580s):")
print(f" Valid: {result.is_valid}")
print(f" Harakat: {result.harakat_count:.1f}")
print(f" Deviation: {result.deviation_percent:.1f}%")
# Test Madd type detection
print("\nMadd Type Detection:")
detected = model.detect_madd_type_from_context('ا', 'ء', None, False, False)
print(f" ا before ء: {detected.value}")
detected = model.detect_madd_type_from_context('ا', 'ب', '\u0651', False, False)
print(f" ا before بّ: {detected.value}")
if __name__ == "__main__":
main()
|