Spaces:
Sleeping
Sleeping
File size: 9,851 Bytes
5d29a69 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | """
Emotional Context Engine for Harmonic Catalyst
Version: 1.1.0 (Phase 1 - Wired to Generation)
"""
from dataclasses import dataclass
from typing import List
@dataclass
class EmotionalContext:
"""Stores emotional parameters for a section"""
energy: float = 0.5
density: str = 'Medium'
role: str = 'Support'
movement: str = 'Flowing'
# Section types for dropdown
SECTION_TYPES = [
'Intro',
'Verse',
'Pre-Chorus',
'Chorus',
'Post-Chorus',
'Bridge',
'Breakdown',
'Drop',
'Outro',
'Custom'
]
# Sections that get numbered
NUMBERED_SECTIONS = ['Verse', 'Pre-Chorus', 'Chorus', 'Post-Chorus', 'Drop']
# Sections that stay single (no number)
SINGLE_SECTIONS = ['Intro', 'Bridge', 'Breakdown', 'Outro']
# Emotional presets
EMOTIONAL_PRESETS = {
"Sparse Intro": {
'energy': 0.1,
'density': 'Sparse',
'role': 'Ambient',
'movement': 'Static',
'description': 'Minimal, atmospheric opening'
},
"Supportive Verse": {
'energy': 0.4,
'density': 'Medium',
'role': 'Support',
'movement': 'Flowing',
'description': 'Behind vocals, smooth transitions'
},
"Building Pre-Chorus": {
'energy': 0.6,
'density': 'Medium',
'role': 'Lead',
'movement': 'Flowing',
'description': 'Increasing tension toward chorus'
},
"Explosive Chorus": {
'energy': 0.9,
'density': 'Thick',
'role': 'Lead',
'movement': 'Agitated',
'description': 'Maximum impact and fullness'
},
"Breakdown Bridge": {
'energy': 0.3,
'density': 'Sparse',
'role': 'Ambient',
'movement': 'Static',
'description': 'Stripped-down, suspended feel'
},
"Resolved Outro": {
'energy': 0.2,
'density': 'Sparse',
'role': 'Ambient',
'movement': 'Static',
'description': 'Peaceful, conclusive ending'
}
}
# Default preset for each section type
SECTION_TYPE_DEFAULTS = {
'Intro': 'Sparse Intro',
'Verse': 'Supportive Verse',
'Pre-Chorus': 'Building Pre-Chorus',
'Chorus': 'Explosive Chorus',
'Post-Chorus': 'Explosive Chorus',
'Bridge': 'Breakdown Bridge',
'Breakdown': 'Breakdown Bridge',
'Drop': 'Explosive Chorus',
'Outro': 'Resolved Outro',
'Custom': 'Supportive Verse'
}
class SectionNamer:
"""Handles auto-numbering of sections"""
@staticmethod
def generate_name(section_type: str, existing_sections: List[dict]) -> str:
if section_type == 'Custom':
return 'Custom Section'
if section_type in SINGLE_SECTIONS:
return section_type
if section_type in NUMBERED_SECTIONS:
count = sum(
1 for s in existing_sections
if s.get('section_type') == section_type
)
return f"{section_type} {count + 1}"
return section_type
@staticmethod
def get_default_preset(section_type: str) -> str:
return SECTION_TYPE_DEFAULTS.get(section_type, 'Supportive Verse')
class EmotionalAdapter:
"""Adapts voicings based on emotional context"""
@staticmethod
def adapt_solo_piano(lh: List[int], rh: List[int], emotional_ctx: EmotionalContext) -> tuple:
"""
Adapt voicing for solo piano performance.
Args:
lh: Left hand MIDI notes
rh: Right hand MIDI notes
emotional_ctx: Emotional parameters
Returns:
Tuple of (adapted_lh, adapted_rh)
"""
lh = list(lh) if lh else []
rh = list(rh) if rh else []
# βββββββββββββββββββββββββββββββββββββββββββββββββββββ
# ENERGY ADAPTATION
# βββββββββββββββββββββββββββββββββββββββββββββββββββββ
if emotional_ctx.energy < 0.3:
# Low energy: Quieter, thinner
if len(rh) > 3:
rh = rh[:3]
if len(lh) > 1:
lh = lh[:1]
elif emotional_ctx.energy > 0.7:
# High energy: Fuller, doubled
if lh and len(lh) < 3:
lh.append(lh[0] + 12) # Add octave
if rh and len(rh) < 6:
rh.append(rh[0] + 12) # Add octave doubling
# βββββββββββββββββββββββββββββββββββββββββββββββββββββ
# DENSITY ADAPTATION
# βββββββββββββββββββββββββββββββββββββββββββββββββββββ
if emotional_ctx.density == 'Sparse':
lh = lh[:1] if lh else []
rh = rh[:2] if len(rh) > 2 else rh
elif emotional_ctx.density == 'Thick':
if lh and len(lh) < 2:
lh.append(lh[0] + 12)
if rh and len(rh) < 5:
lowest = min(rh) if rh else 60
rh.append(lowest + 12)
if len(rh) < 6 and rh:
highest = max(rh)
rh.append(highest + 12)
# Clean up
lh = sorted(list(set(lh))) if lh else []
rh = sorted(list(set(rh))) if rh else []
return lh, rh
@staticmethod
def adapt_arrangement(lh: List[int], rh: List[int], emotional_ctx: EmotionalContext) -> tuple:
"""
Adapt voicing for full band arrangement.
Args:
lh: Left hand (Bass Part) MIDI notes
rh: Right hand (Chord Part) MIDI notes
emotional_ctx: Emotional parameters
Returns:
Tuple of (adapted_lh, adapted_rh)
"""
lh = list(lh) if lh else []
rh = list(rh) if rh else []
# βββββββββββββββββββββββββββββββββββββββββββββββββββββ
# ENERGY ADAPTATION
# βββββββββββββββββββββββββββββββββββββββββββββββββββββ
if emotional_ctx.energy < 0.3:
# Low energy: Minimal arrangement
lh = lh[:1] if lh else []
rh = rh[:2] if len(rh) > 2 else rh
elif emotional_ctx.energy > 0.7:
# High energy: Full, spread arrangement
if lh:
root = lh[0]
if len(lh) < 3:
lh = [root, root + 12, root + 19] # Root, octave, 12th
if rh and len(rh) < 6:
rh_sorted = sorted(rh)
rh.append(rh_sorted[0] + 12)
if len(rh_sorted) > 1:
rh.append(rh_sorted[1] + 12)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββ
# ROLE ADAPTATION
# βββββββββββββββββββββββββββββββββββββββββββββββββββββ
if emotional_ctx.role == 'Support':
# Stay out of vocal range (D4 to B4 = MIDI 62-71)
vocal_zone = range(62, 72)
rh = [n + 12 if n in vocal_zone else n for n in rh]
elif emotional_ctx.role == 'Ambient':
# Very high or very low
if emotional_ctx.energy < 0.5:
# Quiet ambient = high shimmer
if rh:
rh = [n + 24 for n in rh[:2]]
lh = lh[:1] if lh else []
else:
# Louder ambient = deep
rh = rh[:3] if len(rh) > 3 else rh
# βββββββββββββββββββββββββββββββββββββββββββββββββββββ
# DENSITY ADAPTATION
# βββββββββββββββββββββββββββββββββββββββββββββββββββββ
if emotional_ctx.density == 'Sparse':
lh = lh[:1] if lh else []
rh = rh[:2] if len(rh) > 2 else rh
elif emotional_ctx.density == 'Thick':
if lh and len(lh) < 3:
lh.append(lh[0] + 12)
if rh and len(rh) < 6:
lowest = min(rh) if rh else 60
highest = max(rh) if rh else 72
rh.append(lowest + 12)
rh.append(highest + 12)
# Clean up
lh = sorted(list(set(lh))) if lh else []
rh = sorted(list(set(rh))) if rh else []
return lh, rh
@staticmethod
def get_voice_leading_settings(movement: str) -> dict:
"""Returns voice leading settings based on movement style"""
settings = {
'Static': {
'enabled': False,
'max_movement': 24
},
'Flowing': {
'enabled': True,
'max_movement': 5
},
'Agitated': {
'enabled': True,
'max_movement': 12
}
}
return settings.get(movement, settings['Flowing'])
def get_section_types():
"""Returns list of section types"""
return SECTION_TYPES.copy()
|