Module: Lydown::Rendering::Octaves

Defined in:
lib/lydown/rendering/notes.rb

Constant Summary collapse

DIATONICS =
%w{a b c d e f g}

Class Method Summary collapse

Class Method Details

.absolute_octave(note, ref_note = 'c') ⇒ Object

Raises:



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/lydown/rendering/notes.rb', line 120

def self.absolute_octave(note, ref_note = 'c')
  note_diatonic, ref_diatonic = note[0], ref_note[0]
  raise LydownError, "Invalid note #{note}" unless DIATONICS.index(note_diatonic)
  raise LydownError, "Invalid reference note #{ref_note}" unless DIATONICS.index(ref_diatonic)

  # calculate diatonic interval
  note_array = DIATONICS.rotate(DIATONICS.index(ref_diatonic))
  interval = note_array.index(note_diatonic)

  # calculate octave interval and
  note_value = note.count("'") - note.count(',')
  ref_value = ref_note.count("'") - ref_note.count(',')
  octave_interval = ref_value + note_value
  octave_interval -= 1 if interval >= 4

  # generate octave markers
  octave_interval >= 0 ? "'" * octave_interval : "," * -octave_interval
end

.relative_octave(note, ref_note = 'c') ⇒ Object

calculates the octave markers needed to put a first note in the right octave. In lydown, octaves are relative (i.e. lilypond’s relative mode). But the first note gives the octave to start on, rather than a relative note to c (or any other reference note).

In that manner, d’ is d above middle c, g” is g an octave and fifth above middle c, a is a a below middle c, and eß, is great e flat.

The return value is a string with octave markers for relative mode, based on the refence note

Raises:



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/lydown/rendering/notes.rb', line 101

def self.relative_octave(note, ref_note = 'c')
  note_diatonic, ref_diatonic = note[0], ref_note[0]
  raise LydownError, "Invalid note #{note}" unless DIATONICS.index(note_diatonic)
  raise LydownError, "Invalid reference note #{ref_note}" unless DIATONICS.index(ref_diatonic)

  # calculate diatonic interval
  note_array = DIATONICS.rotate(DIATONICS.index(ref_diatonic))
  interval = note_array.index(note_diatonic)

  # calculate octave interval and
  octave_value = note.count("'") - note.count(',')
  ref_value = ref_note.count("'") - ref_note.count(',')
  octave_interval = octave_value - ref_value
  octave_interval += 1 if interval >= 4

  # generate octave markers
  octave_interval >= 0 ? "'" * octave_interval : "," * -octave_interval
end