Class: Tonal::Hertz
Constant Summary collapse
- REFERENCE_FREQUENCY =
440.0
Instance Attribute Summary collapse
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
-
.reference ⇒ Tonal::Hertz
440 Hz.
Instance Method Summary collapse
- #<=>(rhs) ⇒ Object
- #initialize(arg) ⇒ Tonal::Hertz constructor
-
#inspect ⇒ String
The string representation of Tonal::Hertz.
- #method_missing(op, *args, &blk) ⇒ Object
-
#to_cents(reference: self.class.reference) ⇒ Tonal::Cents
The cents difference between self and a reference frequency.
-
#to_f ⇒ Rational
Self as a float.
-
#to_midi_note(modulo: Tonal::Midi::Note::DEFAULT_MODULO, reference_number: Tonal::Midi::Note::A4_MIDI_NUMBER, reference_frequency: Tonal::Hertz::REFERENCE_FREQUENCY) ⇒ Tonal::Midi::Note
(also: #to_midi)
The midi representation of self.
-
#to_r ⇒ Rational
Self as a rational.
Constructor Details
#initialize(arg) ⇒ Tonal::Hertz
13 14 15 16 |
# File 'lib/tonal/hertz.rb', line 13 def initialize(arg) raise ArgumentError, "Argument is not Numeric or Tonal::Hertz" unless arg.kind_of?(Numeric) || arg.kind_of?(self.class) @value = arg.kind_of?(self.class) ? arg.value : arg end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(op, *args, &blk) ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'lib/tonal/hertz.rb', line 75 def method_missing(op, *args, &blk) rhs = args.collect do |arg| arg.kind_of?(self.class) ? arg.value : arg end result = value.send(op, *rhs) return result if op == :coerce result.kind_of?(Numeric) ? self.class.new(result) : result end |
Instance Attribute Details
#value ⇒ Object (readonly)
Returns the value of attribute value.
6 7 8 |
# File 'lib/tonal/hertz.rb', line 6 def value @value end |
Class Method Details
.reference ⇒ Tonal::Hertz
Returns 440 Hz.
22 23 24 |
# File 'lib/tonal/hertz.rb', line 22 def self.reference self.new(REFERENCE_FREQUENCY) end |
Instance Method Details
#<=>(rhs) ⇒ Object
71 72 73 |
# File 'lib/tonal/hertz.rb', line 71 def <=>(rhs) rhs.kind_of?(self.class) ? value <=> rhs.value : value <=> rhs end |
#inspect ⇒ String
Returns the string representation of Tonal::Hertz.
67 68 69 |
# File 'lib/tonal/hertz.rb', line 67 def inspect "#{value} Hz" end |
#to_cents(reference: self.class.reference) ⇒ Tonal::Cents
Returns the cents difference between self and a reference frequency.
47 48 49 |
# File 'lib/tonal/hertz.rb', line 47 def to_cents(reference: self.class.reference) Tonal::Cents.new(ratio: to_r / reference.to_r) end |
#to_f ⇒ Rational
Returns self as a float.
38 39 40 |
# File 'lib/tonal/hertz.rb', line 38 def to_f value.to_f end |
#to_midi_note(modulo: Tonal::Midi::Note::DEFAULT_MODULO, reference_number: Tonal::Midi::Note::A4_MIDI_NUMBER, reference_frequency: Tonal::Hertz::REFERENCE_FREQUENCY) ⇒ Tonal::Midi::Note Also known as: to_midi
Returns the midi representation of self.
58 59 60 |
# File 'lib/tonal/hertz.rb', line 58 def to_midi_note(modulo: Tonal::Midi::Note::DEFAULT_MODULO, reference_number: Tonal::Midi::Note::A4_MIDI_NUMBER, reference_frequency: Tonal::Hertz::REFERENCE_FREQUENCY) Tonal::Midi::Note.new(frequency: to_f, modulo: modulo, reference_number: reference_number, reference_frequency: reference_frequency) end |
#to_r ⇒ Rational
Returns self as a rational.
30 31 32 |
# File 'lib/tonal/hertz.rb', line 30 def to_r Rational(value) end |