Sha256: 21af963782d6c7e81ed7354f140e2c06be86e67677904b569093f699fe10b22b

Contents?: true

Size: 1.14 KB

Versions: 6

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

module Coltrane
  # It describes a set of notes
  class NoteSet
    extend Forwardable

    def_delegators :@notes, :first, :each, :size, :map, :reduce, :index,
                   :[], :index, :empty?, :permutation, :include?

    attr_reader :notes

    alias root first
    alias all notes

    def self.[](*notes)
      new(notes)
    end

    def initialize(arg)
      @notes =
        case arg
        when NoteSet then arg.notes
        when Array   then arg.map { |n| n.is_a?(Note) ? n : Note[n] }
        else raise InvalidNotesError, arg
        end
    end

    def &(other)
      NoteSet[*(notes & other.notes)]
    end

    def degree(note)
      index(note) + 1
    end

    def +(other)
      NoteSet[*(notes + other.notes)]
    end

    def pretty_names
      map(&:pretty_name)
    end

    def names
      map(&:name)
    end

    def transpose_to(note_name)
      transpose_by(root_note.interval_to(note_name).number)
    end

    def transpose_by(interval)
      notes.map do |note|
        note.transpose_by(interval)
      end
    end

    def interval_sequence
      IntervalSequence.new(notes: self)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
coltrane-1.0.26 lib/coltrane/note_set.rb
coltrane-1.0.24 lib/coltrane/note_set.rb
coltrane-1.0.22 lib/coltrane/note_set.rb
coltrane-1.0.21 lib/coltrane/note_set.rb
coltrane-1.0.20 lib/coltrane/note_set.rb
coltrane-1.0.2 lib/coltrane/note_set.rb