Class: Gly::GabcConvertor

Inherits:
Object
  • Object
show all
Defined in:
lib/gly/gabc_convertor.rb

Overview

takes Score, translates it to gabc

Instance Method Summary collapse

Instance Method Details

#clef?(chunk) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/gly/gabc_convertor.rb', line 54

def clef?(chunk)
  chunk =~ /\A[cf][1-4]\Z/
end

#convert(score, out = StringIO.new) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gly/gabc_convertor.rb', line 6

def convert(score, out=StringIO.new)
  score.headers.each_pair do |key,value|
    out.print '% ' unless Headers.gregorio_supported?(key)
    out.puts "#{key}: #{value};"
  end

  out.puts '%%'

  lyric_enum = score.lyrics.each_syllable.to_enum

  score.music.each_with_index do |mus_chunk,i|
    begin
      next_syl = lyric_enum.peek
      out.print lyric_enum.next if next_syl == ' '
    rescue StopIteration
      next_syl = ''
    end until next_syl != ' '

    if no_lyrics? mus_chunk, next_syl
      if i == score.music.size - 1
        out.print ' '
      end
    else
      # regular music chunk
      begin
        out.print strip_directives lyric_enum.next
      rescue StopIteration
        out.print ' ' if i > 0 # don't add space at the very beginning
      end
    end

    out.print "(#{mus_chunk})"
    if no_lyrics?(mus_chunk, next_syl) &&
       i != (score.music.size - 1) &&
       ! score.lyrics.empty?
      out.print ' '
    end
  end

  return out
end

#no_lyrics?(music_chunk, syllable) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
# File 'lib/gly/gabc_convertor.rb', line 48

def no_lyrics?(music_chunk, syllable)
  clef?(music_chunk) ||
    (nonlyrical_chunk?(music_chunk) &&
     ! nonlyrical_lyrics?(syllable))
end

#nonlyrical_chunk?(chunk) ⇒ Boolean

is the given music chunk capable of bearing lyrics?

Returns:

  • (Boolean)


67
68
69
70
# File 'lib/gly/gabc_convertor.rb', line 67

def nonlyrical_chunk?(chunk)
  chunk.size > 0 &&
    without_breaks(without_differentiae(chunk)).empty?
end

#nonlyrical_lyrics?(syl) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/gly/gabc_convertor.rb', line 72

def nonlyrical_lyrics?(syl)
  syl =~ /\A\s*!/ || syl =~ /\A\s*\*\Z/
end

#strip_directives(syl) ⇒ Object



76
77
78
# File 'lib/gly/gabc_convertor.rb', line 76

def strip_directives(syl)
  syl.sub(/(\s*)!/, '\1') # exclamation mark at the beginning - place even under nonlyrical music chunk
end

#without_breaks(chunk) ⇒ Object



62
63
64
# File 'lib/gly/gabc_convertor.rb', line 62

def without_breaks(chunk)
  chunk.gsub /[zZ]/, ''
end

#without_differentiae(chunk) ⇒ Object



58
59
60
# File 'lib/gly/gabc_convertor.rb', line 58

def without_differentiae(chunk)
  chunk.gsub /(([,`])|(:[:']?)|(;[1-6]?))/, ''
end