Java Code Examples for com.sun.media.sound.MidiUtils#isMetaEndOfTrack()

The following examples show how to use com.sun.media.sound.MidiUtils#isMetaEndOfTrack() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Track.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Adds a new event to the track. However, if the event is already contained
 * in the track, it is not added again. The list of events is kept in time
 * order, meaning that this event inserted at the appropriate place in the
 * list, not necessarily at the end.
 *
 * @param  event the event to add
 * @return {@code true} if the event did not already exist in the track and
 *         was added, otherwise {@code false}
 */
public boolean add(MidiEvent event) {
    if (event == null) {
        return false;
    }
    synchronized(eventsList) {

        if (!set.contains(event)) {
            int eventsCount = eventsList.size();

            // get the last event
            MidiEvent lastEvent = null;
            if (eventsCount > 0) {
                lastEvent = eventsList.get(eventsCount - 1);
            }
            // sanity check that we have a correct end-of-track
            if (lastEvent != eotEvent) {
                // if there is no eot event, add our immutable instance again
                if (lastEvent != null) {
                    // set eotEvent's tick to the last tick of the track
                    eotEvent.setTick(lastEvent.getTick());
                } else {
                    // if the events list is empty, just set the tick to 0
                    eotEvent.setTick(0);
                }
                // we needn't check for a duplicate of eotEvent in "eventsList",
                // since then it would appear in the set.
                eventsList.add(eotEvent);
                set.add(eotEvent);
                eventsCount = eventsList.size();
            }

            // first see if we are trying to add
            // and endoftrack event.
            if (MidiUtils.isMetaEndOfTrack(event.getMessage())) {
                // since end of track event is useful
                // for delays at the end of a track, we want to keep
                // the tick value requested here if it is greater
                // than the one on the eot we are maintaining.
                // Otherwise, we only want a single eot event, so ignore.
                if (event.getTick() > eotEvent.getTick()) {
                    eotEvent.setTick(event.getTick());
                }
                return true;
            }

            // prevent duplicates
            set.add(event);

            // insert event such that events is sorted in increasing
            // tick order
            int i = eventsCount;
            for ( ; i > 0; i--) {
                if (event.getTick() >= (eventsList.get(i-1)).getTick()) {
                    break;
                }
            }
            if (i == eventsCount) {
                // we're adding an event after the
                // tick value of our eot, so push the eot out.
                // Always add at the end for better performance:
                // this saves all the checks and arraycopy when inserting

                // overwrite eot with new event
                eventsList.set(eventsCount - 1, event);
                // set new time of eot, if necessary
                if (eotEvent.getTick() < event.getTick()) {
                    eotEvent.setTick(event.getTick());
                }
                // add eot again at the end
                eventsList.add(eotEvent);
            } else {
                eventsList.add(i, event);
            }
            return true;
        }
    }

    return false;
}
 
Example 2
Source File: Track.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Adds a new event to the track.  However, if the event is already
 * contained in the track, it is not added again.  The list of events
 * is kept in time order, meaning that this event inserted at the
 * appropriate place in the list, not necessarily at the end.
 *
 * @param event the event to add
 * @return <code>true</code> if the event did not already exist in the
 * track and was added, otherwise <code>false</code>
 */
public boolean add(MidiEvent event) {
    if (event == null) {
        return false;
    }
    synchronized(eventsList) {

        if (!set.contains(event)) {
            int eventsCount = eventsList.size();

            // get the last event
            MidiEvent lastEvent = null;
            if (eventsCount > 0) {
                lastEvent = (MidiEvent) eventsList.get(eventsCount - 1);
            }
            // sanity check that we have a correct end-of-track
            if (lastEvent != eotEvent) {
                // if there is no eot event, add our immutable instance again
                if (lastEvent != null) {
                    // set eotEvent's tick to the last tick of the track
                    eotEvent.setTick(lastEvent.getTick());
                } else {
                    // if the events list is empty, just set the tick to 0
                    eotEvent.setTick(0);
                }
                // we needn't check for a duplicate of eotEvent in "eventsList",
                // since then it would appear in the set.
                eventsList.add(eotEvent);
                set.add(eotEvent);
                eventsCount = eventsList.size();
            }

            // first see if we are trying to add
            // and endoftrack event.
            if (MidiUtils.isMetaEndOfTrack(event.getMessage())) {
                // since end of track event is useful
                // for delays at the end of a track, we want to keep
                // the tick value requested here if it is greater
                // than the one on the eot we are maintaining.
                // Otherwise, we only want a single eot event, so ignore.
                if (event.getTick() > eotEvent.getTick()) {
                    eotEvent.setTick(event.getTick());
                }
                return true;
            }

            // prevent duplicates
            set.add(event);

            // insert event such that events is sorted in increasing
            // tick order
            int i = eventsCount;
            for ( ; i > 0; i--) {
                if (event.getTick() >= ((MidiEvent)eventsList.get(i-1)).getTick()) {
                    break;
                }
            }
            if (i == eventsCount) {
                // we're adding an event after the
                // tick value of our eot, so push the eot out.
                // Always add at the end for better performance:
                // this saves all the checks and arraycopy when inserting

                // overwrite eot with new event
                eventsList.set(eventsCount - 1, event);
                // set new time of eot, if necessary
                if (eotEvent.getTick() < event.getTick()) {
                    eotEvent.setTick(event.getTick());
                }
                // add eot again at the end
                eventsList.add(eotEvent);
            } else {
                eventsList.add(i, event);
            }
            return true;
        }
    }

    return false;
}
 
Example 3
Source File: Track.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Adds a new event to the track.  However, if the event is already
 * contained in the track, it is not added again.  The list of events
 * is kept in time order, meaning that this event inserted at the
 * appropriate place in the list, not necessarily at the end.
 *
 * @param event the event to add
 * @return <code>true</code> if the event did not already exist in the
 * track and was added, otherwise <code>false</code>
 */
public boolean add(MidiEvent event) {
    if (event == null) {
        return false;
    }
    synchronized(eventsList) {

        if (!set.contains(event)) {
            int eventsCount = eventsList.size();

            // get the last event
            MidiEvent lastEvent = null;
            if (eventsCount > 0) {
                lastEvent = (MidiEvent) eventsList.get(eventsCount - 1);
            }
            // sanity check that we have a correct end-of-track
            if (lastEvent != eotEvent) {
                // if there is no eot event, add our immutable instance again
                if (lastEvent != null) {
                    // set eotEvent's tick to the last tick of the track
                    eotEvent.setTick(lastEvent.getTick());
                } else {
                    // if the events list is empty, just set the tick to 0
                    eotEvent.setTick(0);
                }
                // we needn't check for a duplicate of eotEvent in "eventsList",
                // since then it would appear in the set.
                eventsList.add(eotEvent);
                set.add(eotEvent);
                eventsCount = eventsList.size();
            }

            // first see if we are trying to add
            // and endoftrack event.
            if (MidiUtils.isMetaEndOfTrack(event.getMessage())) {
                // since end of track event is useful
                // for delays at the end of a track, we want to keep
                // the tick value requested here if it is greater
                // than the one on the eot we are maintaining.
                // Otherwise, we only want a single eot event, so ignore.
                if (event.getTick() > eotEvent.getTick()) {
                    eotEvent.setTick(event.getTick());
                }
                return true;
            }

            // prevent duplicates
            set.add(event);

            // insert event such that events is sorted in increasing
            // tick order
            int i = eventsCount;
            for ( ; i > 0; i--) {
                if (event.getTick() >= ((MidiEvent)eventsList.get(i-1)).getTick()) {
                    break;
                }
            }
            if (i == eventsCount) {
                // we're adding an event after the
                // tick value of our eot, so push the eot out.
                // Always add at the end for better performance:
                // this saves all the checks and arraycopy when inserting

                // overwrite eot with new event
                eventsList.set(eventsCount - 1, event);
                // set new time of eot, if necessary
                if (eotEvent.getTick() < event.getTick()) {
                    eotEvent.setTick(event.getTick());
                }
                // add eot again at the end
                eventsList.add(eotEvent);
            } else {
                eventsList.add(i, event);
            }
            return true;
        }
    }

    return false;
}
 
Example 4
Source File: Track.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Adds a new event to the track.  However, if the event is already
 * contained in the track, it is not added again.  The list of events
 * is kept in time order, meaning that this event inserted at the
 * appropriate place in the list, not necessarily at the end.
 *
 * @param event the event to add
 * @return <code>true</code> if the event did not already exist in the
 * track and was added, otherwise <code>false</code>
 */
public boolean add(MidiEvent event) {
    if (event == null) {
        return false;
    }
    synchronized(eventsList) {

        if (!set.contains(event)) {
            int eventsCount = eventsList.size();

            // get the last event
            MidiEvent lastEvent = null;
            if (eventsCount > 0) {
                lastEvent = (MidiEvent) eventsList.get(eventsCount - 1);
            }
            // sanity check that we have a correct end-of-track
            if (lastEvent != eotEvent) {
                // if there is no eot event, add our immutable instance again
                if (lastEvent != null) {
                    // set eotEvent's tick to the last tick of the track
                    eotEvent.setTick(lastEvent.getTick());
                } else {
                    // if the events list is empty, just set the tick to 0
                    eotEvent.setTick(0);
                }
                // we needn't check for a duplicate of eotEvent in "eventsList",
                // since then it would appear in the set.
                eventsList.add(eotEvent);
                set.add(eotEvent);
                eventsCount = eventsList.size();
            }

            // first see if we are trying to add
            // and endoftrack event.
            if (MidiUtils.isMetaEndOfTrack(event.getMessage())) {
                // since end of track event is useful
                // for delays at the end of a track, we want to keep
                // the tick value requested here if it is greater
                // than the one on the eot we are maintaining.
                // Otherwise, we only want a single eot event, so ignore.
                if (event.getTick() > eotEvent.getTick()) {
                    eotEvent.setTick(event.getTick());
                }
                return true;
            }

            // prevent duplicates
            set.add(event);

            // insert event such that events is sorted in increasing
            // tick order
            int i = eventsCount;
            for ( ; i > 0; i--) {
                if (event.getTick() >= ((MidiEvent)eventsList.get(i-1)).getTick()) {
                    break;
                }
            }
            if (i == eventsCount) {
                // we're adding an event after the
                // tick value of our eot, so push the eot out.
                // Always add at the end for better performance:
                // this saves all the checks and arraycopy when inserting

                // overwrite eot with new event
                eventsList.set(eventsCount - 1, event);
                // set new time of eot, if necessary
                if (eotEvent.getTick() < event.getTick()) {
                    eotEvent.setTick(event.getTick());
                }
                // add eot again at the end
                eventsList.add(eotEvent);
            } else {
                eventsList.add(i, event);
            }
            return true;
        }
    }

    return false;
}
 
Example 5
Source File: Track.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Adds a new event to the track.  However, if the event is already
 * contained in the track, it is not added again.  The list of events
 * is kept in time order, meaning that this event inserted at the
 * appropriate place in the list, not necessarily at the end.
 *
 * @param event the event to add
 * @return <code>true</code> if the event did not already exist in the
 * track and was added, otherwise <code>false</code>
 */
public boolean add(MidiEvent event) {
    if (event == null) {
        return false;
    }
    synchronized(eventsList) {

        if (!set.contains(event)) {
            int eventsCount = eventsList.size();

            // get the last event
            MidiEvent lastEvent = null;
            if (eventsCount > 0) {
                lastEvent = (MidiEvent) eventsList.get(eventsCount - 1);
            }
            // sanity check that we have a correct end-of-track
            if (lastEvent != eotEvent) {
                // if there is no eot event, add our immutable instance again
                if (lastEvent != null) {
                    // set eotEvent's tick to the last tick of the track
                    eotEvent.setTick(lastEvent.getTick());
                } else {
                    // if the events list is empty, just set the tick to 0
                    eotEvent.setTick(0);
                }
                // we needn't check for a duplicate of eotEvent in "eventsList",
                // since then it would appear in the set.
                eventsList.add(eotEvent);
                set.add(eotEvent);
                eventsCount = eventsList.size();
            }

            // first see if we are trying to add
            // and endoftrack event.
            if (MidiUtils.isMetaEndOfTrack(event.getMessage())) {
                // since end of track event is useful
                // for delays at the end of a track, we want to keep
                // the tick value requested here if it is greater
                // than the one on the eot we are maintaining.
                // Otherwise, we only want a single eot event, so ignore.
                if (event.getTick() > eotEvent.getTick()) {
                    eotEvent.setTick(event.getTick());
                }
                return true;
            }

            // prevent duplicates
            set.add(event);

            // insert event such that events is sorted in increasing
            // tick order
            int i = eventsCount;
            for ( ; i > 0; i--) {
                if (event.getTick() >= ((MidiEvent)eventsList.get(i-1)).getTick()) {
                    break;
                }
            }
            if (i == eventsCount) {
                // we're adding an event after the
                // tick value of our eot, so push the eot out.
                // Always add at the end for better performance:
                // this saves all the checks and arraycopy when inserting

                // overwrite eot with new event
                eventsList.set(eventsCount - 1, event);
                // set new time of eot, if necessary
                if (eotEvent.getTick() < event.getTick()) {
                    eotEvent.setTick(event.getTick());
                }
                // add eot again at the end
                eventsList.add(eotEvent);
            } else {
                eventsList.add(i, event);
            }
            return true;
        }
    }

    return false;
}
 
Example 6
Source File: Track.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Adds a new event to the track.  However, if the event is already
 * contained in the track, it is not added again.  The list of events
 * is kept in time order, meaning that this event inserted at the
 * appropriate place in the list, not necessarily at the end.
 *
 * @param event the event to add
 * @return <code>true</code> if the event did not already exist in the
 * track and was added, otherwise <code>false</code>
 */
public boolean add(MidiEvent event) {
    if (event == null) {
        return false;
    }
    synchronized(eventsList) {

        if (!set.contains(event)) {
            int eventsCount = eventsList.size();

            // get the last event
            MidiEvent lastEvent = null;
            if (eventsCount > 0) {
                lastEvent = (MidiEvent) eventsList.get(eventsCount - 1);
            }
            // sanity check that we have a correct end-of-track
            if (lastEvent != eotEvent) {
                // if there is no eot event, add our immutable instance again
                if (lastEvent != null) {
                    // set eotEvent's tick to the last tick of the track
                    eotEvent.setTick(lastEvent.getTick());
                } else {
                    // if the events list is empty, just set the tick to 0
                    eotEvent.setTick(0);
                }
                // we needn't check for a duplicate of eotEvent in "eventsList",
                // since then it would appear in the set.
                eventsList.add(eotEvent);
                set.add(eotEvent);
                eventsCount = eventsList.size();
            }

            // first see if we are trying to add
            // and endoftrack event.
            if (MidiUtils.isMetaEndOfTrack(event.getMessage())) {
                // since end of track event is useful
                // for delays at the end of a track, we want to keep
                // the tick value requested here if it is greater
                // than the one on the eot we are maintaining.
                // Otherwise, we only want a single eot event, so ignore.
                if (event.getTick() > eotEvent.getTick()) {
                    eotEvent.setTick(event.getTick());
                }
                return true;
            }

            // prevent duplicates
            set.add(event);

            // insert event such that events is sorted in increasing
            // tick order
            int i = eventsCount;
            for ( ; i > 0; i--) {
                if (event.getTick() >= ((MidiEvent)eventsList.get(i-1)).getTick()) {
                    break;
                }
            }
            if (i == eventsCount) {
                // we're adding an event after the
                // tick value of our eot, so push the eot out.
                // Always add at the end for better performance:
                // this saves all the checks and arraycopy when inserting

                // overwrite eot with new event
                eventsList.set(eventsCount - 1, event);
                // set new time of eot, if necessary
                if (eotEvent.getTick() < event.getTick()) {
                    eotEvent.setTick(event.getTick());
                }
                // add eot again at the end
                eventsList.add(eotEvent);
            } else {
                eventsList.add(i, event);
            }
            return true;
        }
    }

    return false;
}
 
Example 7
Source File: Track.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Adds a new event to the track.  However, if the event is already
 * contained in the track, it is not added again.  The list of events
 * is kept in time order, meaning that this event inserted at the
 * appropriate place in the list, not necessarily at the end.
 *
 * @param event the event to add
 * @return <code>true</code> if the event did not already exist in the
 * track and was added, otherwise <code>false</code>
 */
public boolean add(MidiEvent event) {
    if (event == null) {
        return false;
    }
    synchronized(eventsList) {

        if (!set.contains(event)) {
            int eventsCount = eventsList.size();

            // get the last event
            MidiEvent lastEvent = null;
            if (eventsCount > 0) {
                lastEvent = (MidiEvent) eventsList.get(eventsCount - 1);
            }
            // sanity check that we have a correct end-of-track
            if (lastEvent != eotEvent) {
                // if there is no eot event, add our immutable instance again
                if (lastEvent != null) {
                    // set eotEvent's tick to the last tick of the track
                    eotEvent.setTick(lastEvent.getTick());
                } else {
                    // if the events list is empty, just set the tick to 0
                    eotEvent.setTick(0);
                }
                // we needn't check for a duplicate of eotEvent in "eventsList",
                // since then it would appear in the set.
                eventsList.add(eotEvent);
                set.add(eotEvent);
                eventsCount = eventsList.size();
            }

            // first see if we are trying to add
            // and endoftrack event.
            if (MidiUtils.isMetaEndOfTrack(event.getMessage())) {
                // since end of track event is useful
                // for delays at the end of a track, we want to keep
                // the tick value requested here if it is greater
                // than the one on the eot we are maintaining.
                // Otherwise, we only want a single eot event, so ignore.
                if (event.getTick() > eotEvent.getTick()) {
                    eotEvent.setTick(event.getTick());
                }
                return true;
            }

            // prevent duplicates
            set.add(event);

            // insert event such that events is sorted in increasing
            // tick order
            int i = eventsCount;
            for ( ; i > 0; i--) {
                if (event.getTick() >= ((MidiEvent)eventsList.get(i-1)).getTick()) {
                    break;
                }
            }
            if (i == eventsCount) {
                // we're adding an event after the
                // tick value of our eot, so push the eot out.
                // Always add at the end for better performance:
                // this saves all the checks and arraycopy when inserting

                // overwrite eot with new event
                eventsList.set(eventsCount - 1, event);
                // set new time of eot, if necessary
                if (eotEvent.getTick() < event.getTick()) {
                    eotEvent.setTick(event.getTick());
                }
                // add eot again at the end
                eventsList.add(eotEvent);
            } else {
                eventsList.add(i, event);
            }
            return true;
        }
    }

    return false;
}
 
Example 8
Source File: Track.java    From Java8CN with Apache License 2.0 4 votes vote down vote up
/**
 * Adds a new event to the track.  However, if the event is already
 * contained in the track, it is not added again.  The list of events
 * is kept in time order, meaning that this event inserted at the
 * appropriate place in the list, not necessarily at the end.
 *
 * @param event the event to add
 * @return <code>true</code> if the event did not already exist in the
 * track and was added, otherwise <code>false</code>
 */
public boolean add(MidiEvent event) {
    if (event == null) {
        return false;
    }
    synchronized(eventsList) {

        if (!set.contains(event)) {
            int eventsCount = eventsList.size();

            // get the last event
            MidiEvent lastEvent = null;
            if (eventsCount > 0) {
                lastEvent = (MidiEvent) eventsList.get(eventsCount - 1);
            }
            // sanity check that we have a correct end-of-track
            if (lastEvent != eotEvent) {
                // if there is no eot event, add our immutable instance again
                if (lastEvent != null) {
                    // set eotEvent's tick to the last tick of the track
                    eotEvent.setTick(lastEvent.getTick());
                } else {
                    // if the events list is empty, just set the tick to 0
                    eotEvent.setTick(0);
                }
                // we needn't check for a duplicate of eotEvent in "eventsList",
                // since then it would appear in the set.
                eventsList.add(eotEvent);
                set.add(eotEvent);
                eventsCount = eventsList.size();
            }

            // first see if we are trying to add
            // and endoftrack event.
            if (MidiUtils.isMetaEndOfTrack(event.getMessage())) {
                // since end of track event is useful
                // for delays at the end of a track, we want to keep
                // the tick value requested here if it is greater
                // than the one on the eot we are maintaining.
                // Otherwise, we only want a single eot event, so ignore.
                if (event.getTick() > eotEvent.getTick()) {
                    eotEvent.setTick(event.getTick());
                }
                return true;
            }

            // prevent duplicates
            set.add(event);

            // insert event such that events is sorted in increasing
            // tick order
            int i = eventsCount;
            for ( ; i > 0; i--) {
                if (event.getTick() >= ((MidiEvent)eventsList.get(i-1)).getTick()) {
                    break;
                }
            }
            if (i == eventsCount) {
                // we're adding an event after the
                // tick value of our eot, so push the eot out.
                // Always add at the end for better performance:
                // this saves all the checks and arraycopy when inserting

                // overwrite eot with new event
                eventsList.set(eventsCount - 1, event);
                // set new time of eot, if necessary
                if (eotEvent.getTick() < event.getTick()) {
                    eotEvent.setTick(event.getTick());
                }
                // add eot again at the end
                eventsList.add(eotEvent);
            } else {
                eventsList.add(i, event);
            }
            return true;
        }
    }

    return false;
}
 
Example 9
Source File: Track.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Adds a new event to the track.  However, if the event is already
 * contained in the track, it is not added again.  The list of events
 * is kept in time order, meaning that this event inserted at the
 * appropriate place in the list, not necessarily at the end.
 *
 * @param event the event to add
 * @return <code>true</code> if the event did not already exist in the
 * track and was added, otherwise <code>false</code>
 */
public boolean add(MidiEvent event) {
    if (event == null) {
        return false;
    }
    synchronized(eventsList) {

        if (!set.contains(event)) {
            int eventsCount = eventsList.size();

            // get the last event
            MidiEvent lastEvent = null;
            if (eventsCount > 0) {
                lastEvent = (MidiEvent) eventsList.get(eventsCount - 1);
            }
            // sanity check that we have a correct end-of-track
            if (lastEvent != eotEvent) {
                // if there is no eot event, add our immutable instance again
                if (lastEvent != null) {
                    // set eotEvent's tick to the last tick of the track
                    eotEvent.setTick(lastEvent.getTick());
                } else {
                    // if the events list is empty, just set the tick to 0
                    eotEvent.setTick(0);
                }
                // we needn't check for a duplicate of eotEvent in "eventsList",
                // since then it would appear in the set.
                eventsList.add(eotEvent);
                set.add(eotEvent);
                eventsCount = eventsList.size();
            }

            // first see if we are trying to add
            // and endoftrack event.
            if (MidiUtils.isMetaEndOfTrack(event.getMessage())) {
                // since end of track event is useful
                // for delays at the end of a track, we want to keep
                // the tick value requested here if it is greater
                // than the one on the eot we are maintaining.
                // Otherwise, we only want a single eot event, so ignore.
                if (event.getTick() > eotEvent.getTick()) {
                    eotEvent.setTick(event.getTick());
                }
                return true;
            }

            // prevent duplicates
            set.add(event);

            // insert event such that events is sorted in increasing
            // tick order
            int i = eventsCount;
            for ( ; i > 0; i--) {
                if (event.getTick() >= ((MidiEvent)eventsList.get(i-1)).getTick()) {
                    break;
                }
            }
            if (i == eventsCount) {
                // we're adding an event after the
                // tick value of our eot, so push the eot out.
                // Always add at the end for better performance:
                // this saves all the checks and arraycopy when inserting

                // overwrite eot with new event
                eventsList.set(eventsCount - 1, event);
                // set new time of eot, if necessary
                if (eotEvent.getTick() < event.getTick()) {
                    eotEvent.setTick(event.getTick());
                }
                // add eot again at the end
                eventsList.add(eotEvent);
            } else {
                eventsList.add(i, event);
            }
            return true;
        }
    }

    return false;
}
 
Example 10
Source File: Track.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
/**
 * Adds a new event to the track.  However, if the event is already
 * contained in the track, it is not added again.  The list of events
 * is kept in time order, meaning that this event inserted at the
 * appropriate place in the list, not necessarily at the end.
 *
 * @param event the event to add
 * @return <code>true</code> if the event did not already exist in the
 * track and was added, otherwise <code>false</code>
 */
public boolean add(MidiEvent event) {
    if (event == null) {
        return false;
    }
    synchronized(eventsList) {

        if (!set.contains(event)) {
            int eventsCount = eventsList.size();

            // get the last event
            MidiEvent lastEvent = null;
            if (eventsCount > 0) {
                lastEvent = (MidiEvent) eventsList.get(eventsCount - 1);
            }
            // sanity check that we have a correct end-of-track
            if (lastEvent != eotEvent) {
                // if there is no eot event, add our immutable instance again
                if (lastEvent != null) {
                    // set eotEvent's tick to the last tick of the track
                    eotEvent.setTick(lastEvent.getTick());
                } else {
                    // if the events list is empty, just set the tick to 0
                    eotEvent.setTick(0);
                }
                // we needn't check for a duplicate of eotEvent in "eventsList",
                // since then it would appear in the set.
                eventsList.add(eotEvent);
                set.add(eotEvent);
                eventsCount = eventsList.size();
            }

            // first see if we are trying to add
            // and endoftrack event.
            if (MidiUtils.isMetaEndOfTrack(event.getMessage())) {
                // since end of track event is useful
                // for delays at the end of a track, we want to keep
                // the tick value requested here if it is greater
                // than the one on the eot we are maintaining.
                // Otherwise, we only want a single eot event, so ignore.
                if (event.getTick() > eotEvent.getTick()) {
                    eotEvent.setTick(event.getTick());
                }
                return true;
            }

            // prevent duplicates
            set.add(event);

            // insert event such that events is sorted in increasing
            // tick order
            int i = eventsCount;
            for ( ; i > 0; i--) {
                if (event.getTick() >= ((MidiEvent)eventsList.get(i-1)).getTick()) {
                    break;
                }
            }
            if (i == eventsCount) {
                // we're adding an event after the
                // tick value of our eot, so push the eot out.
                // Always add at the end for better performance:
                // this saves all the checks and arraycopy when inserting

                // overwrite eot with new event
                eventsList.set(eventsCount - 1, event);
                // set new time of eot, if necessary
                if (eotEvent.getTick() < event.getTick()) {
                    eotEvent.setTick(event.getTick());
                }
                // add eot again at the end
                eventsList.add(eotEvent);
            } else {
                eventsList.add(i, event);
            }
            return true;
        }
    }

    return false;
}
 
Example 11
Source File: Track.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * Adds a new event to the track. However, if the event is already contained
 * in the track, it is not added again. The list of events is kept in time
 * order, meaning that this event inserted at the appropriate place in the
 * list, not necessarily at the end.
 *
 * @param  event the event to add
 * @return {@code true} if the event did not already exist in the track and
 *         was added, otherwise {@code false}
 */
public boolean add(MidiEvent event) {
    if (event == null) {
        return false;
    }
    synchronized(eventsList) {

        if (!set.contains(event)) {
            int eventsCount = eventsList.size();

            // get the last event
            MidiEvent lastEvent = null;
            if (eventsCount > 0) {
                lastEvent = eventsList.get(eventsCount - 1);
            }
            // sanity check that we have a correct end-of-track
            if (lastEvent != eotEvent) {
                // if there is no eot event, add our immutable instance again
                if (lastEvent != null) {
                    // set eotEvent's tick to the last tick of the track
                    eotEvent.setTick(lastEvent.getTick());
                } else {
                    // if the events list is empty, just set the tick to 0
                    eotEvent.setTick(0);
                }
                // we needn't check for a duplicate of eotEvent in "eventsList",
                // since then it would appear in the set.
                eventsList.add(eotEvent);
                set.add(eotEvent);
                eventsCount = eventsList.size();
            }

            // first see if we are trying to add
            // and endoftrack event.
            if (MidiUtils.isMetaEndOfTrack(event.getMessage())) {
                // since end of track event is useful
                // for delays at the end of a track, we want to keep
                // the tick value requested here if it is greater
                // than the one on the eot we are maintaining.
                // Otherwise, we only want a single eot event, so ignore.
                if (event.getTick() > eotEvent.getTick()) {
                    eotEvent.setTick(event.getTick());
                }
                return true;
            }

            // prevent duplicates
            set.add(event);

            // insert event such that events is sorted in increasing
            // tick order
            int i = eventsCount;
            for ( ; i > 0; i--) {
                if (event.getTick() >= (eventsList.get(i-1)).getTick()) {
                    break;
                }
            }
            if (i == eventsCount) {
                // we're adding an event after the
                // tick value of our eot, so push the eot out.
                // Always add at the end for better performance:
                // this saves all the checks and arraycopy when inserting

                // overwrite eot with new event
                eventsList.set(eventsCount - 1, event);
                // set new time of eot, if necessary
                if (eotEvent.getTick() < event.getTick()) {
                    eotEvent.setTick(event.getTick());
                }
                // add eot again at the end
                eventsList.add(eotEvent);
            } else {
                eventsList.add(i, event);
            }
            return true;
        }
    }

    return false;
}
 
Example 12
Source File: Track.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Adds a new event to the track.  However, if the event is already
 * contained in the track, it is not added again.  The list of events
 * is kept in time order, meaning that this event inserted at the
 * appropriate place in the list, not necessarily at the end.
 *
 * @param event the event to add
 * @return <code>true</code> if the event did not already exist in the
 * track and was added, otherwise <code>false</code>
 */
public boolean add(MidiEvent event) {
    if (event == null) {
        return false;
    }
    synchronized(eventsList) {

        if (!set.contains(event)) {
            int eventsCount = eventsList.size();

            // get the last event
            MidiEvent lastEvent = null;
            if (eventsCount > 0) {
                lastEvent = (MidiEvent) eventsList.get(eventsCount - 1);
            }
            // sanity check that we have a correct end-of-track
            if (lastEvent != eotEvent) {
                // if there is no eot event, add our immutable instance again
                if (lastEvent != null) {
                    // set eotEvent's tick to the last tick of the track
                    eotEvent.setTick(lastEvent.getTick());
                } else {
                    // if the events list is empty, just set the tick to 0
                    eotEvent.setTick(0);
                }
                // we needn't check for a duplicate of eotEvent in "eventsList",
                // since then it would appear in the set.
                eventsList.add(eotEvent);
                set.add(eotEvent);
                eventsCount = eventsList.size();
            }

            // first see if we are trying to add
            // and endoftrack event.
            if (MidiUtils.isMetaEndOfTrack(event.getMessage())) {
                // since end of track event is useful
                // for delays at the end of a track, we want to keep
                // the tick value requested here if it is greater
                // than the one on the eot we are maintaining.
                // Otherwise, we only want a single eot event, so ignore.
                if (event.getTick() > eotEvent.getTick()) {
                    eotEvent.setTick(event.getTick());
                }
                return true;
            }

            // prevent duplicates
            set.add(event);

            // insert event such that events is sorted in increasing
            // tick order
            int i = eventsCount;
            for ( ; i > 0; i--) {
                if (event.getTick() >= ((MidiEvent)eventsList.get(i-1)).getTick()) {
                    break;
                }
            }
            if (i == eventsCount) {
                // we're adding an event after the
                // tick value of our eot, so push the eot out.
                // Always add at the end for better performance:
                // this saves all the checks and arraycopy when inserting

                // overwrite eot with new event
                eventsList.set(eventsCount - 1, event);
                // set new time of eot, if necessary
                if (eotEvent.getTick() < event.getTick()) {
                    eotEvent.setTick(event.getTick());
                }
                // add eot again at the end
                eventsList.add(eotEvent);
            } else {
                eventsList.add(i, event);
            }
            return true;
        }
    }

    return false;
}
 
Example 13
Source File: Track.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Adds a new event to the track.  However, if the event is already
 * contained in the track, it is not added again.  The list of events
 * is kept in time order, meaning that this event inserted at the
 * appropriate place in the list, not necessarily at the end.
 *
 * @param event the event to add
 * @return <code>true</code> if the event did not already exist in the
 * track and was added, otherwise <code>false</code>
 */
public boolean add(MidiEvent event) {
    if (event == null) {
        return false;
    }
    synchronized(eventsList) {

        if (!set.contains(event)) {
            int eventsCount = eventsList.size();

            // get the last event
            MidiEvent lastEvent = null;
            if (eventsCount > 0) {
                lastEvent = (MidiEvent) eventsList.get(eventsCount - 1);
            }
            // sanity check that we have a correct end-of-track
            if (lastEvent != eotEvent) {
                // if there is no eot event, add our immutable instance again
                if (lastEvent != null) {
                    // set eotEvent's tick to the last tick of the track
                    eotEvent.setTick(lastEvent.getTick());
                } else {
                    // if the events list is empty, just set the tick to 0
                    eotEvent.setTick(0);
                }
                // we needn't check for a duplicate of eotEvent in "eventsList",
                // since then it would appear in the set.
                eventsList.add(eotEvent);
                set.add(eotEvent);
                eventsCount = eventsList.size();
            }

            // first see if we are trying to add
            // and endoftrack event.
            if (MidiUtils.isMetaEndOfTrack(event.getMessage())) {
                // since end of track event is useful
                // for delays at the end of a track, we want to keep
                // the tick value requested here if it is greater
                // than the one on the eot we are maintaining.
                // Otherwise, we only want a single eot event, so ignore.
                if (event.getTick() > eotEvent.getTick()) {
                    eotEvent.setTick(event.getTick());
                }
                return true;
            }

            // prevent duplicates
            set.add(event);

            // insert event such that events is sorted in increasing
            // tick order
            int i = eventsCount;
            for ( ; i > 0; i--) {
                if (event.getTick() >= ((MidiEvent)eventsList.get(i-1)).getTick()) {
                    break;
                }
            }
            if (i == eventsCount) {
                // we're adding an event after the
                // tick value of our eot, so push the eot out.
                // Always add at the end for better performance:
                // this saves all the checks and arraycopy when inserting

                // overwrite eot with new event
                eventsList.set(eventsCount - 1, event);
                // set new time of eot, if necessary
                if (eotEvent.getTick() < event.getTick()) {
                    eotEvent.setTick(event.getTick());
                }
                // add eot again at the end
                eventsList.add(eotEvent);
            } else {
                eventsList.add(i, event);
            }
            return true;
        }
    }

    return false;
}
 
Example 14
Source File: Track.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
/**
 * Adds a new event to the track.  However, if the event is already
 * contained in the track, it is not added again.  The list of events
 * is kept in time order, meaning that this event inserted at the
 * appropriate place in the list, not necessarily at the end.
 *
 * @param event the event to add
 * @return <code>true</code> if the event did not already exist in the
 * track and was added, otherwise <code>false</code>
 */
public boolean add(MidiEvent event) {
    if (event == null) {
        return false;
    }
    synchronized(eventsList) {

        if (!set.contains(event)) {
            int eventsCount = eventsList.size();

            // get the last event
            MidiEvent lastEvent = null;
            if (eventsCount > 0) {
                lastEvent = (MidiEvent) eventsList.get(eventsCount - 1);
            }
            // sanity check that we have a correct end-of-track
            if (lastEvent != eotEvent) {
                // if there is no eot event, add our immutable instance again
                if (lastEvent != null) {
                    // set eotEvent's tick to the last tick of the track
                    eotEvent.setTick(lastEvent.getTick());
                } else {
                    // if the events list is empty, just set the tick to 0
                    eotEvent.setTick(0);
                }
                // we needn't check for a duplicate of eotEvent in "eventsList",
                // since then it would appear in the set.
                eventsList.add(eotEvent);
                set.add(eotEvent);
                eventsCount = eventsList.size();
            }

            // first see if we are trying to add
            // and endoftrack event.
            if (MidiUtils.isMetaEndOfTrack(event.getMessage())) {
                // since end of track event is useful
                // for delays at the end of a track, we want to keep
                // the tick value requested here if it is greater
                // than the one on the eot we are maintaining.
                // Otherwise, we only want a single eot event, so ignore.
                if (event.getTick() > eotEvent.getTick()) {
                    eotEvent.setTick(event.getTick());
                }
                return true;
            }

            // prevent duplicates
            set.add(event);

            // insert event such that events is sorted in increasing
            // tick order
            int i = eventsCount;
            for ( ; i > 0; i--) {
                if (event.getTick() >= ((MidiEvent)eventsList.get(i-1)).getTick()) {
                    break;
                }
            }
            if (i == eventsCount) {
                // we're adding an event after the
                // tick value of our eot, so push the eot out.
                // Always add at the end for better performance:
                // this saves all the checks and arraycopy when inserting

                // overwrite eot with new event
                eventsList.set(eventsCount - 1, event);
                // set new time of eot, if necessary
                if (eotEvent.getTick() < event.getTick()) {
                    eotEvent.setTick(event.getTick());
                }
                // add eot again at the end
                eventsList.add(eotEvent);
            } else {
                eventsList.add(i, event);
            }
            return true;
        }
    }

    return false;
}
 
Example 15
Source File: Track.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Adds a new event to the track.  However, if the event is already
 * contained in the track, it is not added again.  The list of events
 * is kept in time order, meaning that this event inserted at the
 * appropriate place in the list, not necessarily at the end.
 *
 * @param event the event to add
 * @return <code>true</code> if the event did not already exist in the
 * track and was added, otherwise <code>false</code>
 */
public boolean add(MidiEvent event) {
    if (event == null) {
        return false;
    }
    synchronized(eventsList) {

        if (!set.contains(event)) {
            int eventsCount = eventsList.size();

            // get the last event
            MidiEvent lastEvent = null;
            if (eventsCount > 0) {
                lastEvent = (MidiEvent) eventsList.get(eventsCount - 1);
            }
            // sanity check that we have a correct end-of-track
            if (lastEvent != eotEvent) {
                // if there is no eot event, add our immutable instance again
                if (lastEvent != null) {
                    // set eotEvent's tick to the last tick of the track
                    eotEvent.setTick(lastEvent.getTick());
                } else {
                    // if the events list is empty, just set the tick to 0
                    eotEvent.setTick(0);
                }
                // we needn't check for a duplicate of eotEvent in "eventsList",
                // since then it would appear in the set.
                eventsList.add(eotEvent);
                set.add(eotEvent);
                eventsCount = eventsList.size();
            }

            // first see if we are trying to add
            // and endoftrack event.
            if (MidiUtils.isMetaEndOfTrack(event.getMessage())) {
                // since end of track event is useful
                // for delays at the end of a track, we want to keep
                // the tick value requested here if it is greater
                // than the one on the eot we are maintaining.
                // Otherwise, we only want a single eot event, so ignore.
                if (event.getTick() > eotEvent.getTick()) {
                    eotEvent.setTick(event.getTick());
                }
                return true;
            }

            // prevent duplicates
            set.add(event);

            // insert event such that events is sorted in increasing
            // tick order
            int i = eventsCount;
            for ( ; i > 0; i--) {
                if (event.getTick() >= ((MidiEvent)eventsList.get(i-1)).getTick()) {
                    break;
                }
            }
            if (i == eventsCount) {
                // we're adding an event after the
                // tick value of our eot, so push the eot out.
                // Always add at the end for better performance:
                // this saves all the checks and arraycopy when inserting

                // overwrite eot with new event
                eventsList.set(eventsCount - 1, event);
                // set new time of eot, if necessary
                if (eotEvent.getTick() < event.getTick()) {
                    eotEvent.setTick(event.getTick());
                }
                // add eot again at the end
                eventsList.add(eotEvent);
            } else {
                eventsList.add(i, event);
            }
            return true;
        }
    }

    return false;
}
 
Example 16
Source File: Track.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Adds a new event to the track.  However, if the event is already
 * contained in the track, it is not added again.  The list of events
 * is kept in time order, meaning that this event inserted at the
 * appropriate place in the list, not necessarily at the end.
 *
 * @param event the event to add
 * @return <code>true</code> if the event did not already exist in the
 * track and was added, otherwise <code>false</code>
 */
public boolean add(MidiEvent event) {
    if (event == null) {
        return false;
    }
    synchronized(eventsList) {

        if (!set.contains(event)) {
            int eventsCount = eventsList.size();

            // get the last event
            MidiEvent lastEvent = null;
            if (eventsCount > 0) {
                lastEvent = (MidiEvent) eventsList.get(eventsCount - 1);
            }
            // sanity check that we have a correct end-of-track
            if (lastEvent != eotEvent) {
                // if there is no eot event, add our immutable instance again
                if (lastEvent != null) {
                    // set eotEvent's tick to the last tick of the track
                    eotEvent.setTick(lastEvent.getTick());
                } else {
                    // if the events list is empty, just set the tick to 0
                    eotEvent.setTick(0);
                }
                // we needn't check for a duplicate of eotEvent in "eventsList",
                // since then it would appear in the set.
                eventsList.add(eotEvent);
                set.add(eotEvent);
                eventsCount = eventsList.size();
            }

            // first see if we are trying to add
            // and endoftrack event.
            if (MidiUtils.isMetaEndOfTrack(event.getMessage())) {
                // since end of track event is useful
                // for delays at the end of a track, we want to keep
                // the tick value requested here if it is greater
                // than the one on the eot we are maintaining.
                // Otherwise, we only want a single eot event, so ignore.
                if (event.getTick() > eotEvent.getTick()) {
                    eotEvent.setTick(event.getTick());
                }
                return true;
            }

            // prevent duplicates
            set.add(event);

            // insert event such that events is sorted in increasing
            // tick order
            int i = eventsCount;
            for ( ; i > 0; i--) {
                if (event.getTick() >= ((MidiEvent)eventsList.get(i-1)).getTick()) {
                    break;
                }
            }
            if (i == eventsCount) {
                // we're adding an event after the
                // tick value of our eot, so push the eot out.
                // Always add at the end for better performance:
                // this saves all the checks and arraycopy when inserting

                // overwrite eot with new event
                eventsList.set(eventsCount - 1, event);
                // set new time of eot, if necessary
                if (eotEvent.getTick() < event.getTick()) {
                    eotEvent.setTick(event.getTick());
                }
                // add eot again at the end
                eventsList.add(eotEvent);
            } else {
                eventsList.add(i, event);
            }
            return true;
        }
    }

    return false;
}
 
Example 17
Source File: Track.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Adds a new event to the track.  However, if the event is already
 * contained in the track, it is not added again.  The list of events
 * is kept in time order, meaning that this event inserted at the
 * appropriate place in the list, not necessarily at the end.
 *
 * @param event the event to add
 * @return <code>true</code> if the event did not already exist in the
 * track and was added, otherwise <code>false</code>
 */
public boolean add(MidiEvent event) {
    if (event == null) {
        return false;
    }
    synchronized(eventsList) {

        if (!set.contains(event)) {
            int eventsCount = eventsList.size();

            // get the last event
            MidiEvent lastEvent = null;
            if (eventsCount > 0) {
                lastEvent = (MidiEvent) eventsList.get(eventsCount - 1);
            }
            // sanity check that we have a correct end-of-track
            if (lastEvent != eotEvent) {
                // if there is no eot event, add our immutable instance again
                if (lastEvent != null) {
                    // set eotEvent's tick to the last tick of the track
                    eotEvent.setTick(lastEvent.getTick());
                } else {
                    // if the events list is empty, just set the tick to 0
                    eotEvent.setTick(0);
                }
                // we needn't check for a duplicate of eotEvent in "eventsList",
                // since then it would appear in the set.
                eventsList.add(eotEvent);
                set.add(eotEvent);
                eventsCount = eventsList.size();
            }

            // first see if we are trying to add
            // and endoftrack event.
            if (MidiUtils.isMetaEndOfTrack(event.getMessage())) {
                // since end of track event is useful
                // for delays at the end of a track, we want to keep
                // the tick value requested here if it is greater
                // than the one on the eot we are maintaining.
                // Otherwise, we only want a single eot event, so ignore.
                if (event.getTick() > eotEvent.getTick()) {
                    eotEvent.setTick(event.getTick());
                }
                return true;
            }

            // prevent duplicates
            set.add(event);

            // insert event such that events is sorted in increasing
            // tick order
            int i = eventsCount;
            for ( ; i > 0; i--) {
                if (event.getTick() >= ((MidiEvent)eventsList.get(i-1)).getTick()) {
                    break;
                }
            }
            if (i == eventsCount) {
                // we're adding an event after the
                // tick value of our eot, so push the eot out.
                // Always add at the end for better performance:
                // this saves all the checks and arraycopy when inserting

                // overwrite eot with new event
                eventsList.set(eventsCount - 1, event);
                // set new time of eot, if necessary
                if (eotEvent.getTick() < event.getTick()) {
                    eotEvent.setTick(event.getTick());
                }
                // add eot again at the end
                eventsList.add(eotEvent);
            } else {
                eventsList.add(i, event);
            }
            return true;
        }
    }

    return false;
}