Example usage for org.apache.commons.math3.fraction BigFraction reduce

List of usage examples for org.apache.commons.math3.fraction BigFraction reduce

Introduction

In this page you can find the example usage for org.apache.commons.math3.fraction BigFraction reduce.

Prototype

public BigFraction reduce() 

Source Link

Document

Reduce this BigFraction to its lowest terms.

Usage

From source file:com.netflix.subtitles.ttml.TtmlUtils.java

/**
 * Reduce timed objects according to start and end and normalize timeExpressions according to virtual track times.
 * <p></p>/*from  w w  w . j av a2s  .co  m*/
 * dur attributes will be converted to end attributes and all time expression will be in SMPTE time code format. If
 * frameRate and frameRateMultiplier are not specified then default 30 and 1000 1001 values will be used.
 * <p></p>
 * region, span and set &lt;timeExpression&gt; will be ignored. Nested div elements are not supported too.
 *
 * @param tt        root timed object that will be reduced by times
 * @param offsetMS  virtual track time offset in millis
 * @param startMS   ttml content start time in millis
 * @param endMS     ttml content end time in millis
 * @param frameRate ttml new frame rate. If specified, timeExpressions will be normalized according new value.
 */
public static void reduceAccordingSegment(TtEltype tt, long offsetMS, long startMS, long endMS,
        BigFraction frameRate) {
    TtmlTimeConverter ttConverter = new TtmlTimeConverter(tt);

    long totalBegin = ttConverter.parseTimeExpression(tt.getBody().getBegin());

    // remove body timeExpressions
    tt.getBody().setBegin(null);
    tt.getBody().setEnd(null);
    tt.getBody().setDur(null);

    Iterator<DivEltype> divIt = tt.getBody().getDiv().iterator();
    while (divIt.hasNext()) {
        DivEltype div = divIt.next();

        totalBegin += ttConverter.parseTimeExpression(div.getBegin());

        // remove div timeExpressions
        div.setBegin(null);
        div.setEnd(null);
        div.setDur(null);

        // remove nested divs and filter p according interval [startMS; endMS]
        Iterator blockIt = div.getBlockClass().iterator(); // p or div
        while (blockIt.hasNext()) {
            Object blockClass = blockIt.next();
            if (!(blockClass instanceof PEltype)) {
                blockIt.remove();
                continue;
            }

            PEltype p = (PEltype) blockClass;
            long pBegin = totalBegin + ttConverter.parseTimeExpression(p.getBegin());
            long pEnd = totalBegin + getEnd(ttConverter, p.getBegin(), p.getEnd(), p.getDur());
            if (pEnd < startMS || pBegin > endMS) { // remove not matched
                blockIt.remove();
                continue;
            }

            if (pBegin < startMS) {
                pBegin = startMS;
            }
            if (pEnd > endMS) {
                pEnd = endMS;
            }

            // set p timeExpression according to a virtual track times
            p.setBegin(ConversionHelper.msToSmpteTimecode(offsetMS + pBegin - startMS,
                    frameRate == null ? ttConverter.getUnitsInSec() : frameRate));
            p.setEnd(ConversionHelper.msToSmpteTimecode(offsetMS + pEnd - startMS,
                    frameRate == null ? ttConverter.getUnitsInSec() : frameRate));
            p.setDur(null);
        }

        if (div.getBlockClass().isEmpty()) {
            divIt.remove();
        }
    }

    // update ttml values according to new frame rate
    if (frameRate != null) {
        frameRate = frameRate.reduce();
        if (frameRate.getDenominatorAsInt() == 1) {
            tt.setFrameRate(frameRate.getNumerator());
            tt.setFrameRateMultiplier("1 1");
        } else {
            BigInteger ttFrameRate = frameRate.getNumerator().divide(new BigInteger("1000"));
            tt.setFrameRate(ttFrameRate);
            tt.setFrameRateMultiplier("1000" + " " + frameRate.getDenominatorAsInt());
        }
    }
}