Training Model
This commit is contained in:
+10
-55
@@ -18,8 +18,6 @@ import generator.parts
|
||||
import generator.pitch
|
||||
import generator.playback
|
||||
import tests.arrangement_fixtures
|
||||
import tests.rhythm_fixtures
|
||||
import tests.scale_fixtures
|
||||
import tests.solo_fixtures
|
||||
import tests.test_musicxml
|
||||
|
||||
@@ -38,7 +36,7 @@ class PartGenerationTests(unittest.TestCase):
|
||||
self.assertEqual(cursor, generator.model.fraction(measure['duration']))
|
||||
def test_dense_melody_keeps_rhythmic_accompaniment_without_sustaining_across_changes(self):
|
||||
source = tests.test_musicxml.parse(tests.test_musicxml.xml_score(xml_harmony() + xml_harmony('F', offset=3) + ''.join(tests.test_musicxml.xml_note(duration=1, kind='eighth') for _ in range(8)), divisions=2))
|
||||
for result in (tests.arrangement_fixtures.section_document(source, 'acoustic', rhythms=tests.rhythm_fixtures.library()), generator.arrange.generate.arrange_document(source, 'electric', rhythms=tests.rhythm_fixtures.library())):
|
||||
for result in (tests.arrangement_fixtures.section_document(source, 'acoustic'), generator.arrange.generate.arrange_document(source, 'electric')):
|
||||
generator.model.validate_document(result)
|
||||
for measure in result['staves'][0]['measures']:
|
||||
events = measure['voices'][0]['notes']
|
||||
@@ -57,7 +55,7 @@ class PartGenerationTests(unittest.TestCase):
|
||||
def test_harmony_carry_and_explicit_silence(self):
|
||||
content = xml_harmony(offset=1) + xml_harmony(kind='none', offset=2) + xml_harmony('F', offset=3) + tests.test_musicxml.xml_note()
|
||||
source = tests.test_musicxml.xml_score(content).replace('</measure></part>', '</measure><measure number="2">' + tests.test_musicxml.xml_note() + '</measure><measure number="3">' + xml_harmony(kind='none') + tests.test_musicxml.xml_note() + '</measure><measure number="4">' + tests.test_musicxml.xml_note() + '</measure></part>')
|
||||
result = tests.arrangement_fixtures.section_document(tests.test_musicxml.parse(source), 'acoustic', rhythms=tests.rhythm_fixtures.library())
|
||||
result = tests.arrangement_fixtures.section_document(tests.test_musicxml.parse(source), 'acoustic')
|
||||
for measure in result['staves'][0]['measures']:
|
||||
self.assert_complete_measure(measure)
|
||||
sounding = [n for n in tests.test_musicxml.notes(result) if n['pitches']]
|
||||
@@ -73,53 +71,14 @@ class PartGenerationTests(unittest.TestCase):
|
||||
for index in (2, 3):
|
||||
self.assertTrue(all(n['kind'] == 'rest' and not n['pitches'] for n in tests.test_musicxml.notes(result, index)))
|
||||
generator.model.validate_document(result)
|
||||
def test_accompaniment_handles_pickups_meters_and_coincident_markers(self):
|
||||
for duration, kind, attributes, implicit in ((1, 'quarter', '', True), (3, 'half', '<time><beats>3</beats><beat-type>4</beat-type></time>', False)):
|
||||
source = tests.test_musicxml.parse(tests.test_musicxml.xml_score(xml_harmony() + xml_harmony('F') + tests.test_musicxml.xml_note(duration=duration, kind=kind, extra='<dot/>' if duration == 3 else ''), attributes, implicit=implicit))
|
||||
measure = source['staves'][0]['measures'][0]
|
||||
plan = generator.accompaniment.plan_section([measure], tests.rhythm_fixtures.library(), electric=False)
|
||||
events = generator.accompaniment.realize(measure, plan.acoustic_upper[0], None)
|
||||
self.assertEqual(sum(generator.model.fraction(n['duration']) for n in events), duration)
|
||||
sounding = [n for n in events if n['kind'] == 'note']
|
||||
self.assertTrue(sounding)
|
||||
self.assertTrue(all(n['_harmony']['root']['letter'] == 'f' for n in sounding))
|
||||
source = tests.test_musicxml.parse(tests.test_musicxml.xml_score(xml_harmony() + tests.test_musicxml.xml_note(duration=1, kind='quarter') + '<attributes><time><beats>6</beats><beat-type>8</beat-type></time></attributes>' + tests.test_musicxml.xml_note(duration=3, kind='half', extra='<dot/>')))
|
||||
with self.assertRaisesRegex(ValueError, 'mid-measure meter'):
|
||||
tests.arrangement_fixtures.section_document(source, 'acoustic', rhythms=tests.rhythm_fixtures.library())
|
||||
def test_dense_melody_has_accompaniment_in_every_measure(self):
|
||||
def test_sparse_accompaniment_preserves_form_and_intentional_space(self):
|
||||
bar = xml_harmony() + ''.join(tests.test_musicxml.xml_note(duration=1, kind='quarter') for _ in range(4))
|
||||
xml = tests.test_musicxml.xml_score(bar).replace('</part>', ''.join(f'<measure number="{i}">{bar}</measure>' for i in range(2, 13)) + '</part>')
|
||||
result = tests.arrangement_fixtures.section_document(tests.test_musicxml.parse(xml), 'acoustic', rhythms=tests.rhythm_fixtures.library())
|
||||
self.assertEqual(len(result['staves'][0]['measures']), 12)
|
||||
for measure in result['staves'][0]['measures']:
|
||||
self.assertTrue(any(n['pitches'] for v in measure['voices'] for n in v['notes']), measure['id'])
|
||||
result = tests.arrangement_fixtures.section_document(tests.test_musicxml.parse(xml), 'acoustic')
|
||||
self.assertEqual(len(result['staves'][0]['measures']),12)
|
||||
generator.model.validate_document(result)
|
||||
timeline = generator.playback.compile_timeline(result)
|
||||
start = fractions.Fraction()
|
||||
for measure in result['staves'][0]['measures']:
|
||||
end = start + generator.model.fraction(measure['duration'])
|
||||
self.assertTrue(any(generator.model.fraction(e['onset']) < end and generator.model.fraction(e['onset']) + generator.model.fraction(e['duration']) > start for e in timeline['events']), measure['id'])
|
||||
start = end
|
||||
def test_prepared_document_and_parts_have_independent_ownership(self):
|
||||
source = tests.test_musicxml.parse(tests.test_musicxml.xml_score(xml_harmony() + tests.test_musicxml.xml_note()))
|
||||
source['staves'][0]['octave_shifts'] = [{'id': 'octave', 'start': generator.model.rational(0), 'end': generator.model.rational(4), 'octaves': -1}]
|
||||
original = copy.deepcopy(source)
|
||||
prepared = generator.parts.prepare_document(source)
|
||||
before = copy.deepcopy(prepared)
|
||||
plan = generator.arrange.generate.plan_arrangement(prepared, tests.rhythm_fixtures.library())
|
||||
plan_before = copy.deepcopy(plan)
|
||||
parts = [generator.arrange.generate._arrange_document(prepared, part, plan) for part in generator.parts.PART_TYPES]
|
||||
self.assertEqual(prepared, before)
|
||||
self.assertEqual(plan, plan_before)
|
||||
self.assertEqual(parts, [generator.arrange.generate.arrange_document(source, part, rhythms=tests.rhythm_fixtures.library()) for part in generator.parts.PART_TYPES])
|
||||
vocal = copy.deepcopy(parts[2])
|
||||
parts[1]['metadata']['title'] = 'changed'
|
||||
tests.test_musicxml.notes(parts[1])[0]['pitches'].clear()
|
||||
if parts[1]['staves'][0]['octave_shifts']:
|
||||
parts[1]['staves'][0]['octave_shifts'][0]['octaves'] = 9
|
||||
self.assertEqual(parts[2], vocal)
|
||||
self.assertEqual(prepared, before)
|
||||
self.assertEqual(source, original)
|
||||
self.assertTrue(generator.playback.compile_timeline(result)['events'])
|
||||
self.assertTrue(any(n['kind']=='rest' for m in result['staves'][0]['measures'] for n in generator.score.measure_notes(m)))
|
||||
def test_melody_written_pitch_rhythm_rests_and_cross_bar_ties_are_preserved(self):
|
||||
content = ''
|
||||
content += tests.test_musicxml.xml_note(duration=2, kind='quarter', alter=1, extra='<tie type="start"/>')
|
||||
@@ -135,7 +94,7 @@ class PartGenerationTests(unittest.TestCase):
|
||||
for builder in (tests.arrangement_fixtures.section_document, generator.arrange.generate.arrange_document):
|
||||
for part in ('electric', 'vocal'):
|
||||
with self.subTest(builder=builder.__module__, part=part):
|
||||
result = builder(source, part, rhythms=tests.rhythm_fixtures.library())
|
||||
result = builder(source, part)
|
||||
generator.model.validate_document(result)
|
||||
measures = result['staves'][0]['measures']
|
||||
starts = [0] if builder is tests.arrangement_fixtures.section_document else [i for i, m in enumerate(measures) if m.get('section_label') == ('Guitar Melody' if part == 'electric' else 'Vocal Melody')]
|
||||
@@ -170,13 +129,11 @@ class PartGenerationTests(unittest.TestCase):
|
||||
for name in names:
|
||||
(source / (name + '.xml')).write_text(tests.test_musicxml.xml_score(tests.test_musicxml.xml_note()))
|
||||
with self.assertRaisesRegex(ValueError, 'conflicting output path') as raised:
|
||||
generator.arrange.generate.generate_performance_scores(source, output, rhythms=tests.rhythm_fixtures.library(), scales=tests.scale_fixtures.library(), sequences=tests.solo_fixtures.library())
|
||||
generator.arrange.generate.generate_performance_scores(source, output)
|
||||
for name in names:
|
||||
self.assertIn(str(source / (name + '.xml')), str(raised.exception))
|
||||
self.assertEqual([p.name for p in output.iterdir()], ['sentinel'])
|
||||
def test_pipeline_parses_and_normalizes_each_source_once(self):
|
||||
self.enterContext(unittest.mock.patch('generator.solo.sequences.load_library', return_value=tests.solo_fixtures.library()))
|
||||
self.enterContext(unittest.mock.patch('generator.scales.library.load_library', return_value=tests.scale_fixtures.library()))
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
root = pathlib.Path(directory)
|
||||
for category, name in (('songs', 'song'), ('songs', 'other')):
|
||||
@@ -184,7 +141,6 @@ class PartGenerationTests(unittest.TestCase):
|
||||
(root / 'data' / category / (name + '.xml')).write_text(tests.test_musicxml.xml_score(xml_harmony() + tests.test_musicxml.xml_note()))
|
||||
args = argparse.Namespace(data_dir=root / 'data', output_dir=root / 'output', static_assets_dir=pathlib.Path('package/assets'), check=False)
|
||||
with (
|
||||
unittest.mock.patch('generator.rhythm.load_library', return_value=tests.rhythm_fixtures.library()) as load_library,
|
||||
unittest.mock.patch('generator.musicxml.load_musicxml', wraps=generator.musicxml.load_musicxml) as load,
|
||||
unittest.mock.patch('generator.pitch.concert_document', wraps=generator.pitch.concert_document) as normalize,
|
||||
unittest.mock.patch('generator.engraving.pipeline.engrave_tree'),
|
||||
@@ -195,12 +151,11 @@ class PartGenerationTests(unittest.TestCase):
|
||||
generator.__main__.generate_assets(args)
|
||||
# Role catalog loading is replaced by this infrastructure fixture. The
|
||||
# parent asset pipeline must not perform a redundant catalog parse.
|
||||
load_library.assert_not_called()
|
||||
self.assertEqual(load.call_args_list, [unittest.mock.call(args.data_dir.resolve() / 'songs/other.xml'), unittest.mock.call(args.data_dir.resolve() / 'songs/song.xml')])
|
||||
self.assertEqual(normalize.call_count, 2)
|
||||
original = {p.relative_to(args.output_dir): p.read_bytes() for p in args.output_dir.rglob('*') if p.is_file()}
|
||||
(root / 'data/songs/broken.xml').write_text('<broken')
|
||||
with unittest.mock.patch('generator.rhythm.load_library', return_value=tests.rhythm_fixtures.library()), self.assertRaisesRegex(ValueError, 'broken.xml'):
|
||||
with self.assertRaisesRegex(ValueError, 'broken.xml'):
|
||||
generator.__main__.generate_assets(args)
|
||||
self.assertEqual(original, {p.relative_to(args.output_dir): p.read_bytes() for p in args.output_dir.rglob('*') if p.is_file()})
|
||||
self.assertEqual(len(list(args.output_dir.rglob('*.json'))), 6)
|
||||
|
||||
Reference in New Issue
Block a user