-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsynth_narrative.py
More file actions
202 lines (160 loc) · 7.52 KB
/
synth_narrative.py
File metadata and controls
202 lines (160 loc) · 7.52 KB
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
"""
SYNTH_NARRATIVE - Narrative Process Classes
Laurie Spiegel-inspired process transformations for chess music
"""
import math
import numpy as np
from abc import ABC, abstractmethod
class NarrativeProcess(ABC):
"""
Abstract base class for Spiegel-style process transformations
Each process maintains state and evolves over the duration of the piece
"""
def __init__(self, total_duration: float, total_plies: int, rng=None):
self.total_duration = total_duration
self.total_plies = total_plies
self.current_time = 0.0
self.rng = rng if rng is not None else np.random.default_rng()
@abstractmethod
def update(self, current_time: float, key_moment=None) -> dict:
"""
Update process state and return transformation parameters
Args:
current_time: Current position in the composition (seconds/plies)
key_moment: Optional key moment occurring at this time
Returns:
dict: Transformation parameters to apply to synthesis
"""
pass
def get_progress(self) -> float:
"""Get normalized progress through the piece (0.0 to 1.0)"""
return min(1.0, self.current_time / self.total_duration)
class DefaultProcess(NarrativeProcess):
"""Default process that applies no transformations - preserves existing behavior"""
def update(self, current_time: float, key_moment=None) -> dict:
self.current_time = current_time
return {} # No transformations
class TumblingDefeatProcess(NarrativeProcess):
"""
Process for TUMBLING_DEFEAT: gradual deterioration through accumulated mistakes
Inspired by Spiegel's concept of decay and entropy
"""
def __init__(self, total_duration: float, total_plies: int, rng=None):
super().__init__(total_duration, total_plies, rng)
self.stability = 1.0 # Starts coherent
self.error_accumulation = 0.0
self.tempo_drift = 0.0
self.pitch_drift = 0.0
def update(self, current_time: float, key_moment=None) -> dict:
self.current_time = current_time
progress = self.get_progress()
# Each mistake accelerates the decay (compound effect)
if key_moment and key_moment.get('type') in ['MISTAKE', 'BLUNDER', 'INACCURACY']:
mistake_weight = {
'INACCURACY': 0.05,
'MISTAKE': 0.1,
'BLUNDER': 0.2
}.get(key_moment.get('type'), 0.1)
# Later mistakes have more impact (system already unstable)
self.error_accumulation += mistake_weight * (1 + progress)
# Stability decays based on accumulated errors and time
base_decay = progress * 0.3 # Natural decay over time
error_decay = self.error_accumulation * progress # Mistakes accelerate decay
self.stability = max(0.1, 1.0 - (base_decay + error_decay))
# Tempo becomes increasingly erratic
chaos_factor = (1 - self.stability) * 0.02
self.tempo_drift += self.rng.uniform(-chaos_factor, chaos_factor)
self.tempo_drift = max(-0.3, min(0.3, self.tempo_drift)) # Clamp
# Pitch drift increases over time
self.pitch_drift += self.rng.uniform(-0.5, 0.5) * (1 - self.stability)
return {
'pitch_drift_cents': self.pitch_drift * 20, # Up to 20 cents drift
'tempo_multiplier': 1.0 + self.tempo_drift,
'filter_stability': self.stability, # Affects filter consistency
'resonance_chaos': (1 - self.stability) * 0.5, # Add resonance variance
'note_duration_variance': (1 - self.stability) * 0.2, # Timing becomes erratic
'volume_decay': 1.0 - (progress * 0.3) # Gradual volume reduction
}
class AttackingMasterpieceProcess(NarrativeProcess):
"""
Process for ATTACKING_MASTERPIECE: building momentum toward triumph
Based on positive feedback loops and crescendo
"""
def __init__(self, total_duration: float, total_plies: int, rng=None):
super().__init__(total_duration, total_plies, rng)
self.momentum = 0.0
self.brilliance_factor = 0.0
def update(self, current_time: float, key_moment=None) -> dict:
self.current_time = current_time
progress = self.get_progress()
# Brilliant moves build momentum
if key_moment and key_moment.get('type') in ['BRILLIANT', 'STRONG']:
brilliance_weight = {
'STRONG': 0.15,
'BRILLIANT': 0.25
}.get(key_moment.get('type'), 0.15)
self.momentum += brilliance_weight
self.brilliance_factor += 0.1
# Natural crescendo curve (slow start, powerful finish)
natural_curve = progress ** 1.5 # Exponential growth
total_momentum = min(1.2, natural_curve + self.momentum * 0.5)
return {
'tempo_multiplier': 0.8 + total_momentum * 0.5, # Speed up
'filter_brightness': 0.3 + total_momentum * 0.7, # Open filters
'resonance_boost': total_momentum * 1.5, # More dramatic
'harmonic_density': 0.5 + total_momentum * 0.5, # Richer harmonies
'volume_crescendo': 0.7 + total_momentum * 0.3, # Build volume
'attack_sharpness': total_momentum # Crisper note attacks
}
class QuietPrecisionProcess(NarrativeProcess):
"""
Process for QUIET_PRECISION: equilibrium-seeking with gentle breathing
Based on homeostasis and natural oscillation
"""
def __init__(self, total_duration: float, total_plies: int, rng=None):
super().__init__(total_duration, total_plies, rng)
self.balance = 0.0
self.breathing_phase = 0.0
def update(self, current_time: float, key_moment=None) -> dict:
self.current_time = current_time
progress = self.get_progress()
# Small perturbations always return to center
if key_moment:
disturbance = 0.05 # Very small disturbances
else:
disturbance = 0.0
# Self-correcting process - always returns to balance
self.balance = self.balance * 0.95 + disturbance
# Gentle breathing pattern - slow oscillation
self.breathing_phase += 0.1
breath_cycle = math.sin(self.breathing_phase) * 0.08
return {
'tempo_regularity': 1.0, # Metronomic consistency
'filter_precision': 0.9 - abs(self.balance), # Very stable
'dynamic_breathing': breath_cycle, # Gentle volume waves
'pitch_stability': 0.95, # Minimal drift
'resonance_control': 0.3, # Tight, controlled
'harmonic_purity': 0.9 # Clean, simple harmonies
}
def create_narrative_process(narrative: str, duration: float, plies: int, rng=None) -> NarrativeProcess:
"""
Factory function to create appropriate process based on overall narrative
Args:
narrative: Overall narrative type (e.g., 'TUMBLING_DEFEAT')
duration: Total duration in seconds
plies: Total number of plies
rng: Optional numpy RNG for reproducibility
Returns:
NarrativeProcess instance
"""
process_map = {
'TUMBLING_DEFEAT': TumblingDefeatProcess,
'ATTACKING_MASTERPIECE': AttackingMasterpieceProcess,
'QUIET_PRECISION': QuietPrecisionProcess,
# Aliases for similar behaviors
'FIGHTING_DEFEAT': TumblingDefeatProcess,
'TACTICAL_MASTERPIECE': AttackingMasterpieceProcess,
'PEACEFUL_DRAW': QuietPrecisionProcess,
}
ProcessClass = process_map.get(narrative, DefaultProcess)
return ProcessClass(duration, plies, rng)