-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
69 lines (61 loc) · 1.8 KB
/
index.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
// LEDをチカチカさせたい
var grove = require("jsupm_grove")
, LEDBar = require("jsupm_my9221")
, led = new grove.GroveLed(2)
, bar = new LEDBar.GroveLEDBar(8, 9)
, bar2 = new LEDBar.GroveLEDBar(6, 7);
var Songle = require("songle-api");
// トークンの情報を取ってくる
var settings = require("./settings");
// ビート情報と基本情報をもらってくる
var player = new Songle.Player({
accessToken: settings.tokens.access
});
player.addPlugin(new Songle.Plugin.Beat());
player.addPlugin(new Songle.Plugin.Chorus());
player.addPlugin(new Songle.Plugin.SongleSync());
// 何かあったらコンソールに書き出す
player.on("play", (ev) => console.log("play"));
player.on("seek", (ev) => console.log("seek"));
// 曲が止まったらLED消灯
player.on("pause", (ev) => {
console.log("pause");
led.off();
bar.setBarLevel(0);
bar2.setBarLevel(0);
on = false;
});
// 曲が止まったらLED消灯
player.on("finish", (ev) => {
console.log("finish");
led.off();
bar.setBarLevel(0);
bar2.setBarLevel(0);
on = false;
});
// ビートごとにLED点滅
var on = false, inChorus = false;
player.on("chorusSectionLeave", (ev) => {
inChorus = false;
});
player.on("chorusSectionEnter", (ev) => {
inChorus = true;
});
player.on("beatEnter", (ev) => {
console.log("beat:", ev.data.beat.position);
if (on) led.off();
else led.on();
if (inChorus) {
bar.setBarLevel(on ? 10 : 0);
bar2.setBarLevel(on ? 10 : 0);
} else {
bar.setBarLevel(ev.data.beat.position * 10 / ev.data.beat.count, true);
bar2.setBarLevel(ev.data.beat.position * 10 / ev.data.beat.count, false);
}
on = !on;
});
process.on('SIGTERM', function() {
led.off();
bar.setBarLevel(0);
bar2.setBarLevel(0);
});