Class: Coltrane::NoteSet

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/coltrane/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.



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

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.



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

def notes
  @notes
end

Class Method Details

.[](*notes) ⇒ Object



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

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

Instance Method Details

#&(other) ⇒ Object



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

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

#+(other) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/coltrane/note_set.rb', line 38

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



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

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

#accidentalsObject



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

def accidentals
  count(&:accidental?)
end

#alter(alteration) ⇒ Object



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

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

#alter_accidentals(alteration) ⇒ Object



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

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

#degree(note) ⇒ Object



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

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

#flatsObject



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

def flats
  count(&:flat?)
end

#integersObject



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

def integers
  map(&:integer)
end

#interval_sequenceObject



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

def interval_sequence
  IntervalSequence.new(notes: self)
end

#namesObject



57
58
59
# File 'lib/coltrane/note_set.rb', line 57

def names
  map(&:name)
end

#pretty_namesObject



53
54
55
# File 'lib/coltrane/note_set.rb', line 53

def pretty_names
  map(&:pretty_name)
end

#sharpsObject



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

def sharps
  count(&:sharp?)
end