Class: Coltrane::Interval

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(cents) ⇒ Interval

Returns a new instance of Interval.



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

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

Instance Attribute Details

#centsObject (readonly) Also known as: hash

Returns the value of attribute cents.



7
8
9
# File 'lib/coltrane/interval.rb', line 7

def cents
  @cents
end

Instance Method Details

#+(other) ⇒ Object



36
37
38
39
40
41
# File 'lib/coltrane/interval.rb', line 36

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

#-(other) ⇒ Object



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

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

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



29
30
31
# File 'lib/coltrane/interval.rb', line 29

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

#ascending?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/coltrane/interval.rb', line 21

def ascending?
  cents < 0
end

#descending?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/coltrane/interval.rb', line 25

def descending?
  cents > 0
end

#semitonesObject



17
18
19
# File 'lib/coltrane/interval.rb', line 17

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