From 953af9e80e3b165eb99da90d6c64e79803a460c0 Mon Sep 17 00:00:00 2001 From: Loreto Parisi Date: Fri, 20 Jul 2018 16:17:55 +0200 Subject: [PATCH] Added Chromaprint fingerprint comparation --- lib/compare.coffee | 72 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 lib/compare.coffee diff --git a/lib/compare.coffee b/lib/compare.coffee new file mode 100644 index 0000000..12b4bc0 --- /dev/null +++ b/lib/compare.coffee @@ -0,0 +1,72 @@ +# +# Compare Two Chroma print Fingerprint +# Inspired to Python Algorithm https://github.com/bjjb/chromaprint.js/issues/3 +# Other sources from here https://groups.google.com/forum/#!topic/acoustid/7AQk8XlycEk +# @author Loreto Parisi (loretoparisi at gmail dot com) - JavaScript version +# +ord = (string) -> + # discuss at: http://locutus.io/php/ord/ + # original by: Kevin van Zonneveld (http://kvz.io) + # bugfixed by: Onno Marsman (https://twitter.com/onnomarsman) + # improved by: Brett Zamir (http://brett-zamir.me) + # input by: incidence + # example 1: ord('K') + # returns 1: 75 + # example 2: ord('\uD800\uDC00'); // surrogate pair to create a single Unicode character + # returns 2: 65536 + str = string + '' + code = str.charCodeAt(0) + if code >= 0xD800 and code <= 0xDBFF + # High surrogate (could change last hex to 0xDB7F to treat + # high private surrogates as single characters) + hi = code + if str.length == 1 + # This is just a high surrogate with no following low surrogate, + # so we return its value; + return code + # we could also throw an error as it is not a complete character, + # but someone may want to know + low = str.charCodeAt(1) + return (hi - 0xD800) * 0x400 + low - 0xDC00 + 0x10000 + if code >= 0xDC00 and code <= 0xDFFF + # Low surrogate + # This is just a low surrogate with no preceding high surrogate, + # so we return its value; + return code + # we could also throw an error as it is not a complete character, + # but someone may want to know + code + +compare_b64_fingerprint = (a, b) -> + 'returns inverse bit error rate in percent' + a = b64decode(a) + b = b64decode(b) + matching_bits = 0 + different_bits = 0 + if b.length != a.length + # trim to shortest + shortest = min(len(a), len(b)) + longest = max(len(a), len(b)) + a = a.slice(0, shortest) + b = b.slice(0, shortest) + # extra bits count as different + different_bits = 8 * (longest - shortest) + i = 0 + while i < a.length + byte_a = ord(a.slice(i, i + 1)) + byte_b = ord(b.slice(i, i + 1)) + x = 0 + while x < 8 + bit_a = byte_a >> x & 1 + bit_b = byte_b >> x & 1 + if bit_a == bit_b + matching_bits += 1 + else + different_bits += 1 + x++ + i++ + total_bits = different_bits + matching_bits + float(matching_bits) / float(total_bits) + +# --- +# generated by js2coffee 2.2.0 \ No newline at end of file