Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with MusicXML file #1

Open
frnic opened this issue Jan 28, 2018 · 0 comments
Open

Issue with MusicXML file #1

frnic opened this issue Jan 28, 2018 · 0 comments

Comments

@frnic
Copy link

frnic commented Jan 28, 2018

I am new to XML decoding and have a issue decoding this part of a musicXML file with your decoder.

Issues:

(Item 1 I figured out what I was doing wrong.)
// 1. Attributes on measure element (number="4" width="267.83") are missing - or I don't understand
// how to setup the structure to return them - using the XMLParser the attributes show up.

2. <rest/> doesn't get detected/decoded - or I don't know how to differentiate between this and a missing "rest" element since both show in my structure as nil.

(grr - item 3 was a typo on me.)
//3. barline element bar-style is not decoded and/or returns nil and the barline attribute (direction) is //not decoded or I do not know how to setup my structure.

Here is a print of that part of the score struct:

xmlTest.Note(rest: nil, pitch: nil, duration: Optional(1), voice: Optional(1), type: Optional("quarter"), stem: nil)]), barlne: nil)

(fragment of musicXML file)

         <measure number="4" width="267.83">
           <note>
                <rest/>
                <duration>1</duration>
                <voice>1</voice>
                <type>quarter</type>
            </note>
            <barline location="right">
                <bar-style>light-heavy</bar-style>
                <repeat direction="backward"/>
            </barline>
        </measure>

...

(structs to receive decoded data)

struct Measure: Codable  {
    var number: Int?
    var width: Float?
    var attributes: Attributes?
    var note: [Note]?
    var barline: Barline?
}

struct Attributes: Codable  {
    var divisions: Int?
    var key: Key?
    var time: Time?
    var clef: Clef?
}

struct Note: Codable  {
    var rest: String?
    var pitch: Pitch?
    var duration: Int?
    var voice: Int?
    var type: String?
    var stem: String?
}

struct Barline: Codable  {
    var barStyle: String?
    var repeatType: String?
    
    enum CodingKeys: String, CodingKey {
        case barStyle = "bar-style"
        case repeatType = "repeat"
    }
}

And here is the code fragment for the decoding:

            let decoder = XMLDecoder()
            
            let formatter: DateFormatter = {
                let formatter = DateFormatter()
                formatter.dateFormat = "yyyy-MM-dd"
                return formatter
            }()
            
            decoder.dateDecodingStrategy = .formatted(formatter)
            
            do {
                let score = try decoder.decode(Score.self, from: data)
                print("Score: \(score)")
            } catch {
                print(error)
            }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant