Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TimedTextObject -> getCaptionByTime #16

Open
ldimitroff opened this issue Apr 7, 2015 · 3 comments
Open

TimedTextObject -> getCaptionByTime #16

ldimitroff opened this issue Apr 7, 2015 · 3 comments

Comments

@ldimitroff
Copy link

We're using your code to parse captions and display it. Here's a code improvement that did in order to get captions by time

    private static final Integer INVALID_INDEX = -1;
    private Caption currentCaption = null;
    private Caption nextCaption = null;
    private Caption firstCaption = null;
    private Caption lastCaption = null;
    private Iterator<Integer> indexIterator = null;

    public void initCaptionsSearch() {
        currentCaption = null;
        nextCaption = getCaptionAtIndex(getFirstIndex());
        firstCaption = nextCaption;
        lastCaption = getCaptionAtIndex(getLastIndex());
        indexIterator = null;
        if (captions != null)
            indexIterator = captions.navigableKeySet().iterator();
    }

    private Integer getFirstIndex() {
        if (captions != null && !captions.isEmpty())
            return captions.firstKey();

        return INVALID_INDEX;
    }

    private Integer getLastIndex() {
        if (captions != null && !captions.isEmpty())
            return captions.lastKey();

        return INVALID_INDEX;
    }

    private void advanceToNext() {
        currentCaption = nextCaption;
        Integer nextIndex;
        if (indexIterator != null && indexIterator.hasNext())
            nextIndex = indexIterator.next();
        else
            nextIndex = INVALID_INDEX;
        nextCaption = getCaptionAtIndex(nextIndex);
    }

    private void advanceToLast() {
        currentCaption = getCaptionAtIndex(getLastIndex());
        nextCaption = null;
        indexIterator = null;
    }

    private Caption getCaptionAtIndex(Integer index) {
        if (captions != null && index != INVALID_INDEX)
            return captions.get(index);
        return null;
    }

    public Caption getCaption(int currentMillis) {

        if (firstCaption == null || lastCaption == null)
            return null;

        if (currentCaption == null && nextCaption == null)
            return null;

        if (firstCaption.start.mseconds > currentMillis) {
            if (currentCaption != null)
                initCaptionsSearch();
            return null;
        }

        if (lastCaption.end.mseconds < currentMillis) {
            advanceToLast();
            return null;
        }

        Caption testCaption = currentCaption;
        boolean usingNext = false;
        if (testCaption == null) {
            usingNext = true;
            testCaption = nextCaption;
        }

        int comparation = testCaption.compareToMillis(currentMillis);
        switch (comparation) {
        case Caption.COMPARATION_CAPTION_ENCLOSES:
            if (usingNext)
                advanceToNext();
            return testCaption;
        case Caption.COMPARATION_CAPTION_IS_BEFORE:
            return lookForwardToEnd(currentMillis);
        case Caption.COMPARATION_CAPTION_IS_AFTER:
            if (usingNext) {
                return null;
            } else {
                initCaptionsSearch();
                return lookForwardUntilCaption(currentMillis, testCaption);
            }
        }
        return null;
    }

    private Caption lookForwardToEnd(int millis) {
        return lookForwardUntilCaption(millis,
                getCaptionAtIndex(getLastIndex()));
    }

    private Caption lookForwardUntilCaption(int millis, Caption toCaption) {
        if (toCaption == null)
            return null;

        while (nextCaption != null && currentCaption != toCaption) {
            int comparation = nextCaption.compareToMillis(millis);
            switch (comparation) {
            case Caption.COMPARATION_CAPTION_ENCLOSES:
                advanceToNext();
                return currentCaption;
            case Caption.COMPARATION_CAPTION_IS_BEFORE:
                break;
            case Caption.COMPARATION_CAPTION_IS_AFTER:
                return null;
            }
            advanceToNext();
        }
        return null;
    }

And for Caption class:

    public static final int COMPARATION_CAPTION_IS_BEFORE = -1;
    public static final int COMPARATION_CAPTION_IS_AFTER = 1;
    public static final int COMPARATION_CAPTION_ENCLOSES = 0;

    public int compareToMillis(int millis) {
        // Starts before current time
        if (start.mseconds <= millis) {
            // Starts before and ends after millis
            if (end.mseconds >= millis) {
                // millis is between start and end
                return COMPARATION_CAPTION_ENCLOSES;
            } else { // Starts and ends before millis
                return COMPARATION_CAPTION_IS_BEFORE;
            }
        } else { // Starts after millis
            return COMPARATION_CAPTION_IS_AFTER;
        }
    }
@JDaren
Copy link
Owner

JDaren commented Apr 7, 2015

Nice! I'm glad my code is helpful, when I decided to open-source it all I was hoping was for my efforts to not go wasted in just getting me a good university grade. However, the way I stored the captions in a TreeMap means the sorting is inherent, and the search, acceptable... Your code seems very implementation specific, I'm having trouble deciding if it will be usable to others or a good fit in a TTO... I'm guessing this is a sort of informal pull request? You would like to give something to the community and offer them the navigation by time functionality?

@ldimitroff
Copy link
Author

yeah, I mean, if you want to use it, this is what we did adapting on your code and give a navigation as the time goes by, returning the caption related to it's time. It actually can work with anything that uses a tree map. You can always improve this code or just let others implement their own methods, just thought it is a good way to give to the users a navigation by time.

@JDaren
Copy link
Owner

JDaren commented Apr 9, 2015

I'll add it in a helper class, and give you credit in the javadoc. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants