Skip to content

usr-wwelsh/Mood-Detector

Repository files navigation

🎡 Mood Detector

Python 3.8+ License: MIT Powered by librosa FastAPI PRs Welcome

Open-source music mood analysis API. The free alternative to proprietary music intelligence APIs.

Analyze audio files to detect mood, tempo, energy, key, and genre characteristicsβ€”without sending data to third parties or paying per-request fees.


πŸš€ Quick Start

Three ways to use it:

1️⃣ Music Library Analyzer (GUI App)

Analyze your entire music collection and filter by mood, tempo, energy, and key.

python music_library_app.py

Music Library Analyzer

Features:

  • πŸ“ Scan entire folders recursively
  • πŸ” Analyze all tracks automatically
  • 🎚️ Filter by mood, tempo, energy, key
  • πŸ’Ύ Caches results for instant loading
  • 🎨 Dark theme, foobar2000-style UI

2️⃣ REST API

Run a local API server for integration with other apps.

# Install and run
pip install -e .
python api/main.py

# Or with Docker
docker run -p 8000:8000 usr-wwelsh/mood-detector

Test it:

curl -X POST http://localhost:8000/analyze -F "file=@song.mp3"

Response:

{
  "mood": "House/Dance",
  "tempo": 124.5,
  "energy": 0.156,
  "key": "F major",
  "explanation": "Moderate energy (0.156), dance tempo (124.5 BPM), bright/sharp timbre, key of F major"
}

Interactive Docs: Open http://localhost:8000/docs for a web UI to test the API.


3️⃣ Python Library

Use directly in your Python code.

pip install -e .
from mood_detector import analyze_audio

result = analyze_audio("song.mp3")
print(f"Mood: {result.mood}")
print(f"Tempo: {result.tempo} BPM")
print(f"Energy: {result.energy}")
print(f"Key: {result.key}")

✨ Features

  • βœ… Runs locally - No API keys, no cloud services
  • βœ… Fast - ~2 seconds per track
  • βœ… No ML dependencies - Uses signal processing (librosa)
  • βœ… Works offline - 100% local processing
  • βœ… Multiple formats - MP3, WAV, FLAC, OGG, M4A, AAC
  • βœ… DJ-oriented moods - House, Techno, Disco, DnB, Ambient, etc.
  • βœ… Multiple interfaces - GUI app, REST API, Python library, CLI
  • βœ… Free forever - MIT license

🎯 Use Cases

For Developers:

  • Build music player apps with mood-based filtering
  • Create smart playlist generators
  • Add music intelligence to DJ software
  • Organize large music libraries programmatically

For Music Lovers:

  • Analyze your entire music collection
  • Find tracks by mood/energy/tempo
  • Discover forgotten gems with specific vibes
  • Create mood-based playlists

Why not just use [proprietary service]?

  • ❌ Requires internet connection
  • ❌ Costs money per request
  • ❌ Sends your music/data to third parties
  • ❌ API keys, rate limits, vendor lock-in

This project:

  • βœ… Works offline
  • βœ… Free forever
  • βœ… Runs on your machine
  • βœ… Open source, fully transparent

API Reference

See docs/API.md

How It Works

See docs/HOW_IT_WORKS.md

Contributing

PRs welcome! See CONTRIBUTING.md

πŸ“Š Detected Moods

The algorithm detects these mood categories based on tempo, energy, and spectral characteristics:

Dance/Electronic:

  • House/Dance, Dark House
  • Disco/Funk, Club/Groovy
  • Techno/Dark, Techno/Industrial
  • Drum & Bass, Breakbeat/Fast
  • Energetic/Rave, Driving Electronic

Chill/Ambient:

  • Ambient/Drone, Ambient/Atmospheric
  • Atmospheric Pad, Atmospheric/Textural
  • Downtempo/Dark, Downtempo/Relaxed
  • Ambient/Chill, Melancholic/Sad

Experimental:

  • Glitch/Experimental
  • Harsh Noise/Experimental
  • Noise/Experimental

High Energy:

  • Hard/Aggressive
  • Energetic/Rave

Plus tempo (BPM), energy level (0-1), musical key, and brightness characteristics.


πŸ”§ How It Works

  1. Audio Feature Extraction (librosa)

    • Tempo detection via beat tracking with half-tempo correction
    • Energy via RMS (root mean square)
    • Brightness via spectral centroid
    • Key detection via chroma features
    • Zero-crossing rate for noise detection
  2. Rule-Based Classification

    • Combines tempo + energy + brightness + timbre
    • Detects drones/ambient via tempo confidence analysis
    • Handles major/minor keys for mood nuance
    • No neural networks needed
  3. Fast & Lightweight

    • Analyzes 30 seconds from track middle (skips intros)
    • ~2 seconds per track
    • Pure signal processing

πŸ› οΈ Installation

For Development:

git clone https://github.com/usr-wwelsh/mood-detector
cd mood-detector

# Create virtual environment
python -m venv venv
source venv/bin/activate  # or venv\Scripts\activate on Windows

# Install in editable mode
pip install -e .

Run the API:

python api/main.py
# API at http://localhost:8000
# Docs at http://localhost:8000/docs

Run the Music Library App:

python music_library_app.py

πŸ› Known Issues

  • Genre Overlap: Some tracks fit multiple categories (by design - music is fluid!)
  • Key Detection: Works best with clear harmonic content
  • Half-Tempo Detection: Very high energy tracks (>0.8) automatically double BPM - may rarely misclassify

Pull requests welcome to improve accuracy!


🀝 Contributing

PRs welcome! See CONTRIBUTING.md

Areas that need work:

  • More genre categories (experimental/IDM subcategories, jazz, classical, etc.)
  • Better key detection algorithm
  • UI improvements for the music library app
  • Training data for ML-based classification (optional enhancement)

πŸ“ License

MIT - Use it however you want


πŸ’‘ Credits

Uses:

About

Open-source music mood analysis API. The free, local alternative to proprietary music intelligence services. Analyze audio for mood, tempo, energy, and key. no API keys, no cloud. ( currently used in git.wwel.sh/omnimux )

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Contributors