Skip to content

AlishanNaqvi/EmotionSymphony

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🎡 Emotion Symphony - Real-Time Emotion-Driven Music Generator

An innovative machine learning project that combines facial emotion recognition with algorithmic music composition to create music that responds to your emotional state in real-time.

Project Banner Project Banner Project Banner

🌟 Features

Core Functionality

  • Real-Time Emotion Detection: Uses facial recognition to identify 7 distinct emotions (happy, sad, angry, fearful, surprised, neutral, disgusted)
  • Dynamic Music Generation: Composes music in real-time based on detected emotions
  • Advanced Music Theory: Implements proper scales, chord progressions, and rhythmic patterns
  • Live Audio Visualization: Beautiful real-time visualizer showing the generated music
  • Multi-Emotion Transitions: Smoothly transitions between emotional states

Technical Highlights

  • Custom CNN architecture for emotion detection (7 emotion classes)
  • Markov chain-based melody generation
  • Music theory-driven composition (scales, progressions, dynamics)
  • MIDI file export capability
  • WebGL-accelerated facial detection
  • Responsive, futuristic UI design

🎯 What Makes This Unique?

  1. Real-Time Integration: Unlike static ML models, this continuously adapts to your emotional state
  2. Sophisticated Music Theory: Doesn't just play random notes - uses proper scales, chord progressions, and musical structure
  3. Multi-Modal AI: Combines computer vision (emotion detection) with generative AI (music composition)
  4. Production-Ready: Fully functional web interface with stunning visualizations
  5. Educational: Demonstrates multiple ML concepts: CNNs, Markov chains, data augmentation, transfer learning

πŸ“ Project Structure

emotion-music-generator/
β”œβ”€β”€ emotion-music-generator.html    # Main web application (standalone)
β”œβ”€β”€ emotion_model.py                # CNN training & real-time detection
β”œβ”€β”€ music_generator.py              # Advanced music composition engine
β”œβ”€β”€ requirements.txt                # Python dependencies
└── README.md                       # This file

πŸš€ Quick Start

Option 1: Web Interface (Easiest)

Simply open emotion-music-generator.html in a modern browser. No installation required!

  1. Click "Start Camera" to enable your webcam
  2. The system will begin detecting your emotions
  3. Click "Generate Music" to start the music generation
  4. Watch as the music adapts to your emotional state!

Option 2: Python Backend (Advanced)

Installation

# Install Python dependencies
pip install tensorflow numpy pandas opencv-python scikit-learn matplotlib midiutil --break-system-packages

# Or use the requirements file
pip install -r requirements.txt --break-system-packages

Train Custom Emotion Model

# Download FER-2013 dataset from Kaggle
# https://www.kaggle.com/datasets/msambare/fer2013

# Train the model
python emotion_model.py train fer2013.csv

Training will:

  • Load and preprocess the FER-2013 dataset (~35,000 images)
  • Build a custom CNN with 4 convolutional blocks
  • Use data augmentation (rotation, shift, flip, zoom)
  • Train with early stopping and learning rate reduction
  • Save the best model as best_emotion_model.h5
  • Generate training history plots

Real-Time Emotion Detection

# Run webcam emotion detection
python emotion_model.py detect best_emotion_model.h5

Generate Music Files

# Generate MIDI file for a specific emotion
python music_generator.py happy 16 happy_music.mid

# Available emotions: happy, sad, angry, fearful, surprised, neutral

🧠 Model Architecture

Emotion Detection CNN

Input: 48x48 grayscale images
β”œβ”€β”€ Conv Block 1: 2x Conv2D(64) + BatchNorm + MaxPool + Dropout
β”œβ”€β”€ Conv Block 2: 2x Conv2D(128) + BatchNorm + MaxPool + Dropout
β”œβ”€β”€ Conv Block 3: 2x Conv2D(256) + BatchNorm + MaxPool + Dropout
β”œβ”€β”€ Conv Block 4: 2x Conv2D(512) + BatchNorm + MaxPool + Dropout
β”œβ”€β”€ Dense: 512 + BatchNorm + Dropout
β”œβ”€β”€ Dense: 256 + BatchNorm + Dropout
└── Output: 7 classes (softmax)

Total Parameters: ~5.5M
Training Accuracy: ~68-72%
Validation Accuracy: ~65-68%

Music Generation System

  • Markov Chain: Order-2 Markov chain for melody generation
  • Music Theory Engine: Proper scales (12 types), chord progressions, dynamics
  • Emotion Mapping: Each emotion maps to specific musical parameters:
    • Tempo (60-160 BPM)
    • Scale type (major, minor, phrygian, etc.)
    • Rhythm density (40-90%)
    • Note duration (0.125-1.0 beats)
    • Dynamics (pianissimo to fortissimo)

🎨 Emotion-to-Music Mapping

Emotion Tempo Scale Key Articulation Intensity
Happy 140 BPM Major C Staccato 80%
Sad 60 BPM Minor A Legato 40%
Angry 160 BPM Phrygian E Marcato 95%
Fearful 90 BPM Diminished F# Tremolo 60%
Surprised 130 BPM Lydian D Staccato 70%
Neutral 100 BPM Major G Normal 50%

πŸ’‘ Technical Deep Dive

Emotion Detection Pipeline

  1. Face Detection: Haar Cascade classifier locates faces in frame
  2. Preprocessing: Resize to 48x48, normalize pixel values
  3. CNN Inference: Custom trained model predicts emotion probabilities
  4. Smoothing: Rolling average to prevent jittery predictions
  5. Emotion State: Dominant emotion triggers music generation

Music Composition Algorithm

  1. Parameter Extraction: Map emotion to musical parameters
  2. Scale Construction: Build scale from root note and scale type
  3. Melody Generation: Markov chain creates melodic sequence
  4. Harmony Addition: Chord progression based on music theory
  5. Bass Line: Root notes with rhythmic patterns
  6. MIDI Export: Compile to standard MIDI format

Web Audio Implementation

  • Tone.js: Web Audio API wrapper for synthesis
  • PolySynth: Melodic voices with triangle oscillators
  • MonoSynth: Bass with sawtooth oscillators
  • Sequencer: Time-based pattern playback
  • Visualizer: Real-time amplitude analysis

πŸ“Š Performance Metrics

Model Performance

  • Inference Time: ~15-30ms per frame (CPU)
  • Accuracy: 65-72% on FER-2013 validation set
  • Real-time FPS: 25-30 FPS on modern hardware

Music Generation

  • Latency: < 100ms from emotion detection to music start
  • MIDI Generation: ~50ms for 16-bar composition
  • Audio Quality: 44.1kHz sample rate, stereo output

πŸŽ“ Learning Outcomes

This project demonstrates:

  • Computer Vision: Face detection, image preprocessing, data augmentation
  • Deep Learning: CNN architecture, transfer learning, regularization
  • Generative AI: Markov chains, algorithmic composition
  • Music Theory: Scales, chord progressions, rhythm, dynamics
  • Web Development: Real-time audio, canvas rendering, responsive design
  • Full-Stack Integration: Python backend + JavaScript frontend

πŸ› οΈ Advanced Features to Implement

Future Enhancements

  • LSTM-based melody generation for more coherent phrases
  • Multi-instrument orchestration
  • Emotion intensity mapping (not just type)
  • Style transfer (compose in different genres)
  • Real-time harmony detection from audio input
  • Export to multiple formats (WAV, MP3, MusicXML)
  • Collaborative mode (multiple people affect the music)
  • Historical emotion tracking and playback

πŸ“ Code Examples

Custom Emotion Detection

from emotion_model import RealTimeEmotionDetector

detector = RealTimeEmotionDetector('best_emotion_model.h5')
results = detector.detect_emotion(frame)

for result in results:
    print(f"Detected: {result['emotion']} ({result['confidence']*100:.1f}%)")
    print(f"All predictions: {result['all_predictions']}")

Custom Music Generation

from music_generator import EmotionBasedComposer

# Create composer for specific emotion
composer = EmotionBasedComposer('happy')

# Generate 32-bar composition
midi_file = composer.compose(duration_bars=32, output_file='my_music.mid')

# Create multi-emotion journey
from music_generator import MultiEmotionComposer

journey = MultiEmotionComposer([
    ('sad', 8),      # 8 bars of sadness
    ('neutral', 4),  # 4 bars transition
    ('happy', 12)    # 12 bars of happiness
])
journey.compose('emotional_journey.mid')

🀝 Contributing

Contributions are welcome! Areas for improvement:

  • Better emotion detection models (try transfer learning with VGGFace, ResNet)
  • More sophisticated music generation (GANs, Transformers)
  • Additional emotional states (disgust, contempt, etc.)
  • Multi-language support
  • Accessibility features

πŸ“š References & Credits

Datasets

  • FER-2013: Facial Expression Recognition dataset (Kaggle)

Technologies

  • TensorFlow: Deep learning framework
  • Tone.js: Web Audio synthesis
  • OpenCV: Computer vision library
  • MIDIUtil: MIDI file generation

Inspiration

  • Music therapy and emotional regulation research
  • Generative music systems (Brian Eno, David Cope)
  • Affective computing principles

πŸ“„ License

This project is provided for educational and research purposes. Feel free to use and modify as needed.

πŸ™Œ Acknowledgments

Special thanks to:

  • The creators of the FER-2013 dataset
  • TensorFlow and Tone.js communities
  • Music theory educators and composers

Built with ❀️ for the intersection of AI, music, and human emotion

Questions or suggestions? Feel free to reach out or open an issue!

About

A machine learning model that reads your emotions and generates music according to the emotion you are displaying

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors