Class: Echonest::Analysis

Inherits:
Object
  • Object
show all
Defined in:
lib/echonest/analysis.rb

Constant Summary collapse

CHROMATIC =
%w(C C# D D# E F F# G G# A A# B).freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json) ⇒ Analysis

Returns a new instance of Analysis.



8
9
10
# File 'lib/echonest/analysis.rb', line 8

def initialize(json)
  @body = JSON.parse(json)
end

Class Method Details

.new_from_url(url) ⇒ Object



12
13
14
# File 'lib/echonest/analysis.rb', line 12

def self.new_from_url(url)
  new(open(url).read)
end

Instance Method Details

#barsObject



61
62
63
64
65
# File 'lib/echonest/analysis.rb', line 61

def bars
  @body['bars'].map do |bar|
    Bar.new(bar['start'], bar['duration'], bar['confidence'])
  end
end

#beatsObject



67
68
69
70
71
# File 'lib/echonest/analysis.rb', line 67

def beats
  @body['beats'].map do |beat|
    Beat.new(beat['start'], beat['duration'], beat['confidence'])
  end
end

#durationObject



20
21
22
# File 'lib/echonest/analysis.rb', line 20

def duration
  track_info['duration']
end

#end_of_fade_inObject



24
25
26
# File 'lib/echonest/analysis.rb', line 24

def end_of_fade_in
  track_info['end_of_fade_in']
end

#keyObject



28
29
30
# File 'lib/echonest/analysis.rb', line 28

def key
  track_info['key']
end

#key_letterObject

Returns the corresponding letter for the key number value.



33
34
35
# File 'lib/echonest/analysis.rb', line 33

def key_letter
  CHROMATIC[key]
end

#loudnessObject



37
38
39
# File 'lib/echonest/analysis.rb', line 37

def loudness
  track_info['loudness']
end

#major?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/echonest/analysis.rb', line 49

def major?
  !minor?
end

#minor?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/echonest/analysis.rb', line 45

def minor?
  mode == 0
end

#modeObject



41
42
43
# File 'lib/echonest/analysis.rb', line 41

def mode
  track_info['mode']
end

#sectionsObject



73
74
75
76
77
# File 'lib/echonest/analysis.rb', line 73

def sections
  @body['sections'].map do |section|
    Section.new(section['start'], section['duration'], section['confidence'])
  end
end

#segmentsObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/echonest/analysis.rb', line 85

def segments
  @body['segments'].map do |segment|
    loudness = Loudness.new(0.0, segment['loudness_start'])
    max_loudness = Loudness.new(segment['loudness_max_time'], segment['loudness_max'])

    Segment.new(
      segment['start'],
      segment['duration'],
      segment['confidence'],
      loudness,
      max_loudness,
      segment['pitches'],
      segment['timbre']
      )
  end
end

#start_of_fade_outObject



53
54
55
# File 'lib/echonest/analysis.rb', line 53

def start_of_fade_out
  track_info['start_of_fade_out']
end

#tatumsObject



79
80
81
82
83
# File 'lib/echonest/analysis.rb', line 79

def tatums
  @body['tatums'].map do |tatum|
    Tatum.new(tatum['start'], tatum['duration'], tatum['confidence'])
  end
end

#tempoObject



16
17
18
# File 'lib/echonest/analysis.rb', line 16

def tempo
  track_info['tempo']
end

#time_signatureObject



57
58
59
# File 'lib/echonest/analysis.rb', line 57

def time_signature
  track_info['time_signature']
end

#track_infoObject



102
103
104
# File 'lib/echonest/analysis.rb', line 102

def track_info
  @body['track']
end