Class: Musa::Chords::Chord

Inherits:
Object
  • Object
show all
Defined in:
lib/musa-dsl/music/chords.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#chord_definitionObject (readonly)

Returns the value of attribute chord_definition.



134
135
136
# File 'lib/musa-dsl/music/chords.rb', line 134

def chord_definition
  @chord_definition
end

#duplicate(**octaves) ⇒ Object (readonly)

Returns the value of attribute duplicate.



134
135
136
# File 'lib/musa-dsl/music/chords.rb', line 134

def duplicate
  @duplicate
end

#move(**octaves) ⇒ Object (readonly)

Returns the value of attribute move.



134
135
136
# File 'lib/musa-dsl/music/chords.rb', line 134

def move
  @move
end

#scaleObject (readonly)

Returns the value of attribute scale.



134
135
136
# File 'lib/musa-dsl/music/chords.rb', line 134

def scale
  @scale
end

Class Method Details

.with_root(root_note_or_pitch_or_symbol, scale: nil, allow_chromatic: false, name: nil, move: nil, duplicate: nil, **features) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/musa-dsl/music/chords.rb', line 10

def self.with_root(root_note_or_pitch_or_symbol, scale: nil, allow_chromatic: false, name: nil, move: nil, duplicate: nil, **features)
  root =
    case root_note_or_pitch_or_symbol
    when Scales::NoteInScale
      root_note_or_pitch_or_symbol
    when Numeric
      if scale
        scale.note_of_pitch(root_note_or_pitch_or_symbol, allow_chromatic: allow_chromatic)
      else
        scale = Musa::Scales::Scales.default_system.default_tuning[root_note_or_pitch_or_symbol].major
        scale.note_of_pitch(root_note_or_pitch_or_symbol)
      end
    when Symbol
      raise ArgumentError, "Missing scale parameter to calculate root note for #{root_note_or_pitch_or_symbol}" unless scale

      scale[root_note_or_pitch_or_symbol]
    else
      raise ArgumentError, "Unexpected #{root_note_or_pitch_or_symbol}"
    end

  scale ||= root.scale

  if name
    raise ArgumentError, "Received name parameter with value #{name}: features parameter is not allowed" if features.any?

    chord_definition = ChordDefinition[name]

  elsif features.any?
    chord_definition = Helper.find_definition_by_features(root.pitch, features, scale, allow_chromatic: allow_chromatic)

  else
    raise ArgumentError, "Don't know how to find a chord definition without name or features parameters"
  end

  unless chord_definition
    raise ArgumentError,
          "Unable to find chord definition for root #{root}" \
          "#{" with name #{name}" if name}" \
          "#{" with features #{features}" if features.any?}"
  end

  source_notes_map = Helper.compute_source_notes_map(root, chord_definition, scale)

  Chord.new(root, scale, chord_definition, move, duplicate, source_notes_map)
end

Instance Method Details

#==(other) ⇒ Object



185
186
187
188
189
# File 'lib/musa-dsl/music/chords.rb', line 185

def ==(other)
  self.class == other.class &&
    @sorted_notes == other.notes &&
    @chord_definition == other.chord_definition
end

#featuresObject



145
146
147
# File 'lib/musa-dsl/music/chords.rb', line 145

def features
  @chord_definition.features
end

#featuring(*values, allow_chromatic: false, **hash) ⇒ Object

Raises:

  • (ArgumentError)


149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/musa-dsl/music/chords.rb', line 149

def featuring(*values, allow_chromatic: false, **hash)
  # create a new list of features based on current features but
  # replacing the values for the new ones and adding the new features
  #
  features = @chord_definition.features.dup
  ChordDefinition.features_from(values, hash).each { |k, v| features[k] = v }

  chord_definition = Helper.find_definition_by_features(@root.pitch, features, @scale, allow_chromatic: allow_chromatic)

  raise ArgumentError, "Unable to find a chord definition for #{features}" unless chord_definition

  source_notes_map = Helper.compute_source_notes_map(@root, chord_definition, @scale)

  Chord.new(@root,
            (@scale if chord_definition.in_scale?(@scale, chord_root_pitch: @root.pitch)),
            chord_definition,
            @move, @duplicate,
            source_notes_map)
end

#inspectObject Also known as: to_s



191
192
193
# File 'lib/musa-dsl/music/chords.rb', line 191

def inspect
  "<Chord #{@name} root #{@root} notes #{@sorted_notes.collect { |_| "#{_.grade}=#{_.note.grade}|#{_.note.pitch} "} }>"
end

#notesObject



136
137
138
# File 'lib/musa-dsl/music/chords.rb', line 136

def notes
  @sorted_notes
end

#octave(octave) ⇒ Object



169
170
171
172
173
174
175
# File 'lib/musa-dsl/music/chords.rb', line 169

def octave(octave)
  source_notes_map = @source_notes_map.transform_values do |notes|
    notes.collect { |note| note.octave(octave) }.freeze
  end.freeze

  Chord.new(@root.octave(octave), @scale, chord_definition, @move, @duplicate, source_notes_map)
end

#pitches(*grades) ⇒ Object



140
141
142
143
# File 'lib/musa-dsl/music/chords.rb', line 140

def pitches(*grades)
  grades = @notes_map.keys if grades.empty?
  @sorted_notes.select { |_| grades.include?(_.grade) }.collect { |_| _.note.pitch }
end