Class: Musicality::RhythmClass

Inherits:
Object
  • Object
show all
Defined in:
lib/musicality/composition/model/rhythm_class.rb

Overview

Note:

Rests are represented by neagtive portions.

A rhythm pattern based on an array of “portions”. These portions encode an array of fractions that sum to 1 (each portion would be numerator and the sum of all portions would be the denominator). These fractions can be applied to a total duration to form a rhythm (an array of durations).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(portions) ⇒ RhythmClass

Returns a new instance of RhythmClass.



10
11
12
13
14
15
16
# File 'lib/musicality/composition/model/rhythm_class.rb', line 10

def initialize portions
  if portions.find {|x| x.zero? }
    raise ArgumentError, "rhythm class contains portion(s) that are zero"
  end
  @portions = portions.clone.freeze
  @portions_sum = @portions.inject(0) {|sum,x| sum + x.abs}
end

Instance Attribute Details

#portionsObject (readonly)

Returns the value of attribute portions.



9
10
11
# File 'lib/musicality/composition/model/rhythm_class.rb', line 9

def portions
  @portions
end

#portions_sumObject (readonly)

Returns the value of attribute portions_sum.



9
10
11
# File 'lib/musicality/composition/model/rhythm_class.rb', line 9

def portions_sum
  @portions_sum
end

Instance Method Details

#to_rhythm(total_dur) ⇒ Object

Use the rhythm class to generate a rhtyhm



19
20
21
# File 'lib/musicality/composition/model/rhythm_class.rb', line 19

def to_rhythm(total_dur)
  Rhythm.new @portions.map {|x| Rational(x,@portions_sum) * total_dur }
end