Search in sources :

Example 1 with Opus

use of com.xenoage.zong.io.musicxml.opus.Opus in project Zong by Xenoage.

the class MusicXmlFileReader method produce.

@Override
public void produce(final AsyncResult<List<Score>> callback) {
    final List<Score> ret = alist();
    // open stream
    BufferedInputStream bis = new BufferedInputStream(in);
    try {
        bis.mark();
        // file type
        FileType fileType = FileTypeReader.getFileType(bis);
        bis.reset();
        bis.unmark();
        // open file
        if (fileType == FileType.XMLScorePartwise) {
            Score score = new MusicXmlScoreFileInput().read(bis, path);
            ret.add(score);
            callback.onSuccess(ret);
        } else if (fileType == FileType.XMLOpus) {
            // opus
            if (path == null) {
                // no path is given. we can not read the linked files.
                callback.onSuccess(ret);
            } else {
                // read files
                final String directory = FileUtils.getDirectoryName(path);
                OpusFileInput opusInput = new OpusFileInput();
                Opus opus = opusInput.readOpusFile(bis);
                new OpusLinkResolver(opus, null, directory).produce(new AsyncResult<Opus>() {

                    @Override
                    public void onSuccess(Opus opus) {
                        try {
                            List<String> filePaths = scoreFileFilter.filter(opus.getScoreFilenames());
                            processNextScore(directory, filePaths, scoreFileFilter, ret, callback);
                        } catch (IOException ex) {
                            callback.onFailure(ex);
                        }
                    }

                    @Override
                    public void onFailure(Exception ex) {
                        callback.onFailure(ex);
                    }
                });
            }
        } else if (fileType == FileType.Compressed) {
            CompressedFileInput zip = new CompressedFileInput(bis);
            List<String> filePaths = scoreFileFilter.filter(zip.getScoreFilenames());
            for (String filePath : filePaths) {
                Score score = zip.loadScore(filePath);
                ret.add(score);
            }
            zip.close();
            callback.onSuccess(ret);
        } else {
            callback.onFailure(new IOException("Unknown file type"));
        }
    } catch (IOException ex) {
        // try to close input stream
        bis.close();
        // return failure
        callback.onFailure(ex);
    }
}
Also used : IOException(java.io.IOException) IOException(java.io.IOException) Score(com.xenoage.zong.core.Score) BufferedInputStream(com.xenoage.utils.io.BufferedInputStream) FileType(com.xenoage.zong.io.musicxml.FileType) Opus(com.xenoage.zong.io.musicxml.opus.Opus) AsyncResult(com.xenoage.utils.async.AsyncResult)

Example 2 with Opus

use of com.xenoage.zong.io.musicxml.opus.Opus in project Zong by Xenoage.

the class OpusFileInputTest method test.

@Test
public void test() throws Exception {
    OpusFileInput opusInput = new OpusFileInput();
    String dir = "data/test/scores/MxlOpusFileInputTest";
    Opus opus = opusInput.readOpusFile(jsePlatformUtils().openFile(dir + "/SomeOpus.xml"));
    // must contain one score, one opus and one opus-link
    assertTrue(opus.getItems().get(0) instanceof com.xenoage.zong.io.musicxml.opus.Score);
    assertTrue(opus.getItems().get(1) instanceof com.xenoage.zong.io.musicxml.opus.Opus);
    assertTrue(opus.getItems().get(2) instanceof com.xenoage.zong.io.musicxml.opus.OpusLink);
    // resolve links
    opus = sync(new OpusLinkResolver(opus, null, dir));
    // check flattened list of scores and load the files
    List<String> scores = opus.getScoreFilenames();
    assertEquals(4, scores.size());
    String[] scoresExpected = new String[] { "BeetAnGeSample.xml", "BrahWiMeSample.mxl", "DebuMandSample.xml", "SchbAvMaSample.xml" };
    for (String scoreExpected : scoresExpected) {
        try {
            assertTrue(scoreExpected + " not found", scores.contains(scoreExpected));
            String filePath = dir + "/" + scoreExpected;
            sync(new MusicXmlFileReader(jsePlatformUtils().openFile(filePath), filePath, new AllFilter<>()));
        } catch (Exception ex) {
            throw new Exception("Failed to load " + scoreExpected, ex);
        }
    }
}
Also used : Opus(com.xenoage.zong.io.musicxml.opus.Opus) AllFilter(com.xenoage.utils.filter.AllFilter) Opus(com.xenoage.zong.io.musicxml.opus.Opus) Test(org.junit.Test)

Example 3 with Opus

use of com.xenoage.zong.io.musicxml.opus.Opus in project Zong by Xenoage.

the class OpusLinkResolver method processNextItem.

/**
 * Processes the next opus item in the input queue, or finishes the processing
 * if the queue is empty.
 */
private void processNextItem() {
    if (input.size() > 0) {
        // another item to resolve
        OpusItem inputItem = input.remove(0);
        if (inputItem instanceof OpusLink) {
            // opus link; must be resolved
            String filePath = ((OpusLink) inputItem).getLink().getHref();
            if (zip != null) {
                // read zipped opus file
                InputStream opusStream;
                try {
                    opusStream = zip.openFile(filePath);
                    resolveItem(opusStream);
                } catch (com.xenoage.utils.io.FileNotFoundException ex) {
                    callback.onFailure(ex);
                }
            } else if (basePath != null) {
                // read plain opus file
                platformUtils().openFileAsync(basePath + "/" + filePath, new AsyncResult<InputStream>() {

                    @Override
                    public void onSuccess(InputStream opusStream) {
                        resolveItem(opusStream);
                    }

                    @Override
                    public void onFailure(Exception ex) {
                        callback.onFailure(ex);
                    }
                });
            } else {
                callback.onFailure(new IOException("neither zip nor basePath is given"));
            }
        } else if (inputItem instanceof Opus) {
            // opus; can contain links which must be resolved
            Opus childOpus = (Opus) inputItem;
            new OpusLinkResolver(childOpus, zip, basePath).produce(new AsyncResult<Opus>() {

                @Override
                public void onSuccess(Opus opus) {
                    acc.add(opus);
                    // item finished, next one
                    processNextItem();
                }

                @Override
                public void onFailure(Exception ex) {
                    callback.onFailure(ex);
                }
            });
        } else {
            // simple case. item needs not to be resolved, just add it
            acc.add(inputItem);
            processNextItem();
        }
    } else {
        // all items resolved
        callback.onSuccess(new Opus(opus.getTitle(), acc));
    }
}
Also used : InputStream(com.xenoage.utils.io.InputStream) OpusItem(com.xenoage.zong.io.musicxml.opus.OpusItem) Opus(com.xenoage.zong.io.musicxml.opus.Opus) IOException(java.io.IOException) AsyncResult(com.xenoage.utils.async.AsyncResult) IOException(java.io.IOException) OpusLink(com.xenoage.zong.io.musicxml.opus.OpusLink)

Example 4 with Opus

use of com.xenoage.zong.io.musicxml.opus.Opus in project Zong by Xenoage.

the class OpusLinkResolver method resolveItem.

private void resolveItem(InputStream stream) {
    // read opus
    Opus newOpus = null;
    try {
        newOpus = new OpusFileInput().readOpusFile(stream);
    } catch (Exception ex) {
        callback.onFailure(ex);
        return;
    }
    // this opus can again have links. resolve them recursively.
    new OpusLinkResolver(newOpus, zip, basePath).produce(new AsyncResult<Opus>() {

        @Override
        public void onSuccess(Opus opus) {
            acc.add(opus);
            // item finished, next one
            processNextItem();
        }

        @Override
        public void onFailure(Exception ex) {
            callback.onFailure(ex);
        }
    });
}
Also used : Opus(com.xenoage.zong.io.musicxml.opus.Opus) IOException(java.io.IOException)

Aggregations

Opus (com.xenoage.zong.io.musicxml.opus.Opus)4 IOException (java.io.IOException)3 AsyncResult (com.xenoage.utils.async.AsyncResult)2 AllFilter (com.xenoage.utils.filter.AllFilter)1 BufferedInputStream (com.xenoage.utils.io.BufferedInputStream)1 InputStream (com.xenoage.utils.io.InputStream)1 Score (com.xenoage.zong.core.Score)1 FileType (com.xenoage.zong.io.musicxml.FileType)1 OpusItem (com.xenoage.zong.io.musicxml.opus.OpusItem)1 OpusLink (com.xenoage.zong.io.musicxml.opus.OpusLink)1 Test (org.junit.Test)1