class Music
This class contains the variables and methods based upon contemporary western music turning.
Public Class Methods
Returns the allowed chords
# File lib/musical-chords.rb, line 222 def self.allowed_chords @@chords.keys end
Returns the keys allowed an input to class methods
# File lib/musical-chords.rb, line 217 def self.allowed_keys (@@sharp_notes + @@flat_notes).uniq.sort end
Returns the notes allowed as input to class methods
# File lib/musical-chords.rb, line 212 def self.allowed_notes @@note_transforms.keys end
Returns the names/notes of the black keys on a piano
# File lib/musical-chords.rb, line 339 def self.black_keys @@black_keys end
Returns an array of notes in a chord given a chord pattern
Params:
-
base_note: (see
Music::allowed_notes
) -
chord_pattern: am array positive integers representing half-steps (semitones) above the base note
# File lib/musical-chords.rb, line 441 def self.chord( base_note, chord_pattern ) base = @@notes.index( Music.normalize_note(base_note) ) raise "Invalid note: #{base_note}" if base.nil? raise "Invalid chord: #{chord_pattern}" if chord_pattern.nil? chord_pattern.map do |step| @@notes[ ( base + step ) % @@scale_modulus ] end end
Returns the notes in the specifed key and chord type
Params:
-
key: (see
Music::allowed_keys
) -
chord_name : (see
Music::allowed_chords
)
# File lib/musical-chords.rb, line 349 def self.chord_from_name( key, chord_name ) self.chord( key, @@chords[chord_name.downcase] ) end
Returns a hash indexing arrays of chords grouped according to their type
Optional Params:
-
max_length : (integer) specifies the maximum length of any given chord. Useful for instruments that are limited in the number of simultaneous notes they can play. The default is 0, or no maximum length
-
exclude_scales : (boolean) specifies that the scales should not be included. Ignored if max_length = 0
# File lib/musical-chords.rb, line 300 def self.chord_groups( params = { } ) options = { max_length: 0, exclude_scales: false }.merge params case options[:max_length] when 0 @@chord_groups else Hash[ @@chord_groups.map { |name,group| if name == :scales and !options[:exclude_scales] [ name, group ] else [ name, group.select { |chord| @@chords[chord].length <= options[:max_length] } ] end } ] end end
Returns an array of notes comprising the chromatic scale (all semi-tones) beginning at the root note of the key provided.
Params:
-
key: (see
Music::allowed_keys
)
# File lib/musical-chords.rb, line 324 def self.chromatic_scale( key ) self.chord( key, @@chords["chromatic-scale"] ) end
Returns a numeric pointer to the note provided within the @@notes class array
Params:
-
note: (see
Music::allowed_notes
)
# File lib/musical-chords.rb, line 368 def self.index_note( note ) return @@notes.index( @@note_transforms[ note.upcase ] ) end
Returns a major chord : 1-3-5 with the given base_note (key) This method will likelhy be deprecated in the future. Use Music::chord
(base_note,“maj”) instead.
Params:
-
base_note: (see
Music::allowed_notes
)
# File lib/musical-chords.rb, line 391 def self.major( base_note ) self.chord( base_note, @@chords["maj"] ) end
Returns a major scale : 1-2-3-4-5-6-7 with the given base_note (key)
Params:
-
base_note: (see
Music::allowed_notes
)
# File lib/musical-chords.rb, line 400 def self.major_scale( base_note ) self.chord( base_note, @@scales[:major] ) end
Returns a major chord : 1-3b-5 with the given base_note (key) This method will likelhy be deprecated in the future. Use Music::chord
(base_note,“min”) instead.
Params:
-
base_note: (see
Music::allowed_notes
)
# File lib/musical-chords.rb, line 410 def self.minor( base_note ) self.chord( base_note, @@chords["min"] ) end
Returns a manor scale : 1-2-3-4-5-6-7 with the given base_note (key)
Params:
-
base_note: (see
Music::allowed_notes
)
# File lib/musical-chords.rb, line 419 def self.minor_scale( base_note ) self.chord( base_note, @@scales[:minor] ) end
Returns the note as a flat or sharp based upon the given key
Params:
-
key: (see
Music::allowed_keys
) -
note: (see
Music::allowed_notes
)
# File lib/musical-chords.rb, line 250 def self.name_note( key, note ) if @@sharps.include?( key ) note_as_sharp( note ) else note_as_flat( note ) end end
Converts an array of notes to flats or sharps based upon the given key
Params:
-
key: (see
Music::allowed_keys
) -
note: (see
Music::allowed_notes
)
# File lib/musical-chords.rb, line 264 def self.name_notes( key, notes = [] ) notes.map { |n| name_note( key, n ) } end
Returns notes in a normalized fashon. This method is not used and may be deprecated in the future
Params:
-
base_note : of the form “Cb” or “C#” or “c-sharp”
# File lib/musical-chords.rb, line 359 def self.normalize_name( base_note ) @@note_normalized_names[ base_note.upcase ] end
Converts the given note into a normalized form. THis method is used primarly for chord manipulation.
Params:
-
note: (see
Music::allowed_notes
)
Returns:
-
nil : if the note is not valid
# File lib/musical-chords.rb, line 381 def self.normalize_note( note ) return @@notes.include?(note.upcase) ? note.upcase : @@note_transforms[ note.upcase ] end
Returns the note as a flat note.
Params:
-
note: (see
Music::allowed_notes
)
# File lib/musical-chords.rb, line 240 def self.note_as_flat( note ) @@flat_notes[ @@notes.index( @@note_transforms[note] ) ] end
Returns the note as a sharp note.
Params:
-
note: (see
Music::allowed_notes
)
# File lib/musical-chords.rb, line 231 def self.note_as_sharp( note ) @@sharp_notes[ @@notes.index( @@note_transforms[note] ) ] end
Returns the note with a postivie number of semitones away from the base note
Params:
-
base_note: (see
Music::allowed_notes
) -
offset: a positive integer representing half-steps (semitones) above the base note
# File lib/musical-chords.rb, line 429 def self.note_from_offset( base_note, offset, params = { } ) options = { key: nil }.merge params base = @@notes.index( Music.normalize_note(base_note)) raise "Invalid note: #{base_note}" if base.nil? options[:key].nil? ? @@notes[ (base + offset) % @@scale_modulus ] : name_note( options[:key], @@notes[ (base + offset) % @@scale_modulus ] ) end
Returns the position (index in the chromatic scale) of the note in the given key. For instance, C# is note 1 in the chromatic scale of C. (Note 0 in the key of C is C.)
Params:
-
key: (see
Music::allowed_keys
) -
note: (see
Music::allowed_notes
)
# File lib/musical-chords.rb, line 277 def self.note_position( key, note ) chromatic_scale( key ).index( @@note_transforms[note] ) end
Returns the position (index in the chromatic scale) of each note in the array in the given key. For instance, C# is note 1 in the chromatic scale of C. (Note 0 in the key of C is C.)
Params:
-
key: (see
Music::allowed_keys
) -
note: (see
Music::allowed_notes
)
# File lib/musical-chords.rb, line 288 def self.note_positions( key, notes ) notes.map do |note| note_position(key, note) end end
Converts the specified note to html formatting. Useful for sharps and flats
Params:
-
note:
# File lib/musical-chords.rb, line 456 def self.note_to_html( note ) note.gsub("#","♯").gsub(/(\w)[bB]/) { $1 + "♭" } end
Returns a hash of the included scales and their semi-tone members as an array
# File lib/musical-chords.rb, line 329 def self.scales @@scales end
Returns the names/notes of the white keys on a piano
# File lib/musical-chords.rb, line 334 def self.white_keys @@white_keys end