/
soundControl.js
67 lines (59 loc) · 1.74 KB
/
soundControl.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
const D = require("Diagnostics");
const R = require("Reactive");
const Sc = require("Scene");
const Au = require("Audio");
const A = require("Animation");
export class SoundControl {
constructor({ name, smoothness, volumeMin, volumeMax }) {
this.name = name;
this.smoothness = smoothness ? smoothness : 0;
this.volumeMin = volumeMin ? volumeMin : 0;
this.volumeMax = volumeMax ? volumeMax : 1;
this.volumeDriver = null;
this.ctrl = null;
this.speaker = null;
this.isPlaying = false;
}
async init() {
if (!this.name) return { msg: "Need a name!", code: -1 };
const speakerName = this.name + "_speaker";
const ctrlName = this.name + "_controller";
this.ctrl = await Au.getAudioPlaybackController(ctrlName);
if (!this.ctrl)
return { msg: "Controll not found : " + ctrlName, code: -1 };
this.speaker = await Sc.root.findFirst(speakerName);
if (!this.speaker)
return { msg: "Speaker not found : " + speakerName, code: -1 };
this.volumeDriver = A.timeDriver({
durationMilliseconds: this.smoothness,
});
this.speaker.volume = A.animate(
this.volumeDriver,
A.samplers.linear(this.volumeMin, this.volumeMax)
).expSmooth(this.smoothness);
return { msg: "Sound control inited " + this.name, code: 1 };
}
play() {
this.reset();
this.ctrl.setLooping(false);
this.ctrl.setPlaying(true);
if (!this.isPlaying) this.volumeDriver.start();
this.isPlaying = true;
}
stop() {
// this.ctrl.setLooping(false);
// this.ctrl.setPlaying(false);
if (this.isPlaying) this.volumeDriver.reverse();
this.isPlaying = false;
}
loop() {
this.reset();
this.ctrl.setLooping(true);
this.ctrl.setPlaying(true);
if (!this.isPlaying) this.volumeDriver.start();
this.isPlaying = true;
}
reset() {
this.ctrl.reset();
}
}