Class: Alda::Note

Inherits:
Event
  • Object
show all
Defined in:
lib/alda-rb/event.rb

Overview

A note event. An Alda::EventContainer containing an Alda::Note can be derived using Alda::EventList sugar. See Alda::EventList#method_missing.

There cannot be tildes and dots in (usual) ruby method names, so use underlines instead.

The accidentals can be added using #+@, #-@, and #~, or by using exclamation mark, question mark or underline.

Alda::Score.new do
  key_sig! [:d, :major]
  c4_2 d1108ms e2s
  f2!      # F sharp
  g20ms_4? # G flat
  a6_      # A natural
  c__      # C (slur)
  f___     # D natural (slur)
end

Instance Attribute Summary collapse

Attributes inherited from Event

#container, #parent

Instance Method Summary collapse

Methods inherited from Event

#detach_from_parent, #is_event_of?, #on_contained

Constructor Details

#initialize(pitch, duration) ⇒ Note

:call-seq:

new(pitch, duration) -> Alda::Note

The underlines in duration will be converted to tildes “~”. Exclamation mark and question mark in duration will be interpreted as accidentals in #pitch.

The number of underlines at the end of duration means: neither natural nor slur if 0, natural if 1, slur if 2, both natural and slur if 3.



510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
# File 'lib/alda-rb/event.rb', line 510

def initialize pitch, duration
	super()
	@pitch = pitch.to_s
	@duration = duration.to_s.tr ?_, ?~
	case @duration[-1]
	when ?! # sharp
		@pitch.concat ?+
		@duration[-1] = ''
	when ?? # flat
		@pitch.concat ?-
		@duration[-1] = ''
	end
	waves = /(?<str>~+)\z/ =~ @duration ? str.size : return
	@duration[@duration.length - waves..] = ''
	if waves >= 2
		waves -= 2
		@duration.concat ?~
	end
	@pitch.concat ?_ * waves
end

Instance Attribute Details

#durationObject

The string representing the duration.

It ends with a tilde “~” if the note slurs.



495
496
497
# File 'lib/alda-rb/event.rb', line 495

def duration
  @duration
end

#pitchObject

The string representing the pitch



489
490
491
# File 'lib/alda-rb/event.rb', line 489

def pitch
  @pitch
end

Instance Method Details

#+@Object

:call-seq:

+note -> note

Append a sharp sign after #pitch.

Alda::Score.new { piano_; +c }.play
# (plays a C sharp note)


539
540
541
542
# File 'lib/alda-rb/event.rb', line 539

def +@
	@pitch.concat ?+
	self
end

#-@Object

:call-seq:

-note -> note

Append a flat sign after #pitch.

Alda::Score.new { piano_; -d }.play
# (plays a D flat note)


552
553
554
555
# File 'lib/alda-rb/event.rb', line 552

def -@
	@pitch.concat ?-
	self
end

#==(other) ⇒ Object

:call-seq:

note == other -> true or false

Overrides Alda::Event#==. Returns true if other is an Alda::Note and has the same #pitch and #duration as note (using ==).



588
589
590
# File 'lib/alda-rb/event.rb', line 588

def == other
	super || other.is_a?(Alda::Note) && @pitch == other.pitch && @duration == other.duration
end

#to_alda_codeObject

:call-seq:

to_alda_code() -> String

Overrides Alda::Event#to_alda_code.



575
576
577
578
579
# File 'lib/alda-rb/event.rb', line 575

def to_alda_code
	result = @pitch + @duration
	result.concat ?*, @count.to_alda_code if @count
	result
end

#~Object

:call-seq:

~note -> note

Append a natural sign after #pitch.

Alda::Score.new { piano_; key_sig 'f+'; ~f }.play
# (plays an F note)


565
566
567
568
# File 'lib/alda-rb/event.rb', line 565

def ~
	@pitch.concat ?_
	self
end