-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
60 lines (60 loc) · 1.86 KB
/
index.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
// 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", function (ev) { return console.log("play"); });
player.on("seek", function (ev) { return console.log("seek"); });
// 曲が止まったらLED消灯
player.on("pause", function (ev) {
console.log("pause");
led.off();
bar.setBarLevel(0);
bar2.setBarLevel(0);
on = false;
});
// 曲が止まったらLED消灯
player.on("finish", function (ev) {
console.log("finish");
led.off();
bar.setBarLevel(0);
bar2.setBarLevel(0);
on = false;
});
// ビートごとにLED点滅
var on = false, inChorus = false;
player.on("chorusSectionLeave", function (ev) {
inChorus = false;
});
player.on("chorusSectionEnter", function (ev) {
inChorus = true;
});
player.on("beatEnter", function (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);
});