Search in sources :

Example 1 with MusicXMLDocument

use of com.xenoage.zong.musicxml.MusicXMLDocument in project Zong by Xenoage.

the class MusicXmlScoreFileInput method read.

/**
 * Creates a {@link Score} instance from the document
 * behind the given {@link InputStream}.
 * @param inputStream  the input stream with the MusicXML document
 * @param filePath     file path if known, null otherwise
 */
@Override
public Score read(InputStream stream, @MaybeNull String filePath) throws InvalidFormatException, IOException {
    // parse MusicXML file
    MxlScorePartwise score;
    try {
        XmlReader xmlReader = platformUtils().createXmlReader(stream);
        MusicXMLDocument doc = MusicXMLDocument.read(xmlReader);
        score = doc.getScore();
    } catch (XmlDataException ex) {
        // no valid MusicXML
        throw new InvalidFormatException(ex);
    } catch (Exception ex) {
        throw new IOException(ex);
    }
    return read(score, Level.LogErrors);
}
Also used : MxlScorePartwise(com.xenoage.zong.musicxml.types.MxlScorePartwise) XmlReader(com.xenoage.utils.xml.XmlReader) MusicXMLDocument(com.xenoage.zong.musicxml.MusicXMLDocument) XmlDataException(com.xenoage.utils.xml.XmlDataException) IOException(java.io.IOException) InvalidFormatException(com.xenoage.utils.exceptions.InvalidFormatException) IOException(java.io.IOException) XmlDataException(com.xenoage.utils.xml.XmlDataException) InvalidFormatException(com.xenoage.utils.exceptions.InvalidFormatException)

Example 2 with MusicXMLDocument

use of com.xenoage.zong.musicxml.MusicXMLDocument in project Zong by Xenoage.

the class MusicXMLDemoFilesTest method test.

private void test(boolean reload) throws Exception {
    long totalMusicXMLReadingTime = 0;
    long lastTime = 0;
    for (String dir : dirs) {
        for (File file : JseFileUtils.listFiles(new File(dir), plainMusicXMLFilenameFilter, false)) {
            System.out.println(file);
            lastTime = System.currentTimeMillis();
            XmlReader reader = new JseXmlReader(new FileInputStream(file));
            try {
                lastTime = System.currentTimeMillis();
                // load the document
                MusicXMLDocument doc = MusicXMLDocument.read(reader);
                if (reload) {
                    // write the document into memory
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    doc.write(new JseXmlWriter(bos));
                    bos.close();
                    // reload it from memory
                    ByteArrayInputStream in = new ByteArrayInputStream(bos.toByteArray());
                    MusicXMLDocument.read(new JseXmlReader(in));
                    in.close();
                // doc.write(new JseXmlWriter(new JseOutputStream(new File("test.xml"))));
                }
                totalMusicXMLReadingTime += (System.currentTimeMillis() - lastTime);
            } catch (XmlException ex) {
                throw new Exception("Failed for " + dir + "/" + file.getName() + ": " + ex.getMessage(), ex);
            }
        }
    }
    // print time
    System.out.println("Total time for read" + (reload ? "/write/read: " : ": ") + totalMusicXMLReadingTime + " ms");
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) XmlException(com.xenoage.utils.xml.XmlException) JseXmlWriter(com.xenoage.utils.jse.xml.JseXmlWriter) JseXmlReader(com.xenoage.utils.jse.xml.JseXmlReader) XmlReader(com.xenoage.utils.xml.XmlReader) MusicXMLDocument(com.xenoage.zong.musicxml.MusicXMLDocument) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File) FileInputStream(java.io.FileInputStream) XmlException(com.xenoage.utils.xml.XmlException) JseXmlReader(com.xenoage.utils.jse.xml.JseXmlReader)

Example 3 with MusicXMLDocument

use of com.xenoage.zong.musicxml.MusicXMLDocument in project Zong by Xenoage.

the class StavesListReaderTest method createStavesList.

private StavesList createStavesList(String filePath) {
    MusicXMLDocument doc = null;
    try {
        doc = MusicXMLDocument.read(platformUtils().createXmlReader(jsePlatformUtils().openFile(filePath)));
    } catch (FileNotFoundException ex) {
        // file not there. ignore, since copyrighted file. - TODO: ask Michael Good for file license
        return null;
    } catch (Exception ex) {
        fail(ex.toString());
    }
    StavesListReader stavesListReader = new StavesListReader(doc.getScore(), new ErrorHandling(Level.ThrowException));
    StavesList stavesList = null;
    try {
        stavesList = stavesListReader.read();
    } catch (Exception ex) {
        fail(ex.toString());
    }
    return stavesList;
}
Also used : ErrorHandling(com.xenoage.zong.io.musicxml.in.util.ErrorHandling) StavesList(com.xenoage.zong.core.music.StavesList) FileNotFoundException(java.io.FileNotFoundException) MusicXMLDocument(com.xenoage.zong.musicxml.MusicXMLDocument) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

MusicXMLDocument (com.xenoage.zong.musicxml.MusicXMLDocument)3 XmlReader (com.xenoage.utils.xml.XmlReader)2 InvalidFormatException (com.xenoage.utils.exceptions.InvalidFormatException)1 JseXmlReader (com.xenoage.utils.jse.xml.JseXmlReader)1 JseXmlWriter (com.xenoage.utils.jse.xml.JseXmlWriter)1 XmlDataException (com.xenoage.utils.xml.XmlDataException)1 XmlException (com.xenoage.utils.xml.XmlException)1 StavesList (com.xenoage.zong.core.music.StavesList)1 ErrorHandling (com.xenoage.zong.io.musicxml.in.util.ErrorHandling)1 MxlScorePartwise (com.xenoage.zong.musicxml.types.MxlScorePartwise)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1