-
Notifications
You must be signed in to change notification settings - Fork 9
/
audio-fingerprinting.js
102 lines (77 loc) · 1.96 KB
/
audio-fingerprinting.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
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
var audioFingerprint = (function () {
var context = null;
var currentTime = null;
var oscillator = null;
var compressor = null;
var fingerprint = null;
var callback = null
function run(cb, debug = false) {
callback = cb;
try {
setup();
oscillator.connect(compressor);
compressor.connect(context.destination);
oscillator.start(0);
context.startRendering();
context.oncomplete = onComplete;
} catch (e) {
if (debug) {
throw e;
}
}
}
function setup()
{
setContext();
currentTime = context.currentTime;
setOscillator();
setCompressor();
}
function setContext()
{
var audioContext = window.OfflineAudioContext || window.webkitOfflineAudioContext;
context = new audioContext(1, 44100, 44100);
}
function setOscillator()
{
oscillator = context.createOscillator();
oscillator.type = "triangle";
oscillator.frequency.setValueAtTime(10000, currentTime);
}
function setCompressor()
{
compressor = context.createDynamicsCompressor();
setCompressorValueIfDefined('threshold', -50);
setCompressorValueIfDefined('knee', 40);
setCompressorValueIfDefined('ratio', 12);
setCompressorValueIfDefined('reduction', -20);
setCompressorValueIfDefined('attack', 0);
setCompressorValueIfDefined('release', .25);
}
function setCompressorValueIfDefined(item, value)
{
if (compressor[item] !== undefined && typeof compressor[item].setValueAtTime === 'function') {
compressor[item].setValueAtTime(value, context.currentTime);
}
}
function onComplete(event)
{
generateFingerprints(event);
compressor.disconnect();
}
function generateFingerprints(event)
{
var output = null;
for (var i = 4500; 5e3 > i; i++) {
var channelData = event.renderedBuffer.getChannelData(0)[i];
output += Math.abs(channelData);
}
fingerprint = output.toString();
if (typeof callback === 'function') {
return callback(fingerprint);
}
}
return {
run:run
};
})();