1 | package org.apollo.audio.util;
|
---|
2 |
|
---|
3 | /**
|
---|
4 | *
|
---|
5 | * A Timeline for a Frame contains two componants:
|
---|
6 | *
|
---|
7 | * <ul>
|
---|
8 | * <li>The initation point: This is the smallest initiation time
|
---|
9 | * <b>(not neccessarily zero)</b> in the frame - at its X pixel.
|
---|
10 | * <li>The end point: This is how long the frame runs for - and the
|
---|
11 | * x-pixel position of the last played track in the frame.
|
---|
12 | * </ul>
|
---|
13 | *
|
---|
14 | * They are simple imutable objects thus are not kept consistant with model.
|
---|
15 | *
|
---|
16 | * @author Brook Novak
|
---|
17 | *
|
---|
18 | */
|
---|
19 | public class Timeline {
|
---|
20 |
|
---|
21 | private long firstInitiationTime;
|
---|
22 | private long runningTime;
|
---|
23 | private int initiationXPixel;
|
---|
24 | private int pixelWidth;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | *
|
---|
28 | * @param firstInitiationTime
|
---|
29 | *
|
---|
30 | * @param runningTime
|
---|
31 | * Must be larger than zero.
|
---|
32 | *
|
---|
33 | * @param initiationXPixel
|
---|
34 | * Relative can be anything
|
---|
35 | *
|
---|
36 | * @param pixelWidth
|
---|
37 | * Must be larger than zero
|
---|
38 | *
|
---|
39 | */
|
---|
40 | public Timeline(long firstInitiationTime, long runningTime, int initiationXPixel, int pixelWidth) {
|
---|
41 | assert(runningTime > 0);
|
---|
42 | assert(pixelWidth > 0);
|
---|
43 |
|
---|
44 | this.firstInitiationTime = firstInitiationTime;
|
---|
45 | this.runningTime = runningTime;
|
---|
46 | this.initiationXPixel = initiationXPixel;
|
---|
47 | this.pixelWidth = pixelWidth;
|
---|
48 | }
|
---|
49 |
|
---|
50 |
|
---|
51 | public long getFirstInitiationTime() {
|
---|
52 | return firstInitiationTime;
|
---|
53 | }
|
---|
54 |
|
---|
55 |
|
---|
56 | public int getInitiationXPixel() {
|
---|
57 | return initiationXPixel;
|
---|
58 | }
|
---|
59 |
|
---|
60 |
|
---|
61 | public long getRunningTime() {
|
---|
62 | return runningTime;
|
---|
63 | }
|
---|
64 |
|
---|
65 | public int getPixelWidth() {
|
---|
66 | return pixelWidth;
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * @return
|
---|
71 | * The amount of time in milliseconds per pixel.
|
---|
72 | */
|
---|
73 | public double getTimePerPixel() {
|
---|
74 | double msPerPixel = runningTime;
|
---|
75 | return msPerPixel /= (double)pixelWidth;
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * @return
|
---|
80 | * The amount of time in milliseconds per pixel.
|
---|
81 | */
|
---|
82 | public double getPixelPerTime() {
|
---|
83 | double pxPerMS = (double)pixelWidth;
|
---|
84 | return pxPerMS /= runningTime;
|
---|
85 | }
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Calcautes the a time in milliseconds from a given x-pixel position.
|
---|
89 | *
|
---|
90 | * @param x
|
---|
91 | * The X pixel position.
|
---|
92 | *
|
---|
93 | * @return
|
---|
94 | * An extrapolated time value. <B>CAN BE NEGATIVE</B> - if the x position is to the left
|
---|
95 | * of the initiation x pixel and happens to be far enough from the pixel such
|
---|
96 | * that the ms-diff is larget than the initiation time.
|
---|
97 | */
|
---|
98 | public long getMSTimeAtX(int x) {
|
---|
99 | double msPerPixel = getTimePerPixel();
|
---|
100 | return firstInitiationTime + (long)(msPerPixel * (x - initiationXPixel));
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Calculates the x-pixel position in a timeline from a given ms time.
|
---|
105 | *
|
---|
106 | * @param time
|
---|
107 | * The time in ms.
|
---|
108 | *
|
---|
109 | * @return
|
---|
110 | * The x pixel.
|
---|
111 | */
|
---|
112 | public int getXAtMSTime(long time) {
|
---|
113 |
|
---|
114 | long diffTime = time - firstInitiationTime;
|
---|
115 |
|
---|
116 | return initiationXPixel + (int)(diffTime / getTimePerPixel());
|
---|
117 | }
|
---|
118 |
|
---|
119 |
|
---|
120 | @Override
|
---|
121 | public String toString() {
|
---|
122 | return "firstInitiationTime=" + firstInitiationTime
|
---|
123 | + ", runningTime=" + runningTime
|
---|
124 | + ", initiationXPixel=" + initiationXPixel
|
---|
125 | + ", pixelWidth=" + pixelWidth;
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 |
|
---|
130 | } |
---|