This repository was archived by the owner on Sep 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
67 lines (52 loc) · 1.58 KB
/
index.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
/**
* @module audio-through
*/
'use strict';
const convert = require('pcm-convert')
const aformat = require('audio-format')
const createBuffer = require('audio-buffer-from')
const isAudioBuffer = require('is-audio-buffer')
const defined = require('defined')
module.exports = createThrough
function createThrough (fn, format) {
if (!format) format = 'array mono 44100'
format = typeof format === 'string' ? aformat.parse(format) : aformat.detect(format)
format.length = defined(format.length, format.frame, format.block, format.samplesPerFrame, format.frameSize, format.frameLength, format.blockSize, format.blockLength, 1024)
let state = {
count: 0,
time: 0,
format: format
}
// fill passed source with oscillated data
function through (dst, ...args) {
let buf = dst
if (buf == null) buf = format.length
//make sure we deal with audio buffer
if (!isAudioBuffer(buf)) {
buf = createBuffer(buf, format)
}
// fill channels
let data
// audio-buffer interception hack
if (buf._channelData) {
data = buf._channelData
}
else {
data = []
for (let c = 0, l = buf.numberOfChannels; c < l; c++) {
data[c] = buf.getChannelData(c)
}
}
state.time = state.count / state.format.sampleRate
let count = state.count
// generate data by generating function
fn(data, state, ...args)
state.count = count + data[0].length
// convert to target dtype
if (format.type === 'audiobuffer') return buf
if (format.type === 'array' && !dst) return data
if (dst && dst.length) return convert(buf, format, dst)
return convert(buf, format)
}
return through
}