-
Notifications
You must be signed in to change notification settings - Fork 0
/
counterpoint.py
68 lines (63 loc) · 2.63 KB
/
counterpoint.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
__author__ = 'halley'
import probabilityhelpers as ph
import random
import scale as sc
from chunk import *
import pitchhelpers as pth
import accompaniment as accomp
#generate a counterpoint
def genCounter(main_passage, prev_note = 0):
new_cells = []
for main_cell in main_passage.cells: #loop through main cells
if main_cell.chord == None or main_cell.chord == []:
new_pits = []
for pit in main_cell.pits:
new_pits.append(pth.getClosestPCDegree(prev_note, pit, low = -5, high = 14))
prev_note = new_pits[-1]
new_cells.append(Chunk(pits = new_pits, durs = main_cell.durs, key=main_cell.key))
else:
durs = []
pits = []
#use random number to choose what durs
ran = random.uniform(0,1)
if ran < 0.2:
durs = [1.0,1.0]
elif ran < 0.5:
durs = [1.5,0.5]
else:
durs = [2.0]
for i in range(0, len(durs)):
pits.append(sc.closestNoteDegreeInChord(prev_note, main_cell.chord, same=False, low = -3, high = 14))
prev_note = pits[-1]
new_cells.append(Chunk(pits=pits, durs=durs, key = main_cell.key, chord=main_cell.chord))
return new_cells
def genCounterAndAlberti(main_passage, prev_note = 0):
new_cells = []
for main_cell in main_passage.cells: #loop through main cells
if main_cell.chord == None or main_cell.chord == []:
new_pits = []
for pit in main_cell.pits:
new_pits.append(pth.getClosestPCDegree(prev_note, pit, low = -5, high = 14))
prev_note = new_pits[-1]
new_cells.append(Chunk(pits = new_pits, durs = main_cell.durs, key=main_cell.key))
else:
durs = []
pits = []
#use random number to choose what durs
ran = random.uniform(0,1)
if ran < 0.2:
durs = [1.0,1.0]
elif ran < 0.2:
durs = [1.5,0.5]
elif ran < 0.6:
durs = [2.0]
else:
durs = [0.5,0.5,0.5,0.5]
if durs != [0.5,0.5,0.5,0.5]:
for i in range(0, len(durs)):
pits.append(sc.closestNoteDegreeInChord(prev_note, main_cell.chord, same=False, low = -3, high = 14))
prev_note = pits[-1]
else:
pits.extend(accomp.getAlbertiEighths(main_cell.chord, main_cell.pits, main_cell.durs))
new_cells.append(Chunk(pits=pits, durs=durs, key = main_cell.key, chord=main_cell.chord))
return new_cells