|
| 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 | +} |
0 commit comments