Search in sources :

Example 1 with DynamicsType

use of com.xenoage.zong.io.midi.out.dynamics.type.DynamicsType in project Zong by Xenoage.

the class DynamicsFinder method findStaffDynamics.

/**
 * Walk through the measures of the given staff and repetition and find the dynamics.
 * Starts with the given initial dynamic. Returns the dynamic value at the end of the
 * repetition.
 */
private DynamicValue findStaffDynamics(int staff, int repetition, DynamicValue startDynamics) {
    val rep = repetitions.get(repetition);
    // walk through the measures
    Time currentStartTime = rep.start;
    DynamicsType currentDynamic = new FixedDynamics(startDynamics);
    for (int iMeasure : range(rep.start.getMeasure(), rep.end.getMeasure())) {
        if (iMeasure >= score.getMeasuresCount())
            break;
        val measure = score.getMeasure(atMeasure(staff, iMeasure));
        for (val beat : measure.getDirections().getBeats()) {
            val newTime = Companion.time(iMeasure, beat);
            DynamicsType newDynamic = null;
            // if beat is out of the repetition range, ignore it
            if (false == rep.contains(newTime))
                continue;
            // find dynamics change
            if (currentDynamic instanceof GradientDynamics) {
                // wedge ending at this beat?
                val closedWedge = getWedgeEndAt(staff, newTime, (GradientDynamics) currentDynamic);
                if (closedWedge != null) {
                    currentDynamic = closedWedge;
                    // continue with the end dynamic of the wedge
                    newDynamic = new FixedDynamics(closedWedge.end);
                }
            } else if (newDynamic == null) {
                // new dynamic starting? then close currently open period and open new one
                newDynamic = getStaffDynamicStartAt(staff, newTime, currentDynamic.getEndValue());
            }
            // when change was found, apply it
            if (newDynamic != null) {
                if (false == currentStartTime.equals(newTime) && false == currentDynamic.equals(newDynamic)) {
                    val period = new DynamicsPeriod(currentStartTime, newTime, currentDynamic);
                    periods.addPeriodToStaff(period, staff, repetition);
                    currentStartTime = newTime;
                }
                currentDynamic = newDynamic;
            }
        }
    }
    // close the dynamics period at the end of the repetition
    val period = new DynamicsPeriod(currentStartTime, rep.end, currentDynamic);
    periods.addPeriodToStaff(period, staff, repetition);
    return currentDynamic.getEndValue();
}
Also used : lombok.val(lombok.val) DynamicsType(com.xenoage.zong.io.midi.out.dynamics.type.DynamicsType) FixedDynamics(com.xenoage.zong.io.midi.out.dynamics.type.FixedDynamics) Time(com.xenoage.zong.core.position.Time) GradientDynamics(com.xenoage.zong.io.midi.out.dynamics.type.GradientDynamics)

Aggregations

Time (com.xenoage.zong.core.position.Time)1 DynamicsType (com.xenoage.zong.io.midi.out.dynamics.type.DynamicsType)1 FixedDynamics (com.xenoage.zong.io.midi.out.dynamics.type.FixedDynamics)1 GradientDynamics (com.xenoage.zong.io.midi.out.dynamics.type.GradientDynamics)1 lombok.val (lombok.val)1