Skip to content

Instantly share code, notes, and snippets.

@mrflix
Created September 12, 2013 14:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrflix/6538300 to your computer and use it in GitHub Desktop.
Save mrflix/6538300 to your computer and use it in GitHub Desktop.
Soundfinder Bookmarklet
// MIT License
// by @mrflix
(function(){
// Template Matching algorithm using correlation coeffizient
// http://en.wikipedia.org/wiki/Template_matching
// learned at HTW Berlin tought by Prof. Dr. Kai Uwe Barthel
function getCorrelationCoeffizient(data1, data2){
var sumI = 0, sumI2 = 0, sumIR = 0, sumR = 0, sumR2 = 0;
var nR = data1.length;
for(var i=0; i<nR; i++){
var vI = data1[i];
var vR = data2[i];
sumI += vI;
sumI2 += vI * vI;
sumR += vR;
sumR2 += vR * vR;
sumIR += vI * vR;
}
var meanI = sumI / nR;
var meanR = sumI / nR;
var sigmaI = Math.sqrt(sumI2 - nR * meanI * meanI);
var sigmaR = Math.sqrt(sumR2 - nR * meanR * meanR);
return (sumIR - nR * meanI * meanR) / (sigmaI * sigmaR);
}
// place a yellow star onto the album cover
function placeStar(i){
$('.searchList__item').eq(i).find('.image').append($('<span class="star">★</span>').css({
position: 'absolute',
right: '-13px',
bottom: '-18px',
fontSize: '34px',
color: 'hsl(48,100%,50%)',
textShadow: '0 1px 3px rgba(0, 0, 0, 0.21)'
}));
}
var waveformData = [];
// collect all waveforms
$('canvas.waveform__layer:first-child').each(function(i, canvas){
var ctx = canvas.getContext('2d');
waveformData.push( ctx.getImageData(0,0,canvas.clientWidth,canvas.clientHeight).data );
});
// compare each waveform against one another
// and star them if their euqal enough (0.9 threshold)
for(var i=0, l=waveformData.length; i<l; i++){
for(var j=1; j < (l-i); j++){
var coeffizient = getCorrelationCoeffizient(waveformData[i], waveformData[i+j]);
console.log(coeffizient);
if(coeffizient > 0.9){
placeStar(i);
placeStar(i+j);
}
}
}
})()
javascript:(function()%7Bfunction e(e,t)%7Bvar n%3D0,r%3D0,i%3D0,s%3D0,o%3D0%3Bvar u%3De.length%3Bfor(var a%3D0%3Ba<u%3Ba%2B%2B)%7Bvar f%3De%5Ba%5D%3Bvar l%3Dt%5Ba%5D%3Bn%2B%3Df%3Br%2B%3Df*f%3Bs%2B%3Dl%3Bo%2B%3Dl*l%3Bi%2B%3Df*l%7Dvar c%3Dn/u%3Bvar h%3Dn/u%3Bvar p%3DMath.sqrt(r-u*c*c)%3Bvar d%3DMath.sqrt(o-u*h*h)%3Breturn(i-u*c*h)/(p*d)%7Dfunction t(e)%7B%24(".searchList__item").eq(e).find(".image").append(%24(%27<span class%3D"star">★</span>%27).css(%7Bposition:"absolute",right:"-13px",bottom:"-18px",fontSize:"34px",color:"hsl(48,100%25,50%25)",textShadow:"0 1px 3px rgba(0,0,0,0.21)"%7D))%7Dvar n%3D%5B%5D%3B%24("canvas.waveform__layer:first-child").each(function(e,t)%7Bvar r%3Dt.getContext("2d")%3Bn.push(r.getImageData(0,0,t.clientWidth,t.clientHeight).data)%7D)%3Bfor(var r%3D0,i%3Dn.length%3Br<i%3Br%2B%2B)%7Bfor(var s%3D1%3Bs<i-r%3Bs%2B%2B)%7Bvar o%3De(n%5Br%5D,n%5Br%2Bs%5D)%3Bconsole.log(o)%3Bif(o>.9)%7Bt(r)%3Bt(r%2Bs)%7D%7D%7D%7D)()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment