Created
April 7, 2017 12:42
-
-
Save VieVie31/225cb1adc02943bda6dd20d618c8ab7d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from random import * | |
from psonic import * | |
#random major scale chord progression from the transitions allowed by: | |
#http://www.dummies.com/art-center/music/major-and-minor-chord-progressions-for-music-composition/ | |
def get_major_chord(base_note): | |
"""Take the base note and return a list of 3 notes in | |
the root position of the major chord of the base note. | |
:param base_note: the note to get the majo chord | |
:type base_note: int | |
:return: the list of notes in the major chord in the tooy position | |
:rtype: list(int) | |
""" | |
return [base_note, base_note + 4, base_note + 7] | |
def get_minor_chord(base_note): | |
"""Take the base note and return a list of 3 notes in | |
the root position of the minor chord of the base note. | |
:param base_note: the note to get the minor chord | |
:type base_note: int | |
:return: the list of notes in the minor chord in the tooy position | |
:rtype: list(int) | |
""" | |
return [base_note, base_note + 3, base_note + 7] | |
def get_diminished_chord(base_note): | |
"""Take the base note and return a list of 3 notes in | |
the root position of the diminished chord of the base note. | |
:param base_note: the note to get the diminished chord | |
:type base_note: int | |
:return: the list of notes in the diminished chord in the tooy position | |
:rtype: list(int) | |
""" | |
return [base_note, base_note + 3, base_note + 6] | |
class MajorDegree: | |
MAJOR = 0 | |
MINOR = 1 | |
DIMINISHED = 2 | |
MAJOR_CHORD_SEQUENCE = [ | |
None, | |
MAJOR, #I | |
MINOR, #ii | |
MINOR, #iii | |
MAJOR, #IV | |
MAJOR, #V | |
MINOR, #vi | |
DIMINISHED #vii° | |
] | |
def __init__(self, degree): | |
self.d = degree | |
def equals(self, other): | |
return self.d == other.d | |
def __eq__(self, other): | |
return self.equals(other) | |
def __hash__(self): | |
return self.d + 1000 * ord('M') | |
def __repr__(self): | |
return [None, 'I', 'ii', 'iii', 'IV', 'V', 'vi', 'vii°'][self.d] | |
def is_major(self): | |
return MajorDegree.MAJOR_CHORD_SEQUENCE[self.d] == MajorDegree.MAJOR | |
def is_minor(self): | |
return MajorDegree.MAJOR_CHORD_SEQUENCE[self.d] == MajorDegree.MINOR | |
def is_diminished(self): | |
return MajorDegree.MAJOR_CHORD_SEQUENCE[self.d] == MajorDegree.DIMINISHED | |
def get_note_relative_to_tonic(self, tonic): | |
major_scale = [None, 0, 2, 4, 5, 7, 9, 11] | |
return tonic + major_scale[self.d] | |
def get_chord(self, tonic): | |
n = self.get_note_relative_to_tonic(tonic) | |
if self.is_major(): | |
return get_major_chord(n) | |
elif self.is_minor(): | |
return get_minor_chord(n) | |
else: | |
return get_diminished_chord(n) | |
MD = MajorDegree | |
MAJOR_CHORD_PROGRESSION = { | |
MD(1) : [MD(1), MD(2), MD(3), MD(4), MD(5), MD(6), MD(7)], | |
MD(2) : [MD(1), MD(5), MD(7)], | |
MD(3) : [MD(1), MD(2), MD(4), MD(6)], | |
MD(4) : [MD(1), MD(2), MD(3), MD(5), MD(7)], | |
MD(5) : [MD(1), MD(6)], | |
MD(6) : [MD(1), MD(2), MD(3), MD(4), MD(5)], | |
MD(7) : [MD(1), MD(3)] | |
} | |
def play_chord(c): | |
for n in c: | |
play(n) | |
#make a random walk | |
tonic = C5 | |
md = MD(1) | |
print(md) | |
play_chord(md.get_chord(C5)) | |
sleep(1) | |
for i in range(10): | |
md = choice(MAJOR_CHORD_PROGRESSION[md]) | |
print(md) | |
play_chord(md.get_chord(C5)) | |
sleep(1) | |
#end with a perfect cadence... ;) | |
md = MD(5) | |
print(md) | |
play_chord(md.get_chord(C5)) | |
sleep(1) | |
md = MD(1) | |
print(md) | |
play_chord(md.get_chord(C5)) | |
sleep(1) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment