Class: Tonal::Scale::Step

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Comparable
Defined in:
lib/tonal/step.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(modulo: nil, log: nil, step: nil, ratio: nil) ⇒ Step

Returns a new instance of Step.

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/tonal/step.rb', line 10

def initialize(modulo: nil, log: nil, step: nil, ratio: nil)
  raise ArgumentError, "modulo: required" unless modulo
  raise ArgumentError, "One of log:, step: or ratio: must be provided" unless [log, step, ratio].compact.count == 1
  @modulo = modulo.round

  if ratio
    @ratio, @log = derive_ratio_and_log(ratio: ratio)
  elsif step
    @ratio, @log = derive_ratio_and_log(step: step)
  elsif log
    @ratio, @log = derive_ratio_and_log(log: log)
  end

  @step = (modulo * @log).round
  @tempered = 2**(@step.to_f/@modulo)
end

Instance Attribute Details

#logObject (readonly)

Returns the value of attribute log.



8
9
10
# File 'lib/tonal/step.rb', line 8

def log
  @log
end

#moduloObject (readonly)

Returns the value of attribute modulo.



8
9
10
# File 'lib/tonal/step.rb', line 8

def modulo
  @modulo
end

#ratioObject (readonly)

Returns the value of attribute ratio.



8
9
10
# File 'lib/tonal/step.rb', line 8

def ratio
  @ratio
end

#stepObject (readonly)

Returns the value of attribute step.



8
9
10
# File 'lib/tonal/step.rb', line 8

def step
  @step
end

#temperedObject (readonly)

Returns the value of attribute tempered.



8
9
10
# File 'lib/tonal/step.rb', line 8

def tempered
  @tempered
end

Instance Method Details

#+(rhs) ⇒ Object Also known as: %



107
108
109
# File 'lib/tonal/step.rb', line 107

def +(rhs)
  self.class.new(step: (rhs % modulo), modulo: modulo)
end

#<=>(rhs) ⇒ Object



112
113
114
# File 'lib/tonal/step.rb', line 112

def <=>(rhs)
  rhs.kind_of?(self.class) && modulo <=> rhs.modulo && log <=> rhs.log && step <=> rhs.step
end

#convert(new_modulo) ⇒ Tonal::Scale::Step

Returns new step with the ratio mapped to the new modulo.

Examples:

Tonal::Scale::Step.new(ratio: 3/2r, modulo: 31).convert(12)
=> 7\12

Returns:



37
38
39
# File 'lib/tonal/step.rb', line 37

def convert(new_modulo)
  self.class.new(log: log, modulo: new_modulo)
end

#coprime?Boolean

Returns true if the step is coprime to the modulo.

Examples:

Tonal::Scale::Step.new(step: 5, modulo: 12).coprime? => true
Tonal::Scale::Step.new(step: 6, modulo: 12).coprime? => false

Returns:

  • (Boolean)

    true if the step is coprime to the modulo



103
104
105
# File 'lib/tonal/step.rb', line 103

def coprime?
  step.coprime?(modulo)
end

#efficiencyTonal::Cents Also known as: cents_difference

Returns the difference between the tempered number and the ratio.

Examples:

Tonal::Scale::Step.new(ratio: 3/2r, modulo: 31).efficiency => 5.19 ยข

Returns:

  • (Tonal::Cents)

    the difference between the tempered number and the ratio



92
93
94
95
# File 'lib/tonal/step.rb', line 92

def efficiency
  # We want the efficiency from the step (self). The tempered value is the tempered approximation of the ratio, so we want to know how far off the step is from the ratio. So we take the ratio and subtract the tempered value.
  ratio_to_cents - tempered_to_cents
end

#inspectObject Also known as: to_s



27
28
29
# File 'lib/tonal/step.rb', line 27

def inspect
  "#{step}\\#{modulo}"
end

#ratio_to_centsTonal::Cents

Returns measure of ratio in cents.

Examples:

Tonal::Scale::Step.new(ratio: 3/2r, modulo: 31).ratio_to_cents
=> 701.96

Returns:



84
85
86
# File 'lib/tonal/step.rb', line 84

def ratio_to_cents
  ratio.to_cents
end

#ratio_to_rRational

Returns of the ratio.

Examples:

Tonal::Scale::Step.new(ratio: 3/2r, modulo: 31).ratio_to_r
=> (3/2)

Returns:

  • (Rational)

    of the ratio



66
67
68
# File 'lib/tonal/step.rb', line 66

def ratio_to_r
  ratio.to_r
end

#step_to_rRational

Returns of the step.

Examples:

Tonal::Scale::Step.new(ratio: 3/2r, modulo: 31).step_to_r
=> (18/31)
Tonal::Scale::Step.new(ratio: 3/2r, modulo: 34).step_to_r
=> (10/17)

Returns:

  • (Rational)

    of the step



48
49
50
# File 'lib/tonal/step.rb', line 48

def step_to_r
  Rational(step, modulo)
end

#step_to_ratioTonal::Ratio

Returns of the step/modulo.

Examples:

Tonal::Scale::Step.new(ratio: 3/2r, modulo: 31).step_to_ratio
=> 18/31

Returns:



57
58
59
# File 'lib/tonal/step.rb', line 57

def step_to_ratio
  Tonal::Ratio.new(step, modulo)
end

#tempered_to_centsTonal::Cents

Returns measure of tempered in cents.

Examples:

Tonal::Scale::Step.new(ratio: 3/2r, modulo: 31).tempered_to_cents
=> 696.77

Returns:



75
76
77
# File 'lib/tonal/step.rb', line 75

def tempered_to_cents
  tempered.to_cents
end