Skip to content

Commit

Permalink
Adding copyFromChannel for audio fingerprinting
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanKingston committed Feb 23, 2021
1 parent c2a57df commit 38dc3e8
Showing 1 changed file with 61 additions and 42 deletions.
103 changes: 61 additions & 42 deletions privacy-protections/fingerprinting/helpers/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -994,50 +994,31 @@ const tests = [
{
id: 'audio',
category: 'full-fingerprints',
getValue: () => {
let resolve;
const promise = new Promise((res, rej) => { resolve = res; });

// still pefixed in Safari
getValue: async () => {
// eslint-disable-next-line new-cap
const context = window.OfflineAudioContext ? new OfflineAudioContext(1, 44100, 44100) : new webkitOfflineAudioContext(1, 44100, 44100);

const oscillator = context.createOscillator();
oscillator.type = 'triangle';
oscillator.frequency.setValueAtTime(10000, context.currentTime);

const compressor = context.createDynamicsCompressor();

[
['threshold', -50],
['knee', 40],
['ratio', 12],
['reduction', -20],
['attack', 0],
['release', 0.25]
].forEach(function (item) {
if (compressor[item[0]] !== undefined && typeof compressor[item[0]].setValueAtTime === 'function') {
compressor[item[0]].setValueAtTime(item[1], context.currentTime);
}
});

context.oncomplete = (event) => {
const fingerprint = event.renderedBuffer.getChannelData(0)
.slice(4500, 5000)
.reduce(function (acc, val) { return acc + Math.abs(val); }, 0)
.toString();
oscillator.disconnect();
compressor.disconnect();

resolve(fingerprint);
};

oscillator.connect(compressor);
compressor.connect(context.destination);
oscillator.start(0);
context.startRendering();

return promise;
const renderedBuffer = await applyFpExampleDataToAudio(context);
const fingerprint = renderedBuffer.getChannelData(0)
.slice(4500, 5000)
.reduce(function (acc, val) { return acc + Math.abs(val); }, 0)
.toString();
return fingerprint;
}
},
{
id: 'audio-copyFromChannel',
category: 'full-fingerprints',
getValue: async () => {
// eslint-disable-next-line new-cap
const context = window.OfflineAudioContext ? new OfflineAudioContext(1, 44100, 44100) : new webkitOfflineAudioContext(1, 44100, 44100);
const renderedBuffer = await applyFpExampleDataToAudio(context);
const copiedData = new Float32Array(renderedBuffer.length);
renderedBuffer.copyFromChannel(copiedData, 0, 0);

const fingerprint = copiedData.slice(4500, 5000)
.reduce(function (acc, val) { return acc + Math.abs(val); }, 0)
.toString();
return fingerprint;
}
},
{
Expand Down Expand Up @@ -1365,3 +1346,41 @@ function applyFpExampleDataToCanvas (canvas) {
ctx.arc(75, 75, 25, 0, Math.PI * 2, true);
ctx.fill('evenodd');
}

function applyFpExampleDataToAudio (context) {
let resolve;
const promise = new Promise((res, rej) => { resolve = res; });

const oscillator = context.createOscillator();
oscillator.type = 'triangle';
oscillator.frequency.setValueAtTime(10000, context.currentTime);

const compressor = context.createDynamicsCompressor();

[
['threshold', -50],
['knee', 40],
['ratio', 12],
['reduction', -20],
['attack', 0],
['release', 0.25]
].forEach(function (item) {
if (compressor[item[0]] !== undefined && typeof compressor[item[0]].setValueAtTime === 'function') {
compressor[item[0]].setValueAtTime(item[1], context.currentTime);
}
});

context.oncomplete = (event) => {
oscillator.disconnect();
compressor.disconnect();

resolve(event.renderedBuffer);
};

oscillator.connect(compressor);
compressor.connect(context.destination);
oscillator.start(0);
context.startRendering();

return promise;
}

0 comments on commit 38dc3e8

Please sign in to comment.