Class: RBMusic::NoteSet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rb-music/note_set.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(notes = []) ⇒ NoteSet

Returns a new instance of NoteSet.



8
9
10
# File 'lib/rb-music/note_set.rb', line 8

def initialize(notes = [])
  @notes = notes
end

Instance Attribute Details

#notesObject

Returns the value of attribute notes.



6
7
8
# File 'lib/rb-music/note_set.rb', line 6

def notes
  @notes
end

Class Method Details

.from_scale(scale, octave = 0, octaves = 1) ⇒ Object

Raises:



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rb-music/note_set.rb', line 12

def self.from_scale(scale, octave=0, octaves=1)
  raise ArgumentError unless scale.is_a?(Scale) && octaves > 0

  root_note = Note.from_latin("#{scale.key}#{octave}")
  notes = []
  octaves.times do |i|
    notes += scale.degrees.map do |interval_name|
      note = root_note.add(interval_name)
      i.times do |octave_offset|
        note = note.add(:octave)
      end
      note
    end
  end

  self.new(notes)
end

Instance Method Details

#<<(other) ⇒ Object Also known as: push



38
39
40
# File 'lib/rb-music/note_set.rb', line 38

def <<(other)
  @notes << other
end

#==(other) ⇒ Object Also known as: eql?



47
48
49
# File 'lib/rb-music/note_set.rb', line 47

def ==(other)
  @notes == other.notes
end

#[](index) ⇒ Object



34
35
36
# File 'lib/rb-music/note_set.rb', line 34

def [](index)
  @notes[index]
end

#add(that) ⇒ Object



56
57
58
# File 'lib/rb-music/note_set.rb', line 56

def add(that)
  NoteSet.new(@notes.map { |note| note.add(that) })
end

#each(&block) ⇒ Object



30
31
32
# File 'lib/rb-music/note_set.rb', line 30

def each(&block)
  @notes.each(&block)
end

#map(&block) ⇒ Object



43
44
45
# File 'lib/rb-music/note_set.rb', line 43

def map(&block)
  @notes.map(&block)
end

#sizeObject



52
53
54
# File 'lib/rb-music/note_set.rb', line 52

def size
  @notes.size
end

#subtract(that) ⇒ Object



60
61
62
# File 'lib/rb-music/note_set.rb', line 60

def subtract(that)
  NoteSet.new(@notes.map { |note| note.subtract(that) })
end