-
Notifications
You must be signed in to change notification settings - Fork 1
/
track.js
206 lines (179 loc) · 5.02 KB
/
track.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
const { Asset } = require('./asset')
const debug = require('debug')('baptism:track')
const fingerprint = require('./fingerprint')
const flags = require('./flags.json')
const ffmpeg = require('fluent-ffmpeg')
const fs = require('fs')
const peaks = require('./peaks')
const getSoxi = require('./soxi')
const getSpectrogram = require('./spectrogram')
const getStats = require('./stats')
const mime = require('mime')
const Resource = require('nanoresource')
const WaveFile = require('wavefile').WaveFile
class Track extends Resource {
constructor(source, opts = {}) {
super()
this.filename = source
this.fd = 0
this.type = mime.getType(this.filename)
if (opts.tags) {
this.tags = opts.tags
}
if (opts.trackNumber) {
this.trackNumber = opts.trackNumber
}
try {
if (this.type === 'audio/wav') {
this.wav = new WaveFile()
this.wav.fromBuffer(Buffer.from(fs.readFileSync(this.filename)))
if (this.tags) {
for (const tag of this.tags) {
this.wav.setTag(...tag)
}
}
}
} catch (err) {
console.error('Error occurred during Track instantiation! This should ' +
'not happen. Something is very wrong.')
throw new Error(err)
}
}
_open (cb) {
debug('Now opening file ...', this.filename)
fs.open(this.filename, 'r', (err, fd) => {
if (err) return cb(err)
this.fd = fd
cb(null)
})
}
_close (cb) {
debug('Now closing file ...')
fs.close(this.fd, cb)
}
fingerprint (cb) {
this.open((err) => {
if (err) return cb(err)
if (!this.active(cb)) return
fingerprint(this.filename, (err, fp) => {
if (err) return cb(err)
this.fp = fp
this.inactive(cb, null, fp)
})
})
}
peaks (cb) {
this.open((err) => {
if (err) return cb(err)
if (!this.active(cb)) return
peaks(this.filename, (err, pd) => {
if (err) return cb(err)
this.peaksData = pd
this.inactive(cb, null, pd)
})
})
}
silence (cb) {
this.open((err) => {
if (err) return cb(err)
const silences = []
function validateSilences(silObj) {
const silenceStarts = Object.keys(silObj)
return {
start: Number(silenceStarts.shift()) === 0,
end: Number(silenceStarts.pop()) > 0
}
}
function parseSilences(sils) {
const starts = []
const maps = {}
for (const s of sils) {
if (s.includes('silence_start: ')) {
starts.push(parseFloat(s.split('silence_start: ')[1]))
} else if (s.includes('silence_duration: ')) {
maps[starts.pop()] = s.split('silence_duration: ')[1]
}
}
return [ maps, validateSilences(maps) ]
}
const ffmpegCmd = ffmpeg(this.filename)
.audioFilters(flags.silence)
.format('null')
.output('-')
.on('stderr', d => {
if (`${d}`.includes('silencedetect')) {
silences.push(`${d}`)
}
})
.on('error', err => { return this.inactive(cb, err) })
.on('end', () => {
debug('finished', silences)
const parsedSilences = parseSilences(silences)
this.silences = parsedSilences
this.inactive(cb, null, parsedSilences)
})
ffmpegCmd.run()
})
}
size (cb) {
this.open((err) => {
if (err) return cb(err)
if (!this.active(cb)) return
fs.fstat(this.fd, (err, st) => {
if (err) return this.inactive(cb, err)
this.inactive(cb, null, st.size)
})
})
}
soxi (cb) {
this.open((err) => {
if (err) return cb(err)
if (!this.active(cb)) return
getSoxi(this.filename, (err, so) => {
if (err) return cb(err)
this.format = so
this.inactive(cb, null, so)
})
})
}
spectrogram (cb) {
this.open((err) => {
if (err) return cb(err)
if (!this.active(cb)) return
getSpectrogram(this.filename, (err, sp) => {
if (err) return cb(err)
this.spectrogram = new Asset(sp, { hint: 'image' })
this.inactive(cb, null, this.spectrogram)
})
})
}
stats (cb) {
this.open((err) => {
if (err) return cb(err)
if (!this.active(cb)) return
getStats(this.filename, (err, st) => {
if (err) return cb(err)
this.stats = st
this.inactive(cb, null, st)
})
})
}
waveform (cb) {
this.open((err) => {
if (err) return cb(err)
if (!this.active(cb)) return
const waveformPath = `${this.filename}_waveform.png`
const ffmpegCmd = ffmpeg(this.filename)
.complexFilter(flags.waveform)
.output(waveformPath)
.on('error', err => { return this.inactive(cb, err) })
.on('end', () => {
debug('waveform finished', waveformPath)
this.waveform = new Asset(waveformPath, { hint: 'image' })
this.inactive(cb, null, this.waveform)
})
ffmpegCmd.run()
})
}
}
module.exports = Track