Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions lib/compare.coffee
Original file line number Diff line number Diff line change
@@ -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