Class: Synthesizer::Note

Inherits:
Object
  • Object
show all
Defined in:
lib/synthesizer/note.rb

Constant Summary collapse

NOTE_TABLE =
[:"C", :"C#/Db", :"D", :"D#/Eb", :"E", :"F", :"F#/Gb", :"G", :"G#/Ab", :"A", :"A#/Bb", :"B"].freeze
NOTE_NAME_TABLE =
{
  :"C" => 0,
  :"C#/Db" => 1,
  :"C#" => 1,
  :"Db" => 1,
  :"D" =>  2,
  :"D#/Eb" => 3,
  :"D#" => 3,
  :"Eb" => 3,
  :"E" => 4,
  :"F" => 5,
  :"F#/Gb" => 6,
  :"F#" => 6,
  :"Gb" => 6,
  :"G" => 7,
  :"G#/Ab" => 8,
  :"G#" => 8,
  :"Ab" => 8,
  :"A" => 9,
  :"A#/Bb" => 10,
  :"A#" => 10,
  :"Bb" => 10,
  :"B" => 11
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(num) ⇒ Note

Returns a new instance of Note.



31
32
33
# File 'lib/synthesizer/note.rb', line 31

def initialize(num)
  @num = num.to_i
end

Instance Attribute Details

#numObject (readonly)

Returns the value of attribute num.



29
30
31
# File 'lib/synthesizer/note.rb', line 29

def num
  @num
end

Class Method Details

.create(name, octave) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/synthesizer/note.rb', line 47

def self.create(name, octave)
  name = name.to_sym
  octave = octave.to_i

  note_index = NOTE_NAME_TABLE[name]
  if !note_index
    raise Error, "not found note name: #{name}"
  end

  num = (octave + 1) * 12 + note_index
  if num<0
    raise Error, "octave #{octave} outside of note"
  end

  new(num)
end

Instance Method Details

#hz(semis: 0, cents: 0) ⇒ Object



35
36
37
# File 'lib/synthesizer/note.rb', line 35

def hz(semis: 0, cents: 0)
  6.875 * (2 ** ((@num + semis + (cents / 100.0) + 3) / 12.0))
end

#note_nameObject



39
40
41
# File 'lib/synthesizer/note.rb', line 39

def note_name
  NOTE_TABLE[@num % 12]
end

#octave_numObject



43
44
45
# File 'lib/synthesizer/note.rb', line 43

def octave_num
  (@num / 12) - 1
end