Class: Tonal::Midi::Note

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

Constant Summary collapse

REFERENCE_FREQUENCY =
440.0
A4_MIDI_NUMBER =
69
C4_MIDI_NUMBER =
60

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(number: A4_MIDI_NUMBER, frequency: nil) ⇒ Tonal::Midi::Note

Examples:

Tonal::Midi::Note.new(number:60) => 60.0 MIDI

Parameters:



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/tonal/midi.rb', line 16

def initialize(number: A4_MIDI_NUMBER, frequency: nil)
  if frequency
    raise ArgumentError, "Frequency argument is not Numeric or Tonal::Hertz" unless frequency.kind_of?(Numeric) || frequency.kind_of?(Tonal::Hertz)
    @frequency = Tonal::Hertz.new(frequency)
    @number = (A4_MIDI_NUMBER + 12 * Math.log2(frequency.to_f / REFERENCE_FREQUENCY)).round
  else
    raise ArgumentError, "Number argument is not Integer" unless number.kind_of?(Integer)
    @number = number.kind_of?(self.class) ? number.inspect : number
    @frequency = Tonal::Hertz.new(REFERENCE_FREQUENCY * (2 ** ((number - A4_MIDI_NUMBER) / 12.0)))
  end
end

Instance Attribute Details

#frequencyObject (readonly)

Returns the value of attribute frequency.



9
10
11
# File 'lib/tonal/midi.rb', line 9

def frequency
  @frequency
end

#numberObject (readonly) Also known as: value

Returns the value of attribute number.



9
10
11
# File 'lib/tonal/midi.rb', line 9

def number
  @number
end

Instance Method Details

#<=>(other) ⇒ Object



35
36
37
# File 'lib/tonal/midi.rb', line 35

def <=>(other)
  number <=> (other.kind_of?(self.class) ? other.number : other)
end

#inspectString

Returns representation of self.

Returns:

  • (String)

    representation of self



31
32
33
# File 'lib/tonal/midi.rb', line 31

def inspect
  "#{number} MIDI"
end