-
Notifications
You must be signed in to change notification settings - Fork 11
/
ChannelItem.ts
104 lines (89 loc) · 2.3 KB
/
ChannelItem.ts
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { Automaton, Curve } from '.';
import type { SerializedChannelItem } from './types/SerializedChannelItem';
/**
* Represents an item of a [[Channel]].
*/
export class ChannelItem {
/**
* The parent automaton.
*/
protected readonly __automaton: Automaton;
/**
* Beginning timepoint of the item.
*/
public time!: number;
/**
* Length of the item.
*/
public length!: number;
/**
* Value of the item.
*/
public value!: number;
/**
* Whether reset channels value to zero at the end of this item or not.
*/
public reset?: boolean;
/**
* This will only make sense when {@link curve} is specified.
* The time offset of the item.
*/
public offset!: number;
/**
* This will only make sense when {@link curve} is specified.
* The speed rate of the item.
*/
public speed!: number;
/**
* This will only make sense when {@link curve} is specified.
* The scale of the item in the value axis.
*/
public amp!: number;
/**
* The curve of the item.
*/
public curve?: Curve;
/**
* Ending timepoint of the item.
*/
public get end(): number {
return this.time + this.length;
}
/**
* Constructor of the [[ChannelItem]].
* @param automaton Parent automaton
* @param data Data of the item
*/
public constructor( automaton: Automaton, data: SerializedChannelItem ) {
this.__automaton = automaton;
this.deserialize( data );
}
public getValue( time: number ): number {
if ( this.reset && this.length <= time ) {
return 0.0;
}
if ( this.curve ) {
const t = this.offset! + time * this.speed!;
return this.value + this.amp * this.curve.getValue( t );
} else {
return this.value;
}
}
/**
* Deserialize a serialized data of item from [[SerializedChannelItem]].
* @param data A serialized item.
*/
public deserialize( data: SerializedChannelItem ): void {
this.time = data.time ?? 0.0;
this.length = data.length ?? 0.0;
this.value = data.value ?? 0.0;
this.offset = data.offset ?? 0.0;
this.speed = data.speed ?? 1.0;
this.amp = data.amp ?? 1.0;
this.reset = data.reset;
if ( data.curve != null ) {
this.curve = this.__automaton.getCurve( data.curve )!;
this.length = data.length ?? this.curve.length ?? 0.0;
}
}
}