Class: Musa::Chords::Chord

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#chord_definitionObject (readonly)

Returns the value of attribute chord_definition.



164
165
166
# File 'lib/musa-dsl/music/chords.rb', line 164

def chord_definition
  @chord_definition
end

#notesObject (readonly)

Returns the value of attribute notes.



164
165
166
# File 'lib/musa-dsl/music/chords.rb', line 164

def notes
  @notes
end

Instance Method Details

#==(other) ⇒ Object



243
244
245
# File 'lib/musa-dsl/music/chords.rb', line 243

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

#[](position) ⇒ Object



195
196
197
198
199
200
201
202
# File 'lib/musa-dsl/music/chords.rb', line 195

def [](position)
  case position
  when Numeric
    @notes.values[position]
  when Symbol
    @notes[position]
  end
end

#as_scaleObject

Converts the chord to a specific scale with the notes in the chord



218
219
# File 'lib/musa-dsl/music/chords.rb', line 218

def as_scale
end

#duplicate(**octaves) ⇒ Object



208
209
210
# File 'lib/musa-dsl/music/chords.rb', line 208

def duplicate(**octaves)
  Chord.new(_source: self, duplicate: octaves)
end

#featuresObject



174
175
176
# File 'lib/musa-dsl/music/chords.rb', line 174

def features
  @chord_definition&.features
end

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



178
179
180
181
182
183
184
185
# File 'lib/musa-dsl/music/chords.rb', line 178

def featuring(*values, allow_chromatic: nil, **hash)
  features = @chord_definition.features.dup if @chord_definition
  features ||= {}

  ChordDefinition.features_from(values, hash).each { |k, v| features[k] = v }

  Chord.new(_source: self, allow_chromatic: allow_chromatic, features: features)
end

#inspectObject Also known as: to_s



247
248
249
# File 'lib/musa-dsl/music/chords.rb', line 247

def inspect
  "<Chord: notes = #{@notes}>"
end

#move(**octaves) ⇒ Object



204
205
206
# File 'lib/musa-dsl/music/chords.rb', line 204

def move(**octaves)
  Chord.new(_source: self, move: octaves)
end

#name(name = nil) ⇒ Object



166
167
168
169
170
171
172
# File 'lib/musa-dsl/music/chords.rb', line 166

def name(name = nil)
  if name.nil?
    @chord_definition&.name
  else
    Chord.new(_source: self, name: name)
  end
end

#project_on(*scales, allow_chromatic: nil) ⇒ Object



238
239
240
241
# File 'lib/musa-dsl/music/chords.rb', line 238

def project_on(*scales, allow_chromatic: nil)
  allow_chromatic ||= false
  project_on_all(*scales, allow_chromatic: allow_chromatic).first
end

#project_on_all(*scales, allow_chromatic: nil) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/musa-dsl/music/chords.rb', line 221

def project_on_all(*scales, allow_chromatic: nil)
  # TODO add match to other chords... what does it means?
  allow_chromatic ||= false

  note_sets = {}
  scales.each do |scale|
    note_sets[scale] = if allow_chromatic
      @notes.values.flatten(1).collect { |n| n.on(scale) || n.on(scale.chromatic) }
    else
      @notes.values.flatten(1).collect { |n| n.on(scale) }
                       end
  end

  note_sets_in_scale = note_sets.values.reject { |notes| notes.include?(nil) }
  note_sets_in_scale.collect { |notes| Chord.new(notes: notes) }
end

#root(root = nil) ⇒ Object



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

def root(root = nil)
  if root.nil?
    @notes[:root]
  else
    Chord.new(_source: self, root: root)
  end
end

#scaleObject



212
213
214
215
# File 'lib/musa-dsl/music/chords.rb', line 212

def scale
  scales = @notes.values.flatten(1).collect(&:scale).uniq
  scales.first if scales.size == 1
end