Skip to content

Instantly share code, notes, and snippets.

@bikewheat
Created June 24, 2012 08:24
Show Gist options
  • Save bikewheat/2982448 to your computer and use it in GitHub Desktop.
Save bikewheat/2982448 to your computer and use it in GitHub Desktop.
Calculate chord by given Key (Ver 0.1)
require "Set"
require "./mode_scale.rb"
class GuitarChord
attr_accessor :key
@@scale = ["C","#C/bD","D","#D/bE","E","F","#F/bG","G","#G/bA","A","#A/bB","B"]
@@chord_hash = {
# Major chords
"maj" => [0,4,3],
"maj7" => [0,4,3,4],
"M7" => [0,4,3,3],
"add9" => [0,4,3,7],
"maj9" => [0,4,3,4,3],
"M9" => [0,4,3,3,4],
# Minor chords
"min" => [0,3,4]
}
def initialize(key)
@key = key
@chord = Set.new
end
def to_s
"KEY: #{key}"
end
def clac_chord(type)
pos = @@scale.index(key)
@chord.clear
for i in @@chord_hash[type]
pos += i
pos %= 12
@chord << @@scale[pos]
end
@chord.to_a
end
def maj ; clac_chord("maj"); end
def maj7; clac_chord("maj7"); end
def M7; clac_chord("M7"); end
def add9; clac_chord("add9"); end
def maj9; clac_chord("maj9"); end
def M9; clac_chord("M9"); end
def min; clac_chord("min"); end
def recmandMode
var = ModeScale.new(key,"Ionian")
end
end
# MAIN PROGRAM
a = GuitarChord.new("A")
p a.maj
p a.min
p a.add9
p a.maj9
=begin
# OUTPUT:
["A", "#C/bD", "E"]
["A", "C", "E"]
["A", "#C/bD", "E", "B"]
["A", "#C/bD", "E", "#G/bA", "B"]
=end
require "Set"
class ModeScale
attr_accessor :key, :mode
attr_reader :mode_scale
def initialize(key, mode)
@key, @mode = key, mode
showScale
end
def to_s
"KEY:#{@key} in #{@mode} \n" +
"Scale: #{@mode_scale.to_a}"
end
def showScale
scale = ["C","#C/bD","D","#D/bE","E","F","#F/bG","G","#G/bA","A","#A/bB","B"]
@mode_scale= Set.new
mode_hash = {
# 1 means a semitone and 2 means two semitone
"Ionian" => [0,2,2,1,2,2,2,1],
"Dorian" => [0,2,1,2,2,2,1,2],
"Phrygian" => [0,1,2,2,2,1,2,2],
"Lydian" => [0,2,2,2,1,2,2,1],
"Mixolydian"=> [0,2,2,1,2,2,1,2],
"Aeolian" => [0,2,1,2,2,1,2,2],
"Locrian" => [0,1,2,2,1,2,2,2]
}
# ==============================
pos = scale.index(key)
for i in (0..7)
pos += (mode_hash[mode]).at(i)
pos %= 12
@mode_scale << scale[pos]
end
# ==============================
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment