Sha256: a5140a4cacd85741ea616382eba88e6405cc785267f906d83dcda966ba575f27

Contents?: true

Size: 1.68 KB

Versions: 9

Compression:

Stored size: 1.68 KB

Contents

# A module for musical content
module HeadMusic::Content; end

# A placement is a note or rest at a position within a voice in a composition
class HeadMusic::Content::Placement
  include Comparable

  attr_reader :voice, :position, :rhythmic_value, :pitch

  delegate :composition, to: :voice
  delegate :spelling, to: :pitch, allow_nil: true

  def initialize(voice, position, rhythmic_value, pitch = nil)
    ensure_attributes(voice, position, rhythmic_value, pitch)
  end

  def note?
    pitch
  end

  def rest?
    !note?
  end

  def next_position
    @next_position ||= position + rhythmic_value
  end

  def <=>(other)
    position <=> other.position
  end

  def during?(other_placement)
    starts_during?(other_placement) || ends_during?(other_placement) || wraps?(other_placement)
  end

  def to_s
    "#{rhythmic_value} #{pitch || "rest"} at #{position}"
  end

  private

  def starts_during?(other_placement)
    position >= other_placement.position && position < other_placement.next_position
  end

  def ends_during?(other_placement)
    next_position > other_placement.position && next_position <= other_placement.next_position
  end

  def wraps?(other_placement)
    position <= other_placement.position && next_position >= other_placement.next_position
  end

  def ensure_attributes(voice, position, rhythmic_value, pitch)
    @voice = voice
    ensure_position(position)
    @rhythmic_value = HeadMusic::Content::RhythmicValue.get(rhythmic_value)
    @pitch = HeadMusic::Pitch.get(pitch)
  end

  def ensure_position(position)
    @position = if position.is_a?(HeadMusic::Content::Position)
      position
    else
      HeadMusic::Content::Position.new(composition, position)
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
head_music-7.0.5 lib/head_music/content/placement.rb
head_music-7.0.4 lib/head_music/content/placement.rb
head_music-7.0.3 lib/head_music/content/placement.rb
head_music-7.0.2 lib/head_music/content/placement.rb
head_music-7.0.1 lib/head_music/content/placement.rb
head_music-7.0.0 lib/head_music/content/placement.rb
head_music-6.0.1 lib/head_music/content/placement.rb
head_music-6.0.0 lib/head_music/content/placement.rb
head_music-5.0.0 lib/head_music/content/placement.rb