# Add Songs to Riyāz — Two Ways

## Option 1: Browser Processing (Local Files) ✨ ENHANCED

**No Python, no server, works offline. Uses neural networks for accuracy.**

1. On the practice page, find **"Process Local File (browser only)"**
2. **Upload** an MP3 or WAV file from your computer
3. Ensure **"Enhanced mode"** is checked (default)
4. Click **▶ Process** — browser extracts pitch using neural network (~15-40 seconds)
5. **Download** the generated `melody.json`
6. Create a folder (e.g. `MySong/`), put `melody.json` in it
7. Click **Load song folder** and select that folder
8. Sing along with the melody highway!

**What's Enhanced:**
- ✅ **Neural pitch detection**: Uses **Essentia.js CREPE** (convolutional neural network) — same accuracy as professional tools
- ✅ **Model caching**: 75MB CREPE model downloads once, then works offline forever
- ✅ **Stem separation** (if headers configured): Uses **Demucs ONNX** to isolate vocals before pitch extraction
- ✅ **Multi-threaded**: 4x faster stem separation when SharedArrayBuffer is available

**Basic mode** (uncheck "Enhanced mode"):
- Faster processing (~10 seconds)
- Simple autocorrelation pitch detection
- Good for very clear vocals
- No model download needed

**Limitations of browser processing:**
- ❌ No YouTube download (can't process YouTube URLs in browser — fundamental limitation)
- ⚠️ Stem separation requires CloudFront COOP/COEP headers (see [howto/cloudfront-headers.md](cloudfront-headers.md))

For YouTube URLs, use the Python Processor below.

---

## Option 2: Python Processor (YouTube + Full Pipeline)

**Downloads YouTube, separates stems with Demucs, better pitch tracking.**

The live site does **not** host songs. You build a song folder on your PC, then
load it in the browser to sing along.

## Quick path (recommended)

### 1. Download the Processor

On https://bikashacharya.com/riyaz/practice.html click
**Download Processor**, or grab `processor/Riyaz-Processor.zip` from this repo.

Unzip anywhere. You need:

- **Python 3** on PATH  
- **ffmpeg** on PATH  
- Internet (first run downloads ML models — large)

### 2. Run it

- **Windows:** double-click `Process.bat`  
- **Mac/Linux:** `chmod +x Process.sh && ./Process.sh`

Paste a YouTube URL. Output goes to:

```text
~/SingAlong-Songs/Artist - Title/
  meta.json         # title, artist, track, youtubeId
  melody.json
  sargam.txt
  lyrics.json
  backing.mp3
  vocal.mp3
  mix.mp3
```

Folder names use **Artist - Title** (from the optional hint, or YouTube metadata).
If the same title already exists for a different video, a short id is appended.

### 3. Load in the web app

Open Riyāz → **Song** → **Load song folder** → pick that **Artist - Title** folder.

The melody line, **Bhatkhande sargam** (synced to playback; remaps when you change Sa),
a best-effort **BPM / taal / time-signature guess** (override in the Rhythm guess control),
and audio play in your browser. Files are remembered in **this browser**
(IndexedDB). Nothing is uploaded to the website.

---

## Riyāz (alankārs)

Works fully on the live site. **Save locally** downloads a `.txt`; **Load from disk**
opens it again. Playback is in the browser.

---

## Optional: local `server.py`

Developers can still run `python server.py 8777` in the full repo for Add-from-YouTube
inside the page. Most people only need the downloadable Processor + Load song folder.
