| Class | Chord |
| In: |
app/models/chord.rb
|
| Parent: | ActiveRecord::Base |
A chord object represents a chord defined as an unordered collection of tones (non-octaval). Mixes in the KeyContext module to provide optional key context on the chord.
Chords should be created by indexing the Chord class like an array, like so:
Chord[symbol]
An alternative method of resolving a chord is to use the resolve method, though this is likely to become deprecated in the future when chord progression support/representation is added:
Chord.resolve(symbol)
This resolves a specified chord symbol into a new chord object using data regarding chord symbol-to-chord relationships. symbol can be any standard jazz chord symbol representable as plain ASCII (unicode support for other symbols such as full-diminished circle in the works). Can be prefixed with a key.
A chord object has the following associations, exposed by methods:
Chord['maj'] Chord['min7' Chord['maj7#11'] Chord['7#9']
Chord['C7'] Chord['Bbalt'] Chord['Gbmaj7']
Use notes to retrieve a collection of notes, which actually delegates to +tones.notes+:
Chord['C7'].notes # => ['C', 'E', 'G', 'Bb'] Chord['Cmaj7#11'].notes # => ["C", "E", "G", "B", "F#"]
Note the 11 is correctly interpreted as E# and not enharmonic F here:
Chord['Bmaj7#11'].notes # => ["B", "D#", "F#", "A#", "E#"]
Also correctly interpet tones off of enharmonic base keys:
Chord['Gbmaj7'].notes # => ["Gb", "Bb", "Db", "F"] Chord['F#maj7'].notes # => ["F#", "A#", "C#", "E#"]
Chord['min7'].modes.names # .names == .map(&:name) # => ['Dorian'] Chord['min7'].modes[0].scale.name # => "Major" Chord['Amin7'].modes.names # => ['A Dorian']
| resolve | -> | [] |
Retreives Fully-Cached Array
# File app/models/chord.rb, line 139
139: def self.cache(conditions = [])
140: self.find(:all, :include => [:tones, :symbols], :conditions => conditions) # Rails 2.0 Already Caches!
141: end
Resolves a chord symbol into a chord. Implementation is somewhat flakey due to the potential ambiguities arising from specifying key and symbols together.
# File app/models/chord.rb, line 106
106: def resolve(symbol)
107: in_key = nil
108:
109: return nil if symbol.nil?
110:
111: Key.cache.each do |k|
112: if symbol.starts_with?(k.name)
113: in_key = k
114: symbol.sub!(k.name, '').strip
115: break
116: end
117: end
118:
119: chord_symbol = ChordSymbol[symbol]
120:
121: # Perhaps the matched key was really part of the name of the chord, try that:
122: if chord_symbol.nil? && !in_key.nil?
123: symbol = in_key.name + symbol
124: chord_symbol = ChordSymbol[symbol]
125: end
126:
127: # If still not found, must be invalid:
128: return nil if chord_symbol.nil?
129:
130: chord = chord_symbol.chord
131: chord.key = in_key unless in_key.nil?
132: chord
133: end