Sha256: 358b0dcd1192b50b913587b2ec82693451be09b4373f55685c96fbcb3823516f

Contents?: true

Size: 1.35 KB

Versions: 2

Compression:

Stored size: 1.35 KB

Contents

module Music
module Transcription

# Program defines markers (by starting note offset) and subprograms (list which markers are played).
#
# @author James Tunnell
#
class Program
  include Validatable
  
  attr_accessor :segments

  # A new instance of Program.
  # @param [Hash] args Hashed arguments. Required key is :segments.
  def initialize segments = []
    @segments = segments
    @check_methods = [:ensure_increasing_segments, :ensure_nonnegative_segments]
  end

  # @return [Float] the sum of all program segment lengths
  def length
    segments.inject(0.0) { |length, segment| length + (segment.last - segment.first) }
  end
  
    # compare to another Program
  def == other
    return other.respond_to?(:segments) && @segments == other.segments
  end

  def include? offset
    @segments.each do |segment|
      if segment.include?(offset)
        return true
      end
    end
    return false
  end
  
  def ensure_increasing_segments
    non_increasing = @segments.select {|seg| seg.first >= seg.last }
    if non_increasing.any?
      raise SegmentNotIncreasingError, "Non-increasing segments found #{non_increasing}"
    end
  end
  
  def ensure_nonnegative_segments
    negative = @segments.select {|seg| seg.first < 0 || seg.last < 0 }
    if negative.any?
      raise SegmentNegativeError, "Negative segments found #{negative}"
    end
  end
end

end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
music-transcription-0.7.3 lib/music-transcription/program.rb
music-transcription-0.7.2 lib/music-transcription/program.rb