Class: Tonal::Step
- Inherits:
-
Object
- Object
- Tonal::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: #%)
- #<=>(rhs) ⇒ Object
-
#convert(new_modulo) ⇒ Tonal::Step
New step with the ratio mapped to the new modulo.
-
#efficiency ⇒ Tonal::Cents
The difference between the step and the ratio.
-
#initialize(modulo: nil, log: nil, step: nil, ratio: nil) ⇒ Step
constructor
A new instance of Step.
- #inspect ⇒ Object (also: #to_s)
-
#ratio_to_cents ⇒ Tonal::Cents
Measure of ratio in cents.
-
#ratio_to_r ⇒ Rational
Of the ratio.
-
#step_to_cents ⇒ Tonal::Cents
(also: #to_cents)
Measure of step in cents.
-
#step_to_r ⇒ Rational
(also: #to_r)
Of the step.
Constructor Details
#initialize(modulo: nil, log: nil, step: nil, ratio: nil) ⇒ Step
Returns a new instance of Step.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/tonal/step.rb', line 9 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.
7 8 9 |
# File 'lib/tonal/step.rb', line 7 def log @log end |
#modulo ⇒ Object (readonly)
Returns the value of attribute modulo.
7 8 9 |
# File 'lib/tonal/step.rb', line 7 def modulo @modulo end |
#ratio ⇒ Object (readonly)
Returns the value of attribute ratio.
7 8 9 |
# File 'lib/tonal/step.rb', line 7 def ratio @ratio end |
#step ⇒ Object (readonly)
Returns the value of attribute step.
7 8 9 |
# File 'lib/tonal/step.rb', line 7 def step @step end |
#tempered ⇒ Object (readonly)
Returns the value of attribute tempered.
7 8 9 |
# File 'lib/tonal/step.rb', line 7 def tempered @tempered end |
Instance Method Details
#+(rhs) ⇒ Object Also known as: %
87 88 89 |
# File 'lib/tonal/step.rb', line 87 def +(rhs) self.class.new(step: (rhs % modulo), modulo: modulo) end |
#<=>(rhs) ⇒ Object
92 93 94 |
# File 'lib/tonal/step.rb', line 92 def <=>(rhs) rhs.kind_of?(self.class) && modulo <=> rhs.modulo && log <=> rhs.log && step <=> rhs.step end |
#convert(new_modulo) ⇒ Tonal::Step
Returns new step with the ratio mapped to the new modulo.
36 37 38 |
# File 'lib/tonal/step.rb', line 36 def convert(new_modulo) self.class.new(log: log, modulo: new_modulo) end |
#efficiency ⇒ Tonal::Cents
Returns the difference between the step and the ratio.
83 84 85 |
# File 'lib/tonal/step.rb', line 83 def efficiency ratio_to_cents - step_to_cents end |
#inspect ⇒ Object Also known as: to_s
26 27 28 |
# File 'lib/tonal/step.rb', line 26 def inspect "#{step}\\#{modulo}" end |
#ratio_to_cents ⇒ Tonal::Cents
Returns measure of ratio in cents.
74 75 76 |
# File 'lib/tonal/step.rb', line 74 def ratio_to_cents ratio.to_cents end |
#ratio_to_r ⇒ Rational
Returns of the ratio.
55 56 57 |
# File 'lib/tonal/step.rb', line 55 def ratio_to_r ratio.to_r end |
#step_to_cents ⇒ Tonal::Cents Also known as: to_cents
Returns measure of step in cents.
64 65 66 |
# File 'lib/tonal/step.rb', line 64 def step_to_cents tempered.to_cents end |
#step_to_r ⇒ Rational Also known as: to_r
Returns of the step.
45 46 47 |
# File 'lib/tonal/step.rb', line 45 def step_to_r tempered.to_r end |