-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
211 lines (185 loc) · 5.78 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
var createHash = require('crypto').createHash
var bms = require('bms')
var bmson = require('bmson')
var Promise = require('bluebird')
var _ = require('lodash')
var assign = require('object-assign')
var extname = require('path').extname
var invariant = require('invariant')
var lcs = require('./lcs')
var getKeys = require('./keys')
var getBpmInfo = require('./bpm-info')
var getDuration = require('./duration')
var getBmsonBga = require('./bmson-bga')
var readBMS = Promise.promisify(bms.Reader.readAsync, bms.Reader)
exports.extensions = { }
exports.extensions['.bms'] = function (source) {
return readBMS(source).then(function(str) {
var chart = bms.Compiler.compile(str).chart
var info = bms.SongInfo.fromBMSChart(chart)
var notes = bms.Notes.fromBMSChart(chart)
var timing = bms.Timing.fromBMSChart(chart)
return {
info: info,
notes: notes,
timing: timing,
scratch: hasScratch(chart),
keys: getKeys(chart),
}
})
}
exports.extensions['.bmson'] = function (source) {
return (
Promise.try(function () {
return new Buffer(source).toString('utf8')
})
.then(function (string) {
return JSON.parse(string)
})
.then(function(object) {
var info = bmson.songInfoForBmson(object)
var ms = bmson.musicalScoreForBmson(object)
var notes = ms.notes
var timing = ms.timing
var bga = getBmsonBga(object, { timing: timing })
return {
info: info,
notes: notes,
timing: timing,
scratch: bmson.hasScratch(object),
keys: bmson.keysForBmson(object),
bga: bga
}
})
)
}
function getFileInfo(data, meta, options) {
options = options || { }
invariant(typeof meta.name === 'string', 'meta.name must be a string')
var extensions = options.extensions || exports.extensions
var extension = extensions[extname(meta.name).toLowerCase()] || extensions['.bms']
var md5 = meta.md5 || (function () {
var hash = createHash('md5')
hash.update(data)
return hash.digest('hex')
}())
return (
extension(data, meta, options)
.then(function (basis) {
invariant(basis.info, 'basis.info must be a BMS.SongInfo')
invariant(basis.notes, 'basis.notes must be a BMS.Notes')
invariant(basis.timing, 'basis.timing must be a BMS.Timing')
invariant(typeof basis.scratch === 'boolean', 'basis.scratch must be a boolean')
invariant(typeof basis.keys === 'string', 'basis.scratch must be a string')
var info = basis.info
var notes = basis.notes
var timing = basis.timing
var count = notes.all().filter(noteIsPlayable).length
return {
md5: md5,
info: info,
noteCount: count,
bpm: getBpmInfo(notes, timing),
duration: getDuration(notes, timing),
scratch: basis.scratch,
keys: basis.keys,
bga: basis.bga
}
})
)
}
exports.getFileInfo = getFileInfo
function getSongInfo(files, options) {
options = options || { }
var warnings = []
var cache = options.cache || undefined
var extra = options.extra || { }
var report = options.onProgress || function() { }
var onError = options.onError || function(e, name) {
if (global.console && console.error) {
console.error('Error while parsing ' + name, e)
}
}
var processed = 0
var doGetFileInfo = options.getFileInfo || getFileInfo
return Promise.map(files, function(file) {
var name = file.name
var data = file.data
var hash = createHash('md5')
hash.update(data)
var md5 = hash.digest('hex')
return Promise.resolve(cache && cache.get(md5))
.then(function(cached) {
if (cached) {
return cached
} else {
var meta = { name: name, md5: md5 }
return Promise.resolve(doGetFileInfo(data, meta, options)).tap(function(info) {
if (cache) return cache.put(md5, info)
})
}
})
.then(function(info) {
info.file = name
return [info]
})
.catch(function(e) {
onError(e, name)
warnings.push('Unable to parse ' + name + ': ' + e)
return []
})
.finally(function() {
processed += 1
report(processed, files.length, name)
})
}, { concurrency: 2 })
.then(_.flatten)
.then(function(charts) {
if (charts.length === 0) {
warnings.push('No usable charts found!')
}
var song = {
title: common(charts, _.property('info.title')),
artist: common(charts, _.property('info.artist')),
genre: common(charts, _.property('info.genre')),
bpm: median(charts, _.property('bpm.median')),
}
assign(song, getSongVideoFromCharts(charts))
assign(song, extra)
song.charts = charts
song.warnings = warnings
return song
})
}
exports.getSongInfo = getSongInfo
function getSongVideoFromCharts (charts) {
var result = { }
var chart = _.find(charts, 'bga')
if (chart) {
result.video_file = chart.bga.file
result.video_offset = chart.bga.offset
}
return result
}
exports._getSongVideoFromCharts = getSongVideoFromCharts
function noteIsPlayable(note) {
return note.column !== undefined
}
function hasScratch(chart) {
var objects = chart.objects.all()
for (var i = 0; i < objects.length; i ++) {
var object = objects[i]
var channel = +object.channel
if (50 <= channel && channel <= 69) channel -= 20
if (channel === 16 || channel === 26) return true
}
return false
}
function common(array, f) {
var longest = array.map(f).reduce(lcs, [])
return String(longest || f(array[0])).trim()
}
function median(array, f) {
var arr = _(array).map(f).sortBy().value()
return arr[Math.floor(arr.length / 2)]
}