This repository was archived by the owner on Oct 23, 2022. It is now read-only.
forked from atm314/json
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
130 lines (115 loc) · 3.2 KB
/
test.js
File metadata and controls
130 lines (115 loc) · 3.2 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
var obj1 = document.getElementById("selfile");
var bms;
var json_obj = {
header: {
genre: null,
title: null,
artist: null,
bpm: null,
playlevel: 0,
rank: 0,
wav: [],
},
main: [],
start: 0,
bpm: []
};
//ダイアログでファイルが選択された時
if (obj1 != null) {
obj1.addEventListener("change", function(evt) {
var file = evt.target.files;
//FileReaderの作成
var reader = new FileReader();
//テキスト形式で読み込む
reader.readAsText(file[0]);
//読込終了後の処理
reader.onload = function() {
bms = reader.result;
//ヘッダの読み込み
var key;
for (key in json_obj.header) {
json_obj.header[key] = readheader(key);
}
json_obj.main = readmain();
readother();
var blob = new Blob(
[JSON.stringify(json_obj)]
);
var link = document.createElement("a");
link.download = file.name + ".json";
link.href = window.URL.createObjectURL(blob);
var disp = document.getElementById("disp");
disp.innerHTML = "<a href='" + link.href + "' target='_blank'>ファイルダウンロード</a>";
};
}, false);
}
function readheader(name) {
var head = bms.indexOf("#" + name, 0);
if (head == -1)
return null;
if (name == "WAV") {
var wav = [];
while (head != -1) {
wav.push(bms.substring(head + name.length + 4, bms.indexOf("\n", head) - 1));
head = bms.indexOf("#" + name, head + 1);
}
return wav;
}
return bms.substring(head + name.length + 2, bms.indexOf("\n", head) - 1);
}
function Main(_line, _channel, _data) {
this.line = _line;
this.channel = _channel;
this.data = _data;
}
function readmain() {
var head = bms.indexOf("MAIN DATA FIELD");
var measure = 0;
var i;
var main_data = [];
var lane;
while (head != -1) {
for (i = 11; i < 14; i++) {
head = (bms.indexOf("#", head + 1));
if (head == -1)
break;
lane = Number(bms.substr(head + 4, 2));
if (lane < 11 || lane > 13) {
continue;
}
if (Number(bms.substr(head + 1, 3)) != measure || lane != i) {
head--;
continue;
}
var data = slice_two(bms.substring(bms.indexOf(":", head) + 1, bms.indexOf("\n", head) - 1));
var main_obj = new Main(measure, lane - 11, data);
main_data.push(main_obj);
}
measure++;
}
return main_data;
}
function readother() {
var head = bms.indexOf("MAIN DATA FIELD");
while (head != -1) {
head = bms.indexOf("#", head + 1);
if (Number(bms.substr(head + 4, 2)) == 1) {
json_obj.start = Number(bms.substr(head + 1, 3));
}
if (Number(bms.substr(head + 4, 2)) == 3) {
json_obj.bpm.push([Number(bms.substr(head + 1, 3)), parseInt(bms.substr(bms.indexOf(":", head) + 1, 2), 16)]);
if (parseInt(bms.substr(bms.indexOf(":", head) + 1, 2), 16) == 0) {
alert("bpm変更エラー:bpmが正しく設定されていないか、小節の頭に指定されていない可能性があります");
}
}
}
}
function slice_two(data) {
var length = data.length;
var i;
var num = [];
for (i = 0; i < length; i += 2) {
num.push(Number(data.slice(i, i + 2)));
}
return num;
}