View Javadoc
1   package net.kreatious.pianoleopard.midi.track;
2   
3   import javax.sound.midi.Track;
4   
5   import net.kreatious.pianoleopard.intervalset.IntervalSet;
6   import net.kreatious.pianoleopard.midi.event.Event;
7   import net.kreatious.pianoleopard.midi.event.EventPair;
8   import net.kreatious.pianoleopard.midi.event.NoteEvent;
9   import net.kreatious.pianoleopard.midi.event.PedalEvent;
10  import net.kreatious.pianoleopard.midi.event.TempoCache;
11  
12  /**
13   * Represents a parsed MIDI track with efficient retrieval by time range.
14   *
15   * @author Jay-R Studer
16   */
17  class ImmutableParsedTrack implements ParsedTrack {
18      private final IntervalSet<EventPair<NoteEvent>> notes;
19      private final IntervalSet<EventPair<PedalEvent>> pedals;
20  
21      /**
22       * Constructs a new {@link ImmutableParsedTrack} by parsing the specified
23       * {@link Track}.
24       *
25       * @param track
26       *            the MIDI track to parse
27       * @param cache
28       *            the {@link TempoCache} to convert ticks into microseconds
29       */
30      ImmutableParsedTrack(Track track, TempoCache cache) {
31          notes = new IntervalSet<>();
32          pedals = new IntervalSet<>();
33          new TrackVisitor() {
34              @Override
35              @SuppressWarnings("unchecked")
36              protected void visitEventPair(EventPair<? extends Event> pair) {
37                  if (pair.getOff() instanceof NoteEvent) {
38                      notes.put(pair.getOnTime(), pair.getOffTime(), (EventPair<NoteEvent>) pair);
39                  } else if (pair.getOff() instanceof PedalEvent) {
40                      pedals.put(pair.getOnTime(), pair.getOffTime(), (EventPair<PedalEvent>) pair);
41                  }
42              }
43          }.accept(track, cache);
44      }
45  
46      @Override
47      public Iterable<EventPair<NoteEvent>> getNotePairs(long low, long high) {
48          return notes.subSet(low, high);
49      }
50  
51      @Override
52      public Iterable<EventPair<PedalEvent>> getPedalPairs(long low, long high) {
53          return pedals.subSet(low, high);
54      }
55  }