-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstats.js
40 lines (37 loc) · 953 Bytes
/
stats.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
const debug = require('debug')('baptism:stats')
const path = require('path')
const { spawn } = require('child_process')
function parseStats(st) {
try {
const stats = `${st}`.split('\n').map(stat => { return Number(stat.split(':')[1]) })
const statsObj = {
duration: stats[1],
peak: {
db: 20 * Math.log10( stats[3] ),
value: stats[3]
},
rms: {
db: 20 * Math.log10( stats[8] ),
value: stats[8]
}
}
return statsObj
} catch (err) {
throw new Error(err)
}
}
function main(file, cb) {
let statsStr = ''
const statsObj = {}
const soxCmd = spawn('sox', [ path.resolve(file), '-n', 'stat' ])
soxCmd.on('error', err => { cb(new Error(err)) })
soxCmd.stderr.on('data', d => {
statsStr += d
})
soxCmd.on('close', (code) => {
debug('exitcode', code)
Object.assign(statsObj, parseStats(statsStr))
cb(null, statsObj)
})
}
module.exports = main