# TASK-009

## Title

Rhythm Analyzer

---

## Objective

Implement the Rhythm Analyzer for AI MusiMuse.

This analyzer extracts temporal rhythmic characteristics from decoded audio.

The extracted features will become an important component of Music DNA and future similarity calculations.

Do not implement harmony, embeddings, or machine learning.

---

# Expected Commit Message

Add rhythm analyzer

---

# Requirements

Read SPEC.md before implementing anything.

Follow all AI Rules.

Follow the Analysis Framework.

Reuse existing DSP utilities whenever possible.

Do not violate architectural boundaries.

---

# Scope

Create

```
srcanalyzerrhythm
```

Files

```
__init__.py
rhythm_analyzer.py
```

Register the analyzer through

```
build_default_registry()
```

No CLI changes are required.

The existing

```
musimuse analyze
```

must automatically execute this analyzer.

---

# Analyzer

Create

```
RhythmAnalyzer
```

inherits from

```
Analyzer
```

Name

```
rhythm
```

Version

```
1.0.0
```

---

# Shared DSP

Create

```
srcdsprhythm.py
```

Implement reusable rhythm utilities.

Minimum API

```python
compute_onset_envelope()

estimate_tempo()

estimate_beat_period()

compute_autocorrelation()

compute_rhythm_density()
```

Future rhythm analyzers must reuse these functions.

---

# Configuration

Extend

```
srcdspconfig.py
```

with

```python
ONSET_HOP_SIZE = 512

TEMPO_MIN_BPM = 40

TEMPO_MAX_BPM = 220
```

No analyzer may hardcode these values.

---

# Features

Calculate

---

## Estimated Tempo

Feature

```
tempo_bpm
```

Unit

```
BPM
```

---

## Beat Period

Feature

```
beat_period_seconds
```

Unit

```
seconds
```

---

## Rhythm Density

Feature

```
rhythm_density
```

Definition

Estimated number of detected onsets per second.

---

## Beat Strength

Feature

```
beat_strength
```

Range

```
0.0 – 1.0
```

Derived from normalized autocorrelation peak.

---

## Onset Count

Feature

```
onset_count
```

Integer.

---

## Onset Rate

Feature

```
onset_rate
```

Definition

Detected onsets per second.

---

## Rhythm Regularity

Feature

```
rhythm_regularity
```

Range

```
0.0 – 1.0
```

Higher values indicate more periodic rhythmic structure.

---

# DSP Requirements

Use

- numpy
- scipy.signal

Use vectorized operations whenever possible.

Reuse existing spectrum utilities if appropriate.

---

# Channel Handling

Mono

Analyze directly.

Stereo

Convert to mono

```
(left + right)  2
```

before analysis.

---

# Numerical Stability

Correctly handle

- silence
- constant signals
- empty audio
- NaN
- Inf

Raise

```
AnalysisError
```

when appropriate.

---

# Persistence

No database access.

Pipeline automatically stores generated features.

---

# Logging

No logging inside the analyzer.

---

# Tests

Create tests covering

- silence
- metronome click track
- regular pulse
- irregular pulse
- white noise
- sine wave
- mono
- stereo
- tempo estimation
- onset detection
- beat strength
- onset count
- onset rate
- rhythm density
- rhythm regularity
- deterministic output
- invalid input
- pipeline integration
- persistence integration

---

# Forbidden

Do NOT implement

- beat tracking
- beat alignment
- downbeat detection
- swing estimation
- groove analysis
- harmony
- key detection
- Music DNA
- similarity
- machine learning

---

# Definition of Done

✓ RhythmAnalyzer implemented

✓ Registered through build_default_registry()

✓ Shared rhythm DSP utilities implemented

✓ DSP configuration extended

✓ Produces deterministic results

✓ Handles mono and stereo

✓ Handles silence correctly

✓ Handles invalid input safely

✓ Produces immutable FeatureSet

✓ Automatically persists features

✓ Tests pass

✓ Ruff passes

✓ Black passes

✓ isort passes

---

# Architectural Notes

This analyzer introduces temporal rhythm analysis.

The shared DSP rhythm module becomes the project's reusable rhythm processing layer.

Future beat tracking, groove analysis, and Music DNA must reuse these utilities rather than implementing rhythm estimation independently.

The extracted rhythm descriptors intentionally remain deterministic and low-level.

They are designed to become stable inputs for Music DNA and similarity search.