1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
use crate::model::{
EventAttachments, Fraction, GraceEvent, GraceEventKind, GraceGroupAttachment, Part,
};
use super::{
GraceNoteWrite, MeasureSequence, MusicXmlWriter, NoteWrite, TupletNumbers,
unsupported_grace_warning,
};
impl<'score> MusicXmlWriter<'score> {
pub(crate) fn write_grace_groups(
&mut self,
attachments: &EventAttachments,
sequence: &MeasureSequence<'score>,
part: &Part,
tuplet_numbers: &TupletNumbers,
) {
self.write_grace_group_list(&attachments.grace_groups, sequence, part, tuplet_numbers);
}
pub(crate) fn write_after_grace_groups(
&mut self,
attachments: &EventAttachments,
sequence: &MeasureSequence<'score>,
part: &Part,
tuplet_numbers: &TupletNumbers,
) {
self.write_grace_group_list(
&attachments.after_grace_groups,
sequence,
part,
tuplet_numbers,
);
}
fn write_grace_group_list(
&mut self,
groups: &[GraceGroupAttachment],
sequence: &MeasureSequence<'score>,
part: &Part,
tuplet_numbers: &TupletNumbers,
) {
for group in groups {
if group.events.is_empty() && group.note_count > 0 {
self.diagnostics.push(unsupported_grace_warning(group.span));
continue;
}
self.write_grace_group(group, sequence, part, tuplet_numbers);
}
}
fn write_grace_group(
&mut self,
group: &GraceGroupAttachment,
sequence: &MeasureSequence<'score>,
part: &Part,
tuplet_numbers: &TupletNumbers,
) {
// Slurs that opened before the grace `{` (`({grace}note)`) still bind
// to the FIRST grace note. Slurs written inside the braces bind to the
// individual grace events that lowering paired from source order.
let mut first_note = true;
let mut first_chord_member = true;
for event in &group.events {
match &event.kind {
GraceEventKind::Note(note) => {
let attachments = grace_note_attachments(group, event, first_note);
self.write_grace_note(
GraceNoteWrite {
note,
source: event.source_span,
chord_member: false,
slash: group.slash.is_some(),
display_duration: grace_display_duration(
group.note_count,
note.length_multiplier,
),
},
&attachments,
sequence,
part,
tuplet_numbers,
);
first_note = false;
first_chord_member = false;
}
GraceEventKind::Rest(rest) => {
let attachments = grace_note_attachments(group, event, first_note);
self.write_note(
NoteWrite {
pitch: None,
rest: Some(rest),
duration: grace_base_unit(group.note_count),
source: event.source_span,
written_accidental: None,
attachments: &attachments,
chord_member: false,
measure_rest: false,
grace: true,
grace_slash: group.slash.is_some(),
},
sequence,
part,
tuplet_numbers,
);
first_note = false;
first_chord_member = false;
}
GraceEventKind::Chord(notes) => {
let mut event_first_note = true;
for note in notes {
let attachments = if event_first_note {
grace_note_attachments(group, event, first_note)
} else {
EventAttachments::default()
};
self.write_grace_note(
GraceNoteWrite {
note,
source: event.source_span,
chord_member: !first_chord_member,
slash: group.slash.is_some(),
display_duration: grace_display_duration(
group.note_count,
note.length_multiplier,
),
},
&attachments,
sequence,
part,
tuplet_numbers,
);
first_note = false;
first_chord_member = false;
event_first_note = false;
}
}
}
}
}
fn write_grace_note(
&mut self,
grace_note: GraceNoteWrite<'_>,
attachments: &EventAttachments,
sequence: &MeasureSequence<'score>,
part: &Part,
tuplet_numbers: &TupletNumbers,
) {
self.write_note(
NoteWrite {
pitch: Some(&grace_note.note.pitch),
rest: None,
duration: grace_note.display_duration,
source: grace_note.source,
written_accidental: grace_note.note.written_accidental.as_ref(),
attachments,
chord_member: grace_note.chord_member,
measure_rest: false,
grace: true,
grace_slash: grace_note.slash,
},
sequence,
part,
tuplet_numbers,
);
}
}
fn grace_note_attachments(
group: &GraceGroupAttachment,
event: &GraceEvent,
first_note: bool,
) -> EventAttachments {
let mut slurs = Vec::new();
if first_note {
slurs.extend(group.slurs.iter().copied());
}
slurs.extend(event.slurs.iter().copied());
EventAttachments {
slurs,
..EventAttachments::default()
}
}
/// Count-based grace base unit, matching abc2xml: 1/8 for a single grace note in
/// the group, 1/16 otherwise. The grace note's written length modifier is
/// applied on top of this (see [`grace_display_duration`]).
fn grace_base_unit(note_count: u32) -> Fraction {
if note_count <= 1 {
Fraction {
numerator: 1,
denominator: 8,
}
} else {
Fraction {
numerator: 1,
denominator: 16,
}
}
}
/// Display duration of a single grace note: the count-based base unit scaled by
/// the grace note's written length modifier (`/` -> 1/2, `2` -> 2, ...). The
/// resulting fraction drives the `<type>`/`<dots>` spelling; grace notes still
/// carry no `<duration>` element.
fn grace_display_duration(note_count: u32, length_multiplier: Fraction) -> Fraction {
grace_base_unit(note_count).checked_mul(length_multiplier)
}