class InevitableCacophony::Rhythm

Constants

AFTER_DELAY

Amount of silence after notes, as a fraction of duration.

START_DELAY

Amount of silence before a note, as a fraction of the note's duration

Attributes

beats[R]

Public Class Methods

new(beats) click to toggle source
# File lib/inevitable_cacophony/rhythm.rb, line 77
def initialize(beats)
        @beats = beats
end

Public Instance Methods

==(other) click to toggle source
# File lib/inevitable_cacophony/rhythm.rb, line 122
def == other
        self.class == other.class &&
                self.beats == other.beats
end
canonical() click to toggle source

@return [Array<Numeric,NilClass>] An array where a is the amplitude of the beat at time-step i

(rests are 0), or nil if no beat is played then.
This will be as long as necessary to represent the rhythm accurately,
including early and late beats.
# File lib/inevitable_cacophony/rhythm.rb, line 96
def canonical
        if duration != duration.to_i
                raise "Cannot yet canonicalise rhythms with non-integer length"
        end
        
        # Figure out the timing offset we need to allow for,
        # and space the beats enough to make it work.
        timing_offset_denominators = self.beats.map do |beat|
                beat.start_offset.rationalize.denominator
        end
        denominator = timing_offset_denominators.inject(1, &:lcm)
        
        scaled_duration = duration * denominator
        Array.new(scaled_duration).tap do |spaced_beats|
                self.beats.each_with_index do |beat, index|
                        offset_index = index + beat.start_offset
                        scaled_index = offset_index * denominator
                        spaced_beats[scaled_index] = beat.amplitude
                end
        end
end
duration() click to toggle source

@return [Integer] Total duration of all beats in this rhythm.

# File lib/inevitable_cacophony/rhythm.rb, line 88
def duration
        each_beat.sum(&:duration)
end
each_beat(&block) click to toggle source
# File lib/inevitable_cacophony/rhythm.rb, line 83
def each_beat(&block)
        @beats.each(&block)
end
inspect() click to toggle source
# File lib/inevitable_cacophony/rhythm.rb, line 118
def inspect
        "<#Rhythm duration=#{duration} @beats=#{beats.inspect}>"
end