class Music

This class contains the variables and methods based upon contemporary western music turning.

Public Class Methods

allowed_chords() click to toggle source

Returns the allowed chords

# File lib/musical-chords.rb, line 222
def self.allowed_chords
  @@chords.keys
end
allowed_keys() click to toggle source

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
allowed_notes() click to toggle source

Returns the notes allowed as input to class methods

# File lib/musical-chords.rb, line 212
def self.allowed_notes
  @@note_transforms.keys
end
black_keys() click to toggle source

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
chord( base_note, chord_pattern ) click to toggle source

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
chord_from_name( key, chord_name ) click to toggle source

Returns the notes in the specifed key and chord type

Params:

# File lib/musical-chords.rb, line 349
def self.chord_from_name( key, chord_name )
  self.chord( key, @@chords[chord_name.downcase] )
end
chord_groups( params = { } ) click to toggle source

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
chromatic_scale( key ) click to toggle source

Returns an array of notes comprising the chromatic scale (all semi-tones) beginning at the root note of the key provided.

Params:

# File lib/musical-chords.rb, line 324
def self.chromatic_scale( key )
  self.chord( key, @@chords["chromatic-scale"] )
end
index_note( note ) click to toggle source

Returns a numeric pointer to the note provided within the @@notes class array

Params:

# File lib/musical-chords.rb, line 368
def self.index_note( note )
  return @@notes.index( @@note_transforms[ note.upcase ] )
end
major( base_note ) click to toggle source

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:

# File lib/musical-chords.rb, line 391
def self.major( base_note )
  self.chord( base_note, @@chords["maj"] )
end
major_scale( base_note ) click to toggle source

Returns a major scale : 1-2-3-4-5-6-7 with the given base_note (key)

Params:

# File lib/musical-chords.rb, line 400
def self.major_scale( base_note )
  self.chord( base_note, @@scales[:major] )
end
minor( base_note ) click to toggle source

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:

# File lib/musical-chords.rb, line 410
def self.minor( base_note )
  self.chord( base_note, @@chords["min"]  )
end
minor_scale( base_note ) click to toggle source

Returns a manor scale : 1-2-3-4-5-6-7 with the given base_note (key)

Params:

# File lib/musical-chords.rb, line 419
def self.minor_scale( base_note )
  self.chord( base_note, @@scales[:minor] )
end
name_note( key, note ) click to toggle source

Returns the note as a flat or sharp based upon the given key

Params:

# 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
name_notes( key, notes = [] ) click to toggle source

Converts an array of notes to flats or sharps based upon the given key

Params:

# File lib/musical-chords.rb, line 264
def self.name_notes( key, notes = [] )
  notes.map { |n|
    name_note( key, n )
  }
end
normalize_name( base_note ) click to toggle source

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
normalize_note( note ) click to toggle source

Converts the given note into a normalized form. THis method is used primarly for chord manipulation.

Params:

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
note_as_flat( note ) click to toggle source

Returns the note as a flat note.

Params:

# File lib/musical-chords.rb, line 240
def self.note_as_flat( note )
  @@flat_notes[ @@notes.index( @@note_transforms[note] ) ]
end
note_as_sharp( note ) click to toggle source

Returns the note as a sharp note.

Params:

# File lib/musical-chords.rb, line 231
def self.note_as_sharp( note )
  @@sharp_notes[ @@notes.index( @@note_transforms[note] ) ]
end
note_from_offset( base_note, offset, params = { } ) click to toggle source

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
note_position( key, note ) click to toggle source

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:

# File lib/musical-chords.rb, line 277
def self.note_position( key, note )
  chromatic_scale( key ).index( @@note_transforms[note] )
end
note_positions( key, notes ) click to toggle source

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:

# File lib/musical-chords.rb, line 288
def self.note_positions( key, notes )
  notes.map do |note|
    note_position(key, note)
  end
end
note_to_html( note ) click to toggle source

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("#","&#9839;").gsub(/(\w)[bB]/) { $1 + "&#9837;" }
end
scales() click to toggle source

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
white_keys() click to toggle source

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