-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtonepad.js
84 lines (67 loc) · 2.1 KB
/
tonepad.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
import * as Tone from 'tone';
import { ToneBaseInstrument } from './tonebaseinstrument.js';
import { padGain, padChance } from './toneparams.js';
import { ToneNotesGenerator } from './tonenotesgenerator.js';
class TonePad extends ToneBaseInstrument {
constructor() {
super();
this.baseSound = new Tone.PolySynth(Tone.FMSynth, {
oscillator: {
partialCount: [0],
},
}).connect(this.filter);
this.baseSound.set({
envelope: {
attack: 1.0 * Tone.Time('1m').toSeconds(),
decay: 0.5 * Tone.Time('1m').toSeconds(),
sustain: 0.3,
release: 2.0 * Tone.Time('1m').toSeconds(),
},
});
// update filter frequency cutoff
this.updateFilterFrequency(700);
// update filter rolloff
this.updateFilterRolloff(-24);
// update delay
this.updateDelayTime(2.0 * Tone.Time('4n').toSeconds());
this.updateDelayFeedback(0.4);
this.gain = padGain;
this.chance = padChance;
this.generator = new ToneNotesGenerator();
this.currentEvent = null;
this.baseNote = 'B2';
this.baseChord = this.generator.getChord('chord4Min7th');
for (let i = 0; i < this.baseChord.length; i++) {
this.baseChord[i] = Tone.Frequency(this.baseNote).transpose(
this.baseChord[i],
);
}
}
init() {
this.updateGain(this.gain);
if (this.currentEvent != null) {
this.currentEvent.dispose();
}
this.currentEvent = new Tone.ToneEvent((time, chord) => {
this.baseSound.triggerAttackRelease(
chord.slice(
0,
1 +
Math.min(
Tone.Transport.position.split(':')[0] / 16,
chord.length - 1,
),
),
12.0 * Tone.Time('1m').toSeconds(),
time,
);
}, this.baseChord);
this.currentEvent.probability = this.chance;
this.currentEvent.playbackRate = 1;
this.currentEvent.loop = true;
// loopEnd is the length of the loop, as in, how often it loops
this.currentEvent.loopEnd = 16.0 * Tone.Time('1m').toSeconds();
this.currentEvent.start(Tone.now());
}
}
export { TonePad };