Pitch Loop Finder — User Guide
Turbo‑speed pitch‑based loop detection: finds repeating melodic/harmonic patterns in audio by comparing pitch contours across time offsets using optimized matrix formulas.
What this does
This script detects melodic/harmonic loops — repeating pitch patterns in audio — by comparing pitch contours at different time offsets. Unlike beat‑based loop finders that look for rhythmic repetitions, this algorithm focuses specifically on pitch similarity, making it ideal for finding melodic motifs, vocal phrases, or harmonic progressions that repeat at various time distances.
Key Features:
- Turbo‑Speed Computation — Uses Praat's C++ Formula engine for O(1) matrix operations
- Pitch‑Contour Focus — Detects melodic/harmonic repetitions, not just rhythmic loops
- Tolerance‑Based Matching — Allows slight pitch variations (tunings, vibrato, expressiveness)
- Automatic Annotation — Creates TextGrid with loop source and repeat locations
- Overlap Prevention — Smart filtering to avoid overlapping/nested loops
- Adjustable Detection — Control loop duration, pitch tolerance, time resolution
- Exact repetition: Same notes/phrase repeated (e.g., chorus melody)
- Transposed repetition: Same contour at different pitch level
- Varied repetition: Similar but not identical (ornamentation, improvisation)
- Delayed repetition: Pattern repeats after time gap (verse‑chorus structure)
Technical Implementation: (1) Extract pitch contour using Praat's pitch tracker. (2) Convert to matrix and compute similarity scores using fast formula: S(i,j) = 1 - |pitch_i - pitch_j|/tolerance if both pitched and difference < tolerance, else 0. (3) Scan diagonal bands in similarity matrix for continuous high‑similarity segments. (4) Filter candidates by duration and score, preventing overlaps. (5) Annotate source and repeat locations in TextGrid. The "turbo" optimization uses Praat's built‑in C++ matrix operations instead of slow Praat‑script loops.
Quick start
- In Praat, select exactly one Sound object (mono works best).
- Run script… →
pitch_loop_finder.praat. - Set time_step:
- 0.05 (fast) — for initial exploration, long files
- 0.02 (high precision) — for detailed analysis, short phrases
- Adjust pitch_floor and pitch_ceiling to match your audio's pitch range.
- Set tolerance_hz (default 50 Hz) — higher values allow more pitch variation.
- Define min_loop_duration (default 0.4s) — shortest loop to detect.
- Set num_loops_to_find (default 5) — top N loops to annotate.
- Click OK — script extracts pitch, computes similarity matrix, finds loops.
- Output: TextGrid appears with "Loops" and "Repeats" tiers.
- Info window shows detected loops with timings.
- Select Sound + TextGrid, click View & Edit to see annotations.
Loop Detection Algorithm
Step 1: Pitch Extraction
🎵 Praat Pitch Tracking
Frame conversion:
Step 2: Similarity Matrix Computation
📊 Pitch Similarity Scoring
Similarity formula (for each cell i,j):
Why this formula?
- Linear decay: Similarity decreases linearly with pitch difference
- Thresholded: Only considers differences within tolerance
- Unvoiced handling: Unpitched frames score 0 (not compared)
- Interpretable: Score directly relates to pitch difference
Step 3: Diagonal Scanning
🔍 Finding Loops Along Diagonals
Similarity Matrix (S[i,j]):
Time j →
1 2 3 4 5 6 7 8 9 10
┌─────────────────────
1 │ X . . . . . . . . .
2 │ . X . . . . . . . .
3 │ . . X . . ■ ■ ■ . .
4 │ . . . X . . . . . .
T 5 │ . . . . X . . . . .
i 6 │ . . ■ . . X . . . .
m 7 │ . . ■ . . . X . . .
e 8 │ . . ■ . . . . X . .
9 │ . . . . . . . . X .
10 │ . . . . . . . . . X
Diagonal (gap = 3):
S[3,6] = ■ (similar)
S[4,7] = ■ (similar)
S[5,8] = ■ (similar)
→ Detected loop: frames 3‑5 repeating at frames 6‑8
Algorithm: For each possible time gap (offset), scan along that diagonal looking for consecutive high‑similarity cells.
🔄 Loop Candidate Detection
Scanning process:
Parameters in frames:
- min_len_frames = min_loop_duration / time_step
- max_gap = num_frames - min_len_frames
- threshold = 0.5 (hard‑coded, corresponds to pitch difference = tolerance/2)
Step 4: Candidate Scoring & Filtering
🏆 Scoring & Ranking
Score calculation:
Filtering process:
- Sort by score: Highest score = best loop candidate
- Prevent overlaps: Skip candidates overlapping with higher‑scoring loops
- Limit output: Keep top num_loops_to_find non‑overlapping loops
Overlap check: Two loops overlap if their source segments (t1 to t2) overlap in time.
Complete Pipeline
🔄 Full Processing Flow
Turbo Optimization Explained
The Speed Problem
🐌 Naïve Approach: O(N³) Disaster
Without optimization:
Why slow? Praat script interpreter loops are slow. Each iteration involves function calls, variable lookups, etc.
The Turbo Solution
⚡ Praat's Formula Engine
Key insight: Praat's Formula command for matrices runs in compiled C++ code, not interpreted Praat script.
Speed comparison:
- Praat script loops: ~1‑10 milliseconds per comparison
- C++ Formula: ~0.001‑0.01 milliseconds per comparison
- Speedup: 100‑1000× faster!
How the Formula Works
🔧 Matrix Formula Internals
Special variables in Praat Formula:
Formula execution:
- Praat parses formula string once
- Compiles to internal bytecode
- Executes in optimized C++ loop
- Processes all N×N cells in single pass
- No Praat script interpreter overhead
Important: The referenced matrix (ThePitchData) must exist and be named exactly that.
Additional Optimizations
🎯 Smart Scanning Heuristics
1. Variable step size:
2. Early termination:
3. Vectorized pitch extraction:
Performance Benchmarks
| Audio Length | time_step | Frames (N) | Naïve Time | Turbo Time | Speedup |
|---|---|---|---|---|---|
| 10 seconds | 0.05s | 200 | 10‑20s | 0.1‑0.2s | 100× |
| 30 seconds | 0.05s | 600 | 90‑180s | 0.5‑1s | 180× |
| 60 seconds | 0.05s | 1200 | 360‑720s | 2‑4s | 180× |
| 30 seconds | 0.02s | 1500 | 560‑1120s | 3‑6s | 180× |
| 180s (3min) | 0.05s | 3600 | 3240‑6480s | 18‑36s | 180× |
Parameters Explained
Speed Settings
⚡ Time Resolution
| Parameter | Default | Range | Description |
|---|---|---|---|
| time_step | 0.05s | 0.01‑0.10s | Time between pitch analysis frames. Smaller = more precise timing but slower. |
Recommendations:
- 0.05s (20 fps): Default. Good for most music/speech. Fast computation.
- 0.02s (50 fps): High precision. For detailed melodic analysis, fast passages.
- 0.10s (10 fps): Very fast. For long files, initial exploration.
- 0.01s (100 fps): Extremely detailed. For micro‑timing, but very slow.
Frame rate formula: frames/second = 1 / time_step
Pitch Settings
🎵 Pitch Detection Range
| Parameter | Default | Range | Description |
|---|---|---|---|
| pitch_floor | 75 Hz | 50‑200 Hz | Minimum fundamental frequency to detect. Lower = more sensitive but more errors. |
| pitch_ceiling | 600 Hz | 200‑2000 Hz | Maximum fundamental frequency. Set to expected range of audio. |
| tolerance_hz | 50 Hz | 10‑200 Hz | Maximum pitch difference to consider "similar". Accounts for tuning variations, vibrato, expression. |
Pitch range recommendations:
- Male speech: 75‑300 Hz
- Female speech: 100‑500 Hz
- Singing (bass‑tenor): 80‑500 Hz
- Singing (alto‑soprano): 150‑1000 Hz
- Violin: 200‑2000 Hz
- Cello: 65‑1000 Hz
Tolerance guidelines:
- 10‑20 Hz: Very strict. Only near‑identical pitches match.
- 30‑50 Hz: Default. Allows slight variations, tuning differences.
- 60‑100 Hz: Lenient. Allows expressive variations, larger intervals.
- 100‑200 Hz: Very lenient. May match different notes in same region.
Loop Timing Settings
⏱️ Loop Duration Constraints
| Parameter | Default | Range | Description |
|---|---|---|---|
| min_loop_duration | 0.4s | 0.2‑5.0s | Shortest loop to detect. Shorter = more candidates but more false positives. |
| max_loop_duration | 10.0s | 1.0‑60.0s | Maximum time gap between loop start and repeat. Longer = search more offsets. |
Duration guidelines:
- 0.2‑0.5s: Short motifs, rhythmic cells, phonetic units
- 0.5‑1.5s: Musical phrases, spoken phrases
- 1.5‑3.0s: Longer phrases, musical themes
- 3.0‑10.0s: Complete musical sections, spoken sentences
- >10.0s: Extended passages, verse‑chorus relationships
max_loop_duration tip: Set to slightly longer than expected repetition interval. For verse‑chorus form, 20‑30s. For motif repetition, 5‑10s.
Output Settings
📝 Results Configuration
| Parameter | Default | Range | Description |
|---|---|---|---|
| num_loops_to_find | 5 | 1‑50 | Number of top‑scoring non‑overlapping loops to annotate in TextGrid. |
Selection process:
- All candidates sorted by score (highest first)
- Starting from top, add to output if not overlapping with already‑selected loops
- Continue until num_loops_to_find reached or candidates exhausted
When to increase: Complex audio with many repeating patterns. When to decrease: Simple audio, or to focus on best matches only.
Parameter Interactions
- time_step × min_loop_duration = minimum frames needed for detection
- tolerance_hz / pitch_ceiling = relative tolerance (e.g., 50/600 = 8.3% of range)
- max_loop_duration / time_step = maximum gap to search (in frames)
- pitch_ceiling - pitch_floor = detection range (wider = more processing)
- num_loops_to_find vs overlap:
- High value → may include shorter/weaker loops
- Low value → only strongest loops shown
Output & Interpretation
Info Window Report
📋 Processing Summary
Interpretation:
- "Extracted N frames": Total pitch analysis points
- "Time Step": Resolution of analysis
- "Loop X: A.s -> B.s": Source segment at time A repeats at time B
- Loops listed in score order (highest first)
TextGrid Output
🏷️ Two‑Tier Annotation
Tier 1: "Loops" — Source segments (original occurrences)
Tier 2: "Repeats" — Repeat segments (later occurrences)
TextGrid structure:
- Identical numbering: "Loop 1" corresponds to "Repeat 1"
- Same duration: Source and repeat segments have equal length
- Time alignment: Intervals show exact start/end times
- Non‑overlapping: Loops don't overlap within each tier (but can across tiers)
Using in Praat: Select Sound + TextGrid, click View & Edit. Click on intervals to hear segments.
Understanding Loop Scores
📈 What Makes a "Good" Loop?
Score components:
Score interpretation:
- High score (e.g., >20): Long, accurate repetition
- Medium score (10‑20): Moderate length with good similarity, or long with moderate similarity
- Low score (<10): Short loop, or longer loop with low similarity
Note: Scores are relative — absolute values depend on time_step and audio length.
Common Output Patterns
🎵 What to Expect
For speech with repetitions:
- Loops correspond to repeated words/phrases
- Similar durations for source and repeat
- May capture filler words ("um", "ah")
For music with motifs:
- Melodic motifs appear as loops
- Transposed motifs may be detected if within tolerance
- Rhythmic patterns without pitch won't appear
For singing/chanting:
- Repeated phrases detected
- Chorus sections show multiple loops
- May detect vocalise/melisma patterns
For instrumental music:
- Thematic material appears as loops
- Basslines/harmonic progressions may be detected
- Solo passages with repetition show loops
Troubleshooting Output
Possible causes:
- Audio has no pitched content
- min_loop_duration too long
- tolerance_hz too low
- pitch_floor/pitch_ceiling wrong for audio
- time_step too coarse
Possible causes:
- tolerance_hz too high
- min_loop_duration too short
- Audio has steady pitch (e.g., drone)
- Pitch tracking errors (octave jumps)
Possible causes:
Possible causes:
- time_step resolution limitations
- Pitch tracking timing errors
- Loop boundaries at frame edges
Practical Applications
Music Analysis
🎵 Motif & Theme Detection
Use case: Identify recurring melodic motifs in classical music
Workflow:
- Extract solo instrument track or vocal line
- Set parameters: time_step=0.02, pitch_range=instrument_range, tolerance=20‑40 Hz
- Run Pitch Loop Finder
- Examine detected loops in TextGrid
- Map loops to score positions
Example analysis: Beethoven's 5th Symphony opening motif
Speech & Language Analysis
🗣️ Repetition Analysis in Speech
Use cases:
- Language learning: Detect repeated phrases in practice
- Speech therapy: Identify repetitive patterns in stuttering
- Forensic analysis: Find repeated phrases in recordings
- Conversation analysis: Identify recurring topics/questions
Parameters for speech:
Example: Political speech — detects repeated slogans/catchphrases.
Composition & Sound Design
🎼 Loop‑Based Composition
Use case: Create compositions from found audio loops
Creative workflow:
- Record improvisation or free playing
- Run Pitch Loop Finder to identify interesting motifs
- Extract loop segments using TextGrid boundaries
- Rearrange loops to create new composition
- Use repetition structure as formal guide
Parameters for improvisation:
Example: Jazz improvisation — detects recurring licks/phrases.
Music Education
🏫 Teaching Musical Form
Classroom activity: Visualizing repetition in music
- Students analyze favorite songs
- Predict where repetitions occur
- Run Pitch Loop Finder
- Compare predictions to detected loops
- Discuss why some repetitions detected/not detected
Learning outcomes:
- Understand repetition in musical structure
- Learn about pitch vs. timbre perception
- Explore algorithmic music analysis
- Connect hearing to visual representation
Ethnomusicology & Field Recordings
🌍 Oral Tradition Analysis
Use case: Analyze repetitive structures in oral traditions
Applications:
- Folk songs: Detect verse/chorus patterns
- Chanting: Identify mantra/incantation repetitions
- Storytelling: Find repeated phrases in oral narratives
- Ceremonial music: Analyze ritual repetition structures
Methodology:
Advanced Techniques
- Multi‑stage analysis: Run with different parameters, compare results
- Combined features: Export loops, then analyze with other tools (MFCC, rhythm)
- Statistical analysis: Collect loop statistics across corpus
- Real‑time adaptation: Adjust parameters based on initial results
- Integration with other scripts: Use output TextGrid as input for segmentation, annotation
- Custom scoring: Modify script to use different scoring formulas
Limitations & Complementary Tools
⚠️ What This Script Doesn't Do
Pitch‑only limitation:
- Misses rhythmic repetitions without pitch change
- Misses timbral repetitions (same pitch, different instrument)
- May miss transposed repetitions beyond tolerance
Complementary Praat scripts:
- Rhythmic loop finder: Based on onset detection
- MFCC similarity: For timbral repetitions
- Multi‑feature loop detection: Combines pitch, rhythm, timbre
- Novelty curve analysis: For boundary detection
Best practice: Use pitch‑based detection first, then verify with listening and other analyses.