-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpatterns.py
More file actions
89 lines (75 loc) · 2.54 KB
/
patterns.py
File metadata and controls
89 lines (75 loc) · 2.54 KB
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
from pysc.time_management import Synth
from nodes import MusicNode
from time_management import read_buffer
class Pattern(object):
def __init__(self, pattern):
self.pattern = pattern
def note_for(self, beat):
index = beat.num % len(self.pattern)
return self.pattern[index]
class ProbabilityPattern(object):
def __init__(self, weights):
if isinstance(weights, basestring):
self.weights = []
for item in weights.split(" "):
self.weights.append(float(item))
else:
self.weights = weights
self.generate()
def tweak_weights(self, amount=0.2):
old = self.weights
self.weights = []
for weight in old:
if weight < 0 or weight > 1:
self.weights.append(weight)
continue
new = weight + (random.random() * amount - (amount / 2))
self.weights.append(new)
def generate(self):
self.pattern = []
for item in self.weights:
if random.random() < item:
new_dict = {'velocity':item}
else:
new_dict = None
self.pattern.append(new_dict)
def note_for(self, beat):
index = beat.num % len(self.pattern)
note = self.pattern[index]
if index == len(self.pattern) - 1:
self.generate()
return note
class PatternReader(object):
def __init__(self, filename):
in_file = open(filename, "r")
self.patterns = []
for line in in_file:
pattern = []
for char in line:
if char == '0':
pattern.append(True)
else:
pattern.append(False)
self.patterns.append(pattern)
print self.patterns
def num(self, num):
return Pattern(self.patterns[num-1])
class PatternPlayer(MusicNode):
def init(self, weights, filename, subdiv=4):
self.bufnum = read_buffer(filename)
self.pattern = ProbabilityPattern(weights)
self.subdiv = subdiv
self.freq = 10000
self.amp = 1
self.out_bus = 0
def plan(self, beat):
note = self.pattern.note_for(beat)
if not note:
return
synth = Synth("SampleHit")
synth.set(bufnum=self.bufnum)
synth.set(amp=self.amp * note.get('velociy', 1))
synth.set(freq=self.freq)
synth.set(out_bus=self.out_bus)
synth.play_at(beat.time)