musicxml 1.1.2

MusicXML parsing, manipulation, and export library
Documentation
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
use crate::elements::{ScorePartwise, ScoreTimewise};
use alloc::{collections::BTreeMap, string::String, vec::Vec};
use musicxml_internal::{bytes_to_string, ElementDeserializer, ElementSerializer, XmlElement};

#[cfg(feature = "std")]
extern crate std;

#[cfg(feature = "std")]
use {alloc::string::ToString, std::io::Write};

mod xml_parser;
mod zip_parser;

#[inline]
fn is_mxl_data(data: Option<&[u8]>) -> bool {
  if let Some(data) = data {
    *data == [0x50, 0x4b, 0x03, 0x04]
  } else {
    false
  }
}

fn get_musicxml_contents(data: Vec<u8>) -> Result<String, String> {
  let mut contents = String::new();
  if is_mxl_data(data.get(0..4)) {
    let mut xml_path: Option<String> = None;
    let mut mxl_data = zip_parser::ZipData::new();
    mxl_data.content = data;
    let archive = zip_parser::ZipArchive::new(&mut mxl_data);
    for file_name in archive.iter() {
      if file_name == "META-INF/container.xml" {
        let container = xml_parser::parse_from_string(archive.read_file_to_string(file_name)?.as_str())?;
        xml_path = container
          .elements
          .iter()
          .find(|&el| el.name == "rootfiles")
          .and_then(|el| el.elements.iter().find(|&el| el.name == "rootfile"))
          .and_then(|el| el.attributes.iter().find(|&attr| attr.0 == "full-path"))
          .map(|attr| attr.1.clone());
      }
    }
    if let Some(full_path) = &xml_path {
      contents = archive.read_file_to_string(full_path)?;
    } else {
      Err(String::from("Cannot find MusicXML file in compressed archive"))?;
    }
  } else {
    contents = bytes_to_string(&data)?;
  }
  Ok(contents)
}

#[cfg(feature = "std")]
fn get_musicxml_contents_from_file(path: &str) -> Result<String, String> {
  get_musicxml_contents(std::fs::read(path).map_err(|e| e.to_string())?)
}

#[cfg(not(feature = "std"))]
fn get_musicxml_contents_from_file(_path: &str) -> Result<String, String> {
  Err(String::from(
    "Reading MusicXML files is not supported in a 'no_std' environment",
  ))
}

fn put_musicxml_contents(xml: &XmlElement, pretty_print: bool) -> Vec<u8> {
  let mut buffer = Vec::new();
  buffer.extend_from_slice(b"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
  if xml.name == "score-partwise" {
    buffer.extend_from_slice(b"<!DOCTYPE score-partwise PUBLIC \"-//Recordare//DTD MusicXML 3.0 Partwise//EN\" \"http://www.musicxml.org/dtds/partwise.dtd\">\n");
  } else if xml.name == "score-timewise" {
    buffer.extend_from_slice(b"<!DOCTYPE score-timewise PUBLIC \"-//Recordare//DTD MusicXML 3.0 Timewise//EN\" \"http://www.musicxml.org/dtds/timewise.dtd\">\n");
  }
  buffer.extend_from_slice(xml_parser::parse_to_string(xml, if pretty_print { 0 } else { -1 }).as_ref());
  buffer
}

fn write_musicxml_contents(xml: &XmlElement, compressed: bool, pretty_print: bool) -> Vec<u8> {
  if compressed {
    let mut archiver = zip_parser::ZipArchiver::new();
    archiver.start_file("META-INF/container.xml");
    archiver.write_data(b"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<container>\n  <rootfiles>\n    <rootfile full-path=\"score.musicxml\" media-type=\"application/vnd.recordare.musicxml+xml\"/>\n  </rootfiles>\n</container>");
    archiver.start_file("score.musicxml");
    archiver.write_data(put_musicxml_contents(xml, pretty_print).as_slice());
    archiver.finish()
  } else {
    put_musicxml_contents(xml, pretty_print)
  }
}

#[cfg(feature = "std")]
fn write_musicxml_contents_to_file(
  path: &str,
  xml: &XmlElement,
  compressed: bool,
  pretty_print: bool,
) -> Result<(), String> {
  let mut file = std::fs::OpenOptions::new()
    .write(true)
    .create(true)
    .truncate(true)
    .open(path)
    .map_err(|e| e.to_string())?;
  file
    .write_all(&write_musicxml_contents(xml, compressed, pretty_print))
    .map_err(|e| e.to_string())
}

#[cfg(not(feature = "std"))]
fn write_musicxml_contents_to_file(
  _path: &str,
  _xml: &XmlElement,
  _compressed: bool,
  _pretty_print: bool,
) -> Result<(), String> {
  Err(String::from(
    "Writing MusicXML files is not supported in a 'no_std' environment",
  ))
}

fn convert_xml_partwise_to_timewise(xml: XmlElement) -> Result<XmlElement, String> {
  if xml.name == "score-timewise" {
    Ok(xml)
  } else if xml.name == "score-partwise" {
    let mut measures: BTreeMap<usize, XmlElement> = BTreeMap::new();
    let mut converted_xml = XmlElement {
      name: String::from("score-timewise"),
      attributes: xml.attributes,
      elements: Vec::new(),
      text: xml.text,
    };
    for element in xml.elements {
      if element.name == "part" {
        for measure_element in element.elements {
          let measure_number: usize = measure_element
            .attributes
            .iter()
            .find(|(key, _)| key == "number")
            .ok_or("Missing \"number\" attribute in <measure> element")?
            .1
            .parse()
            .map_err(|err| format!("Invalid \"number\" attribute in <measure> element: {err}"))?;
          measures
            .entry(measure_number)
            .or_insert_with(|| XmlElement {
              name: measure_element.name,
              attributes: measure_element.attributes,
              elements: Vec::new(),
              text: measure_element.text,
            })
            .elements
            .push(XmlElement {
              name: element.name.clone(),
              attributes: element.attributes.clone(),
              elements: measure_element.elements,
              text: element.text.clone(),
            });
        }
      } else {
        converted_xml.elements.push(element);
      }
    }
    measures
      .into_values()
      .for_each(|measure| converted_xml.elements.push(measure));
    Ok(converted_xml)
  } else {
    Err(String::from(
      "Root element in a MusicXML file must be either <score-partwise> or <score-timewise>",
    ))
  }
}

fn convert_xml_timewise_to_partwise(xml: XmlElement) -> Result<XmlElement, String> {
  if xml.name == "score-partwise" {
    Ok(xml)
  } else if xml.name == "score-timewise" {
    let mut parts: BTreeMap<String, XmlElement> = BTreeMap::new();
    let mut converted_xml = XmlElement {
      name: String::from("score-partwise"),
      attributes: xml.attributes,
      elements: Vec::new(),
      text: xml.text,
    };
    for element in xml.elements {
      if element.name == "measure" {
        for part_element in element.elements {
          let part_id = part_element
            .attributes
            .iter()
            .find(|(key, _)| key == "id")
            .ok_or("Missing \"id\" attribute in <measure> element")?
            .1
            .clone();
          parts
            .entry(part_id)
            .or_insert_with(|| XmlElement {
              name: part_element.name,
              attributes: part_element.attributes,
              elements: Vec::new(),
              text: part_element.text,
            })
            .elements
            .push(XmlElement {
              name: element.name.clone(),
              attributes: element.attributes.clone(),
              elements: part_element.elements,
              text: element.text.clone(),
            });
        }
      } else {
        converted_xml.elements.push(element);
      }
    }
    parts.into_values().for_each(|part| converted_xml.elements.push(part));
    Ok(converted_xml)
  } else {
    Err(String::from(
      "Root element in a MusicXML file must be either <score-partwise> or <score-timewise>",
    ))
  }
}

/// Parses a MusicXML string into a MusicXML element.
///
/// This function can be used to parse any MusicXML datatype or element from a string. It is not required that the
/// element being parsed be a top-level element such as `<score-partwise>` or `<score-timewise>`.
///
/// # Errors
///
/// If the string cannot be parsed into a valid MusicXML element, an error message will be returned.
pub fn parse_from_xml_str<T: ElementDeserializer>(str: &str) -> Result<T, String> {
  let xml = xml_parser::parse_from_string(str)?;
  T::deserialize(&xml)
}

/// Parses a MusicXML element into a MusicXML string.
///
/// This function can be used to convert any MusicXML datatype or element into a string. It is not required that the
/// element being parsed be a top-level element such as `<score-partwise>` or `<score-timewise>`.
pub fn parse_to_xml_str<T: ElementSerializer>(data: &T, pretty_print: bool) -> String {
  let xml = T::serialize(data);
  xml_parser::parse_to_string(&xml, if pretty_print { 0 } else { -1 })
}

/// Parses the contents of the specified MusicXML file into a [ScorePartwise] element.
///
/// The specified file can be either a `.musicxml` file or a compressed `.mxl` file.
///
/// # Errors
///
/// If the file does not exist, cannot be read, or is not a valid MusicXML file, an
/// error message will be returned.
pub fn parse_score_partwise_from_file(path: &str) -> Result<ScorePartwise, String> {
  let contents = get_musicxml_contents_from_file(path)?;
  let xml = xml_parser::parse_from_string(&contents)?;
  convert_xml_timewise_to_partwise(xml).and_then(|xml| ScorePartwise::deserialize(&xml))
}

/// Parses the contents of the specified MusicXML file into a [ScoreTimewise] element.
///
/// The specified file can be either a `.musicxml` file or a compressed `.mxl` file.
///
/// # Errors
///
/// If the file does not exist, cannot be read, or is not a valid MusicXML file, an
/// error message will be returned.
pub fn parse_score_timewise_from_file(path: &str) -> Result<ScoreTimewise, String> {
  let contents = get_musicxml_contents_from_file(path)?;
  let xml = xml_parser::parse_from_string(&contents)?;
  convert_xml_partwise_to_timewise(xml).and_then(|xml| ScoreTimewise::deserialize(&xml))
}

/// Parses the contents of the specified MusicXML data into a [ScorePartwise] element.
///
/// The specified data should have been read directly from either a `.musicxml` file or a compressed `.mxl` file.
///
/// # Errors
///
/// If the data cannot be parsed or does not represent valid MusicXML contents, an error message will be returned.
pub fn parse_score_partwise_from_data(data: Vec<u8>) -> Result<ScorePartwise, String> {
  let contents = get_musicxml_contents(data)?;
  let xml = xml_parser::parse_from_string(&contents)?;
  convert_xml_timewise_to_partwise(xml).and_then(|xml| ScorePartwise::deserialize(&xml))
}

/// Parses the contents of the specified MusicXML data into a [ScoreTimewise] element.
///
/// The specified data should have been read directly from either a `.musicxml` file or a compressed `.mxl` file.
///
/// # Errors
///
/// If the data cannot be parsed or does not represent valid MusicXML contents, an error message will be returned.
pub fn parse_score_timewise_from_data(data: Vec<u8>) -> Result<ScoreTimewise, String> {
  let contents = get_musicxml_contents(data)?;
  let xml = xml_parser::parse_from_string(&contents)?;
  convert_xml_partwise_to_timewise(xml).and_then(|xml| ScoreTimewise::deserialize(&xml))
}

/// Writes the contents of the specified [ScorePartwise] element into a MusicXML file.
///
/// If the `compressed` parameter is set to `true`, the MusicXML file will be written as a compressed `.mxl` file.
/// If the `write_timewise` parameter is set to `true`, the MusicXML file will be converted into a timewise format and
/// written as a `<score-timewise>` element.
///
/// The `pretty_print` parameter specifies whether the MusicXML file should be written with indentation and newlines.
///
/// # Errors
///
/// If the file cannot be written or the data cannot be serialized into a valid MusicXML format, an error message
/// will be returned.
pub fn parse_score_partwise_to_file(
  path: &str,
  score: &ScorePartwise,
  compressed: bool,
  pretty_print: bool,
  write_timewise: bool,
) -> Result<(), String> {
  let xml = ScorePartwise::serialize(score);
  if write_timewise {
    convert_xml_partwise_to_timewise(xml)
      .and_then(|xml| write_musicxml_contents_to_file(path, &xml, compressed, pretty_print))
  } else {
    write_musicxml_contents_to_file(path, &xml, compressed, pretty_print)
  }
}

/// Writes the contents of the specified [ScoreTimewise] element into a MusicXML file.
///
/// If the `compressed` parameter is set to `true`, the MusicXML file will be written as a compressed `.mxl` file.
/// If the `write_partwise` parameter is set to `true`, the MusicXML file will be converted into a partwise format and
/// written as a `<score-partwise>` element.
///
/// The `pretty_print` parameter specifies whether the MusicXML file should be written with indentation and newlines.
///
/// # Errors
///
/// If the file cannot be written or the data cannot be serialized into a valid MusicXML format, an error message
/// will be returned.
pub fn parse_score_timewise_to_file(
  path: &str,
  score: &ScoreTimewise,
  compressed: bool,
  pretty_print: bool,
  write_partwise: bool,
) -> Result<(), String> {
  let xml = ScoreTimewise::serialize(score);
  if write_partwise {
    convert_xml_timewise_to_partwise(xml)
      .and_then(|xml| write_musicxml_contents_to_file(path, &xml, compressed, pretty_print))
  } else {
    write_musicxml_contents_to_file(path, &xml, compressed, pretty_print)
  }
}

/// Writes the contents of the specified [ScorePartwise] element into a MusicXML data buffer.
///
/// If the `compressed` parameter is set to `true`, the MusicXML contents will be written as compressed `.mxl` data.
/// If the `write_timewise` parameter is set to `true`, the MusicXML contents will be converted into a timewise
/// format and written as a `<score-timewise>` element.
///
/// The `pretty_print` parameter specifies whether the MusicXML contents should be written with indentation
/// and newlines.
///
/// # Errors
///
/// If the data cannot be serialized into a valid MusicXML format, an error message will be returned.
pub fn parse_score_partwise_to_data(
  score: &ScorePartwise,
  compressed: bool,
  pretty_print: bool,
  write_timewise: bool,
) -> Result<Vec<u8>, String> {
  let xml = ScorePartwise::serialize(score);
  if write_timewise {
    convert_xml_partwise_to_timewise(xml).map(|xml| write_musicxml_contents(&xml, compressed, pretty_print))
  } else {
    Ok(write_musicxml_contents(&xml, compressed, pretty_print))
  }
}

/// Writes the contents of the specified [ScoreTimewise] element into a MusicXML data buffer.
///
/// If the `compressed` parameter is set to `true`, the MusicXML contents will be written as compressed `.mxl` data.
/// If the `write_partwise` parameter is set to `true`, the MusicXML contents will be converted into a partwise
/// format and written as a `<score-partwise>` element.
///
/// The `pretty_print` parameter specifies whether the MusicXML contents should be written with indentation
/// and newlines.
///
/// # Errors
///
/// If the data cannot be serialized into a valid MusicXML format, an error message will be returned.
pub fn parse_score_timewise_to_data(
  score: &ScoreTimewise,
  compressed: bool,
  pretty_print: bool,
  write_partwise: bool,
) -> Result<Vec<u8>, String> {
  let xml = ScoreTimewise::serialize(score);
  if write_partwise {
    convert_xml_timewise_to_partwise(xml).map(|xml| write_musicxml_contents(&xml, compressed, pretty_print))
  } else {
    Ok(write_musicxml_contents(&xml, compressed, pretty_print))
  }
}