Class: Echonest::Song

Inherits:
Base
  • Object
show all
Defined in:
lib/echonest-ruby-api/song.rb

Instance Method Summary collapse

Methods inherited from Base

base_uri, #endpoint, #entity_name, #get, get_api_endpoint, #get_response, version, #which

Constructor Details

#initialize(api_key) ⇒ Song

Returns a new instance of Song.



10
11
12
# File 'lib/echonest-ruby-api/song.rb', line 10

def initialize(api_key)
  @api_key = api_key
end

Instance Method Details

#echoprint_code(filepath) ⇒ Object

Generates an acoustic fingerprint using the echoprint-codegen binary.

Examples:

echoprint_code('path/to/song.mp3')
#=> Echoprint code as String

Raises an Echoprint::Error if the echoprint-codegen binary is not accessible to Ruby on $PATH

  • filepath - Path (absolute or relative) to an audio file atleast 21 seconds in length

Returns a String



56
57
58
59
60
61
62
63
64
# File 'lib/echonest-ruby-api/song.rb', line 56

def echoprint_code(filepath)
  if which('echoprint-codegen').nil?
    error = Error.new(6)
    raise Error.new(6), error.description
  else
    response = `echoprint-codegen #{ filepath } 1 20`
    JSON.parse(response)[0]['code']
  end
end

#identify(code) ⇒ Object

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
41
# File 'lib/echonest-ruby-api/song.rb', line 33

def identify(code)
  raise ArgumentError, 'Not a valid Echoprint or ENFMP fingerprint' if code.empty?
  response = get_response(code: code)
  results = []
  response[:songs].each do |song|
    results << { score: song[:score], title: song[:title], artist_name: song[:artist_name] }
  end
  results
end

#profile(options = {}) ⇒ Object

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
30
31
# File 'lib/echonest-ruby-api/song.rb', line 23

def profile(options = {})
  raise ArgumentError, 'You must include a song id' if options[:id].nil?
  defaults = { api_key: @api_key }
  response = get_response(options)
  songs = []
  response[:songs].each do |song|
    songs << song
  end
end

#search(options = {}) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/echonest-ruby-api/song.rb', line 14

def search(options = {})
  defaults = { api_key: @api_key }
  response = get_response(options)
  songs = []
  response[:songs].each do |song|
    songs << song
  end
end