151 lines
9.3 KiB
Python
151 lines
9.3 KiB
Python
"""Accompaniment geometry and whole-form movement, using authored small cases."""
|
|
import copy
|
|
import fractions
|
|
import itertools
|
|
import pathlib
|
|
import tempfile
|
|
import unittest
|
|
|
|
import generator.accompaniment
|
|
import generator.comping
|
|
import generator.fretboard
|
|
import generator.guitar
|
|
import generator.harmony
|
|
import generator.model
|
|
import generator.production
|
|
import generator.score
|
|
import generator.vocabulary
|
|
import tests.solo_fixtures
|
|
|
|
def options(symbol, part):
|
|
return generator.comping.voicings(generator.comping.semantic_key(generator.harmony.parse_symbol(symbol)), part)
|
|
|
|
def path_cost(path, costs, part, left=(), right=(), close=False):
|
|
total = [sum(costs[i][v][j] for i, v in enumerate(path)) for j in range(5)]
|
|
total[2] += sum(generator.comping.movement(part, a, b) for a, b in zip((left, *path), path))
|
|
total[2] += generator.comping.movement(part, path[-1], path[0] if close else right)
|
|
return tuple(total)
|
|
|
|
class CompingVoicings(unittest.TestCase):
|
|
def test_acoustic_geometry_complete_triads_and_component_fret_limits(self):
|
|
for symbol in ('C', 'Cm', 'F#', 'Bbm', 'C7', 'Cm6', 'Em7b5', 'A7b9', 'C7/E', 'C/D'):
|
|
with self.subTest(chord=symbol):
|
|
chord = generator.harmony.parse_symbol(symbol)
|
|
required = generator.comping.requirements(chord, 'acoustic')
|
|
for grip in options(symbol, 'acoustic'):
|
|
self.assertIn(tuple(p.string_number for p in grip), ((5, 4, 3, 2), (6, 4, 3, 2)))
|
|
self.assertLessEqual(grip.bass.fret, 12)
|
|
self.assertTrue(all(1 <= p.fret <= 20 for p in grip))
|
|
self.assertTrue(generator.fretboard.has_fret_anchor(grip[1:]))
|
|
self.assertTrue(generator.fretboard.within_fret_span(grip))
|
|
self.assertEqual(grip.bass.midi, min(p.midi for p in grip))
|
|
self.assertTrue(required.covers({p.midi % 12 for p in grip}))
|
|
self.assertTrue(generator.fretboard.playable_attack((p, fractions.Fraction(1)) for p in grip))
|
|
if symbol in ('C', 'Cm', 'F#', 'Bbm'):
|
|
self.assertEqual({p.midi % 12 for p in grip}, set(required.allowed))
|
|
def test_bass_thirds_only_for_plain_major_and_minor(self):
|
|
for symbol, expected in (('C', {0, 4, 7}), ('Cm', {0, 3, 7}), ('C7', {0, 7}), ('Cm6', {0, 7}), ('Em7b5', {4, 10})):
|
|
with self.subTest(chord=symbol):
|
|
required = generator.comping.requirements(generator.harmony.parse_symbol(symbol), 'acoustic')
|
|
self.assertEqual(set(required.basses), expected)
|
|
self.assertEqual({v.bass.midi % 12 for v in options(symbol, 'acoustic')}, expected)
|
|
self.assertEqual(set(generator.comping.requirements(generator.harmony.parse_symbol('Cdim'), 'acoustic').basses), {0, 6})
|
|
def test_explicit_slash_or_inversion_overrides_default_bass(self):
|
|
inversion = generator.harmony.parse_symbol('C7')
|
|
inversion['inversion'] = 1
|
|
for chord, expected in ((generator.harmony.parse_symbol('C7/E'), 4), (generator.harmony.parse_symbol('C/D'), 2), (inversion, 4)):
|
|
self.assertEqual(generator.comping.requirements(chord, 'acoustic').basses, (expected,))
|
|
grips = generator.comping.voicings(generator.comping.semantic_key(chord), 'acoustic')
|
|
self.assertEqual({v.bass.midi % 12 for v in grips}, {expected})
|
|
def test_electric_uses_melody_voicing_families_and_chord_semantics(self):
|
|
for symbol in ('C', 'Cm', 'C7', 'CmM7', 'Csus', 'Cdim7', 'Caug7', 'C7b9', 'C13', 'C/E'):
|
|
with self.subTest(chord=symbol):
|
|
required = generator.fretboard.chord_requirements(generator.harmony.parse_symbol(symbol), respect_bass=False)
|
|
melody = generator.guitar.chord_positions(required, profile='electric', prune=False)
|
|
self.assertEqual(set(options(symbol, 'electric')), set(melody))
|
|
for grip in options(symbol, 'electric'):
|
|
self.assertIn(tuple(p.string_number for p in grip), generator.guitar.ELECTRIC_STRINGS)
|
|
self.assertTrue(required.covers({p.midi % 12 for p in grip}))
|
|
# Bass responsibility belongs to the acoustic voicing, as in electric melody.
|
|
self.assertEqual(options('C/E', 'electric'), options('C', 'electric'))
|
|
def test_impossible_grip_reports_source_location_without_relaxation(self):
|
|
source = tests.solo_fixtures.song(count=1, chords=('C7b9#11',))
|
|
for part in ('acoustic', 'electric'):
|
|
with self.subTest(part=part), self.assertRaisesRegex(ValueError, 'source measure 1, beat 1: C7b9#11: no legal'):
|
|
generator.comping.Canonical(source, part, 'test')
|
|
def test_complete_path_beats_greedy_and_matches_exhaustive_search(self):
|
|
for part in ('acoustic', 'electric'):
|
|
with self.subTest(part=part):
|
|
chords = ('C', 'C', 'Dm7')
|
|
layers = tuple(options(s, part) for s in chords)
|
|
costs = tuple({v: generator.comping.local_cost(part, generator.comping.semantic_key(generator.harmony.parse_symbol(s)), v, 55) for v in layer} for s, layer in zip(chords, layers))
|
|
path, cost = generator.comping.select_grips(layers, costs, part, 'test')
|
|
greedy = []
|
|
for i, layer in enumerate(layers):
|
|
greedy.append(min(layer, key=lambda v: (path_cost((*greedy, v), costs[:i + 1], part), v)))
|
|
self.assertLess(cost, path_cost(greedy, costs, part))
|
|
self.assertEqual(cost, min(path_cost(p, costs, part) for p in itertools.product(*layers)))
|
|
self.assertEqual(cost, path_cost(path, costs, part))
|
|
self.assertEqual((path, cost), generator.comping.select_grips(tuple(tuple(reversed(l)) for l in layers), costs, part, 'test'))
|
|
def test_repeat_join_and_neighbor_boundaries_participate_in_selection(self):
|
|
for part in ('acoustic', 'electric'):
|
|
layers = tuple(options(s, part) for s in ('C', 'Dm7', 'G7'))
|
|
costs = tuple({v: generator.comping.VoicingCost(generator.fretboard.span_penalty(v)) for v in layer} for layer in layers)
|
|
for endpoints in ({'close': True}, {'left': options('Am7', part)[0], 'right': options('FM7', part)[-1]}):
|
|
with self.subTest(part=part, endpoints=endpoints):
|
|
path, cost = generator.comping.select_grips(layers, costs, part, 'test', **endpoints)
|
|
self.assertEqual(cost, min(path_cost(p, costs, part, **endpoints) for p in itertools.product(*layers)))
|
|
self.assertEqual(cost, path_cost(path, costs, part, **endpoints))
|
|
def test_rest_does_not_reset_the_previous_hand_position(self):
|
|
source = tests.solo_fixtures.song(count=3, chords=('C', 'C', 'Dm7'))
|
|
source['staves'][0]['measures'][1]['harmonies'][0]['kind'] = 'none'
|
|
for part in ('acoustic', 'electric'):
|
|
canonical = generator.comping.Canonical(source, part, 'rest')
|
|
self.assertEqual({i for i, _, _ in canonical.order}, {0, 2})
|
|
expected = generator.comping.movement(part, canonical.order[0][2], canonical.order[1][2])
|
|
self.assertEqual(next(r['movement'] for r in canonical.report if r['source_index'] == 2), expected)
|
|
def test_harmony_variants_are_independent_of_request_order(self):
|
|
source = tests.solo_fixtures.song(count=3, chords=('C7', 'FM7', 'G7'))
|
|
measures = source['staves'][0]['measures']
|
|
carried = generator.accompaniment.carried_chords(measures)
|
|
variants = []
|
|
for index, symbol in ((1, 'Dm7'), (2, 'Am7')):
|
|
changed = copy.deepcopy(measures[index])
|
|
changed['harmonies'][0] = {**generator.harmony.parse_symbol(symbol), 'id': 'variant', 'onset': generator.model.rational(0)}
|
|
variants.append((changed, carried[index], index))
|
|
for part in ('acoustic', 'electric'):
|
|
a, b = (generator.comping.Canonical(source, part, 'variants') for _ in range(2))
|
|
forward = {args[2]: a(*args) for args in variants}
|
|
reverse = {args[2]: b(*args) for args in reversed(variants)}
|
|
self.assertEqual(forward, reverse)
|
|
self.assertEqual(a.report, b.report)
|
|
self.assertEqual(a.base, b.base)
|
|
self.assertEqual(a(measures[0], carried[0], 0), b(measures[0], carried[0], 0))
|
|
def test_register_preference_does_not_change_rhythm(self):
|
|
resources = generator.vocabulary.load()
|
|
source = tests.solo_fixtures.song(count=4)
|
|
measures = source['staves'][0]['measures']
|
|
carried = generator.accompaniment.carried_chords(measures)
|
|
def rhythm(canonical):
|
|
return [[(n['onset'], n['duration'], n['written'], n['kind']) for n in generator.score.measure_notes(canonical(m, p, i))] for i, (m, p) in enumerate(zip(measures, carried))]
|
|
for part in ('acoustic', 'electric'):
|
|
with generator.vocabulary.using(resources, part + '_accompaniment', {'center': 45}):
|
|
low = generator.comping.Canonical(source, part, 'fixed-rhythm')
|
|
with generator.vocabulary.using(resources, part + '_accompaniment', {'center': 70}):
|
|
high = generator.comping.Canonical(source, part, 'fixed-rhythm')
|
|
self.assertEqual(rhythm(low), rhythm(high))
|
|
def test_accompaniment_cache_matches_clean_generation(self):
|
|
source = generator.production.SourceAnalysis.read(tests.solo_fixtures.song(count=3))
|
|
for part in ('acoustic', 'electric'):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
role = part + '_accompaniment'
|
|
cold = generator.production.generate_role(source, role, pathlib.Path('data'), cache_dir=directory)
|
|
warm = generator.production.generate_role(source, role, pathlib.Path('data'), cache_dir=directory)
|
|
clean = generator.production.generate_role(source, role, pathlib.Path('data'))
|
|
self.assertTrue(warm['cache_hit'])
|
|
self.assertEqual(cold['document'], warm['document'])
|
|
self.assertEqual(cold['document'], clean['document'])
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|