-
Notifications
You must be signed in to change notification settings - Fork 3
/
getNotes.js
97 lines (86 loc) · 2.3 KB
/
getNotes.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
'use strict'
const fs = require('fs')
const NotechartLoader = require('bemuse-notechart/lib/loader').NotechartLoader
const path = require('path')
const _ = require('lodash')
const Promise = require('bluebird')
function cut(sortedTimes) {
var last = {}
sortedTimes = _.cloneDeep(sortedTimes)
sortedTimes.forEach(function(note) {
try {
if (last[note.keysound]) {
last[note.keysound].cutTime = note.time
}
} finally {
last[note.keysound] = note
}
})
return _.reject(sortedTimes, function(note) {
return note.cutTime === note.time
})
}
function getHash(times) {
const out = {}
for (const note of times) {
if (note.src) {
const frame = Math.round(note.time * 60)
out[note.src + ':' + frame] = true
}
}
const data = _.sortBy(Object.keys(out)).join('\n')
fs.writeFileSync('/tmp/d' + Date.now() + '.nfo', data)
return require('crypto')
.createHash('md5')
.update(data)
.digest('hex')
}
const getNotes = Promise.coroutine(function*(filepath) {
const buffer = fs.readFileSync(filepath)
const loader = new NotechartLoader()
const notechart = yield loader.load(buffer, { name: filepath }, {})
const notes = notechart.notes.concat(notechart.autos)
const info = notechart.songInfo
const keys = {}
const times = _(notes)
.filter(note => !note.keysoundStart)
.map(note => ({
time: note.time,
src: lookup(note.keysound),
keysound: note.keysound
}))
.sortBy('time')
.thru(cut)
.value()
const xinfo = {
sequenceHash: getHash(times)
}
return {
path: filepath,
info: Object.assign(xinfo, info),
data: times,
keysounds: _(keys)
.values()
.map('result')
.compact()
.value()
}
function lookup(k) {
var result = keys[k] || (keys[k] = { result: find(k) })
return result.result
}
function find(k) {
var wav = notechart.keysounds[k.toLowerCase()]
if (!wav) return null
wav = path.resolve(filepath, '..', wav)
if (fs.existsSync(wav)) return wav
wav = wav.replace(/\.\w\w\w$/, '.wav')
if (fs.existsSync(wav)) return wav
wav = wav.replace(/\.\w\w\w$/, '.ogg')
if (fs.existsSync(wav)) return wav
wav = wav.replace(/\.\w\w\w$/, '.mp3')
if (fs.existsSync(wav)) return wav
return null
}
})
module.exports = getNotes