Class: LilyPond::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/lilypond/builder.rb

Overview

The Builder class is responsible for building LilyPond notation.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBuilder

Creates a new LilyPond Builder object.



8
9
10
11
# File 'lib/lilypond/builder.rb', line 8

def initialize
  @buffer = "\\version \"#{LilyPond.version_number}\"\n"
  @indent_level = 0
end

Class Method Details

.sampleLilyPond::Builder

Generates a sample LilyPond notation.

Returns:



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/lilypond/builder.rb', line 93

def sample
  parallel = true
  options = {
    :midi_maximum_volume => 0.4,
    :instrument_name     => "Piano"
  }
  ly = LilyPond::Builder.new

  ly.append "%% MUSIC %%"
  ly.define_content("global") do |global|
    ly.append "\\numericTimeSignature"
    ly.append "\\time 4/4"
    ly.append "s1 * 4 |"
    ly.append "\\bar \"|.\""
  end #global
  ly.define_content("sopranoMusic") do |sopranoMusic|
    ly.append "c'4 d'4 e'4 f'4 |"
    ly.append "c'4 d'4 e'4 f'4 |"
    ly.append "c'4 d'4 e'4 f'4 |"
    ly.append "c'4 d'4 e'4 f'4 |"
  end #sopranoMusic
  ly.append "%% LYRICS %%"
  ly.define_content("sopranoLyrics", true) do |sopranoLyrics|
    ly.append "do re me fa"
    ly.append "do re me fa"
    ly.append "do re me fa"
    ly.append "do re me fa"
  end #sopranoLyrics
  ly.append "%% BOOKS %%"
  ly.block("book") do |book|
    ly.block("score") do |score|
      ly.block(nil, parallel) do |score_content|
        ly.block("new Staff", parallel, options) do |staff|
          ly.block("new Voice", parallel) do |voice|
            ly.use_content("global")
            ly.use_content("sopranoMusic")
          end #voice
          ly.block("new Lyrics") do |lyrics|
            ly.use_content("sopranoLyrics")
          end #lyrics
        end #staff
      end #score_content
      ly.block("midi")
      ly.block("layout")
    end #score
  end #book

  return ly
end

Instance Method Details

#append(string) ⇒ Object

Appends a string of LilyPond notation to the buffer.

Parameters:

  • string (String)

    the LilyPond notation to append



78
79
80
# File 'lib/lilypond/builder.rb', line 78

def append(string)
  @buffer << "#{indent}#{string}\n"
end

#block(name = nil, parallel = false, options = {}) { ... } ⇒ Object

Generates a block of LilyPond notation.

Parameters:

  • name (String) (defaults to: nil)

    the name of the block

  • parallel (Boolean) (defaults to: false)

    whether the block should be parallel

  • options (Hash) (defaults to: {})

    the options for the block

Yields:

  • the block content



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/lilypond/builder.rb', line 26

def block(name = nil, parallel = false, options = {})
  @buffer << "#{indent}"
  @buffer << "\\#{name} " unless name == nil
  if options.any?
    @buffer << "\\with { \n"
    @indent_level +=1
    options.each do |key, value|
      @buffer << "#{indent}#{key.to_s.camelize_lower} = #"
      @buffer << (value.class.name == "String" ? "\"#{value}\"" : "#{value}")
      @buffer << "\n"
    end
    @indent_level -=1
    @buffer << "#{indent}} "
  end
  @buffer << "#{parallel ? "<<" : "{" }"
  if block_given?
    @buffer << "\n"
    @indent_level += 1
    yield
    @indent_level -= 1
    @buffer << "#{indent}#{parallel ? ">>" : "}" }\n"
  else
    @buffer << "#{parallel ? ">>" : "}" }\n"
  end
end

#define_content(variable_name = nil, lyricmode = false) { ... } ⇒ Object

Defines the content for a LilyPond variable.

Parameters:

  • variable_name (String) (defaults to: nil)

    the name of the variable

  • lyricmode (Boolean) (defaults to: false)

    whether the variable should be in lyric mode

Yields:

  • the variable content



57
58
59
60
61
62
63
64
65
66
# File 'lib/lilypond/builder.rb', line 57

def define_content(variable_name = nil, lyricmode = false)
  @buffer << "#{indent}"
  @buffer << "#{variable_name} = " unless variable_name.nil?
  @buffer << "\\lyricmode " if lyricmode
  @buffer << "{\n"
  @indent_level += 1
  yield
  @indent_level -= 1
  @buffer << "}\n"
end

#indentString

Returns the current indentation level.

Returns:

  • (String)

    the current indentation level in spaces



16
17
18
# File 'lib/lilypond/builder.rb', line 16

def indent
  " " * @indent_level * 2
end

#to_sString

Returns the current buffer as a string of LilyPond notation.

Returns:

  • (String)

    the LilyPond notation buffer



85
86
87
# File 'lib/lilypond/builder.rb', line 85

def to_s
  @buffer
end

#use_content(variable_name) ⇒ Object

Uses the content of a LilyPond variable.

Parameters:

  • variable_name (String)

    the name of the variable to use



71
72
73
# File 'lib/lilypond/builder.rb', line 71

def use_content(variable_name)
  @buffer << "#{indent}\\#{variable_name}\n"
end