Class: Coltrane::Interval

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/coltrane/interval.rb

Overview

Interval describe the logarithmic distance between 2 frequencies. It’s measured in cents.

Direct Known Subclasses

IntervalClass

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cents) ⇒ Interval

Returns a new instance of Interval.



19
20
21
# File 'lib/coltrane/interval.rb', line 19

def initialize(cents)
  @cents = cents.round
end

Instance Attribute Details

#centsObject (readonly) Also known as: hash

Returns the value of attribute cents.



9
10
11
# File 'lib/coltrane/interval.rb', line 9

def cents
  @cents
end

Class Method Details

.method_missing(method, *args) ⇒ Object



14
15
16
# File 'lib/coltrane/interval.rb', line 14

def method_missing(method, *args)
  IntervalClass.send(method, *args)
end

Instance Method Details

#+(other) ⇒ Object



42
43
44
45
46
47
# File 'lib/coltrane/interval.rb', line 42

def +(other)
  case other
  when Numeric then Interval[cents + other]
  when Interval then Interval[cents + other.cents]
  end
end

#-(other) ⇒ Object



49
50
51
52
53
54
# File 'lib/coltrane/interval.rb', line 49

def -(other)
  case other
  when Numeric then Interval[cents - other]
  when Interval then Interval[cents - other.cents]
  end
end

#-@Object



56
57
58
# File 'lib/coltrane/interval.rb', line 56

def -@
  Interval[-cents]
end

#<=>(other) ⇒ Object



60
61
62
# File 'lib/coltrane/interval.rb', line 60

def <=>(other)
  cents <=> other.cents
end

#==(other) ⇒ Object Also known as: eql?



35
36
37
# File 'lib/coltrane/interval.rb', line 35

def ==(other)
  cents == other.cents
end

#ascending?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/coltrane/interval.rb', line 27

def ascending?
  cents < 0
end

#descending?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/coltrane/interval.rb', line 31

def descending?
  cents > 0
end

#semitonesObject



23
24
25
# File 'lib/coltrane/interval.rb', line 23

def semitones
  (cents.to_f / 100).round
end