Comping
This commit is contained in:
@@ -6,6 +6,7 @@ import pathlib
|
||||
import unittest
|
||||
|
||||
import generator.accompaniment
|
||||
import generator.acoustic
|
||||
import generator.arrange.accompaniment
|
||||
import generator.comping
|
||||
import generator.comping_rhythm
|
||||
@@ -29,56 +30,64 @@ class LayeredComping(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.source = tests.solo_fixtures.song(count=6, chords=('C7', 'C7', 'FM7', 'FM7', 'Dm7', 'G7'))
|
||||
self.resources = generator.vocabulary.load()
|
||||
def test_candidates_preserve_all_counts_and_articulations(self):
|
||||
m = self.source['staves'][0]['measures'][0]
|
||||
for lane, expected in (('electric', {0, 1, 2}), ('bass', {1, 2}), ('upper', {1, 2, 3})):
|
||||
policy = generator.vocabulary.accompaniment('electric' if lane == 'electric' else 'acoustic', self.resources)
|
||||
candidates = generator.comping_rhythm.lane_candidates(m, m['harmonies'][0], lane, (), policy, 'test')
|
||||
self.assertEqual({len(c.events) for c in candidates}, expected)
|
||||
self.assertTrue(any(e.end - e.start <= fractions.Fraction(1, 2) for c in candidates for e in c.events) if lane != 'bass' else True)
|
||||
if lane == 'electric':
|
||||
self.assertTrue(any(e.end - e.start >= 2 for c in candidates for e in c.events))
|
||||
else:
|
||||
self.assertEqual({e.end - e.start for c in candidates for e in c.events}, {fractions.Fraction(1, 2), fractions.Fraction(1)})
|
||||
if lane == 'bass':
|
||||
self.assertTrue(all(c.events[0].start == 0 for c in candidates))
|
||||
def test_ranges_downbeats_and_overlap(self):
|
||||
canonical = generator.comping.Canonical(self.source, 'acoustic', 'light')
|
||||
carried = generator.accompaniment.carried_chords(self.source['staves'][0]['measures'])
|
||||
for i, (m, prior) in enumerate(zip(self.source['staves'][0]['measures'], carried)):
|
||||
result = canonical(m, prior, i)
|
||||
voices = {v['id']: v['notes'] for v in result['voices']}
|
||||
self.assertEqual(set(voices), {'bass', 'upper'})
|
||||
self.assertTrue(voices['bass'][0]['pitches'])
|
||||
self.assertEqual(generator.model.fraction(voices['bass'][0]['onset']), 0)
|
||||
for lane, high in (('bass', 2), ('upper', 3)):
|
||||
attacks = [n for n in voices[lane] if n['pitches'] and not any(p.get('tie_from') for p in n['pitches'])]
|
||||
self.assertTrue(1 <= len(attacks) <= high)
|
||||
self.assertTrue(any(fractions.Fraction(r['overlap_beats']) > 0 for r in canonical.rhythm_report))
|
||||
self.assertTrue(all(abs(sum(r['contributions'].values()) - r['selection_cost']) < 1e-10 for r in canonical.rhythm_report))
|
||||
def test_combined_crowding_favors_shared_attacks(self):
|
||||
m = self.source['staves'][0]['measures'][0]
|
||||
candidates = generator.comping_rhythm.candidates(m, None, 'acoustic', (), 'joint')
|
||||
self.assertTrue(any(dict(c.contributions)['combined_crowding'] == 0 for c in candidates))
|
||||
self.assertTrue(any(len({e.start for e in c.events}) < len(c.events) for c in candidates))
|
||||
shared = (generator.comping_rhythm.Attack('bass', fractions.Fraction(), fractions.Fraction(2)), generator.comping_rhythm.Attack('upper', fractions.Fraction(), fractions.Fraction(1)))
|
||||
separated = (shared[0], dataclasses.replace(shared[1], start=fractions.Fraction(1, 2)))
|
||||
self.assertLess(generator.comping_rhythm.crowding(shared), generator.comping_rhythm.crowding(separated))
|
||||
def test_harmonic_changes_share_one_measure_budget(self):
|
||||
m = copy.deepcopy(self.source['staves'][0]['measures'][0])
|
||||
m['harmonies'] = [{**generator.harmony.parse_symbol(s), 'id': str(i), 'onset': generator.model.rational(t)} for i, (s, t) in enumerate((('C7', 0), ('FM7', 1), ('G7', 2)))]
|
||||
def block(self, part, source=None, melody=()):
|
||||
source = source or self.source
|
||||
measures = source['staves'][0]['measures'][:2]
|
||||
inputs = tuple((m, prior, i) for i, (m, prior) in enumerate(zip(measures, generator.accompaniment.carried_chords(measures))))
|
||||
flat = generator.comping_rhythm.flatten(inputs)
|
||||
lengths = tuple(generator.model.fraction(m['duration']) for m in measures)
|
||||
return generator.comping_rhythm.candidates(flat, None, part, melody, 'test', lengths=lengths)
|
||||
def test_all_ten_families_are_reachable_and_roles_have_distinct_onsets(self):
|
||||
expected = {'charleston', 'shifted_charleston', 'forward_motion', 'anticipated_heavy_beats', 'syncopated_riff', 'quarters', 'dotted_spacing', 'shifting_motif', 'call_response', 'anticipated_four'}
|
||||
choices = {part: self.block(part) for part in ('acoustic', 'electric')}
|
||||
for part, candidates in choices.items():
|
||||
self.assertTrue(expected <= {dict(c.families)['pattern'] for c in candidates})
|
||||
for c in candidates:
|
||||
generator.comping_rhythm.validate_events(c.events, part)
|
||||
for start in (0, 4):
|
||||
self.assertLessEqual(sum(start <= e.start < start + 4 for e in c.events), 3 if part == 'acoustic' else 2)
|
||||
self.assertAlmostEqual(c.cost, sum(dict(c.contributions).values()))
|
||||
a = {tuple(e.start for e in c.events) for c in choices['acoustic']}
|
||||
e = {tuple(e.start for e in c.events) for c in choices['electric']}
|
||||
self.assertFalse(a & e)
|
||||
self.assertTrue(any(c.events[0].start > 0 for c in choices['acoustic']))
|
||||
self.assertTrue(any(c.events[0].lane == 'upper' for c in choices['acoustic']))
|
||||
self.assertTrue(any(e.end - e.start > 1 for c in choices['electric'] for e in c.events))
|
||||
def test_separate_short_attacks_and_only_synchronized_long_combinations(self):
|
||||
F = fractions.Fraction
|
||||
attack = generator.comping_rhythm.Attack
|
||||
generator.comping_rhythm.validate_events((attack('bass', F(0), F(1)), attack('upper', F(1), F(3, 2))), 'acoustic')
|
||||
generator.comping_rhythm.validate_events((attack('combined', F(0), F(3, 2)),), 'acoustic')
|
||||
for events in ((attack('combined', F(0), F(1)),), (attack('bass', F(0), F(1)), attack('upper', F(1, 2), F(1))), (attack('bass', F(0), F(2)),)):
|
||||
with self.assertRaises(ValueError):
|
||||
generator.comping_rhythm.validate_events(events, 'acoustic')
|
||||
def test_harmonic_changes_clip_direct_holds_without_extra_attacks(self):
|
||||
source = copy.deepcopy(self.source)
|
||||
for m in source['staves'][0]['measures'][:2]:
|
||||
m['harmonies'] = [{**generator.harmony.parse_symbol(s), 'id': str(i), 'onset': generator.model.rational(t)} for i, (s, t) in enumerate((('C7', 0), ('FM7', 1), ('G7', 2)))]
|
||||
for part in ('acoustic', 'electric'):
|
||||
choices = generator.comping_rhythm.candidates(m, None, part, (), 'changes')
|
||||
for c in choices:
|
||||
for lane, high in (('bass', 2), ('upper', 3), ('electric', 2)):
|
||||
count = sum(e.lane == lane for e in c.events)
|
||||
self.assertLessEqual(count, high + 1)
|
||||
if count > high:
|
||||
self.assertIn((lane, 'extra attack for harmonic changes'), c.exceptions)
|
||||
for c in self.block(part, source):
|
||||
self.assertLessEqual(len(c.events), 6 if part == 'acoustic' else 4)
|
||||
for e in c.events:
|
||||
self.assertLessEqual(e.end, 1 if e.start < 1 else 2 if e.start < 2 else 4)
|
||||
if part == 'electric':
|
||||
self.assertTrue(all(c.exceptions for c in choices))
|
||||
if e.harmonic_time is None:
|
||||
self.assertFalse(any(e.start < boundary < e.end for boundary in (1, 2, 4, 5, 6)))
|
||||
def test_anticipation_binds_exact_upcoming_chord_and_keeps_direct_alternative(self):
|
||||
source = tests.solo_fixtures.song(count=2, chords=('C7', 'FM7'))
|
||||
for part in ('acoustic', 'electric'):
|
||||
candidates = self.block(part, source)
|
||||
anticipated = [c for c in candidates if any(e.harmonic_time is not None for e in c.events)]
|
||||
self.assertTrue(anticipated)
|
||||
for c in anticipated:
|
||||
for e in c.events:
|
||||
if e.harmonic_time is not None:
|
||||
self.assertIn(e.lane, ('upper', 'electric'))
|
||||
self.assertEqual(e.harmonic_time - e.start, fractions.Fraction(1, 2))
|
||||
self.assertLess(e.harmonic_time, e.end)
|
||||
self.assertTrue(any(all(e.harmonic_time is None for e in c.events) for c in candidates))
|
||||
flat = generator.comping_rhythm.flatten(tuple((m, p, i) for i, (m, p) in enumerate(zip(source['staves'][0]['measures'], generator.accompaniment.carried_chords(source['staves'][0]['measures'])))))
|
||||
pattern = next(p for p in self.resources['accompaniment']['instruments']['electric']['patterns'] if p['id'] == 'electric.forward_motion.statement')
|
||||
events, _ = generator.comping_rhythm.fit(pattern, flat, None, 'electric', anticipate=True, limit=fractions.Fraction(4))
|
||||
self.assertTrue(all(e.harmonic_time is None for e in events))
|
||||
def test_no_chord_is_silent_and_partial_measure_is_bounded(self):
|
||||
m = copy.deepcopy(self.source['staves'][0]['measures'][0])
|
||||
m['harmonies'][0]['kind'] = 'none'
|
||||
@@ -86,23 +95,21 @@ class LayeredComping(unittest.TestCase):
|
||||
m['harmonies'][0] = {**generator.harmony.parse_symbol('C7'), 'id': 'chord', 'onset': generator.model.rational(0)}
|
||||
m['duration'] = generator.model.rational(fractions.Fraction(1, 2))
|
||||
self.assertTrue(all(0 <= e.start < e.end <= fractions.Fraction(1, 2) for c in generator.comping_rhythm.candidates(m, None, 'acoustic', (), 'pickup') for e in c.events))
|
||||
def test_two_other_meters_preserve_full_bar_bounds(self):
|
||||
def test_two_other_meters_use_authored_patterns(self):
|
||||
for meter in (3, 5):
|
||||
source = tests.solo_fixtures.song(meter=meter, count=2, chords=('C7',))
|
||||
for part in ('acoustic', 'electric'):
|
||||
canonical = generator.comping.Canonical(source, part, 'meter')
|
||||
self.assertTrue(all(str(meter) + '_beat' in r.pattern_id for r in canonical.block_rhythms.values()))
|
||||
for rhythm in canonical.rhythms.values():
|
||||
self.assertTrue(all(0 <= e.start < e.end <= meter for e in rhythm.events))
|
||||
def test_melody_changes_the_objective_without_changing_seed(self):
|
||||
e = generator.comping_rhythm.Attack('electric', fractions.Fraction(1), fractions.Fraction(3, 2))
|
||||
m = self.source['staves'][0]['measures'][0]
|
||||
harmony = generator.comping_rhythm.spans(m, None)
|
||||
busy = tuple((fractions.Fraction(i), fractions.Fraction(i + 1)) for i in range(4))
|
||||
rest = ((fractions.Fraction(), fractions.Fraction(1, 2)),)
|
||||
first = dict(generator.comping_rhythm.lane_score((e,), 'electric', busy, harmony, fractions.Fraction(4)))
|
||||
second = dict(generator.comping_rhythm.lane_score((e,), 'electric', rest, harmony, fractions.Fraction(4)))
|
||||
self.assertGreater(first['melody_support'], second['melody_support'])
|
||||
self.assertNotEqual(generator.comping_rhythm.candidates(m, None, 'electric', busy, 'same'), generator.comping_rhythm.candidates(m, None, 'electric', rest, 'same'))
|
||||
busy = tuple((fractions.Fraction(i), fractions.Fraction(i + 1)) for i in range(8))
|
||||
space = ((fractions.Fraction(), fractions.Fraction(1, 2)),)
|
||||
first = {c.pattern_id: dict(c.contributions) for c in self.block('electric', melody=busy)}
|
||||
second = {c.pattern_id: dict(c.contributions) for c in self.block('electric', melody=space)}
|
||||
common = first.keys() & second.keys()
|
||||
self.assertTrue(any(first[k]['melody_support'] != second[k]['melody_support'] for k in common))
|
||||
def test_tempo_changes_metadata_but_not_accompaniment_music(self):
|
||||
changed = copy.deepcopy(self.source)
|
||||
changed['metadata']['tempo'] = 240
|
||||
@@ -144,24 +151,39 @@ class LayeredComping(unittest.TestCase):
|
||||
m['voices'] = [{'id': str(i), 'notes': [generator.model.make_event(str(i), fractions.Fraction(i), fractions.Fraction(2), 'note', [p])]} for i, p in enumerate(pitches)]
|
||||
with self.assertRaisesRegex(ValueError, 'overlapping guitar voices'):
|
||||
generator.fretboard.validate_document(doc)
|
||||
def test_sustained_join_is_one_attack_and_keeps_position(self):
|
||||
def test_explicit_cross_bar_hold_survives_projection_without_reattack(self):
|
||||
source = tests.solo_fixtures.song(count=2, chords=('C7',))
|
||||
canonical = generator.comping.Canonical(source, 'electric', 'tie')
|
||||
inputs = [(m, p, i) for i, (m, p) in enumerate(zip(source['staves'][0]['measures'], generator.accompaniment.carried_chords(source['staves'][0]['measures'])))]
|
||||
chord = inputs[0][0]['harmonies'][0]
|
||||
grip = generator.comping.voicings(generator.comping.semantic_key(chord), 'electric')[0]
|
||||
for m, p, i in inputs:
|
||||
note = generator.model.make_event('held' + str(i), fractions.Fraction(), fractions.Fraction(4), 'note', generator.fretboard.chord_pitches(chord, grip))
|
||||
note['voice'] = 'electric'
|
||||
canonical.cache[canonical.key(m, p, i)]['voices'] = [{'id': 'electric', 'notes': [note]}]
|
||||
canonical._rhythm_reports[canonical.key(m, p, i)]['layers']['electric'].update(attacks=1, tied_continuations=0)
|
||||
canonical.connect_holds(inputs)
|
||||
a, b = [canonical.cache[canonical.key(*args)]['voices'][0]['notes'][0] for args in inputs]
|
||||
for p, q in zip(a['pitches'], b['pitches']):
|
||||
self.assertEqual(p['tie_to'], q['id'])
|
||||
self.assertEqual(q['tie_from'], p['id'])
|
||||
self.assertEqual(p['tablature'], q['tablature'])
|
||||
self.assertEqual(canonical.rhythm_report[1]['layers']['electric']['attacks'], 0)
|
||||
for part, pattern_id in (('electric', 'electric.forward_motion.statement'), ('acoustic', 'acoustic.call_response.held_answer')):
|
||||
resources = copy.deepcopy(self.resources)
|
||||
patterns = resources['accompaniment']['instruments'][part]['patterns']
|
||||
resources['accompaniment']['instruments'][part]['patterns'] = [p for p in patterns if p['id'] == pattern_id]
|
||||
with generator.vocabulary.using(resources, part + '_accompaniment'):
|
||||
canonical = generator.comping.Canonical(source, part, 'held')
|
||||
measures = source['staves'][0]['measures']
|
||||
rendered = [canonical(m, p, i) for i, (m, p) in enumerate(zip(measures, generator.accompaniment.carried_chords(measures)))]
|
||||
a = [p for n in generator.score.measure_notes(rendered[0]) for p in n['pitches'] if p.get('tie_to')]
|
||||
b = {p['id']: p for n in generator.score.measure_notes(rendered[1]) for p in n['pitches']}
|
||||
cross = [p for p in a if p['tie_to'] in b]
|
||||
self.assertTrue(cross)
|
||||
for p in cross:
|
||||
self.assertEqual(p['tablature'], b[p['tie_to']]['tablature'])
|
||||
self.assertEqual(p['id'], b[p['tie_to']]['tie_from'])
|
||||
if part == 'acoustic':
|
||||
document = copy.deepcopy(source)
|
||||
document['staves'][0]['measures'] = rendered
|
||||
generator.acoustic.validate_comping(document)
|
||||
def test_separate_repeated_attacks_are_never_joined(self):
|
||||
source = tests.solo_fixtures.song(count=2, chords=('C7',))
|
||||
resources = copy.deepcopy(self.resources)
|
||||
resources['accompaniment']['instruments']['electric']['patterns'] = [{'id': 'test.repeated', 'family': 'quarters', 'lengths': ['4', '4'],
|
||||
'events': [{'onset': str(t), 'duration': '4', 'layer': 'electric'} for t in (0, 4)]}]
|
||||
with generator.vocabulary.using(resources, 'electric_accompaniment'):
|
||||
canonical = generator.comping.Canonical(source, 'electric', 'repeat')
|
||||
measures = source['staves'][0]['measures']
|
||||
rendered = [canonical(m, p, i) for i, (m, p) in enumerate(zip(measures, generator.accompaniment.carried_chords(measures)))]
|
||||
notes = [n for m in rendered for n in generator.score.measure_notes(m) if n['pitches']]
|
||||
self.assertEqual(len(notes), 2)
|
||||
self.assertTrue(all(not p.get('tie_from') and not p.get('tie_to') for n in notes for p in n['pitches']))
|
||||
|
||||
class SoloRhythmConditioning(unittest.TestCase):
|
||||
def test_melody_rhythm_and_progression_change_learned_probabilities(self):
|
||||
|
||||
Reference in New Issue
Block a user