-
Notifications
You must be signed in to change notification settings - Fork 8
/
GenRythm.py
89 lines (75 loc) · 3.1 KB
/
GenRythm.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import random
import ttcommon.Config as Config
import ttcommon.Util.InstrumentDB as InstrumentDB
from ttcommon.Generation.GenerationConstants import GenerationConstants
class GenRythm:
def __init__(self):
self.instrumentDB = InstrumentDB.getRef()
def drumRythmSequence(self, instrumentName, nbeats, density, regularity):
rythmSequence = []
binSelection = []
downBeats = []
upBeats = []
countDown = 0
onsetTime = None
instrument = self.instrumentDB.instNamed[instrumentName]
if instrument.instrumentRegister == Config.PUNCH:
registerDensity = 0.5
downBeatRecurence = 4
downBeats = [x for x in GenerationConstants.DRUM_PUNCH_ACCENTS[
nbeats]]
for downBeat in downBeats:
upBeats.append(downBeat + Config.TICKS_PER_BEAT / 2)
if instrument.instrumentRegister == Config.LOW:
registerDensity = 1
downBeatRecurence = 4
downBeats = [x for x in GenerationConstants.DRUM_LOW_ACCENTS[
nbeats]]
for downBeat in downBeats:
upBeats.append(downBeat + Config.TICKS_PER_BEAT / 2)
if instrument.instrumentRegister == Config.MID:
registerDensity = .75
downBeatRecurence = 1
downBeats = [x for x in GenerationConstants.DRUM_MID_ACCENTS[
nbeats]]
for downBeat in downBeats:
upBeats.append(downBeat + Config.TICKS_PER_BEAT / 4)
if instrument.instrumentRegister == Config.HIGH:
registerDensity = 1.5
downBeatRecurence = 1
downBeats = [x for x in GenerationConstants.DRUM_HIGH_ACCENTS[
nbeats]]
for downBeat in downBeats:
upBeats.append(downBeat + Config.TICKS_PER_BEAT / 4)
realDensity = density * registerDensity
if realDensity > 1.:
realDensity = 1.
list = list(range(int(realDensity * len(downBeats))))
for i in list:
if random.random() < (regularity * downBeatRecurence) and \
binSelection.count(1) < len(downBeats):
binSelection.append(1)
else:
if binSelection.count(0) < len(downBeats):
binSelection.append(0)
else:
binSelection.append(1)
countDown = binSelection.count(1)
length = len(downBeats) - 1
for i in range(countDown):
ran1 = random.randint(0, length)
ran2 = random.randint(0, length)
randMin = min(ran1, ran2)
onsetTime = downBeats.pop(randMin)
rythmSequence.append(onsetTime)
length -= 1
length = len(upBeats) - 1
for i in range(len(binSelection) - countDown):
ran1 = random.randint(0, length)
ran2 = random.randint(0, length)
randMin = min(ran1, ran2)
onsetTime = upBeats.pop(randMin)
rythmSequence.append(onsetTime)
length -= 1
rythmSequence.sort()
return rythmSequence