-
Notifications
You must be signed in to change notification settings - Fork 0
/
webmo.ts
86 lines (75 loc) · 2.75 KB
/
webmo.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// ローカルホストのWebmoに接続
var WebmoWs = require("webmo-client-nodejs").ws;
var motor = new WebmoWs("webmo.local");
motor.onopen = onMotorConnected;
// motor.onmessage = (json) => console.log(json);
var Songle = require("songle-api");
// トークンの情報を取ってくる
var settings = require("./settings");
// Songle IoMT APIのエンドポイント指定
Songle.System.defaultEndpointWebClientProtocol = "https:";
Songle.System.defaultEndpointWebClientHost = "api.songle.jp";
Songle.System.defaultEndpointWebSocketProtocol = "https:";
Songle.System.defaultEndpointWebSocketHost = "api.songle.jp";
Songle.System.showLogMode = true;
// ビート情報と基本情報をもらってくる
var player = new Songle.Player({
accessToken: settings.tokens.access
});
player.addPlugin(new Songle.Plugin.Beat());
// player.addPlugin(new Songle.Plugin.Chord());
// player.addPlugin(new Songle.Plugin.Melody());
player.addPlugin(new Songle.Plugin.Chorus());
player.addPlugin(new Songle.Plugin.SongleSync());
var initialized = false;
function onMotorConnected() {
console.log("Webmo: connected");
if (initialized) return;
var isClockwise = false;
// 何はともあれモーターを止める
motor.stop();
// 何かあったらコンソールに書き出す
player.on("play", (ev) => console.log("play"));
player.on("seek", (ev) => console.log("seek"));
// 曲が止まったらモーターも止める
player.on("pause", (ev) => {
console.log("pause");
motor.stop();
isClockwise = false;
});
// 曲が止まったらモーターも止める
player.on("finish", (ev) => {
console.log("finish");
motor.stop();
isClockwise = false;
});
// 1ビート目に到達するたびにモーターの動作を反転する
var inChorus = false;
player.on("repeatSectionLeave", (ev) => {
var index = parseInt(ev.data.section.index);
if (index === 0) inChorus = false;
console.log('chorus: leave, section index:', index);
});
player.on("repeatSectionEnter", (ev) => {
var index = parseInt(ev.data.section.index);
if (index === 0) inChorus = true;
console.log('chorus: enter, section index:', index);
});
player.on("beatEnter", (ev) => {
var position = ev.data.beat.position;
console.log("beat:", position, ", chorus?:", inChorus);
if (position === 1 || (inChorus && position === 3)) {
if (isClockwise) {
motor.rotate(360);
} else {
motor.rotate(-360);
}
isClockwise = !isClockwise;
}
});
initialized = true;
}
process.on('SIGTERM', function() {
if (!initialized) return;
motor.stop();
});