summaryrefslogtreecommitdiff
path: root/grammar.py
blob: e90873064d0d046c1fbbe78f5a20893f42731d38 (plain)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
from cadence import *
from numpy import array
from numpy.random import choice, seed

class Grammar(object):

	def __init__(self, phonology=None, morphology=None, syntax=None):
		self.phonology = phonology
		self.morphology = morphology
		self.syntax = syntax



class Morphology(object):

	def __init__(self):
		self.anticipationRules = []
		self.prolongationRules = []

	def addAnticipationRule(self, projectedFeatures):
		self.anticipationRules.append(projectedFeatures)

	def addProlongationRule(self, projectedFeatures):
		self.prolongationRules.append(projectedFeatures)

	def selectAnticipation(self):
		return choice(self.anticipationRules)

	def selectProlongation(self):
		return choice(self.prolongationRules)

	def print(self):
		features = {
			'anticipations': self.anticipationRules,
			'prolongations': self.prolongationRules,
		}
		print("\nMorphology")
		pprint(features)
		print()


class Phonology(object):
	"""weighted repository of rhythmic, harmonic and gestural features, and feature dependencies"""

	def __init__(self, ligatures=[], functions=[], paths=[]):
		self.ligatures = ligatures
		self.functions = functions
		self.paths = paths

	def print(self):
		features = {
			'ligatures': self.ligatures,
			'functions': self.functions,
			'paths': self.paths,
		}
		print("\nPhonology")
		pprint(features)
		print()

	def addligatureRule(self, ligatureRule, weight=1):
		self.ligatures.append((ligatureRule, weight))

	def addFunctionRule(self, functionRule, weight=1):
		self.functions.append((functionRule, weight))

	def addPathRule(self, pathRule, weight=1):
		self.paths.append((pathRule, weight))

	def makeCadence(self): # (duration=None, harmonicFilter=None)
		""" assemble bundle of features from phonology rules """
		cadence = Cadence()
		cadence.ligature = self.makeligature()
		cadence.function = self.makeFunction()
		cadence.path = self.makePath()
		return cadence

	def makeligature(self):
		# select ligature rule
		weights = array([rule[1] for rule in self.ligatures])
		normalized_weights = weights.astype(float) / weights.sum()
		ligatureChoices = array(self.ligatures)
		index = choice(len(ligatureChoices), p=normalized_weights)
		ligatureRule = ligatureChoices[index][0]

		# select feature value from range where necessary
		ligature = []
		for footRule in ligatureRule:
			foot = {}
			# each foot option is composite
			footChoices = array(footRule['foot'])
			index = choice(len(footChoices))
			foot['foot'] = list(footChoices[index])
			# other feature options aren't
			foot['span'] = choice(footRule['span'])
			foot['gait'] = choice(footRule['gait'])
			# timing options are per note:
			foot['sync'] = []
			for steps in footRule['sync']:
				foot['sync'].append( choice(steps) )
			ligature.append(foot)

		return ligature


	def makeFunction(self):
		# select function rule
		weights = array([rule[1] for rule in self.functions])
		normalized_weights = weights.astype(float) / weights.sum()
		functionChoices = array(self.functions)
		index = choice(len(functionChoices), p=normalized_weights)
		functionRule = functionChoices[index][0]

		# select value from range for each feature
		function = {}
		function['address'] = choice(functionRule['address'])
		function['transposition'] = choice(functionRule['transposition'])

		return function


	def makePath(self):

		# select path rule
		weights = array([rule[1] for rule in self.paths])
		normalized_weights = weights.astype(float) / weights.sum()
		pathChoices = array(self.paths)
		index = choice(len(pathChoices), p=normalized_weights)
		pathRule = pathChoices[index][0]


		# select value from range for each feature
		path = {}
		path['range'] = choice(pathRule['range'])
		path['figure'] = choice(pathRule['figure'])
		path['direction'] = choice(pathRule['direction'])

		return path