Class: Tonal::Hertz

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/tonal/hertz.rb

Constant Summary collapse

REFERENCE_FREQUENCY =
440.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arg) ⇒ Tonal::Hertz

Examples:

Tonal::Hertz.new(1000.0) => 1000.0 Hz

Parameters:

Raises:

  • (ArgumentError)


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

#valueObject (readonly)

Returns the value of attribute value.



6
7
8
# File 'lib/tonal/hertz.rb', line 6

def value
  @value
end

Class Method Details

.referenceTonal::Hertz

Returns 440 Hz.

Examples:

Tonal::Hertz.reference => 440.0 Hz

Returns:



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

#inspectString

Returns the string representation of Tonal::Hertz.

Examples:

Tonal::Hertz(1000.0).inspect => "1000.0 Hz"

Returns:

  • (String)

    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.

Examples:

Tonal::Hertz.new(880).to_cents => 1200.0

Parameters:

  • reference (Tonal::Hertz, Numeric) (defaults to: self.class.reference)

    the reference frequency to compare to

Returns:

  • (Tonal::Cents)

    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_fRational

Returns self as a float.

Examples:

Tonal::Hertz.new(440).to_f => 440.0

Returns:

  • (Rational)

    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.

Examples:

Tonal::Hertz.new(440).to_midi => 69.0 MIDI

Parameters:

  • modulo (Integer) (defaults to: Tonal::Midi::Note::DEFAULT_MODULO)

    the modulo to use for the MIDI note calculation

  • reference_number (Integer) (defaults to: Tonal::Midi::Note::A4_MIDI_NUMBER)

    the MIDI note number that anchors the conversion

  • reference_frequency (Numeric) (defaults to: Tonal::Hertz::REFERENCE_FREQUENCY)

    the frequency (Hz) that anchors the conversion

Returns:



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_rRational

Returns self as a rational.

Examples:

Tonal::Hertz.new(440).to_r => (440/1)

Returns:

  • (Rational)

    self as a rational



30
31
32
# File 'lib/tonal/hertz.rb', line 30

def to_r
  Rational(value)
end