-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalbum.js
102 lines (91 loc) · 2.59 KB
/
album.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
const Batch = require('batch')
const debug = require('debug')('baptism:album')
const fs = require('fs')
const os = require('os')
const path = require('path')
const { Master, Premaster } = require('./master')
const { Pool } = require('nanoresource-pool')
const Track = require('./track')
class Album extends Pool {
constructor(dir, opts={}) {
super(Track)
this.metadata = {}
this.duration = 0.0
this.dir = path.resolve(dir)
this.sources = fs.readdirSync(this.dir).map(p => `${this.dir}/${p}`)
.filter(f => path.extname(f) === '.wav')
this.infoTags = []
if (opts.metadata) {
if (opts.metadata.artist) {
this.infoTags.push(['IART', opts.metadata.artist])
}
if (opts.metadata.comment) {
this.infoTags.push(['ICMT'], opts.metadata.comment)
}
if (opts.metadata.album) {
this.infoTags.push(['IPRD', opts.metadata.album])
}
}
let counter = 0
for (const source of this.sources) {
counter++
debug('track counter', counter)
this.add(new Premaster(source, {
trackNumber: counter,
tags: this.infoTags
}))
}
}
probe(callback) {
const probes = {}
const batch = new Batch().concurrency(4)
for (const source of this.query()) {
batch.push((next) => {
source.stats((err, info) => {
if (err) { return next(err) }
probes[source.filename] = info
source.silence((err) => {
if (err) debug(err)
next(null)
})
})
})
batch.push((next) => {
source.fingerprint((err) => {
if (err) debug(err)
next(null)
})
})
batch.push((next) => {
source.soxi((err) => {
if (err) debug(err)
next(null)
})
})
}
batch.end((err) => {
if (err) { return callback(err) }
// Sum duration of all tracks
this.duration += Object.keys(probes).map(p => probes[p].duration)
.reduce((a, b) => a + b)
this.validate
callback(null, probes)
})
}
get validate() {
const validations = {
format: {
bitDepth: this.query()
.every((tr, i, arr) => tr.format.bitDepth === arr[0].format.bitDepth),
channels: this.query()
.every((tr, i, arr) => tr.format.channels === 2),
sampleRate: this.query()
.every((tr, i, arr) => tr.format.sampleRate === arr[0].format.sampleRate)
},
silences: this.query()
.every(tr => tr.silences.start && tr.silences.end)
}
return this.ready = validations
}
}
module.exports = Album