Class: Coltrane::Theory::NoteSet

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/coltrane/theory/note_set.rb

Overview

It describes a set of notes

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arg) ⇒ NoteSet

Returns a new instance of NoteSet.



22
23
24
25
26
27
28
29
# File 'lib/coltrane/theory/note_set.rb', line 22

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

Instance Attribute Details

#notesObject (readonly) Also known as: all

Returns the value of attribute notes.



13
14
15
# File 'lib/coltrane/theory/note_set.rb', line 13

def notes
  @notes
end

Class Method Details

.[](*notes) ⇒ Object



18
19
20
# File 'lib/coltrane/theory/note_set.rb', line 18

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

Instance Method Details

#&(other) ⇒ Object



37
38
39
# File 'lib/coltrane/theory/note_set.rb', line 37

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

#+(other) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/coltrane/theory/note_set.rb', line 45

def +(other)
  case other
  when Note then NoteSet[*(notes + [other])]
  when NoteSet then NoteSet[*notes, *other.notes]
  when Interval then NoteSet[*notes.map { |n| n + other }]
  end
end

#-(other) ⇒ Object



53
54
55
56
57
58
# File 'lib/coltrane/theory/note_set.rb', line 53

def -(other)
  case other
  when NoteSet then NoteSet[*(notes - other.notes)]
  when Interval then NoteSet[*notes.map { |n| n - other }]
  end
end

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



31
32
33
# File 'lib/coltrane/theory/note_set.rb', line 31

def ==(other)
  (self & other).size == self.size
end

#accidentalsObject



76
77
78
# File 'lib/coltrane/theory/note_set.rb', line 76

def accidentals
  count(&:accidental?)
end

#alter(alteration) ⇒ Object



88
89
90
# File 'lib/coltrane/theory/note_set.rb', line 88

def alter(alteration)
  NoteSet[*map { |n| n.alter(alteration) }]
end

#alter_accidentals(alteration) ⇒ Object



92
93
94
# File 'lib/coltrane/theory/note_set.rb', line 92

def alter_accidentals(alteration)
  NoteSet[*map { |n| n.alter(alteration) if n.accidental? }]
end

#degree(note) ⇒ Object



41
42
43
# File 'lib/coltrane/theory/note_set.rb', line 41

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

#flatsObject



84
85
86
# File 'lib/coltrane/theory/note_set.rb', line 84

def flats
  count(&:flat?)
end

#hashObject



68
69
70
# File 'lib/coltrane/theory/note_set.rb', line 68

def hash
  integers.join.to_i
end

#integersObject



72
73
74
# File 'lib/coltrane/theory/note_set.rb', line 72

def integers
  map(&:integer)
end

#interval_sequenceObject



96
97
98
# File 'lib/coltrane/theory/note_set.rb', line 96

def interval_sequence
  IntervalSequence.new(notes: self)
end

#namesObject



64
65
66
# File 'lib/coltrane/theory/note_set.rb', line 64

def names
  map(&:name)
end

#pretty_namesObject



60
61
62
# File 'lib/coltrane/theory/note_set.rb', line 60

def pretty_names
  map(&:pretty_name)
end

#sharpsObject



80
81
82
# File 'lib/coltrane/theory/note_set.rb', line 80

def sharps
  count(&:sharp?)
end