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

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:



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

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.



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

def frequency
  @frequency
end

#numberObject (readonly) Also known as: value

Returns the value of attribute number.



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

def number
  @number
end

Instance Method Details

#<=>(other) ⇒ Object



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

def <=>(other)
  number <=> other.number
end

#inspectString

Returns representation of self.

Returns:

  • (String)

    representation of self



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

def inspect
  "#{number} MIDI"
end