Skip to content

Commit 6a75d0c

Browse files
committed
introduce ClefSignature
1 parent 6572c63 commit 6a75d0c

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

lib/music_xml.dart

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export 'src/basic_attributes.dart';
22
export 'src/barline.dart';
33
export 'src/chord_symbol.dart';
44
export 'src/key_signature.dart';
5+
export 'src/clef_signature.dart';
56
export 'src/kind.dart';
67
export 'src/lyric.dart';
78
export 'src/measure.dart';

lib/src/clef_signature.dart

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import 'package:xml/xml.dart';
2+
3+
import 'music_xml_parser_state.dart';
4+
5+
/// Internal representation of a MusicXML clef signature.
6+
class ClefSignature {
7+
// The <sign> element represents the clef symbol
8+
late String sign;
9+
// Line numbers are counted from the bottom of the staff. They are only needed with the G, F, and C signs in order to position a pitch correctly on the staff. Standard values are 2 for the G sign (treble clef), 4 for the F sign (bass clef), and 3 for the C sign (alto clef). Line values can be used to specify positions outside the staff, such as a C clef positioned in the middle of a grand staff.
10+
late int? line;
11+
// The <clef-octave-change> element is used for transposing clefs. A treble clef for tenors would have a value of -1.
12+
late int? clefOctaveChange;
13+
14+
ClefSignature();
15+
16+
factory ClefSignature.parse(MusicXMLParserState state, [XmlElement? xmlKey]) {
17+
final instance = ClefSignature()..sign = '';
18+
19+
if (xmlKey != null) instance._parse(xmlKey, state);
20+
return instance;
21+
}
22+
23+
/// Parse the MusicXML <clef> element.
24+
///
25+
/// Raises:
26+
/// KeyParseError: If the sign element is missing.
27+
void _parse(XmlElement xmlKey, MusicXMLParserState state) {
28+
// parse sign
29+
final signElem = xmlKey.getElement('sign');
30+
if (signElem == null) {
31+
throw XmlParserException(
32+
'Could not find sign element in clef signature.');
33+
}
34+
sign = signElem.innerText;
35+
36+
// parse line
37+
final lineElem = xmlKey.getElement('line');
38+
if (lineElem != null && lineElem.innerText.isNotEmpty) {
39+
line = int.parse(lineElem.innerText);
40+
}
41+
42+
// parse clefOctaveChange
43+
final cocElem = xmlKey.getElement('clef-octave-change');
44+
if (cocElem != null && cocElem.innerText.isNotEmpty) {
45+
clefOctaveChange = int.parse(cocElem.innerText);
46+
}
47+
}
48+
}

lib/src/measure.dart

+5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:music_xml/src/barline.dart';
2+
import 'package:music_xml/src/clef_signature.dart';
23
import 'package:music_xml/src/print.dart';
34
import 'package:xml/xml.dart';
45

@@ -16,6 +17,7 @@ class Measure {
1617
final chordSymbols = <ChordSymbol>[];
1718
final tempos = <Tempo>[];
1819
final prints = <Print>[];
20+
ClefSignature? clefSignature;
1921
TimeSignature? timeSignature;
2022
KeySignature? keySignature;
2123
int duration = 0;
@@ -91,6 +93,9 @@ class Measure {
9193
case 'divisions':
9294
state.divisions = int.parse(child.innerText);
9395
break;
96+
case 'clef':
97+
clefSignature = ClefSignature.parse(state, child);
98+
break;
9499
case 'key':
95100
keySignature = KeySignature.parse(state, child);
96101
break;

0 commit comments

Comments
 (0)