Skip to content

Commit

Permalink
fix: discard noise from audio challenge
Browse files Browse the repository at this point in the history
  • Loading branch information
dessant committed Feb 1, 2020
1 parent e985b7d commit a57cdb8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 18 deletions.
28 changes: 11 additions & 17 deletions src/background/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import {
getBrowser,
getPlatform,
getRandomInt,
arrayBufferToBase64
arrayBufferToBase64,
normalizeAudio,
sliceAudio
} from 'utils/common';
import {
captchaGoogleSpeechApiLangCodes,
Expand Down Expand Up @@ -200,23 +202,15 @@ function removeBackgroundRequestLitener() {
}

async function prepareAudio(audio) {
const ctx = new AudioContext();
const data = await ctx.decodeAudioData(audio);
await ctx.close();

const offlineCtx = new OfflineAudioContext(
// force mono output
1,
16000 * data.duration,
16000
);
const source = offlineCtx.createBufferSource();
source.buffer = data;
source.connect(offlineCtx.destination);
// discard 1.5 second noise from beginning/end
source.start(0, 1.5, data.duration - 3);
const audioBuffer = await normalizeAudio(audio);

const audioSlice = await sliceAudio({
audioBuffer,
start: 1.5,
end: audioBuffer.duration - 1.5
});

return audioBufferToWav(await offlineCtx.startRendering());
return audioBufferToWav(audioSlice);
}

async function getWitSpeechApiKey(speechService, language) {
Expand Down
43 changes: 42 additions & 1 deletion src/utils/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,45 @@ function sleep(ms) {
return new Promise(resolve => window.setTimeout(resolve, ms));
}

async function normalizeAudio(buffer) {
const ctx = new AudioContext();
const audioBuffer = await ctx.decodeAudioData(buffer);
ctx.close();

const offlineCtx = new OfflineAudioContext(
1,
audioBuffer.duration * 16000,
16000
);
const source = offlineCtx.createBufferSource();
source.connect(offlineCtx.destination);
source.buffer = audioBuffer;
source.start();

return offlineCtx.startRendering();
}

async function sliceAudio({audioBuffer, start, end}) {
const sampleRate = audioBuffer.sampleRate;
const channels = audioBuffer.numberOfChannels;

const startOffset = sampleRate * start;
const endOffset = sampleRate * end;
const frameCount = endOffset - startOffset;

const ctx = new AudioContext();
const audioSlice = ctx.createBuffer(channels, frameCount, sampleRate);
ctx.close();

const tempArray = new Float32Array(frameCount);
for (var channel = 0; channel < channels; channel++) {
audioBuffer.copyFromChannel(tempArray, channel, startOffset);
audioSlice.copyToChannel(tempArray, channel, 0);
}

return audioSlice;
}

export {
getText,
createTab,
Expand All @@ -175,5 +214,7 @@ export {
functionInContext,
getRandomInt,
getRandomFloat,
sleep
sleep,
normalizeAudio,
sliceAudio
};

0 comments on commit a57cdb8

Please sign in to comment.