-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsfx.js
142 lines (122 loc) · 3.05 KB
/
sfx.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
class Sfx {
constructor() {
this.timestamp = +new Date()
window.play = this.tradeToSong.bind(this)
this.connect()
}
connect() {
this.context = new(window.AudioContext || window.webkitAudioContext)()
this.queued = 0
var tuna = new Tuna(this.context)
this.output = new tuna.PingPongDelay({
wetLevel: 0.5, //0 to 1
feedback: 0.01, //0 to 1
delayTimeLeft: 175, //1 to 10000 (milliseconds)
delayTimeRight: 100, //1 to 10000 (milliseconds)
})
var filter = new tuna.Filter({
frequency: 700, //20 to 22050
Q: 10, //0.001 to 100
gain: -10, //-40 to 40 (in decibels)
filterType: 'highpass', //lowpass, highpass, bandpass, lowshelf, highshelf, peaking, notch, allpass
bypass: 0,
})
this.output.connect(filter)
filter.connect(this.context.destination)
}
tradeToSong(factor, side) {
const now = +new Date()
this.queued++
setTimeout(() => {
this.queued--
if (side) {
if (factor >= 10) {
;
[659.26, 830.6, 987.76, 1318.52].forEach((f, i, a) =>
setTimeout(
() =>
this.play(f, 0.05 + Math.sqrt(factor) / 15, 0.1 + factor * 0.1),
i * 80
)
)
} else if (factor >= 1) {
;
[659.26, 830.6].forEach((f, i) =>
setTimeout(
() =>
this.play(f, 0.05 + Math.sqrt(factor) / 10, 0.1 + factor * 0.1),
i * 80
)
)
} else {
this.play(
659.26,
Math.sqrt(factor) / 10,
0.1 + Math.sqrt(factor) / 10
)
}
} else {
if (factor >= 10) {
;
[493.88, 369.99, 293.66, 246.94].forEach((f, i, a) =>
setTimeout(
() =>
this.play(
f,
0.05 + Math.sqrt(factor) / 10,
i > 2 ? 0.1 + factor * 0.1 : 0.2
),
i > 2 ? 80 * 3 : i * 80
)
)
} else if (factor >= 1) {
;
[493.88, 392].forEach((f, i) =>
setTimeout(
() =>
this.play(f, 0.05 + Math.sqrt(factor) / 10, 0.1 + factor * 0.1),
i * 80
)
)
} else {
this.play(
493.88,
Math.sqrt(factor) / 10,
0.1 + Math.sqrt(factor) / 10
)
}
}
}, this.timestamp - now)
this.timestamp = Math.max(this.timestamp, now) + (this.queued > 20 ? 40 : 80)
}
play(frequency, value = 0.5, length = 0.1, type = 'triangle', volume = 0.8) {
if (this.context.state !== 'running') {
return
}
const time = this.context.currentTime
const oscillator = this.context.createOscillator()
const gain = this.context.createGain()
oscillator.frequency.value = frequency
oscillator.type = type
oscillator.onended = () => {
oscillator.disconnect()
}
gain.connect(this.output)
oscillator.connect(gain)
length *= 1.1
gain.gain.value = volume
gain.gain.setValueAtTime(gain.gain.value, time)
gain.gain.exponentialRampToValueAtTime(0.001, time + length)
oscillator.start(time)
oscillator.stop(time + length)
}
liquidation() {
;
[329.63, 329.63].forEach((f, i, a) =>
setTimeout(() => this.play(f, 0.5, 0.25, 'sine'), i * 80)
)
}
disconnect() {
this.context && this.context.state === 'running' && this.context.close()
}
}