2026-03-16 00:39:32 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
"""
|
|
|
|
|
Path and PathStep classes for storing path state from PathFinder.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
from dataclasses import dataclass, field
|
2026-03-16 16:53:22 +01:00
|
|
|
from typing import Any
|
2026-03-16 00:39:32 +01:00
|
|
|
|
|
|
|
|
from .pitch import Pitch
|
|
|
|
|
from .chord import Chord
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
|
class PathStep:
|
2026-03-16 14:00:10 +01:00
|
|
|
"""Stores data for a single step (edge) in the path."""
|
2026-03-16 00:39:32 +01:00
|
|
|
|
2026-03-16 14:00:10 +01:00
|
|
|
source_node: Chord
|
|
|
|
|
destination_node: Chord
|
|
|
|
|
source_chord: Chord
|
|
|
|
|
destination_chord: Chord
|
2026-03-16 00:39:32 +01:00
|
|
|
transposition: Pitch | None = None
|
|
|
|
|
movements: dict[int, int] = field(default_factory=dict)
|
|
|
|
|
scores: dict[str, float] = field(default_factory=dict)
|
2026-03-16 16:53:22 +01:00
|
|
|
weight: float = 0.0 # computed later by _compute_weights
|
2026-03-16 17:35:07 +01:00
|
|
|
last_visited_counts_before: dict | None = None
|
|
|
|
|
last_visited_counts_after: dict | None = None
|
|
|
|
|
sustain_counts_before: tuple[int, ...] | None = None
|
|
|
|
|
sustain_counts_after: tuple[int, ...] | None = None
|
2026-03-16 00:39:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class Path:
|
|
|
|
|
"""Stores the complete state of a generated path."""
|
|
|
|
|
|
|
|
|
|
def __init__(
|
2026-03-16 01:11:15 +01:00
|
|
|
self, initial_chord: Chord | None, weights_config: dict[str, Any] | None = None
|
2026-03-16 00:39:32 +01:00
|
|
|
):
|
|
|
|
|
self.initial_chord = initial_chord
|
|
|
|
|
self.steps: list[PathStep] = []
|
|
|
|
|
self.weights_config = weights_config if weights_config is not None else {}
|
|
|
|
|
|
2026-03-16 14:00:10 +01:00
|
|
|
# State needed for step computation
|
2026-03-16 01:11:15 +01:00
|
|
|
self._voice_map: list[int] = [] # which voice is at each position
|
|
|
|
|
self._cumulative_trans: Pitch | None = None # cumulative transposition
|
2026-03-16 14:00:10 +01:00
|
|
|
self._graph_nodes: set = set() # all graph nodes for visit tracking
|
|
|
|
|
self._num_voices: int = 0 # number of voices
|
2026-03-16 01:11:15 +01:00
|
|
|
|
|
|
|
|
def init_state(
|
|
|
|
|
self, graph_nodes: set, num_voices: int, initial_chord: Chord
|
2026-03-16 00:39:32 +01:00
|
|
|
) -> None:
|
2026-03-16 01:11:15 +01:00
|
|
|
"""Initialize state after graph is known."""
|
2026-03-16 14:00:10 +01:00
|
|
|
self._graph_nodes = graph_nodes
|
|
|
|
|
self._num_voices = num_voices
|
2026-03-16 01:11:15 +01:00
|
|
|
self._voice_map = list(range(num_voices)) # voice i at position i
|
|
|
|
|
|
|
|
|
|
dims = initial_chord.dims
|
|
|
|
|
self._cumulative_trans = Pitch(tuple(0 for _ in range(len(dims))), dims)
|
|
|
|
|
|
2026-03-16 14:00:10 +01:00
|
|
|
def _get_last_visited_counts(self) -> dict:
|
|
|
|
|
"""Get last visited counts from the last step, or initialize fresh."""
|
|
|
|
|
if self.steps:
|
|
|
|
|
last_step = self.steps[-1]
|
2026-03-16 17:35:07 +01:00
|
|
|
if last_step.last_visited_counts_after is not None:
|
|
|
|
|
return dict(last_step.last_visited_counts_after)
|
2026-03-16 14:00:10 +01:00
|
|
|
|
|
|
|
|
# Initialize fresh: all nodes start at 0 (except initial which we set to 0 explicitly)
|
|
|
|
|
return {node: 0 for node in self._graph_nodes}
|
|
|
|
|
|
|
|
|
|
def _get_sustain_counts(self) -> tuple:
|
|
|
|
|
"""Get sustain counts from the last step, or initialize fresh."""
|
|
|
|
|
if self.steps:
|
|
|
|
|
last_step = self.steps[-1]
|
2026-03-16 17:35:07 +01:00
|
|
|
if last_step.sustain_counts_after is not None:
|
|
|
|
|
return last_step.sustain_counts_after
|
2026-03-16 14:00:10 +01:00
|
|
|
|
|
|
|
|
# Initialize fresh: all voices start at 0
|
|
|
|
|
return tuple(0 for _ in range(self._num_voices))
|
|
|
|
|
|
2026-03-16 16:53:22 +01:00
|
|
|
def step(self, step: PathStep) -> PathStep:
|
|
|
|
|
"""Add a completed step to the path.
|
2026-03-16 01:11:15 +01:00
|
|
|
|
2026-03-16 16:53:22 +01:00
|
|
|
Takes a PathStep (computed as a hypothetical step), updates internal state,
|
|
|
|
|
and adds it to the path.
|
2026-03-16 01:11:15 +01:00
|
|
|
"""
|
|
|
|
|
# Update cumulative transposition
|
2026-03-16 16:53:22 +01:00
|
|
|
if step.transposition is not None:
|
|
|
|
|
self._cumulative_trans = self._cumulative_trans.transpose(
|
|
|
|
|
step.transposition
|
|
|
|
|
)
|
2026-03-16 01:11:15 +01:00
|
|
|
|
|
|
|
|
# Update voice map based on movement
|
|
|
|
|
new_voice_map = [None] * len(self._voice_map)
|
2026-03-16 16:53:22 +01:00
|
|
|
for src_idx, dest_idx in step.movements.items():
|
2026-03-16 01:11:15 +01:00
|
|
|
new_voice_map[dest_idx] = self._voice_map[src_idx]
|
|
|
|
|
self._voice_map = new_voice_map
|
|
|
|
|
|
2026-03-16 14:00:10 +01:00
|
|
|
# Get BEFORE state from last step (or initialize fresh)
|
|
|
|
|
last_visited_before = self._get_last_visited_counts()
|
|
|
|
|
sustain_before = self._get_sustain_counts()
|
|
|
|
|
|
|
|
|
|
# Compute AFTER state
|
|
|
|
|
last_visited_after = dict(last_visited_before)
|
|
|
|
|
for node in last_visited_after:
|
|
|
|
|
last_visited_after[node] += 1
|
2026-03-16 16:53:22 +01:00
|
|
|
last_visited_after[step.destination_node] = 0
|
2026-03-16 14:00:10 +01:00
|
|
|
|
|
|
|
|
sustain_after = list(sustain_before)
|
|
|
|
|
for voice_idx in range(len(sustain_after)):
|
2026-03-16 16:53:22 +01:00
|
|
|
curr_cents = step.source_chord.pitches[voice_idx].to_cents()
|
|
|
|
|
next_cents = step.destination_chord.pitches[voice_idx].to_cents()
|
2026-03-16 02:27:21 +01:00
|
|
|
if curr_cents == next_cents:
|
2026-03-16 14:00:10 +01:00
|
|
|
sustain_after[voice_idx] += 1
|
2026-03-16 01:11:15 +01:00
|
|
|
else:
|
2026-03-16 14:00:10 +01:00
|
|
|
sustain_after[voice_idx] = 0
|
2026-03-16 01:11:15 +01:00
|
|
|
|
2026-03-16 16:53:22 +01:00
|
|
|
# Update step with computed state
|
2026-03-16 17:35:07 +01:00
|
|
|
step.last_visited_counts_before = last_visited_before
|
|
|
|
|
step.last_visited_counts_after = last_visited_after
|
|
|
|
|
step.sustain_counts_before = sustain_before
|
|
|
|
|
step.sustain_counts_after = tuple(sustain_after)
|
2026-03-16 01:11:15 +01:00
|
|
|
|
2026-03-16 00:39:32 +01:00
|
|
|
self.steps.append(step)
|
2026-03-16 01:11:15 +01:00
|
|
|
return step
|
2026-03-16 00:39:32 +01:00
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def graph_chords(self) -> list[Chord]:
|
2026-03-16 14:00:10 +01:00
|
|
|
"""Get list of destination graph nodes."""
|
|
|
|
|
return [self.initial_chord] + [step.destination_node for step in self.steps]
|
2026-03-16 00:39:32 +01:00
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def output_chords(self) -> list[Chord]:
|
2026-03-16 14:00:10 +01:00
|
|
|
"""Get list of destination chords (transposed)."""
|
|
|
|
|
return [self.initial_chord] + [step.destination_chord for step in self.steps]
|
2026-03-16 00:39:32 +01:00
|
|
|
|
|
|
|
|
def __len__(self) -> int:
|
|
|
|
|
"""Total number of chords in path."""
|
|
|
|
|
return len(self.steps) + 1
|
|
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
|
"""Iterate over output chords."""
|
|
|
|
|
return iter(self.output_chords)
|
|
|
|
|
|
2026-03-16 14:00:10 +01:00
|
|
|
def get_influence(self, weights: dict[str, Any]) -> dict[str, float]:
|
|
|
|
|
"""Compute weighted score contribution per factor for chosen candidates.
|
|
|
|
|
|
|
|
|
|
Returns a dict mapping factor name to accumulated influence (weight * score)
|
|
|
|
|
for all steps in the path.
|
|
|
|
|
"""
|
|
|
|
|
influence = {
|
|
|
|
|
"melodic": 0.0,
|
|
|
|
|
"contrary_motion": 0.0,
|
|
|
|
|
"dca_hamiltonian": 0.0,
|
|
|
|
|
"dca_voice_movement": 0.0,
|
|
|
|
|
"target_range": 0.0,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for step in self.steps:
|
|
|
|
|
scores = step.scores
|
|
|
|
|
w_melodic = weights.get("weight_melodic", 1)
|
|
|
|
|
w_contrary = weights.get("weight_contrary_motion", 0)
|
|
|
|
|
w_hamiltonian = weights.get("weight_dca_hamiltonian", 1)
|
|
|
|
|
w_dca = weights.get("weight_dca_voice_movement", 1)
|
|
|
|
|
w_target = weights.get("weight_target_range", 1)
|
|
|
|
|
|
|
|
|
|
influence["melodic"] += scores.get("melodic_threshold", 0) * w_melodic
|
|
|
|
|
influence["contrary_motion"] += (
|
|
|
|
|
scores.get("contrary_motion", 0) * w_contrary
|
|
|
|
|
)
|
|
|
|
|
influence["dca_hamiltonian"] += (
|
|
|
|
|
scores.get("dca_hamiltonian", 0) * w_hamiltonian
|
|
|
|
|
)
|
|
|
|
|
influence["dca_voice_movement"] += (
|
|
|
|
|
scores.get("dca_voice_movement", 0) * w_dca
|
|
|
|
|
)
|
|
|
|
|
influence["target_range"] += scores.get("target_range", 0) * w_target
|
|
|
|
|
|
|
|
|
|
return influence
|