# Implementation Guide This guide explains how the audio files and word-by-word timestamp data are structured in this repository, and provides implementation examples to help you build a seamless playback and highlighting experience across various major technology stacks. ## 🗂️ Data Structure The repository uses a highly structured directory format to make programmatic access to any verse's audio and timing data incredibly simple. ### Directory Layout Files are organized in the following hierarchy: `/{reciter_name}/{surah_index}/{surah_index}{verse_index}.[opus|json]` Where: - `{reciter_name}`: The slugged directory name of the reciter (e.g., `abdul-basit-abdul-samad-murattal-hafs-950`). - `{surah_index}`: The Surah number, strictly 3-digit zero-padded (e.g., `001` for Al-Fatihah, `114` for An-Nas). - `{verse_index}`: The Verse number, strictly 3-digit zero-padded. Combined, `{surah_index}{verse_index}` creates a 6-digit filename prefix (e.g., `001001` or `114006`). **Example Paths**: - 🎵 **Audio**: `https://cdn.jsdelivr.net/gh/zaibihsn/recitation-all@main/abdul-basit-abdul-samad-murattal-hafs-950/001/001001.opus` - ⏱️ **Metadata**: `https://cdn.jsdelivr.net/gh/zaibihsn/recitation-all@main/abdul-basit-abdul-samad-murattal-hafs-950/001/001001.json` ### JSON Metadata Formats When fetching the `.json` files, you will encounter one of two primary formats depending on the data source. Your parsing logic should seamlessly handle both. **Format A (Flat Array)** A direct array of timestamps. Each inner array element represents a specific word. ```json [ [0, 1, 0, 920], [1, 2, 1080, 1440], [2, 3, 2120, 2440] ] ``` *Structure: `[current_word_index, next_word_index, start_time_ms, end_time_ms]`* **Format B (Detailed Object)** An object containing verse metadata along with an array of word segments. ```json { "verse_key": "1:1", "timestamp_from": 0, "timestamp_to": 8037, "segments": [ [1, 0, 579], [2, 579, 1331] ] } ``` *Structure within `segments`: `[word_index, start_time_ms, end_time_ms]`* --- ## 💻 Tech Stack Implementations The core concept across all frameworks is: 1. Fetch the JSON timestamp array for a verse. 2. Load the Audio track for that verse. 3. Listen to the audio stream's current position (time). 4. Given the current time in milliseconds, loop over the timestamps and find the word where `start_time <= current_time <= end_time`. 5. Apply a "highlighted" style/class to that word index. ### 1. Vanilla JavaScript ```javascript const audio = new Audio("https://cdn.jsdelivr.net/gh/zaibihsn/recitation-all@main/abdul-basit.../001/001001.opus"); let timestamps = []; // E.g., [[0,1,0,920], [1,2,1080,1440]] let activeWordIndex = -1; audio.addEventListener("timeupdate", () => { const currentTimeMs = audio.currentTime * 1000; // Find matching word const match = timestamps.find(t => { // Handle both format variants appropriately (Format A in this example) return currentTimeMs >= t[2] && currentTimeMs <= t[3]; }); if (match) { const newWordIndex = match[0]; if (activeWordIndex !== newWordIndex) { activeWordIndex = newWordIndex; highlightWordInDOM(activeWordIndex); // Your custom function } } }); audio.play(); ``` ### 2. React.js ```tsx import React, { useState, useEffect, useRef } from 'react'; const QuranPlayer = ({ audioUrl, timestamps, words }) => { const audioRef = useRef(null); const [activeWordIndex, setActiveWordIndex] = useState(-1); const handleTimeUpdate = () => { if (!audioRef.current || !timestamps) return; const currentTimeMs = audioRef.current.currentTime * 1000; // Assume Format A timestamps: [wordIdx, nextIdx, startMs, endMs] const match = timestamps.find( (t) => currentTimeMs >= t[2] && currentTimeMs <= t[3] ); if (match && match[0] !== activeWordIndex) { setActiveWordIndex(match[0]); } else if (!match && activeWordIndex !== -1) { setActiveWordIndex(-1); // Resets when pause/delay happens } }; return (
{{ word }}