🎵 Music Generation with PLGL

Create personalized music by learning from simple ratings - no musical knowledge required

How It Works

1

Rate Melodies

Listen to generated melodies and rate them

→
2

Learn Preferences

PLGL learns your musical taste from ratings

→
3

Generate Music

Create new compositions matching your preferences

Key Implementation Details

1. Music VAE Architecture

The system uses a Variational Autoencoder (VAE) to learn a latent representation of music. This creates a continuous space where similar melodies are close together.

class MusicVAE(nn.Module):
    def __init__(self, latent_dim: int = 256):
        super().__init__()
        self.latent_dim = latent_dim
        
        # Encoder: Piano roll → Latent space
        self.encoder = nn.Sequential(
            nn.Conv2d(1, 32, kernel_size=(12, 4), stride=(2, 2)),
            nn.ReLU(),
            nn.Conv2d(32, 64, kernel_size=(12, 4), stride=(2, 2)),
            # ... compress to latent dimensions
        )
        
        # Decoder: Latent space → Piano roll
        self.decoder = nn.Sequential(
            nn.Linear(latent_dim, 512),
            nn.ReLU(),
            # ... expand back to piano roll
            nn.Sigmoid()  # Note probabilities 0-1
        )

2. Musical Feature Extraction

PLGL extracts interpretable musical features to understand what makes melodies appealing:

def extract_musical_features(self, melody: torch.Tensor) -> Dict[str, float]:
    """Extract interpretable musical features"""
    features = {
        'pitch_range': self._compute_pitch_range(piano_roll),      # How wide
        'note_density': self._compute_note_density(piano_roll),    # How busy
        'rhythmic_complexity': self._compute_rhythmic_complexity(), # Rhythm variety
        'melodic_contour': self._compute_melodic_contour(),       # Up/down movement
        'harmonic_consistency': self._compute_harmonic_consistency() # Musical intervals
    }
    return features

3. Preference Learning

A neural network learns to predict your ratings from musical features:

class MusicPreferenceLearner:
    def __init__(self, music_vae: MusicVAE):
        self.vae = music_vae
        
        # Preference model: Piano roll → Rating (0-1)
        self.preference_model = nn.Sequential(
            nn.Linear(128 * 32, 512),  # Flattened piano roll
            nn.ReLU(),
            nn.Dropout(0.3),
            nn.Linear(512, 256),
            nn.ReLU(),
            nn.Linear(256, 1),
            nn.Sigmoid()  # Output preference score
        )

4. Personalized Generation

Navigate the latent space to find melodies with high preference scores:

def generate_personalized_melody(self, optimization_steps: int = 500):
    # Start from random point in latent space
    z = torch.randn(1, self.latent_dim, requires_grad=True)
    optimizer = torch.optim.Adam([z], lr=0.01)
    
    for step in range(optimization_steps):
        # Generate melody from latent code
        melody = self.vae.decode(z)
        
        # Score with preference model
        score = self.preference_model(melody.flatten())
        
        # Optimize to maximize score
        loss = -score
        loss.backward()
        optimizer.step()
        
        # Add exploration noise
        z += torch.randn_like(z) * 0.01
    
    return self.generate_melody(z)

5. Playlist Generation

Create diverse playlists that still match your preferences:

def generate_playlist(self, n_songs: int = 10, diversity: float = 0.5):
    playlist = []
    
    for i in range(n_songs):
        if i % 2 == 0:
            # High-preference melody (exploitation)
            melody = self.generate_personalized_melody()
        else:
            # Diverse but still good (exploration)
            z = torch.randn(1, self.latent_dim)
            
            # Ensure different from previous songs
            if playlist:
                distances = torch.norm(z - prev_latents, dim=1)
                while distances.min() < diversity:
                    z = torch.randn(1, self.latent_dim)
                    
        playlist.append(melody)
    
    return playlist

Real-World Applications

🎧 Personalized Playlists

Generate infinite playlists that match your mood and taste, discovering new melodies you'll love.

🎮 Adaptive Game Music

Create dynamic soundtracks that adapt to player preferences in real-time.

🎬 Film Scoring

Generate music that matches director preferences without describing technical requirements.

📱 Music Discovery Apps

Like TikTok for music - swipe through generated melodies, no searching or prompting needed.

Key Advantages

  • No Musical Knowledge Required: Users don't need to know tempo, key, or music theory
  • Continuous Learning: The more you rate, the better it understands your taste
  • Infinite Variety: Generate new music that doesn't exist yet
  • Controlled Diversity: Balance between your favorites and discovering new styles
  • Privacy Preserving: Learn preferences without analyzing your existing playlists

Try It Yourself

Ready to implement PLGL for music generation?