-
Notifications
You must be signed in to change notification settings - Fork 3
/
render.js
executable file
·166 lines (152 loc) · 4.37 KB
/
render.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
#!/usr/bin/env node
'use strict'
const argv = require('yargs')
.usage('Usage: $0 <input.bms> <output.wav>')
.boolean('full')
.boolean('info')
.describe('info', 'Only prints the song info.')
.demand(2, 2)
.version().argv
const childProcess = require('child_process')
const getNotes = require('./getNotes')
const _ = require('lodash')
const satisfies = require('semver').satisfies
const Promise = require('bluebird')
const renderers = {
ffi(song, outfilepath) {
const snd = require('./snd')
const samples = {}
function frameForTime(seconds) {
return Math.floor(seconds * 44100)
}
process.stderr.write('Loading samples...\n')
song.keysounds.forEach(function(samplepath) {
var sound = (samples[samplepath] = snd.read(samplepath))
if (sound.samplerate !== 44100) {
throw new Error(samplepath + ' must be 44100 hz')
}
if (sound.channels !== 2) {
throw new Error(samplepath + ' must be 2 channels')
}
process.stderr.write('.')
})
process.stderr.write('\n')
var validNotes = _(song.data)
.map(function(note) {
return _.assign({ sound: samples[note.src] }, note)
})
.filter('sound')
.value()
process.stderr.write('Number of valid notes: ' + validNotes.length + '\n')
var last = _(validNotes)
.map(function(note) {
var length = note.sound.frames
return frameForTime(note.time) + length
})
.max()
var skip = argv.full
? 0
: _(validNotes)
.map(function(note) {
return frameForTime(note.time)
})
.min()
var frames = last - skip
process.stderr.write('Total song length: ' + frames / 44100 + '\n')
process.stderr.write('Writing notes...' + '\n')
var buffer = Buffer.alloc(frames * 2 * 4)
_.each(validNotes, function(note) {
var sound = note.sound
var start = frameForTime(note.time) - skip
var cut = note.cutTime && frameForTime(note.cutTime) - skip
var offset = start * 2 * 4
var framesToCopy = sound.frames
if (cut > 0) {
framesToCopy = Math.min(framesToCopy, cut - start)
}
var soundBuffer = sound.buffer
var length = Math.min(soundBuffer.length, framesToCopy * 2 * 4)
for (var i = 0; i < length; i += 4) {
var position = offset + i
buffer.writeFloatLE(
buffer.readFloatLE(position) + soundBuffer.readFloatLE(i),
position
)
}
process.stderr.write('.')
})
process.stderr.write('\n')
process.stderr.write('Writing output!\n')
snd.write(outfilepath, {
samplerate: 44100,
channels: 2,
frames: frames,
buffer: buffer
})
},
bmsampler(song, outfilepath) {
const stdin = []
const writeKeysoundFactory = _.memoize(
note =>
_.once(() =>
stdin.push(
JSON.stringify({
type: 'sample',
key: note.keysound,
path: note.src
})
)
),
note => note.keysound
)
const writeNote = note =>
stdin.push(
JSON.stringify({
type: 'play',
key: note.keysound,
time: note.time
})
)
const notes = song.data.filter(note => note.src)
notes.map(writeKeysoundFactory).forEach(thunk => thunk())
notes.forEach(writeNote)
stdin.push(JSON.stringify({ type: 'go' }))
childProcess.execFileSync('bmsampler', [outfilepath], {
input: stdin.join('\n'),
stdio: ['pipe', 'pipe', process.stderr]
})
}
}
function canUseBmsampler() {
const BMSAMPLER_VERSION = '^0.2.0'
try {
const output = childProcess
.execFileSync('bmsampler', ['-v'], { input: '', encoding: 'utf8' })
.trim()
if (satisfies(output, BMSAMPLER_VERSION)) {
return true
} else {
console.error(
'bmsampler version ' +
output +
' is incompatible with bms-renderer. Required version range: ' +
BMSAMPLER_VERSION
)
return false
}
} catch (e) {
return false
}
}
{
const filepath = argv._[0]
const outfilepath = argv._[1]
Promise.coroutine(function*() {
const song = yield getNotes(filepath)
console.log(JSON.stringify(song.info, null, 2))
if (!argv.info) {
const render = canUseBmsampler() ? renderers.bmsampler : renderers.ffi
render(song, outfilepath)
}
})().done()
}