Class: Tonal::Scale::Step
- Inherits:
-
Object
- Object
- Tonal::Scale::Step
- Extended by:
- Forwardable
- Includes:
- Comparable
- Defined in:
- lib/tonal/step.rb
Instance Attribute Summary collapse
-
#log ⇒ Object
readonly
Returns the value of attribute log.
-
#modulo ⇒ Object
readonly
Returns the value of attribute modulo.
-
#ratio ⇒ Object
readonly
Returns the value of attribute ratio.
-
#step ⇒ Object
readonly
Returns the value of attribute step.
-
#tempered ⇒ Object
readonly
Returns the value of attribute tempered.
Instance Method Summary collapse
-
#+(rhs) ⇒ Object
(also: #%)
A new [Tonal::Scale::Step] with the step added to the current step.
- #<=>(rhs) ⇒ Object
-
#convert(new_modulo) ⇒ Object
A new [Tonal::Scale::Step] with the ratio mapped to the new modulo.
-
#coprime? ⇒ Boolean
True if the step is coprime to the modulo.
- #efficiency ⇒ Object (also: #cents_difference)
-
#initialize(modulo: nil, log: nil, step: nil, ratio: nil) ⇒ Step
constructor
A new instance of Step.
- #inspect ⇒ Object (also: #to_s)
- #ratio_to_cents ⇒ Object
- #ratio_to_r ⇒ Object
- #step_to_midi(midi_root: Tonal::Midi::Note::C4_MIDI_NUMBER) ⇒ Object
- #step_to_r ⇒ Object
- #step_to_ratio ⇒ Object
- #tempered_to_cents ⇒ Object
Constructor Details
#initialize(modulo: nil, log: nil, step: nil, ratio: nil) ⇒ Step
Returns a new instance of Step.
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
#log ⇒ Object (readonly)
Returns the value of attribute log.
8 9 10 |
# File 'lib/tonal/step.rb', line 8 def log @log end |
#modulo ⇒ Object (readonly)
Returns the value of attribute modulo.
8 9 10 |
# File 'lib/tonal/step.rb', line 8 def modulo @modulo end |
#ratio ⇒ Object (readonly)
Returns the value of attribute ratio.
8 9 10 |
# File 'lib/tonal/step.rb', line 8 def ratio @ratio end |
#step ⇒ Object (readonly)
Returns the value of attribute step.
8 9 10 |
# File 'lib/tonal/step.rb', line 8 def step @step end |
#tempered ⇒ Object (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: %
Returns a new [Tonal::Scale::Step] with the step added to the current step.
127 128 129 |
# File 'lib/tonal/step.rb', line 127 def +(rhs) self.class.new(step: (rhs % modulo), modulo: modulo) end |
#<=>(rhs) ⇒ Object
132 133 134 |
# File 'lib/tonal/step.rb', line 132 def <=>(rhs) rhs.kind_of?(self.class) && modulo <=> rhs.modulo && log <=> rhs.log && step <=> rhs.step end |
#convert(new_modulo) ⇒ Object
Returns a new [Tonal::Scale::Step] with the ratio mapped to the new modulo.
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.
119 120 121 |
# File 'lib/tonal/step.rb', line 119 def coprime? step.coprime?(modulo) end |
#efficiency ⇒ Object Also known as: cents_difference
108 109 110 111 |
# File 'lib/tonal/step.rb', line 108 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 |
#inspect ⇒ Object Also known as: to_s
27 28 29 |
# File 'lib/tonal/step.rb', line 27 def inspect "#{step}\\#{modulo}" end |
#ratio_to_cents ⇒ Object
100 101 102 |
# File 'lib/tonal/step.rb', line 100 def ratio_to_cents ratio.to_cents end |
#ratio_to_r ⇒ Object
82 83 84 |
# File 'lib/tonal/step.rb', line 82 def ratio_to_r ratio.to_r end |
#step_to_midi(midi_root: Tonal::Midi::Note::C4_MIDI_NUMBER) ⇒ Object
73 74 75 |
# File 'lib/tonal/step.rb', line 73 def step_to_midi(midi_root: Tonal::Midi::Note::C4_MIDI_NUMBER) Tonal::Midi::Note.new(number: step + midi_root) end |
#step_to_r ⇒ Object
48 49 50 |
# File 'lib/tonal/step.rb', line 48 def step_to_r Rational(step, modulo) end |
#step_to_ratio ⇒ Object
57 58 59 |
# File 'lib/tonal/step.rb', line 57 def step_to_ratio Tonal::Ratio.new(step, modulo) end |
#tempered_to_cents ⇒ Object
91 92 93 |
# File 'lib/tonal/step.rb', line 91 def tempered_to_cents tempered.to_cents end |