Module: Lydown::Rendering::Notes

Includes:
Figures
Included in:
Chord, Command, FiguresSilence, Note, Rest, Setting, ShortTie, Silence, StandAloneFigures
Defined in:
lib/lydown/rendering/notes.rb

Constant Summary collapse

TRANSPARENT_TIE =
"\\once \\override Tie #'transparent = ##t"
TRANSPARENT_NOTE =
<<EOF
\\once \\override NoteHead #'transparent = ##t
\\once \\override Dots #'extra-offset = #'(-1.3 . 0)
\\once \\override Stem #'transparent = ##t
EOF
LILYPOND_EXPRESSIONS =
{
  '_' => '--',
  '.' => '-.',
  '`' => '-!'
}
MARKUP_ALIGNMENT =
{
  '<' => 'right-align',
  '>' => 'left-align',
  '|' => 'center-align'
}
DYNAMICS =
%w{
  pppp ppp pp p mp mf f ff fff ffff fp sf sff sp spp sfz rfz
}
TEXTMATE_URL =
"txmt://open?url=file://%s&line=%d&column=%d"
'\once \override NoteHead.after-line-breaking =
#(add-link "%s") '

Constants included from Figures

Figures::ALTERATION, Figures::ALTERATION_RE, Figures::BLANK_EXTENDER, Figures::BLANK_EXTENDER_START, Figures::BLANK_EXTENDER_STOP, Figures::EXTENDERS_OFF, Figures::EXTENDERS_ON, Figures::HIDDEN_FORMAT

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Figures

#add_figures, #add_stand_alone_figures, #check_tenues, #lilypond_figures, #next_figures_event, #translate_figures

Class Method Details

.add_duration_macro_group(context, group) ⇒ Object



386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/lydown/rendering/notes.rb', line 386

def self.add_duration_macro_group(context, group)
  opts = (context[:options] || {}).merge({
    filename: context['process/macro_filename'],
    source:   context['process/macro_source']
  }).deep!
  code = LydownParser.parse_macro_group(group, opts)

  # stash macro
  macro = context['process/duration_macro']
  context['process/duration_macro'] = nil
  context['process/macro_group'] = nil
  context['process/macro_group_note_count'] = nil

  context.translate(code, macro_group: true)
ensure
  # restore macro
  context['process/duration_macro'] = macro
end

.cleanup_duration_macro(context) ⇒ Object

emits the current macro group up to the first placeholder character. this method is called



376
377
378
379
380
381
382
383
384
# File 'lib/lydown/rendering/notes.rb', line 376

def self.cleanup_duration_macro(context)
  return unless context['process/macro_group_note_count'] &&
    context['process/macro_group_note_count'] > 0

  # truncate macro group up until first placeholder
  group = context['process/macro_group'].sub(/(?<!\<)_.*$/, '')

  add_duration_macro_group(context, group)
end

Instance Method Details

#add_chord(event, options = {}) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/lydown/rendering/notes.rb', line 194

def add_chord(event, options = {})
  value = @context['process/duration_values'].first
  @context['process/duration_values'].rotate!

  add_figures(event[:figures], value) if event[:figures]

  # push value into running values accumulator. This is used to synthesize
  # the bass figures durations.
  unless event[:figures]
    @context['process/running_values'] ||= []
    if event[:rest_value]
      @context['process/running_values'] << event[:rest_value]
    else
      @context['process/running_values'] << value
    end
  end

  # only add the value if different than the last used
  if value == @context['process/last_value']
    value = ''
  else
    @context['process/last_value'] = value
  end

  notes = event[:notes].map do |note|
    lilypond_note(note)
  end

  options = options.merge(value: value)
  @context.emit(event[:stream] || :music, lilypond_chord(event, notes, options))
end

#add_macro_event(code) ⇒ Object



405
406
407
408
409
410
411
412
413
# File 'lib/lydown/rendering/notes.rb', line 405

def add_macro_event(code)
  case @context['process/macro_group']
  when nil
    @context['process/macro_group'] = @context['process/duration_macro'].clone
    @context['process/macro_group'].insert(0, " #{code} ")
  when /_/
    @context['process/macro_group'].sub!(/([_∞])/, " #{code} \\0")
  end
end

#add_macro_note(event) ⇒ Object



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/lydown/rendering/notes.rb', line 334

def add_macro_note(event)
  @context['process/macro_group'] ||= @context['process/duration_macro'].clone
  underscore_count = 0

  lydown_note = "{%d:%d}%s%s%s%s%s%s%s" % [
    event[:line] || 0,
    event[:column] || 0,
    lydown_phrasing_open(event),
    event[:head], event[:octave], event[:accidental_flag],
    lydown_phrasing_close(event),
    event[:figures] ? "<#{event[:figures].join}>" : '',
    event[:expressions] ? event[:expressions].join + ' ' : ''
  ]

  # replace place holder and repeaters in macro group with actual note
  @context['process/macro_group'].gsub!(/[_∞]/) do |match|
    case match
    when '_'
      underscore_count += 1
      underscore_count == 1 ? lydown_note : match
    when ''
      underscore_count < 2 ? event[:head] : match
    end
  end

  # keep filename and source in order to ensure source references are kept
  # correct
  @context['process/macro_filename'] = event[:filename]
  @context['process/macro_source'] = event[:source]

  # increment group note count
  @context['process/macro_group_note_count'] ||= 0
  @context['process/macro_group_note_count'] += 1

  # if group is complete, compile it just like regular code
  unless @context['process/macro_group'].include?('_')
    Notes.add_duration_macro_group(@context, @context['process/macro_group'])
  end
end

#add_note(event, options = {}) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/lydown/rendering/notes.rb', line 143

def add_note(event, options = {})
  @context.set_setting(:got_music, true)

  return add_macro_note(event) if @context['process/duration_macro']

  # calculate relative octave markers for first note
  unless @context['process/first_note'] || event[:head] =~ /^[rsR]/
    note = event[:head] + (event[:octave] || '')
    event[:octave] = Lydown::Rendering::Octaves.relative_octave(note)
    @context['process/first_note'] = note
  end

  if event[:head] == '@'
    # replace repeating note head
    event[:head] = @context['process/last_note_head']
  else
    @context['process/last_note_head'] = event[:head]
  end

  value = @context['process/duration_values'].first
  @context['process/duration_values'].rotate!

  add_figures(event[:figures], value) if event[:figures]

  # push value into running values accumulator. This is used to synthesize
  # the bass figures durations.
  unless event[:figures]
    @context['process/running_values'] ||= []
    if event[:rest_value]
      @context['process/running_values'] << event[:rest_value]
    else
      @context['process/running_values'] << value
    end
  end

  # only add the value if different than the last used
  if options[:no_value] || (value == @context['process/last_value'])
    value = ''
  else
    @context['process/last_value'] = value
  end

  if event[:line] && @context['options/proof_mode']
    @context.emit(event[:stream] || :music, note_event_url_link(event))
    # @context.emit(event[:stream] || :music, "%{#{event[:line]}:#{event[:column]}%}")
  end

  code = lilypond_note(event, options.merge(value: value))
  @context.emit(event[:stream] || :music, code)
end

#cross_bar_dot_lilypond_note(event, options) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/lydown/rendering/notes.rb', line 263

def cross_bar_dot_lilypond_note(event, options)
  @context['process/cross_bar_dotting'] = nil

  original_duration = @context['process/duration_values'][0]
  original_duration =~ /([0-9]+)(\.+)/
  value, dots =  $1, $2

  main_note = lilypond_note(event, options.merge(value: value))

  cross_bar_note_head = lilypond_note(event, options.merge(head_only: true))
  cross_bar_note = "#{cross_bar_note_head}#{original_duration}*0"

  silence = "s#{value.to_i * 2} "

  [
    TRANSPARENT_TIE,
    main_note,
    '~',
    TRANSPARENT_NOTE,
    cross_bar_note,
    silence
  ].join(' ')
end

#lilypond_chord(event, notes, options = {}) ⇒ Object



287
288
289
290
291
292
293
294
295
296
297
# File 'lib/lydown/rendering/notes.rb', line 287

def lilypond_chord(event, notes, options = {})
  [
    '<',
    notes.join(' ').strip, # strip trailing whitespace
    '>',
    options[:value],
    lilypond_phrasing(event),
    event[:expressions] ? event[:expressions].join : '',
    ' '
  ].join
end

#lilypond_note(event, options = {}) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/lydown/rendering/notes.rb', line 226

def lilypond_note(event, options = {})
  if @context['process/cross_bar_dotting']
    return cross_bar_dot_lilypond_note(event, options)
  end

  head = Accidentals.translate_note_name(@context, event[:head])
  if options[:head_only]
    head
  else
    if event[:accidental_flag] =~ /\^/
      accidental_flag = event[:accidental_flag].gsub('^', '')
      prefix = '\ficta '
    else
      accidental_flag = event[:accidental_flag]
      prefix = ''
    end

    [
      prefix,
      head,
      event[:octave],
      accidental_flag,
      options[:value],
      lilypond_phrasing(event),
      event[:expressions] ? event[:expressions].join : '',
      options[:no_whitespace] ? '' : ' '
    ].join
  end
end

#lilypond_phrasing(event) ⇒ Object



299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/lydown/rendering/notes.rb', line 299

def lilypond_phrasing(event)
  phrasing = ''
  if @context['process/open_beam']
    phrasing << '['
    @context['process/open_beam'] = nil
  end
  if @context['process/open_slur']
    phrasing << '('
    @context['process/open_slur'] = nil
  end
  phrasing << ']' if event[:beam_close]
  phrasing << ')' if event[:slur_close]
  phrasing
end

#lydown_phrasing_close(event) ⇒ Object



327
328
329
330
331
332
# File 'lib/lydown/rendering/notes.rb', line 327

def lydown_phrasing_close(event)
  phrasing = ''
  phrasing << ']' if event[:beam_close]
  phrasing << ')' if event[:slur_close]
  phrasing
end

#lydown_phrasing_open(event) ⇒ Object



314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/lydown/rendering/notes.rb', line 314

def lydown_phrasing_open(event)
  phrasing = ''
  if @context['process/open_beam']
    phrasing << '['
    @context['process/open_beam'] = nil
  end
  if @context['process/open_slur']
    phrasing << '('
    @context['process/open_slur'] = nil
  end
  phrasing
end


464
465
466
467
468
469
470
471
472
# File 'lib/lydown/rendering/notes.rb', line 464

def note_event_url_link(event)
  url = TEXTMATE_URL % [
    File.expand_path(event[:filename]).uri_escape,
    event[:line],
    event[:column]
  ]

  ADD_LINK_COMMAND % [url]
end

#translate_expressionsObject



431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
# File 'lib/lydown/rendering/notes.rb', line 431

def translate_expressions
  return unless @event[:expressions]

  @event[:expressions] = @event[:expressions].map do |expr|
    if expr =~ /^(?:\\(_?)([<>\|])?)?"(.+)"$/
      v_pos = ($1 == '_') ? '_' : '^'
      content = translate_string_expression($3)
      if MARKUP_ALIGNMENT[$2]
        content = "\\#{MARKUP_ALIGNMENT[$2]} { #{content} }"
      end
      "#{v_pos}\\markup { #{content} }"
    elsif expr =~ /^\\([_^]?)(.+)$/
      v_pos = $1
      "#{v_pos}\\#{$2}"
    elsif LILYPOND_EXPRESSIONS[expr]
      LILYPOND_EXPRESSIONS[expr]
    else
      raise LydownError, "Invalid expression #{expr.inspect}"
    end
  end
end

#translate_string_expression(expr) ⇒ Object



453
454
455
456
457
# File 'lib/lydown/rendering/notes.rb', line 453

def translate_string_expression(expr)
  expr.unescape.
      gsub(/__([^_]+)__/) {|m| "\\bold { #{$1} }" }.
      gsub(/_([^_]+)_/) {|m| "\\italic { #{$1} }" }
end