Spaces:
Running
Running
File size: 4,652 Bytes
6bcb42f |
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 |
import 'get-float-time-domain-data';
import getUserMedia from 'get-user-media-promise';
import SharedAudioContext from './shared-audio-context.js';
import {computeRMS, computeChunkedRMS} from './audio-util.js';
class AudioRecorder {
constructor () {
this.audioContext = new SharedAudioContext();
this.bufferLength = 8192;
this.userMediaStream = null;
this.mediaStreamSource = null;
this.sourceNode = null;
this.scriptProcessorNode = null;
this.recordedSamples = 0;
this.recording = false;
this.started = false;
this.buffers = [];
this.disposed = false;
}
startListening (onStarted, onUpdate, onError) {
try {
getUserMedia({audio: true})
.then(userMediaStream => {
if (!this.disposed) {
this.started = true;
onStarted();
this.attachUserMediaStream(userMediaStream, onUpdate);
}
})
.catch(e => {
if (!this.disposed) {
onError(e);
}
});
} catch (e) {
if (!this.disposed) {
onError(e);
}
}
}
startRecording () {
this.recording = true;
}
attachUserMediaStream (userMediaStream, onUpdate) {
this.userMediaStream = userMediaStream;
this.mediaStreamSource = this.audioContext.createMediaStreamSource(userMediaStream);
this.sourceNode = this.audioContext.createGain();
this.scriptProcessorNode = this.audioContext.createScriptProcessor(this.bufferLength, 1, 1);
this.scriptProcessorNode.onaudioprocess = processEvent => {
if (this.recording && !this.disposed) {
this.buffers.push(new Float32Array(processEvent.inputBuffer.getChannelData(0)));
}
};
this.analyserNode = this.audioContext.createAnalyser();
this.analyserNode.fftSize = 2048;
const bufferLength = this.analyserNode.frequencyBinCount;
const dataArray = new Float32Array(bufferLength);
const update = () => {
if (this.disposed) return;
this.analyserNode.getFloatTimeDomainData(dataArray);
onUpdate(computeRMS(dataArray));
requestAnimationFrame(update);
};
requestAnimationFrame(update);
// Wire everything together, ending in the destination
this.mediaStreamSource.connect(this.sourceNode);
this.sourceNode.connect(this.analyserNode);
this.analyserNode.connect(this.scriptProcessorNode);
this.scriptProcessorNode.connect(this.audioContext.destination);
}
stop () {
const buffer = new Float32Array(this.buffers.length * this.bufferLength);
let offset = 0;
for (let i = 0; i < this.buffers.length; i++) {
const bufferChunk = this.buffers[i];
buffer.set(bufferChunk, offset);
offset += bufferChunk.length;
}
const chunkLevels = computeChunkedRMS(buffer);
const maxRMS = Math.max.apply(null, chunkLevels);
const threshold = maxRMS / 8;
let firstChunkAboveThreshold = null;
let lastChunkAboveThreshold = null;
for (let i = 0; i < chunkLevels.length; i++) {
if (chunkLevels[i] > threshold) {
if (firstChunkAboveThreshold === null) firstChunkAboveThreshold = i + 1;
lastChunkAboveThreshold = i + 1;
}
}
let trimStart = Math.max(2, firstChunkAboveThreshold - 2) / this.buffers.length;
let trimEnd = Math.min(this.buffers.length - 2, lastChunkAboveThreshold + 2) / this.buffers.length;
// With very few samples, the automatic trimming can produce invalid values
if (trimStart >= trimEnd) {
trimStart = 0;
trimEnd = 1;
}
return {
levels: chunkLevels,
samples: buffer,
sampleRate: this.audioContext.sampleRate,
trimStart: trimStart,
trimEnd: trimEnd
};
}
dispose () {
if (this.started) {
this.scriptProcessorNode.onaudioprocess = null;
this.scriptProcessorNode.disconnect();
this.analyserNode.disconnect();
this.sourceNode.disconnect();
this.mediaStreamSource.disconnect();
this.userMediaStream.getAudioTracks()[0].stop();
}
this.disposed = true;
}
}
export default AudioRecorder;
|