Class: LilyPond::Builder
- Inherits:
-
Object
- Object
- LilyPond::Builder
- Defined in:
- lib/lilypond/builder.rb
Overview
The Builder class is responsible for building LilyPond notation.
Class Method Summary collapse
-
.sample ⇒ LilyPond::Builder
Generates a sample LilyPond notation.
Instance Method Summary collapse
-
#append(string) ⇒ Object
Appends a string of LilyPond notation to the buffer.
-
#block(name = nil, parallel = false, options = {}) { ... } ⇒ Object
Generates a block of LilyPond notation.
-
#define_content(variable_name = nil, lyricmode = false) { ... } ⇒ Object
Defines the content for a LilyPond variable.
-
#indent ⇒ String
Returns the current indentation level.
-
#initialize ⇒ Builder
constructor
Creates a new LilyPond Builder object.
-
#to_s ⇒ String
Returns the current buffer as a string of LilyPond notation.
-
#use_content(variable_name) ⇒ Object
Uses the content of a LilyPond variable.
Constructor Details
#initialize ⇒ Builder
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
.sample ⇒ LilyPond::Builder
Generates a sample LilyPond notation.
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 = { :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, ) 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.
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.
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, = {}) @buffer << "#{indent}" @buffer << "\\#{name} " unless name == nil if .any? @buffer << "\\with { \n" @indent_level +=1 .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.
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 |
#indent ⇒ String
Returns the current indentation level.
16 17 18 |
# File 'lib/lilypond/builder.rb', line 16 def indent " " * @indent_level * 2 end |
#to_s ⇒ String
Returns the current buffer as a string of LilyPond notation.
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.
71 72 73 |
# File 'lib/lilypond/builder.rb', line 71 def use_content(variable_name) @buffer << "#{indent}\\#{variable_name}\n" end |