"""Pure score navigation, cloning and identity operations.""" from __future__ import annotations import copy import re def slugify(value: str) -> str: return re.sub(r'[^a-z0-9]+', '_', value.strip().lower()).strip('_') or 'score' def source_staff(document: dict) -> dict: if len(document.get('staves', [])) != 1: raise ValueError('exactly one source staff is required') return document['staves'][0] def measure_notes(measure: dict) -> list[dict]: return [note for voice in measure['voices'] for note in voice['notes']] def leading_pickup_measure_index(measures: list[dict]) -> int | None: return 0 if len(measures) > 1 and measures[0].get('implicit') else None def body_start_measure_index(measures: list[dict]) -> int: return 1 if leading_pickup_measure_index(measures) is not None else 0 def copy_source_voices(measure: dict, *, strip_lyrics: bool) -> list[dict]: voices = copy.deepcopy(measure['voices']) if strip_lyrics: for note in (n for v in voices for n in v['notes']): note.pop('lyric', None) note.pop('lyric_notation', None) note.pop('lyrics', None) return voices def make_measure(measure_id: str, source: dict, notes: list[dict], *, section_label: str = '') -> dict: result = copy.deepcopy(source) result.update({'id': measure_id, 'section_label': section_label, 'voices': [{'id': 'voice_arranged', 'notes': notes}]}) for note in notes: note['voice'] = 'voice_arranged' return result def reidentify(measures: list[dict], source_tuplets: list[dict]) -> list[dict]: """Clone-local links survive; links cut by an arrangement boundary become attacks.""" tone_map, event_map = {}, {} tuplets = [] for measure in measures: for hi, harmony in enumerate(measure.get('harmonies', [])): harmony['id'] = f'{measure["id"]}_h{hi + 1}' for ni, note in enumerate(measure_notes(measure)): old = note['id'] note['id'] = f'{measure["id"]}_n{ni + 1}' event_map[old] = note['id'] for pi, pitch in enumerate(note['pitches']): old_pitch = pitch['id'] pitch['id'] = f'{note["id"]}_p{pi + 1}' tone_map[old_pitch] = pitch['id'] for measure in measures: for note in measure_notes(measure): for p in note['pitches']: for field in ('tie_from', 'tie_to'): if p.get(field) in tone_map: p[field] = tone_map[p[field]] else: p.pop(field, None) if note.get('tremolo', {}).get('partner'): partner = note['tremolo']['partner'] if partner in event_map: note['tremolo']['partner'] = event_map[partner] else: note['tremolo'] = {**note['tremolo'], 'type': 'single'} note['tremolo'].pop('partner', None) for source in source_tuplets: members = [n for m in measures for n in measure_notes(m) if source['id'] in n.get('tuplet_ids', [])] if not members: continue t = copy.deepcopy(source) t.update({'id': f'{measures[0]["id"]}_{source["id"]}', 'start': members[0]['id'], 'end': members[-1]['id'], 'continued_start': source['start'] not in event_map, 'continued_end': source['end'] not in event_map}) for n in members: n['tuplet_ids'] = [t['id'] if v == source['id'] else v for v in n['tuplet_ids']] tuplets.append(t) return tuplets def make_arranged_document(source_document: dict, staff: dict, *, staff_id: str, staff_name: str, role: str, arrangement_part_type: str, measures: list[dict], tuplets=None, octave_shifts=None) -> dict: return {'metadata': copy.deepcopy(source_document['metadata']), 'staves': [{'id': staff_id, 'name': staff_name, 'clef': copy.deepcopy(staff['clef']), 'role': role, 'arranged': True, 'arrangement_part_type': arrangement_part_type, 'measures': measures, 'tuplets': tuplets or [], 'octave_shifts': copy.deepcopy(octave_shifts or []), 'instruments': copy.deepcopy(staff.get('instruments', []))}]}