Class: Chromaprint::Context

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

Overview

Calculates fingerprints of audio data.

Examples:

chromaprint = Chromaprint::Context.new(44100, 1)
data        = File.binread('song.wav')
fingerprint = chromaprint.get_fingerprint(data)

Instance Method Summary collapse

Constructor Details

#initialize(rate, num_channels, algorithm = ALGORITHM_DEFAULT) ⇒ Context

Returns a new instance of Context.

Parameters:

  • rate (Integer)

    sample rate of audio

  • num_channels (Integer)

    number of channels (1 or 2)

  • algorithm (Integer) (defaults to: ALGORITHM_DEFAULT)

    specify algorithm to be used to Chromaprint library. Must be ALGORITHM_TEST1, ALGORITHM_TEST2 or ALGORITHM_TEST3.



14
15
16
17
18
# File 'lib/chromaprint/context.rb', line 14

def initialize(rate, num_channels, algorithm = ALGORITHM_DEFAULT)
  @rate         = rate
  @num_channels = num_channels
  @algorithm    = ALGORITHM_DEFAULT
end

Instance Method Details

#get_fingerprint(data) ⇒ Chromaprint::Fingerprint

Calculate raw and compressed fingerprints of the audio data.

Parameters:

  • data (String)

    raw audio data preseted by 16-bit signed integers

Returns:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/chromaprint/context.rb', line 27

def get_fingerprint(data)
  # Allocate memory for context and initialize
  p_context = Lib.chromaprint_new(@algorithm)
  Lib.chromaprint_start(p_context, @rate, @num_channels)

  # Create a pointer to the data
  p_data = FFI::MemoryPointer.from_string(data)

  # Calculate number of samples and feed data.
  data_size = p_data.size / BYTES_PER_SAMPLE
  Lib.chromaprint_feed(p_context, p_data, data_size)

  # Calculate the fingerprint
  Lib.chromaprint_finish(p_context)

  # Get compressed fingerprint
  p_p_fingerprint = FFI::MemoryPointer.new(:pointer)
  Lib.chromaprint_get_fingerprint(p_context, p_p_fingerprint)
  p_fingerprint = p_p_fingerprint.get_pointer(0)
  fingerprint   = p_fingerprint.get_string(0)

  # Get raw fingerprint
  p_p_raw_fingerprint = FFI::MemoryPointer.new(:pointer)
  p_size              = FFI::MemoryPointer.new(:pointer)
  Lib.chromaprint_get_raw_fingerprint(p_context, p_p_raw_fingerprint, p_size)
  p_raw_fingerprint = p_p_raw_fingerprint.get_pointer(0)
  raw_fingerprint   = p_raw_fingerprint.get_array_of_uint(0, p_size.get_int32(0))

  Fingerprint.new(fingerprint, raw_fingerprint)
ensure
  # Free memory
  Lib.chromaprint_free(p_context)            if p_context
  Lib.chromaprint_dealloc(p_fingerprint)     if p_fingerprint
  Lib.chromaprint_dealloc(p_raw_fingerprint) if p_raw_fingerprint
end