MusicPattern.java
1.13 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
public class MusicPattern {
/* Pattern definition :
* 0 : low time
* 1 : rise time
* 2 : high time
* 3 : fall time
*/
private int[] pattern; // in ms
private int totalLentgh;
private int[] patternTiming = new int[4]; // in ms
private long begin; // in ms
private long stop; // in ms
public MusicPattern(int[] pattern, int repeat, long begin) {
if(pattern.length != 4)
throw new InvalidPatternException();
this.pattern = pattern;
this.totalLentgh = pattern[0] + pattern[1] + pattern[2] + pattern[3];
this.patternTiming[0] = pattern[0];
for(int i=1; i<4; i++)
this.patternTiming[i] = this.patternTiming[i-1] + pattern[i];
this.begin = begin;
this.stop = repeat * totalLentgh;
}
public double render(long tick) { // tick in ms
if(tick <= begin)
return 0;
if(tick >= stop)
return 0;
int tickMod = (int) (tick % totalLentgh);
if(tickMod < patternTiming[0])
return 0;
if(tickMod < patternTiming[1])
return (double) (tickMod - patternTiming[0]) / (double) (pattern[1]);
if(tickMod < patternTiming[2])
return 1;
return 1 - ((double) (tickMod - patternTiming[2]) / (double) (pattern[3]));
}
}