Skip to content

Instantly share code, notes, and snippets.

@zonque
Last active August 29, 2015 14:10
Show Gist options
  • Save zonque/1d1c26952785ce6fc1e5 to your computer and use it in GitHub Desktop.
Save zonque/1d1c26952785ce6fc1e5 to your computer and use it in GitHub Desktop.
class Tone
NAMES = ["C", "Cis/Des", "D", "Dis/Es", "E", "F", "Fis/Ges", "G", "Gis/As", "A", "Ais/B", "H" ]
C = 1
Cis = 2
Des = 2
D = 3
Dis = 4
Es = 4
E = 5
F = 6
Fis = 7
Ges = 7
G = 8
Gis = 9
As = 9
A = 10
B = 11
Ais = 11
H = 12
def initialize(*args)
@n = args[0]
end
def transpose!(ht)
@n += ht + 12
@n %= 12
end
def transpose(ht)
Tone.new((@n + ht + 12) % 12, @gender)
end
def to_s
NAMES[@n - 1]
end
end
class Interval
Unison = 0
MinorSecond = 1
MajorSecond = 2
MinorThird = 3
MajorThird = 4
PerfectForth = 5
DiminishedFifth = 6
AugmentedForth = 6
PerfectFifth = 7
MinorSixth = 8
MajorSixth = 9
MinorSeventh = 10
MajorSeventh = 11
Octave = 12
end
class NoteArray
def initialize(base, gender)
@base = base
@gender = gender
@t = []
end
def transpose(ht)
self.class.new(@base.transpose(ht), @gender)
end
def transpose!(ht)
@t.each { |t| t.transpose!(ht) }
end
def to_s
@t.join(" - ")
end
end
class Triad < NoteArray
Major = 0
Minor = 1
Diminished = 2
Augmented = 3
def initialize(base, gender)
super base, gender
if @gender == Major
@t = [ @base.dup, @base.transpose(Interval::MajorThird), @base.transpose(Interval::MajorThird + Interval::MinorThird) ]
elsif @gender == Minor
@t = [ @base.dup, @base.transpose(Interval::MinorThird), @base.transpose(Interval::MinorThird + Interval::MajorThird) ]
elsif @gender == Diminished
@t = [ @base.dup, @base.transpose(Interval::MinorThird), @base.transpose(Interval::MinorThird + Interval::MinorThird) ]
elsif @gender == Augmented
@t = [ @base.dup, @base.transpose(Interval::MajorThird), @base.transpose(Interval::MajorThird + Interval::MajorThird) ]
end
end
end
class Scale < NoteArray
Major = 0
Minor = 1
def initialize(base, gender)
super base, gender
if @gender == Major
@ht = [ 3, 7 ]
elsif @gender == Minor
@ht = [ 2, 5 ]
end
t = @base.dup
@t = [ t ]
7.times do |n|
if @ht.include? (n + 1)
t = t.transpose(1)
else
t = t.transpose(2)
end
@t << t
end
end
def relative_key
if @gender == Scale::Major
Scale.new(@base.transpose(-Interval::MinorThird), Scale::Minor)
elsif @gender == Scale::Minor
Scale.new(@base.transpose(-Interval::MinorThird), Scale::Major)
end
end
end
n = Tone.new(Tone::C)
puts Triad.new(n, Triad::Augmented).transpose(1)
12.times do |i|
n = Tone.new(Tone::C + i)
puts Scale.new(n, Scale::Major)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment