Search in sources :

Example 1 with FileType

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

the class CompressedFileInput method loadScore.

/**
 * Loads and returns the {@link com.xenoage.zong.core.Score} at the given path.
 */
public com.xenoage.zong.core.Score loadScore(String path) throws InvalidFormatException, IOException {
    BufferedInputStream bis = new BufferedInputStream(zip.openFile(path));
    // XML or compressed?
    bis.mark();
    FileType fileType = FileTypeReader.getFileType(bis);
    bis.reset();
    bis.unmark();
    if (fileType == null)
        throw new InvalidFormatException("Score has invalid format: " + path);
    com.xenoage.zong.core.Score ret = null;
    switch(fileType) {
        case Compressed:
            ret = loadCompressedScore(path);
            break;
        case XMLScorePartwise:
            ret = new MusicXmlScoreFileInput().read(bis, path);
            break;
        case XMLScoreTimewise:
            throw new IllegalStateException("score-timewise is currently not implemented");
        default:
            throw new InvalidFormatException("Score has invalid format: " + path);
    }
    bis.close();
    return ret;
}
Also used : Score(com.xenoage.zong.io.musicxml.opus.Score) BufferedInputStream(com.xenoage.utils.io.BufferedInputStream) FileType(com.xenoage.zong.io.musicxml.FileType) InvalidFormatException(com.xenoage.utils.exceptions.InvalidFormatException)

Example 2 with FileType

use of com.xenoage.zong.io.musicxml.FileType 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 3 with FileType

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

the class DirToMidiConvert method execute.

@Override
public void execute() {
    DirectoryChooser dc = new DirectoryChooser();
    // use last document directory
    File initDir = FileSettings.getLastDir();
    if (initDir != null)
        dc.setInitialDirectory(initDir);
    /* TODO - how to extend the DirectoryChooser in JavaFX?
		//see http://stackoverflow.com/questions/25982945/javafx-filechooser-and-directorychooser-accessory-component
		JCheckBox chkSubdir = new JCheckBox(Lang.get(Voc.IncludeSubdirectories), true);
		JCheckBox chkCancel = new JCheckBox(Lang.get(Voc.CancelAtFirstError), false);
		JPanel pnlOptions = new JPanel();
		pnlOptions.setLayout(new BoxLayout(pnlOptions, BoxLayout.Y_AXIS));
		pnlOptions.add(chkSubdir);
		pnlOptions.add(chkCancel);
		fc.setAccessory(pnlOptions); */
    // TODO
    boolean subDirs = true;
    // TODO
    boolean cancelOnFirstError = false;
    File dir = dc.showDialog(ownerWindow);
    if (dir != null) {
        // remember directory
        FileSettings.rememberDir(dir);
        // start conversion - TODO: show progress
        List<File> files = listFiles(dir, subDirs);
        int countOK = 0;
        int countFailed = 0;
        for (File file : files) {
            try {
                // only process MusicXML files
                FileType fileType = FileTypeReader.getFileType(new JseInputStream(file));
                if (fileType != null) {
                    String filePath = file.getAbsolutePath();
                    List<Score> scores = pApp().loadMxlScores(filePath, new AllFilter<>());
                    if ((scores.size() == 0)) /* TODO && chkCancel.isSelected() */
                    {
                        countFailed++;
                        break;
                    }
                    boolean useNumber = scores.size() > 1;
                    It<Score> scoresIt = new It<>(scores);
                    for (Score score : scoresIt) {
                        Sequence seq = MidiConverter.convertToSequence(score, optionsForFileExport, new JseMidiSequenceWriter()).getSequence();
                        String number = (useNumber ? ("-" + (scoresIt.getIndex() + 1)) : "");
                        String newPath = filePath;
                        if (filePath.toLowerCase().endsWith(".xml") || filePath.toLowerCase().endsWith(".mxl")) {
                            newPath = newPath.substring(0, filePath.length() - 4);
                        }
                        newPath += (number + ".mid");
                        MidiSystem.write(seq, 1, new File(newPath));
                        countOK++;
                    }
                }
            } catch (IOException ex) {
                countFailed++;
                if (cancelOnFirstError) {
                    break;
                }
            }
        }
        app().showMessageDialog(Lang.get(Voc.DirectoryConversionResult, "" + countOK, "" + countFailed));
    }
}
Also used : It(com.xenoage.utils.iterators.It) Sequence(javax.sound.midi.Sequence) IOException(java.io.IOException) Score(com.xenoage.zong.core.Score) FileType(com.xenoage.zong.io.musicxml.FileType) JseInputStream(com.xenoage.utils.jse.io.JseInputStream) JseMidiSequenceWriter(com.xenoage.zong.desktop.io.midi.out.JseMidiSequenceWriter) File(java.io.File) DirectoryChooser(javafx.stage.DirectoryChooser)

Aggregations

FileType (com.xenoage.zong.io.musicxml.FileType)3 BufferedInputStream (com.xenoage.utils.io.BufferedInputStream)2 Score (com.xenoage.zong.core.Score)2 IOException (java.io.IOException)2 AsyncResult (com.xenoage.utils.async.AsyncResult)1 InvalidFormatException (com.xenoage.utils.exceptions.InvalidFormatException)1 It (com.xenoage.utils.iterators.It)1 JseInputStream (com.xenoage.utils.jse.io.JseInputStream)1 JseMidiSequenceWriter (com.xenoage.zong.desktop.io.midi.out.JseMidiSequenceWriter)1 Opus (com.xenoage.zong.io.musicxml.opus.Opus)1 Score (com.xenoage.zong.io.musicxml.opus.Score)1 File (java.io.File)1 DirectoryChooser (javafx.stage.DirectoryChooser)1 Sequence (javax.sound.midi.Sequence)1