Search in sources :

Example 1 with It

use of com.xenoage.utils.iterators.It in project Zong by Xenoage.

the class LyricStamper method createUnderscoreStampings.

/**
 * Creates and returns at least one {@link StaffTextStamping} containing
 * an underscore line ("___") behind the given lyric stamping
 * up to the given notehead stamping.
 * If more than one line is needed, a stamping for each line is returned.
 *
 * A list of consecutive staff stampings must be given, containing at least
 * the beginning and the ending staff of the underscore. If the underscore needs
 * only one staff, it may also be null.
 *
 * The left syllable must be given, even if it is not in the current frame.
 * The right notehead is optional. If not given, the underscore is drawn over
 * all given following staves.
 */
public IList<StaffTextStamping> createUnderscoreStampings(StaffTextStamping syllableLeft, NoteheadStamping noteheadRight, FormattedTextStyle style, List<StaffStamping> staffStampings) {
    if (syllableLeft == null)
        throw new IllegalArgumentException("Left syllable must be given");
    CList<StaffTextStamping> ret = clist();
    // measure width of "_"
    float widthU = Companion.fText("_", style, Alignment.Center).getFirstParagraph().getMetrics().getWidth();
    // compute the horizontal start position, base line and element
    float widthLeft = syllableLeft.getText().getFirstParagraph().getMetrics().getWidth();
    // widthU / 4: just some distance
    float startX = syllableLeft.position.xMm + widthLeft / 2 + widthU / 4;
    float baseLine = syllableLeft.position.lp;
    Object element = syllableLeft.element;
    // if end notehead is given, compute the end position
    float endX = 0;
    if (noteheadRight != null) {
        endX = noteheadRight.position.xMm;
    }
    // underscore line on a single staff?
    if (noteheadRight != null && syllableLeft.parentStaff == noteheadRight.parentStaff) {
        // simple case
        ret.add(createUnderscoreStamping(startX, endX, baseLine, widthU, style, syllableLeft.parentStaff, element));
    } else {
        It<StaffStamping> staves = new It<>(staffStampings);
        StaffStamping currentStaff = null;
        // only true, when start stamping is found
        boolean firstStaffFound = false;
        // only true, when stop stamping is found
        boolean lastStaffFound = false;
        // find the start staff
        StaffStamping s1 = syllableLeft.parentStaff;
        while (staves.hasNext()) {
            currentStaff = staves.next();
            if (currentStaff == s1) {
                firstStaffFound = true;
                break;
            }
        }
        // if not found, begin at the very beginning
        if (!firstStaffFound) {
            staves = new It<>(staffStampings);
        }
        // first staff (if any): begin at the stamping, go to the end of the system
        if (firstStaffFound) {
            ret.add(createUnderscoreStamping(startX, currentStaff.positionMm.x + currentStaff.lengthMm, baseLine, widthU, style, currentStaff, element));
        }
        // to the end of the system
        while (staves.hasNext()) {
            currentStaff = staves.next();
            if (noteheadRight != null && currentStaff == noteheadRight.parentStaff) {
                // stop, because this is the last staff, where we have the stop notehead
                lastStaffFound = true;
                break;
            }
            // create underscore over whole staff
            ret.add(createUnderscoreStamping(currentStaff.positionMm.x, currentStaff.positionMm.x + currentStaff.lengthMm, baseLine, widthU, style, currentStaff, element));
        }
        // system, stop at the notehead
        if (lastStaffFound) {
            ret.add(createUnderscoreStamping(currentStaff.positionMm.x, noteheadRight.position.xMm, baseLine, widthU, style, currentStaff, element));
        }
    }
    return ret;
}
Also used : StaffStamping(com.xenoage.zong.musiclayout.stampings.StaffStamping) StaffTextStamping(com.xenoage.zong.musiclayout.stampings.StaffTextStamping) It(com.xenoage.utils.iterators.It)

Example 2 with It

use of com.xenoage.utils.iterators.It in project Zong by Xenoage.

the class FileToMidiConvert method execute.

@Override
public void execute() {
    FileChooser fileChooser = new FileChooser();
    // use last document directory
    File initDir = FileSettings.getLastDir();
    if (initDir != null)
        fileChooser.setInitialDirectory(initDir);
    // add filters
    SupportedFormats<?> supportedFormats = app().getSupportedFormats();
    for (FileFormat<?> fileFormat : supportedFormats.getReadFormats()) {
        addFilter(fileChooser, fileFormat);
    }
    // show the dialog
    File file = fileChooser.showOpenDialog(ownerWindow);
    if (file != null) {
        INSTANCE.log(Companion.remark("Dialog closed (OK), converting file \"" + file.getName() + "\""));
        // save document directory
        FileSettings.rememberDir(file);
        // convert - TODO: show progress
        String lastPath = file.getAbsolutePath();
        List<Score> scores = pApp().loadMxlScores(lastPath, new AllFilter<>());
        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 newPath = lastPath;
            String number = (useNumber ? ("-" + (scoresIt.getIndex() + 1)) : "");
            if (// TIDY: share code: FilenameUtils.numberFiles
            newPath.toLowerCase().endsWith(".xml") || newPath.toLowerCase().endsWith(".mxl")) {
                newPath = newPath.substring(0, newPath.length() - 4);
            }
            newPath += (number + ".mid");
            try {
                MidiSystem.write(seq, 1, new File(newPath));
            } catch (Exception ex) {
                handle(warning(Voc.ErrorSavingFile));
            }
        }
    } else {
        INSTANCE.log(Companion.remark("Dialog closed (Cancel)"));
    }
}
Also used : It(com.xenoage.utils.iterators.It) Sequence(javax.sound.midi.Sequence) Score(com.xenoage.zong.core.Score) FileChooser(javafx.stage.FileChooser) JseMidiSequenceWriter(com.xenoage.zong.desktop.io.midi.out.JseMidiSequenceWriter) File(java.io.File)

Example 3 with It

use of com.xenoage.utils.iterators.It 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

It (com.xenoage.utils.iterators.It)3 Score (com.xenoage.zong.core.Score)2 JseMidiSequenceWriter (com.xenoage.zong.desktop.io.midi.out.JseMidiSequenceWriter)2 File (java.io.File)2 Sequence (javax.sound.midi.Sequence)2 JseInputStream (com.xenoage.utils.jse.io.JseInputStream)1 FileType (com.xenoage.zong.io.musicxml.FileType)1 StaffStamping (com.xenoage.zong.musiclayout.stampings.StaffStamping)1 StaffTextStamping (com.xenoage.zong.musiclayout.stampings.StaffTextStamping)1 IOException (java.io.IOException)1 DirectoryChooser (javafx.stage.DirectoryChooser)1 FileChooser (javafx.stage.FileChooser)1