Class: Alda::Octave

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

Overview

An octave event. An Alda::EventContainer containing an Alda::Octave can be derived using event list sugar. See Alda::EventList#method_missing.

o! means octave up, and o? means octave down. You can also use #+@ and #-@ to denote octave up and down.

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(num) ⇒ Octave

:call-seq:

new(num) -> Alda::Octave

Creates an Alda::Octave.



672
673
674
675
676
# File 'lib/alda-rb/event.rb', line 672

def initialize num
	super()
	@num = num.to_s
	@up_or_down = 0
end

Instance Attribute Details

#numObject

The string representing the octave’s number.

It can be empty, in which case it is purely serving for #+@ and #-@.



657
658
659
# File 'lib/alda-rb/event.rb', line 657

def num
  @num
end

#up_or_downObject

Positive for up, negative for down, and 0 as default.

Alda::Score.new do
  p((++++o).event.up_or_down) # => 4
end


665
666
667
# File 'lib/alda-rb/event.rb', line 665

def up_or_down
  @up_or_down
end

Instance Method Details

#+@Object

:call-seq:

+octave -> octave

Octave up.

Alda::Score.new { piano_; c; +o; c }.play
# (plays C4, then C5)

See #-@.



688
689
690
691
# File 'lib/alda-rb/event.rb', line 688

def +@
	@up_or_down += 1
	self
end

#-@Object

:call-seq:

-octave -> octave

Octave down. See #+@.



699
700
701
702
# File 'lib/alda-rb/event.rb', line 699

def -@
	@up_or_down -= 1
	self
end

#==(other) ⇒ Object

:call-seq:

octave == other -> true or false

Overrides Alda::Event#==. Returns true if other is an Alda::Octave and has the same #num and #up_or_down as octave (using ==).



727
728
729
# File 'lib/alda-rb/event.rb', line 727

def == other
	super || other.is_a?(Alda::Octave) && @num == other.num && @up_or_down == other.up_or_down
end

#to_alda_codeObject

:call-seq:

to_alda_code() -> String

Overrides Alda::Event#to_alda_code.



709
710
711
712
713
714
715
716
717
718
# File 'lib/alda-rb/event.rb', line 709

def to_alda_code
	case @up_or_down <=> 0
	when 0
		?o + @num
	when 1
		?> * @up_or_down
	when -1
		?< * -@up_or_down
	end
end