-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSoundManager.js
77 lines (63 loc) · 2.03 KB
/
SoundManager.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
import * as Tone from "tone";
import { Assets, Sound } from "./utils/constants";
class SoundManager {
constructor() {
// this._boxSequence = ["D4", "F4", "A4", "C5", "E5"];
this._boxSequence = ["D4", "E4", "F#4", "G4", "A4", "B4", "C#4", "D5"];
this._currNoteIdx = 0;
this._sequenceOrder = Sound.SequenceOrder.Ascending;
this.membrane = new Tone.MembraneSynth().toDestination();
this.noise = new Tone.NoiseSynth().toDestination();
this.metal = new Tone.MetalSynth().toDestination();
this.membrane.volume.value = -5;
this.noise.volume.value = -5;
this.metal.volume.value = -5;
}
start() {
Tone.start();
this.backgroundMusic();
}
backgroundMusic() {
const player = new Tone.Player(Assets.BackgroundMusic).toDestination();
player.loop = true
Tone.loaded().then(() => {
player.start();
});
}
playCollisionSound() {
// const synth = new Tone.PolySynth().toDestination();
// synth.volume.value = 1
// const now = Tone.now();
const currNote = this._boxSequence[this.currNoteIdx];
// synth.triggerAttack(currNote, now);
// synth.triggerRelease(currNote, now + .5);
this.membrane.triggerAttackRelease(currNote, "2n");
this.noise.triggerAttackRelease("8n");
this.metal.triggerAttackRelease(currNote, "32n");
}
get currNoteIdx() {
// return 0
const maxIdx = this._boxSequence.length - 1;
const minIdx = 0;
switch (this._sequenceOrder) {
case Sound.SequenceOrder.Ascending:
if (this._currNoteIdx === maxIdx) {
this._sequenceOrder = Sound.SequenceOrder.Descending;
this._currNoteIdx--;
} else {
this._currNoteIdx++;
}
break;
case Sound.SequenceOrder.Descending:
if (this._currNoteIdx === minIdx) {
this._sequenceOrder = Sound.SequenceOrder.Ascending;
this._currNoteIdx++;
} else {
this._currNoteIdx--;
}
break;
}
return this._currNoteIdx;
}
}
export const soundManager = new SoundManager();