-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtonehihat.js
70 lines (56 loc) · 1.89 KB
/
tonehihat.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
import * as Tone from 'tone';
import { ToneBaseInstrument } from './tonebaseinstrument.js';
import { hihatGain, hihatChance } from './toneparams.js';
class ToneHihat extends ToneBaseInstrument {
constructor() {
super();
// then we create the hihat base sound
// and connect to filter
this.baseSound = new Tone.NoiseSynth({
envelope: {
// duration of the hihat
attack: 0.1 * Tone.Time('4n').toSeconds(),
decay: 0.1 * Tone.Time('4n').toSeconds(),
sustain: 0,
release: 0.1 * Tone.Time('4n').toSeconds(),
},
}).connect(this.filter);
// update delay
this.updateDelayTime(2.0 * Tone.Time('4n').toSeconds());
this.updateDelayFeedback(0.2);
// update filter
this.filter.type = 'bandpass';
this.filter.Q.value = 5.0;
// update frequency cutoff
this.updateFilterFrequency(8000);
this.gain = hihatGain;
this.chance = hihatChance;
this.currentEvent = null;
this.baseChord = null;
}
init() {
this.updateGain(this.gain);
// cancel all next scheduled repeats
if (this.currentEvent != null) {
this.currentEvent.cancel(Tone.now());
}
this.currentEvent = new Tone.ToneEvent((time, chord) => {
if (Tone.Transport.position.split(':')[0] >= 48) {
this.updateFilterFrequency(10000 + Math.random() * 3000);
this.baseSound.triggerAttackRelease(
// first parameter is commented out because no note for noise
// chord
1.0 * Tone.Time('4n').toSeconds(),
time,
);
}
}, this.baseChord);
this.currentEvent.probability = this.chances;
this.currentEvent.playbackRate = 1.0;
this.currentEvent.loop = true;
// loopEnd is the length of the loop, as in, how often it loops
this.currentEvent.loopEnd = 0.25 * Tone.Time('1m').toSeconds();
this.currentEvent.start(Tone.now());
}
}
export { ToneHihat };