Blame view

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