-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsox.js
493 lines (456 loc) · 12.9 KB
/
sox.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
const Hypercore = require('hypercore')
const nanoprocess = require('nanoprocess')
const { PassThrough } = require('stream')
const process = require('process')
const ram = require('random-access-memory')
const Wavecore = require('./index')
const WaveFile = require('wavefile').WaveFile
/**
* The `WavecoreSox` class provides a SoX interface for Wavecore.
* @class
* @extends {Wavecore}
*/
class WavecoreSox extends Wavecore {
/**
* The `WavecoreSox` class constructor.
* @arg {Object} [opts={}] - Options
* @arg {Hypercore} [opts.core=null] - Provide a previously-made hypercore.
* @arg {Integer} [opts.indexSize=null] - Declare an alternate index size.
* @arg {Buffer|Readable|PassThrough|Array|ArrayBuffer} [opts.source] - PCM
* source.
* @arg {Buffer} [opts.encryptionKey=null] - Encryption key
* @arg {random-access-storage} [opts.storage=ram] - Provide storage instance.
* @returns {WavecoreSox} wavecoreSox - The new Wavecore with SoX methods.
*/
constructor(opts={
core: null,
key: null,
encryptionKey: null,
hypercoreOpts: null,
indexSize: 96000,
parent: null,
source: null,
storage: null
}) {
super(opts)
}
/**
* Get the maximum volume adjustment value for the Wavecore's PCM audio data.
* Used by the `norm()` method to ensure the normalized audio does not clip.
* @arg {Object} [opts={}] - Optional options object
* @arg {Number} [opts.start=0] - Index from which to start the stream
* @arg {Number} [opts.end=-1] - Index where the stream should end.
* @returns {Number} vol - The SoX `vol -v` value.
*/
_volAdjust(opts = { start: 0, end: -1 }) {
const { start, end } = opts
return new Promise((resolve, reject) => {
const statsCmd = nanoprocess('sox', [
'-r',
'48000',
'-b',
'16',
'-e',
'signed',
'-t',
'raw',
'-',
'-n',
'stat',
'-v',
])
statsCmd.open((err) => {
if (err) reject(err)
const statsOut = []
const pt = new PassThrough()
pt.on('data', (d) => statsOut.push(`${d}`))
statsCmd.on('close', (code) => {
if (code !== 0) reject(new Error('Non-zero exit code'))
resolve(Number(statsOut.join('')))
})
statsCmd.stderr.pipe(pt)
this._rawStream(start, end).pipe(statsCmd.stdin)
})
})
}
/**
* Apply gain attenuation or amplification to the Wavecore audio.
* @arg {String} g - A string beginning with `+` or `-` indicating gain
* operation to perform on the audio, i.e., `+8` or `-2.3`.
* @arg {Object} [opts={}] - Optional options object
* @arg {Number} [opts.start=0] - Start index
* @arg {Number} [opts.end=-1] - End index
* @arg {Boolean} [opts.limiter=false] - Whether to apply a limiter to the
* gain function to prevent clipping.
* @returns {Wavecore} - New Wavecore with the gain processing applied.
*/
async gain(g, opts = { start: 0, end: -1, limiter: false }) {
const { start, end, limiter } = opts
const rs = this._rawStream(start, end)
const cmdOpts = [
'-r',
'48000',
'-b',
'16',
'-e',
'signed',
'-t',
'raw',
'-',
'-t',
'raw',
'-',
'gain',
]
if (limiter) cmdOpts.push('-l')
cmdOpts.push(`${g}`)
const gainCmd = nanoprocess('sox', cmdOpts)
const prom = new Promise((resolve, reject) => {
gainCmd.open((err) => {
if (err) reject(err)
const newGainCore = new WavecoreSox(ram)
const ws = newGainCore.createWriteStream({
highWaterMark: this.indexSize,
})
ws.on('close', () => {
newGainCore
.update()
.then(() => resolve(newGainCore))
})
gainCmd.stdout.pipe(ws)
rs.pipe(gainCmd.stdin)
})
})
return await Promise.resolve(prom)
}
/**
* Listen live to the audio data coming in to the Wavecore. Great way to
* monitor the audio inputs or broadcast the content to others.
*/
monitor() {
const cmdOpts = [
'-r',
'48000',
'-b',
'16',
'-e',
'signed',
'-t',
'raw',
'-',
]
const playCmd = nanoprocess('play', cmdOpts)
playCmd.open((err) => {
playCmd.stdout.pipe(process.stdout)
this.liveStream.pipe(playCmd.stdin)
})
}
/**
* Normalize the audio data in the Wavecore. Returns a new Wavecore instance.
* @arg {Object} [opts={}] - Optional options object
* @arg {Number} [opts.start=0] - Index from which to start the stream
* @arg {Number} [opts.end=-1] - Index where the stream should end.
*/
async norm(opts = { start: 0, end: -1 }) {
const { start, end } = opts
try {
const vol = await this._volAdjust({ start, end })
const normCmd = nanoprocess('sox', [
'-r',
'48000',
'-b',
'16',
'-e',
'signed',
'-t',
'raw',
'-',
'-t',
'raw',
'-',
'vol',
vol,
])
const rs = this._rawStream(start, end)
const prom = new Promise((resolve, reject) => {
normCmd.open((err) => {
if (err) reject(err)
// TODO figure out why number of indeces higher in new wavecore
const newCore = new WavecoreSox(ram)
const pt = new PassThrough({ highWaterMark: this.indexSize })
pt.on('error', (err) => reject(err))
pt.on('data', (d) => newCore.append(d))
normCmd.on('close', (code) => {
if (code !== 0) reject(new Error('Non-zero exit code'))
resolve(newCore)
})
normCmd.stdout.pipe(pt)
rs.pipe(normCmd.stdin)
})
})
return await Promise.resolve(prom)
} catch (err) {
throw err
}
}
/**
* Play the raw Wavecore PCM audio via a nanoprocess
* @arg {Object} [opts={}] - Optional options object
* @arg {Number} [opts.start=0] - Start index
* @arg {Number} [opts.end=-1] - End index
* @arg {nanoprocess} [opts.np=null] - Declare a custom nanoprocess for playback
* @see {@link https://github.com/little-core-labs/nanoprocess nanoprocess}
*/
play(opts = { start: 0, end: -1, np: null }) {
const { np, start, end } = opts
let proc = null
if (np) {
proc = np
} else {
proc = nanoprocess('play', [
'-r',
'48000',
'-b',
'16',
'-e',
'signed',
'-t',
'raw',
'-',
])
}
if (!proc) throw new Error('nanoprocess didnt work wtf')
const rs = this._rawStream(start, end)
proc.open((err) => {
if (err) throw err
rs.on('end', () => console.log('ended'))
proc.stderr.pipe(process.stderr)
proc.stdout.pipe(process.stdout)
rs.pipe(proc.stdin)
})
}
/** Record into the Wavecore via the `rec` CLI application.
* @arg {String} [dur="30:00"] - Duration string for recording; defaults to
* 30min.
*/
async _rec(dur = '30:00') {
const cmdOpts = [
'-r',
'48000',
'-c',
'1',
'-b',
'16',
'-e',
'signed-integer',
'-t',
'raw',
'-',
'trim',
'0',
`${dur}`,
]
const recCmd = nanoprocess('rec', cmdOpts)
const prom = new Promise((resolve, reject) => {
recCmd.open((err) => {
if (err) reject(err)
recCmd.on('close', (code) => {
if (code !== 0) reject(new Error('Non-Zero exit code!', code))
this.update().then(() => resolve())
})
recCmd.stdout.pipe(
this.createWriteStream({ highWaterMark: this.indexSize })
)
})
})
await Promise.resolve(prom)
}
/**
* Runs `sox -n stats` on the raw audio in the Wavecore, via a nanoprocess.
* @async
* @arg {Object} [opts={}] - Optional opts object for declaring index
* @arg {Number} [opts.index=null] - Declare index to get stats on
* @returns {String} statsOut - The string of stats information returned by
* SoX
*/
async stats(opts = { index: null }) {
const { index } = opts
const statsCmd = nanoprocess('sox', [
'-r',
'48000',
'-b',
'16',
'-e',
'signed',
'-t',
'raw',
'-',
'-n',
'stats',
'stat',
])
const statsOut = []
const pt = new PassThrough()
pt.on('data', (d) => statsOut.push(`${d}`))
const prom = new Promise((resolve, reject) => {
statsCmd.open((err) => {
if (err) reject(err)
statsCmd.on('close', (code) => {
if (code !== 0) reject(new Error('Non-zero exit code'))
resolve(statsOut.join(''))
})
statsCmd.stderr.pipe(pt)
let rs = null
if (index !== null) {
rs = this._rawStream(index, index + 1)
} else {
rs = this.createReadStream()
}
rs.pipe(statsCmd.stdin)
})
})
return await Promise.resolve(prom)
}
/**
* Increase or decrease playback speed of audio samples, without changing the
* pitch of the audio itself. Returns a new Wavecore containing the
* time-stretched samples.
* @arg {Float} f - The new tempo factor. 0.9 = slow down by 10%; 1.1 = faster
* by 10%.
* @arg {Object} [opts={}] - Optional options object
* @arg {Boolean} [opts.stats=false] - Whether to also get SoX stats on the
* time-stretched audio data. Currently these stats are output to `stdout`.
* Useful for getting the new Wavecore's audio duration.
* @returns {Wavecore} stretchedCore - The new time-stretched Wavecore.
*/
async tempo(f, opts = { stats: false }) {
const { stats } = opts
const cmdOpts = [
'-r',
'48000',
'-b',
'16',
'-e',
'signed',
'-t',
'raw',
'-',
'-t',
'raw',
'-',
'tempo',
'-s',
`${f}`,
]
if (stats) cmdOpts.push('stats')
const tempoCmd = nanoprocess('sox', cmdOpts)
const prom = new Promise((resolve, reject) => {
tempoCmd.open((err) => {
if (err) reject(err)
const newCore = new WavecoreSox(ram)
const pt = new PassThrough()
pt.on('data', (d) => newCore.append(d))
tempoCmd.on('close', (code) => {
if (code !== 0) reject(new Error('Non-zero exit code'))
resolve(newCore)
})
tempoCmd.stdout.pipe(pt)
if (stats) tempoCmd.stderr.pipe(process.stdout)
let rs = this._rawStream()
rs.pipe(tempoCmd.stdin)
})
})
return await Promise.resolve(prom)
}
/**
* Runs `sox vad` on the Wavecore audio. Trims excessive silence from the
* front of voice recordings.
* @returns {Wavecore}
*/
async vad() {
const cmdOpts = [
'-r',
'48000',
'-b',
'16',
'-e',
'signed',
'-t',
'raw',
'-',
'-t',
'raw',
'-',
'vad',
]
const cmd = nanoprocess('sox', cmdOpts)
const newCore = new WavecoreSox(ram)
const normCore = await this.norm()
const prom = new Promise((resolve, reject) => {
cmd.open((err) => {
if (err) reject(err)
cmd.on('close', (code) => {
if (code !== 0) reject(new Error('Non-zero exit!', code))
resolve(newCore)
})
cmd.stdout.pipe(
newCore.createWriteStream({ highWaterMark: this.indexSize })
)
normCore._rawStream().pipe(cmd.stdin)
})
})
return await Promise.resolve(prom)
}
/**
* Returns a `Promise` which resolves a `Buffer` of a PCM WAV file. Requires
* `sox` in PATH.
* @arg {Object} [opts={}] - Optional options object
* @arg {Boolean} [opts.store=false] - Whether to store the wav as a buffer in
* the Wavecore class instance.
* @returns {Buffer} wavBuf - WAV file Buffer
*/
async wav(opts = { store: false }) {
const { store } = opts
const bufs = []
const pt = new PassThrough()
pt.on('error', (err) => reject(err))
pt.on('data', (d) => bufs.push(d))
const soxCmd = nanoprocess('sox', [
'-r',
'48000',
'-b',
'16',
'-e',
'signed',
'-t',
'raw',
'-',
'-t',
'wav',
'-',
])
const np = new Promise((resolve, reject) => {
soxCmd.open((err) => {
if (err) reject(err)
soxCmd.on('close', (code) => {
if (code !== 0) reject(new Error('Non-Zero exit-code!', code))
const wavBuf = Buffer.concat(bufs)
if (store) this.wavBuffer = wavBuf
if (this.wavBuffer) this.wavFile = new WaveFile(this.wavBuffer)
if (this.wavFile) {
Array.from(this.tags).forEach((t) =>
this.wavFile.setTag(t[0], t[1])
)
this.wavBuffer = Buffer.from(this.wavFile.toBuffer())
}
resolve(wavBuf)
})
soxCmd.stdout.pipe(pt)
const rs = this.createReadStream()
rs.pipe(soxCmd.stdin)
})
})
return await Promise.resolve(np)
}
}
module.exports = WavecoreSox