Class: NoteFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/lygre/gabcpitchreader.rb

Overview

an interface to create RBMusicTheory::Notes using lilypond notation

Class Method Summary collapse

Class Method Details

.create(notesym) ⇒ Object Also known as: create_note, []

notesym is a String - absolute pitch in the lilypond format. (currently alterations are not supported, as they are not necessary for our dealing with Gregorian chant.) returns a coresponding RBMusicTheory::Note



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/lygre/gabcpitchreader.rb', line 50

def create(notesym)
  unless notesym =~ /^[a-g]('+|,+)?$/i
    raise ArgumentError.new('#{notesym} is not a valid lilypond absolute pitch')
  end

  note = notesym[0]
  octaves = notesym[1..-1]

  n = RBMusicTheory::Note.new note.upcase
  sign = 0
  base_octave_shift = -1 # Note.new('C') returns c'=60, not c=48
  if octaves then
    sign = (octaves[0] == ',' ? -1 : 1)
    octave_shift = (octaves.size * sign) + base_octave_shift
  else
    octave_shift = base_octave_shift
  end
  n += octave_shift * RBMusicTheory::NoteInterval.octave.value # strangely, NoteInterval cannot be multiplied

  return n
end

.lily_abs_pitch(note) ⇒ Object

returns a lilypond absolute pitch for the given RbMusicTheory::Note

this method doesn’t fit well in a factory, but #create translates lilypond pitch to Note and #lily_abs_pitch does the reverse translation, so maybe just the class should be renamed



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/lygre/gabcpitchreader.rb', line 80

def lily_abs_pitch(note)
  octave_shifts = ''
  octave_diff = note.value - create('c').value

  octave_value = RBMusicTheory::NoteInterval.octave.value
  octave_shift = octave_diff.abs / octave_value
  if octave_diff < 0 and (octave_diff.abs % octave_value) > 0 then
    octave_shift += 1
  end

  octave_signs = (octave_diff >= 0 ? "'" : ",") * octave_shift
  note.name.downcase + octave_signs
end