| 15 | }; |
| 16 | |
| 17 | constructor(source, cfg) { |
| 18 | Object.assign(this.config, cfg); |
| 19 | this.context = source.context; |
| 20 | this.node = (this.context.createScriptProcessor || |
| 21 | this.context.createJavaScriptNode).call(this.context, |
| 22 | this.config.bufferLen, this.config.numChannels, this.config.numChannels); |
| 23 | |
| 24 | this.node.onaudioprocess = (e) => { |
| 25 | if (!this.recording) return; |
| 26 | |
| 27 | var buffer = []; |
| 28 | for (var channel = 0; channel < this.config.numChannels; channel++) { |
| 29 | buffer.push(e.inputBuffer.getChannelData(channel)); |
| 30 | } |
| 31 | this.worker.postMessage({ |
| 32 | command: 'record', |
| 33 | buffer: buffer |
| 34 | }); |
| 35 | }; |
| 36 | |
| 37 | source.connect(this.node); |
| 38 | this.node.connect(this.context.destination); //this should not be necessary |
| 39 | |
| 40 | let self = {}; |
| 41 | this.worker = new InlineWorker(function () { |
| 42 | let recLength = 0, |
| 43 | recBuffers = [], |
| 44 | sampleRate, |
| 45 | numChannels; |
| 46 | |
| 47 | this.onmessage = function (e) { |
| 48 | switch (e.data.command) { |
| 49 | case 'init': |
| 50 | init(e.data.config); |
| 51 | break; |
| 52 | case 'record': |
| 53 | record(e.data.buffer); |
| 54 | break; |
| 55 | case 'exportWAV': |
| 56 | exportWAV(e.data.type); |
| 57 | break; |
| 58 | case 'getBuffer': |
| 59 | getBuffer(); |
| 60 | break; |
| 61 | case 'clear': |
| 62 | clear(); |
| 63 | break; |
| 64 | } |
| 65 | }; |
| 66 | |
| 67 | function init(config) { |
| 68 | sampleRate = config.sampleRate; |
| 69 | numChannels = config.numChannels; |
| 70 | initBuffers(); |
| 71 | } |
| 72 | |
| 73 | function record(inputBuffer) { |
| 74 | for (var channel = 0; channel < numChannels; channel++) { |