Blame view

VRGNYMusicLights/Sources/ApplicationJava/MusicPattern.java 1.54 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;
d175ba55   pfrison   VRGNYMusicLight a...
26
  		this.stop = repeat * totalLength + begin;
278cdee0   pfrison   VRGNYMusicLights ...
27
  		this.repeat = repeat;
a89c030d   pfrison   VRGNYMusicLights ...
28
  	}
278cdee0   pfrison   VRGNYMusicLights ...
29
  
d175ba55   pfrison   VRGNYMusicLight a...
30
  	public int[] getPattern() { return pattern; }
278cdee0   pfrison   VRGNYMusicLights ...
31
32
33
34
35
  	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 ...
36
37
38
39
40
41
  	
  	public double render(long tick) { // tick in ms
  		if(tick <= begin)
  			return 0;
  		if(tick >= stop)
  			return 0;
278cdee0   pfrison   VRGNYMusicLights ...
42
  		int tickMod = (int) (tick % totalLength);
a89c030d   pfrison   VRGNYMusicLights ...
43
44
45
46
47
48
  		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 ...
49
50
51
  		if(tickMod < patternTiming[3])
  			return 1 - ((double) (tickMod - patternTiming[2]) / (double) (pattern[3]));
  		return 0;
742429d1   pfrison   VRGNYMusicLight b...
52
53
  	}
  }