MusicPattern.java
1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
public class MusicPattern {
/* Pattern definition :
* 0 : low time
* 1 : rise time
* 2 : high time
* 3 : fall time
* 4 : low time
*/
private int[] pattern; // in ms
private int totalLength;
private int[] patternTiming = new int[5]; // in ms
private long begin; // in ms
private long stop; // in ms
private int repeat;
public MusicPattern(int[] pattern, int repeat, long begin) {
if(pattern.length != 5)
throw new InvalidPatternException();
this.pattern = pattern;
this.totalLength = pattern[0] + pattern[1] + pattern[2] + pattern[3] + pattern[4];
this.patternTiming[0] = pattern[0];
for(int i=1; i<5; i++)
this.patternTiming[i] = this.patternTiming[i-1] + pattern[i];
this.begin = begin;
this.stop = repeat * totalLength + begin;
this.repeat = repeat;
}
public int[] getPattern() { return pattern; }
public int[] getPatternTiming() { return patternTiming; }
public long getStop() { return stop; }
public long getBegin() { return begin; }
public int getRepeat() { return repeat; }
public int getTotalLength () { return totalLength; }
public double render(long tick) { // tick in ms
if(tick <= begin)
return 0;
if(tick >= stop)
return 0;
int tickMod = (int) (tick % totalLength);
if(tickMod < patternTiming[0])
return 0;
if(tickMod < patternTiming[1])
return (double) (tickMod - patternTiming[0]) / (double) (pattern[1]);
if(tickMod < patternTiming[2])
return 1;
if(tickMod < patternTiming[3])
return 1 - ((double) (tickMod - patternTiming[2]) / (double) (pattern[3]));
return 0;
}
}