-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
88 lines (88 loc) · 2.46 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// LEDをチカチカさせたい
var GrovePi = require('node-grovepi').GrovePi, led, fdd, initialized = false, paused = true;
var board = new GrovePi.board({
debug: true,
onError: function (err) {
console.log('Something wrong just happened');
console.log(err);
},
onInit: function (res) {
if (!res)
return;
var version = board.version();
console.log('GrovePi Version : ' + version);
if (!version) {
console.log('GrovePi is not connected.');
process.exit(1);
}
led = new GrovePi.sensors.DigitalOutput(2);
fdd = new GrovePi.sensors.FourDigitDigital(4);
fdd.init();
fdd.setBrightness(8);
initialized = true;
}
});
board.init();
var SW = require("songle-api");
// トークンの情報を取ってくる
var settings = require("./settings");
// ビート情報と基本情報をもらってくる
var player = new SW.Player({
accessToken: settings.tokens.access
});
player.addPlugin(new SW.Plugin.Beat());
// player.addPlugin(new SongleWidget.Plugin.Chord());
// player.addPlugin(new SongleWidget.Plugin.Melody());
// player.addPlugin(new SongleWidget.Plugin.Chorus());
player.addPlugin(new SW.Plugin.SongleSync());
// 何かあったらコンソールに書き出す
player.on("play", function (ev) {
console.log("play");
paused = false;
});
player.on("seek", function (ev) { return console.log("seek"); });
// 曲が止まったらLED消灯
player.on("pause", function (ev) {
console.log("pause");
if (initialized) {
led.turnOff();
fdd.off();
}
on = false;
paused = true;
});
// 曲が止まったらLED消灯
player.on("finish", function (ev) {
console.log("finish");
if (initialized) {
led.turnOff();
fdd.off();
}
on = false;
paused = true;
});
// ビートごとにLED点滅
var on = false;
player.on("beatEnter", function (ev) {
console.log("beat:", ev.data.beat.position);
if (initialized) {
if (on)
led.turnOff();
else
led.turnOn();
}
on = !on;
});
// 死なないようにする & 秒数をアップデート
setInterval(function () {
if (initialized && player && !paused) {
var position = Math.round(player.position) / 1000;
fdd.setScore(position / 60, position % 60);
}
}, 250);
process.on('SIGTERM', function () {
if (initialized) {
led.turnOff();
fdd.off();
}
});