Class: Chromaprint::Fingerprint

Inherits:
Object
  • Object
show all
Defined in:
lib/chromaprint/fingerprint.rb

Overview

Contains compressed and raw fingerprints and provides a method to compare them against other fingerprints.

Constant Summary collapse

BITS_PER_RAW_ITEM =

Number of bits in one item of raw fingerprint

32

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(compressed, raw) ⇒ Fingerprint

Returns a new instance of Fingerprint.

Parameters:

  • compressed (String)

    compressed fingerprint

  • raw (Array<Integer>)

    raw fingerprint



17
18
19
20
# File 'lib/chromaprint/fingerprint.rb', line 17

def initialize(compressed, raw)
  @compressed = compressed
  @raw        = raw
end

Instance Attribute Details

#compressedObject (readonly)



9
10
11
# File 'lib/chromaprint/fingerprint.rb', line 9

def compressed
  @compressed
end

#rawObject (readonly)



13
14
15
# File 'lib/chromaprint/fingerprint.rb', line 13

def raw
  @raw
end

Instance Method Details

#compare(fingerprint) ⇒ Float

Compare a fingerprint against another fingerprint.

Parameters:

Returns:

  • (Float)

    float in 0..1 range where 1 is 100% match



27
28
29
30
31
32
33
34
# File 'lib/chromaprint/fingerprint.rb', line 27

def compare(fingerprint)
  max_raw_size = [@raw.size, fingerprint.raw.size].max
  bit_size     = max_raw_size * BITS_PER_RAW_ITEM

  distance     = hamming_distance(@raw, fingerprint.raw)

  1 - distance.to_f / bit_size
end