Skip to content

Commit 3206156

Browse files
committed
now make the sound a little dynamic and add loudness per note
1 parent 67ba6c0 commit 3206156

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

gui/src/sounds/sounds.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ export async function playSoundOnResetEnded(resetType: ResetType, volume = 1) {
1919
offset: 0.15,
2020
type: 'custom',
2121
volume,
22+
loudness: [1.5, 1, 1, 1],
2223
});
2324
break;
2425
}
2526
case ResetType.Full: {
2627
xylophone.play({
27-
notes: ['G4', 'C3',],
28+
notes: ['G4', 'C3'],
2829
offset: 0.15,
2930
type: 'custom',
3031
volume,

gui/src/sounds/xylophone.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ export interface IMeasure {
171171
offset?: number;
172172
type?: OscillatorOptions['type'];
173173
volume?: number;
174+
loudness?: number[];
174175
}
175176

176177
export interface INote {
@@ -219,18 +220,21 @@ export default class Xylophone {
219220

220221
return;
221222
}
222-
let i = 0;
223223
await Promise.all(
224-
measure.notes.map((note) => {
224+
measure.notes.map((note, i) => {
225225
let offset = delay;
226-
if (measure.offset) offset += measure.offset * i++;
226+
if (measure.offset) offset += measure.offset * i;
227+
let volume = measure.volume;
228+
if (volume && measure.loudness) {
229+
volume *= measure.loudness[i];
230+
}
227231

228232
return this.playTone({
229233
length: measure.length,
230234
note,
231235
offset,
232236
type: measure.type,
233-
volume: measure.volume,
237+
volume,
234238
});
235239
})
236240
);
@@ -302,7 +306,16 @@ export default class Xylophone {
302306
);
303307
const gain = Math.min(1, Math.pow(volume ?? 1, Math.E) * freqGain) * 0.5;
304308

305-
this.oscillator.frequency.value = Xylophone.toHertz(note);
309+
// this.oscillator.frequency.value = Xylophone.toHertz(note);
310+
this.oscillator.frequency.setValueAtTime(0.5 * Xylophone.toHertz(note), offset);
311+
this.oscillator.frequency.linearRampToValueAtTime(
312+
1 * Xylophone.toHertz(note),
313+
offset + 0.05
314+
);
315+
this.oscillator.frequency.exponentialRampToValueAtTime(
316+
Xylophone.toHertz(note) + 5,
317+
offset + length
318+
);
306319
this.gainNode.gain.setValueAtTime(0, offset);
307320
this.gainNode.gain.linearRampToValueAtTime(1 * gain, offset + 0.02);
308321

0 commit comments

Comments
 (0)