-
Notifications
You must be signed in to change notification settings - Fork 6
/
voice.js
91 lines (74 loc) · 2.12 KB
/
voice.js
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
class Voice{
constructor(interval, subdivide, synth, start, end, repeat){
if(!synth)throw new Error("no synth specified!")
this.synth = synth;
this.seq = new Tone.Part(this._trigger.bind(this), []);
this.seq.start(0);
this.seq.loop = repeat > 1 ? repeat : false;
this.seq.loopStart = mfrc(start || 0);
this.seq.loopEnd = mfrc(end || 1);
this._start = start || 0;
this._length = end - start || 1;
this._subdivide = subdivide || 1;
this._interval = interval || 1/4;
this._update_sequence();
}
set interval(v){
if(v == this._interval)return;
this._interval = v;
this._update_sequence();
}
get interval(){
return this._interval;
}
set subdivide(v){
if(v == this._subdivide)return;
if(typeof v != "number")throw new Error("subdivide value is not a number!");
this._subdivide = v;
this._update_sequence();
}
get subdivide(){
return this._subdivide;
}
set repeat(v){
if(v <= 0 || (v|0) != v)throw new Error("the value must be an int and superior to zero");
this.seq.loop = v;
}
get repeat(){
return this.seq.loop;
}
setLoop(start=null, end=null){
this._start = start;
this._length = end - start;
this.seq.loopStart = mfrc(this._start);
this.seq.loopEnd = mfrc(end);
this._update_sequence(); // length changed
}
_trigger(time, value){
this.synth.triggerAttackRelease(
this.synth.m_note_to_play || 'C4',
this.synth.m_duration_to_play || 0.01, // m_note_to_play and m_duration_to_play is bound in synths.js
time,
value.accent ? 1 : 0.1);
// this.synth.triggerRelease(time);
// this.synth.triggerAttack(
// this.synth.m_note_to_play || 'C4',
// Tone.Time(time).toSeconds() + 0.01,
// value.accent ? 1 : 0.3);
}
dispose(){
this.seq.stop();
this.seq.removeAll();
this.seq.dispose();
}
_update_sequence(){
this.seq.removeAll();
var D = this._interval * this._length / this._subdivide;
var n = this._length / D |0;
var td = this._start;
for(var i=0; i<n; i++){
this.seq.add(mfrc(td), {accent: (i%this._subdivide)==0 });
td += D;
}
}
}