-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtonejs-renderer.ts
233 lines (190 loc) · 5.9 KB
/
tonejs-renderer.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import { IAudioEventListener, EventName, EventSource, Audio } from '../audio';
import { QuadrangularChannel, WAVE_DUTY_MAP } from '../channels/quadrangular-channel';
import { NoiseChannel } from '../channels/noise-channel';
import { requireOptional } from '../../utils';
const Tone: any = requireOptional('tone', 'TonejsRenderer');
// Used to scale the volume in the
// setVolume/getVolume methods.
const VOLUME_SCALING = -38;
export class TonejsRenderer implements IAudioEventListener {
private audio: Audio;
private panVol: any;
private channel1: PulseWave;
private channel2: PulseWave;
private channel4: NoiseWave;
public constructor(audio: Audio) {
this.audio = audio;
this.panVol = new Tone.PanVol(0, -Infinity);
Tone.Master.chain(this.panVol);
this.channel1 = new PulseWave(this.audio.ch1);
this.channel2 = new PulseWave(this.audio.ch2);
this.channel4 = new NoiseWave(this.audio.ch4);
}
public setVolume(volume: number): void {
const cappedVolume = Math.max(0, Math.min(100, volume));
const db = (volume === 0) ?
-Infinity :
((cappedVolume / 100) * Math.abs(VOLUME_SCALING)) + VOLUME_SCALING;
Tone.Master.volume.rampTo(db, 0.05);
}
public getVolume(): number {
const db = Tone.Master.volume.value;
if (db === -Infinity) {
return 0;
}
return Math.max(0, Math.min(100,
100 * (Tone.Master.volume.value - VOLUME_SCALING) / Math.abs(VOLUME_SCALING)
));
}
public onAudioEvent(source: EventSource, name: EventName): void {
switch (source) {
case EventSource.GLOBAL:
this.updateGlobal(name);
break;
case EventSource.CHANNEL1:
this.updateChannel1(name);
break;
case EventSource.CHANNEL2:
this.updateChannel2(name);
break;
case EventSource.CHANNEL4:
this.updateChannel4(name);
break;
}
}
private updateGlobal(name: EventName): void {
switch (name) {
case EventName.RESET:
case EventName.ON_OFF:
case EventName.VOLUME_CHANGED:
const volume = this.audio.enabled ?
(this.audio.leftVolume + this.audio.rightVolume) :
0;
if (volume > 0) {
this.panVol.volume.value = volume - 0x0F;
this.panVol.pan.value = ((0 - this.audio.leftVolume) + this.audio.rightVolume) / 0x0F;
} else {
this.panVol.volume.value = -Infinity;
}
break;
}
}
private updateChannel1(name: EventName): void {
switch (name) {
case EventName.RESET:
this.channel1.updateAll();
break;
case EventName.ON_OFF:
case EventName.VOLUME_CHANGED:
this.channel1.updateVolume();
break;
case EventName.FREQUENCY_CHANGED:
this.channel1.updateFrequency();
break;
case EventName.DUTY_CHANGED:
this.channel1.updateWaveDuty();
break;
}
}
private updateChannel2(name: EventName): void {
switch (name) {
case EventName.RESET:
this.channel2.updateAll();
break;
case EventName.ON_OFF:
case EventName.VOLUME_CHANGED:
this.channel2.updateVolume();
break;
case EventName.FREQUENCY_CHANGED:
this.channel2.updateFrequency();
break;
case EventName.DUTY_CHANGED:
this.channel2.updateWaveDuty();
break;
}
}
private updateChannel4(name: EventName): void {
switch (name) {
case EventName.RESET:
case EventName.ON_OFF:
case EventName.VOLUME_CHANGED:
this.channel4.updateVolume();
break;
}
}
}
class PulseWave {
private channel: QuadrangularChannel;
private oscillator: any;
private panner: any;
public constructor(channel: QuadrangularChannel) {
this.channel = channel;
this.oscillator = new Tone.PulseOscillator(0, 0.5);
this.panner = new Tone.Panner(0);
this.setVolume(-Infinity);
this.oscillator.chain(this.panner, Tone.Master);
this.oscillator.start();
}
public updateAll(): void {
this.updateVolume();
this.updateFrequency();
this.updateWaveDuty();
}
public updateFrequency(): void {
this.setFrequency(131072 / (2048 - this.channel.frequency));
}
public updateWaveDuty(): void {
this.setWaveDuty(WAVE_DUTY_MAP[this.channel.waveDuty]);
}
public updateVolume(): void {
if (this.channel.enabled && (this.channel.outputLeft || this.channel.outputRight)) {
// tslint:disable-next-line:max-line-length
this.panner.pan.value = (this.channel.outputLeft ? -1 : 0) + (this.channel.outputRight ? 1 : 0);
this.setVolume(this.channel.volume);
} else {
this.setVolume(0);
}
}
public setFrequency(frequency: number): void {
this.oscillator.frequency.value = frequency;
}
public setWaveDuty(waveDuty: number): void {
this.oscillator.width.value = waveDuty;
}
private setVolume(volume: number): void {
if (volume > 0) {
this.oscillator.volume.value = (volume - 0x0F);
} else {
this.oscillator.volume.value = -Infinity;
}
}
}
class NoiseWave {
private channel: NoiseChannel;
private oscillator: any;
private panner: any;
public constructor(channel: NoiseChannel) {
this.channel = channel;
this.oscillator = new Tone.Noise('white');
this.panner = new Tone.Panner(0);
this.setVolume(-Infinity);
this.oscillator.chain(this.panner, Tone.Master);
this.oscillator.start();
}
public updateVolume(): void {
if (this.channel.enabled && (this.channel.outputLeft || this.channel.outputRight)) {
// tslint:disable-next-line:max-line-length
this.panner.pan.value = (this.channel.outputLeft ? -1 : 0) + (this.channel.outputRight ? 1 : 0);
this.setVolume(this.channel.volume);
} else {
this.setVolume(0);
}
}
private setVolume(volume: number): void {
if (volume > 0) {
this.oscillator.volume.value = (volume - 0x0F);
} else {
this.oscillator.volume.value = -Infinity;
}
}
}