Class: AudioFingerprint::Fingerprint
- Inherits:
-
Object
- Object
- AudioFingerprint::Fingerprint
- Defined in:
- lib/audio_fingerprint/fingerprint.rb
Constant Summary collapse
- BITS_PER_RAW_ITEM =
32
- THRESHOLD =
0.85
Instance Attribute Summary collapse
-
#file ⇒ Object
Returns the value of attribute file.
-
#fingerprint ⇒ Object
Returns the value of attribute fingerprint.
Instance Method Summary collapse
- #compare(fp) ⇒ Object
- #create_fingerprint ⇒ Object
-
#initialize(file) ⇒ Fingerprint
constructor
A new instance of Fingerprint.
Constructor Details
#initialize(file) ⇒ Fingerprint
Returns a new instance of Fingerprint.
19 20 21 22 |
# File 'lib/audio_fingerprint/fingerprint.rb', line 19 def initialize(file) @fingerprint = [] @file = file end |
Instance Attribute Details
#file ⇒ Object
Returns the value of attribute file.
17 18 19 |
# File 'lib/audio_fingerprint/fingerprint.rb', line 17 def file @file end |
#fingerprint ⇒ Object
Returns the value of attribute fingerprint.
16 17 18 |
# File 'lib/audio_fingerprint/fingerprint.rb', line 16 def fingerprint @fingerprint end |
Instance Method Details
#compare(fp) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/audio_fingerprint/fingerprint.rb', line 44 def compare(fp) max_raw_size = [@fingerprint.size, fp.size].max bit_size = max_raw_size * BITS_PER_RAW_ITEM distance = hamming_distance(@fingerprint, fp) match = 1 - distance.to_f / bit_size if match >= THRESHOLD match else false end end |
#create_fingerprint ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/audio_fingerprint/fingerprint.rb', line 24 def create_fingerprint w = AudioFingerprint::WaveFile.open(@file) samples = w.sample_data[0, [w.sample_rate * 10, w.sample_data.size].min] duration = samples.size / w.sample_rate na = NArray.float(2, samples.size) @fingerprint ||= [] samples.each_with_index do |v, i| na[0, i - 1] = i.to_f / w.sample_rate.to_f na[1, i - 1] = v end fa = FFTW3.fft(na) fa = fa.real.abs fa.each do |f| @fingerprint << f end end |