summaryrefslogtreecommitdiff
path: root/passage.py
blob: 7f288e6e7adf3ae6c1ab7e6911ad9ff6038175b2 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
from binarytree import *
from utils import *
from math import ceil, floor
from copy import copy, deepcopy
from pprint import pprint
import rhythm
import path
import harmony

class Passage(object):
	"""represents a syntactic structure (wrapper for binary tree)"""

	def __init__(self, height=3, size=None):
		self.tree = tree(height=height)
		if isinstance(size, int):
			if size>=height and size<((2**(height+1))-1):
				while self.tree.size != size:
					self.tree = tree(height=height)
			else:
				print('Syntax tree of height '+str(height)+' must have between '+str(height+1)+' and '+str((2**(height+1))-1)+' nodes, but you asked for '+str(size)+' nodes.')
		# relabel nodes in level order
		for i, node in enumerate(self.tree.levelorder):
			node.value = i
			node.unify = {}

		# sequence of lexeme objects constituting passage
		self.spelling = []

		# duration of each individual foot in each ligature (including any pickup)
		self.durations = []

		# duration of the pre-barline portion of each lexeme 
		self.anacruses = []

		# duration of the post-barline portion of each lexeme
		self.stations = []

		self.bars = []

	def __str__(self):
		passage = []
		for i, lex in enumerate(self.spelling):
			passage.append(str(i)+": "+str(lex)),
		return "\n".join(passage)

	def printSyntax(self):
		print(self.tree)

	def print(self):
		lexemes = []
		for lex in self.spelling:
			lexemes.append({
				'_label': lex.label,
				'ligature': lex.cadence.ligature,
				'function': lex.cadence.function,
				'path': lex.cadence.path,
				'realization': lex.realization,
			})
		pprint(lexemes)

	def spellout(self, lexicon):
		self.lexicon=lexicon
		# prolonged features trickle down to leaves
		for node in self.tree.levelorder:
			node.lexeme = lexicon.selectLexeme()
			node.value = node.lexeme.label

			if node.left:
				node.left.unify = copy(node.unify)
				for feat in node.lexeme.government['anticipation']:
					# if feat == 'ligature': <- take off first condition for 'agree' prolongation
					if feat not in node.left.unify.keys() and feat == 'ligature':
						node.left.unify['ligature'] = node.lexeme.cadence.ligature
					if feat not in node.left.unify.keys() and feat in node.lexeme.cadence.function:
						node.left.unify[feat] = node.lexeme.cadence.function[feat]
					if feat not in node.left.unify.keys() and feat in node.lexeme.cadence.path:
						node.left.unify[feat] = node.lexeme.cadence.path[feat]

			if node.right:
				node.right.unify = copy(node.unify)
				for feat in node.lexeme.government['prolongation']:
					if feat not in node.right.unify.keys() and feat == 'ligature':
						node.right.unify['ligature'] = node.lexeme.cadence.ligature
					if feat not in node.right.unify.keys() and feat in node.lexeme.cadence.function:
						node.right.unify[feat] = node.lexeme.cadence.function[feat]
					if feat not in node.right.unify.keys() and feat in node.lexeme.cadence.path:
						node.right.unify[feat] = node.lexeme.cadence.path[feat]

		spellout = []
		# read out nodes into list
		for node in self.tree.inorder:
			next_lexeme = node.lexeme # deepcopy(node.lexeme)
			# prolonged features replace lexical features, unless lexeme projects that feature itself
			reprojectedFeatures = []
			for feat in node.unify.keys():
				if feat in next_lexeme.government['prolongation'] or next_lexeme.government['anticipation']:
					reprojectedFeatures.append(feat)
			for feat in reprojectedFeatures:
				del node.unify[feat]
			for feat in node.unify.keys():
				if feat == 'ligature':
					next_lexeme.cadence.ligature = node.unify[feat]
				if feat in ['address', 'transposition']:
					next_lexeme.cadence.function[feat] = node.unify[feat]
				if feat in ['range','figure','direction']:
					next_lexeme.cadence.path[feat] = node.unify[feat]
			spellout.append(next_lexeme)
		self.spelling = spellout
		self.setMeter(lexicon.minimumMetricUnit)
		self.setFigure()
		self.setHarmony()
		return spellout

	def setHarmony(self):
		for i, node in enumerate(self.tree.levelorder):
			node.lexeme.realization['lens'] = harmony.makeLens(node.lexeme.cadence.function, node.lexeme.cadence.ligature)

	def setFigure(self):
		for i, node in enumerate(self.tree.levelorder):
			if i == 0:
				node.lexeme.realization['height'] = 0
			if node.left:
				leadingDirection = node.left.lexeme.cadence.path['direction']
				node.left.lexeme.realization['height'] = node.lexeme.realization['height'] - leadingDirection
			if node.right:
				trailingDirection = node.right.lexeme.cadence.path['direction']
				node.right.lexeme.realization['height'] = node.lexeme.realization['height'] + trailingDirection
			node.lexeme.realization['outline'] = path.makeOutline(node.lexeme.cadence.path, node.lexeme.realization)

	def setMeter(self, minimumMetricUnit):
		durations = self.getDurations(self.spelling, minimumMetricUnit)
		pickupBar = max([fl(ceil(-self.anacruses[0])),2.0])
		bars = []
		for i in range(len(self.stations)-1):
			bars.append(self.stations[i]-self.anacruses[i+1])
		bars.append(self.stations[-1])
		# partitionedBars = self.partition(bars, self.tree)

		removedIndices = []
		prolongedIndices = []
		while isOversliced(bars):
			bars, removedIndices, prolongedIndices = self.desliceBars(bars, removedIndices, prolongedIndices)

		removedIndices.sort()
		removedIndices.reverse()
		for index in removedIndices:
			bars.pop(index)

		bars.insert(0, pickupBar)
		self.bars = bars
		return bars

	def desliceBars(self, bars, removedIndices, prolongedIndices):
		# targets = [bar < 2.5 for bar in bars]
		# i = targets.index(True)
		targets = []
		for b, bar in enumerate(bars):
			if bar > 0 and bar < 2.0:
				targets.append(self.tree.inorder[b].size)
			else:
				targets.append(0)

		i = targets.index(max(targets))
		prev = i-1
		next = i+1
		while prev in removedIndices:
			prev -= 1
		while next in removedIndices:
			next += 1

		scores = [node.size for node in self.tree.inorder]
		if bars[i] < 2.0:
			right = False
			if i == 0 or prev < 0:
				right = True
			elif i == len(bars)-1 or next > len(bars)-1:
				right = False
			else:
				if bars[i]+bars[next] <= 4.0 and bars[i]+bars[prev] > 4.0:
					right = True
				elif bars[i]+bars[next] > 4.0 and bars[i]+bars[prev] <= 4.0:
					right = False
				if bars[i]+bars[next] <= 5.0 and bars[i]+bars[prev] > 5.0:
					right = True
				elif bars[i]+bars[next] > 5.0 and bars[i]+bars[prev] <= 5.0:
					right = False
				if bars[i]+bars[next] <= 6.0 and bars[i]+bars[prev] > 6.0:
					right = True
				elif bars[i]+bars[next] > 6.0 and bars[i]+bars[prev] <= 6.0:
					right = False
				else:
					if (scores[i] > scores[prev]) and (next not in prolongedIndices):
						right = True
					else:
						right = False

			if right == True:
				bars[i] += bars[next]
				bars[next] = 0.0
				removedIndices.append(next)
				prolongedIndices.append(i)
			else:
				bars[i] += bars[prev]
				bars[prev] = 0.0
				removedIndices.append(prev)

		return bars, removedIndices, prolongedIndices


	def partition(self, bars, syntaxTree):

		if syntaxTree.left: 
			if syntaxTree.left.height > 0:
				splitAt = syntaxTree.inorder.index(syntaxTree.left.inorder[-1])
				leftBars = bars[0:splitAt+1]
				leftBars = self.partition(leftBars, syntaxTree.left)
			else:
				leftIndex = syntaxTree.inorder.index(syntaxTree.left.inorder[-1])
				leftBars = bars[leftIndex]
		else:
			leftBars = None

		if syntaxTree.right: 
			if syntaxTree.right.height > 0:
				splitAt = syntaxTree.inorder.index(syntaxTree.right.inorder[0])
				rightBars = bars[splitAt:]
				rightBars = self.partition(rightBars, syntaxTree.right)
				if isinstance(rightBars, float) or isinstance(rightBars, int):
					rightBars = [bars[splitAt-1], rightBars]
				else:
					rightBars.insert(0, bars[splitAt-1])
			else:
				rightIndex = syntaxTree.inorder.index(syntaxTree.right.inorder[0])
				rightBars = bars[rightIndex-1:rightIndex+1]
		else:
			# rightIndex = syntaxTree.inorder.index(syntaxTree.right.inorder[-1])
			rightBars = bars[-1]

		if leftBars != None and rightBars != None:
			newBars = [leftBars, rightBars]
		elif leftBars != None:
			newBars = leftBars
		elif rightBars != None:
			newBars = rightBars
		else:
			newBars = []

		return newBars


	def getDurations(self, spelling, minimumMetricUnit=0.5):
		durations = []
		anacruses = []
		denominator = 1/minimumMetricUnit
		for lexeme in spelling:
			# special extra bit before barline
			anacrusis = 0.0
			boundaryEvents = []
			metricBoundaries = []
			subdurations = []
			lexeme.realization['feet'] = []
			for i, foot in enumerate(lexeme.cadence.ligature):
				offsets = rhythm.getOffsets(foot)
				lexeme.realization['feet'].append(offsets)

				firstAttack = offsets[0]
				lastAttack = offsets[-1]
				boundaryEvents.append( (firstAttack, offsets[-1]) )

				leadingBoundary = 0
				if firstAttack < 0:
					leadingBoundary = floor((firstAttack*denominator)) / denominator

				trailingBoundary = 0
				if lastAttack > 0:
					trailingBoundary = ceil((lastAttack*denominator)) / denominator

				# give last note some time to sustain
				if lastAttack - floor(lastAttack) == 0:
					trailingBoundary += minimumMetricUnit

				# pad out to minimum duration
				if trailingBoundary < foot['span']:
					trailingBoundary = fl(foot['span'])

				metricBoundaries.append( (leadingBoundary, trailingBoundary) )
				subdurations.append(trailingBoundary - leadingBoundary)

				# remember anacrusis of first foot - barline will come after it
				if i == 0:
					anacruses.append(leadingBoundary)

			durations.append(subdurations)

		self.durations = durations
		self.anacruses = anacruses
		self.stations = [sum(self.durations[i]) + self.anacruses[i] for i in range(len(durations))]
		return durations

	def fitToInstrument(self, instrument):
		floor, ceiling = (instrument.lowestNote.ps, instrument.highestNote.ps)
		self.fitConstituent(self.tree, floor, ceiling)

	def fitConstituent(self, constituent, floor, ceiling):
		pitches = []
		for node in constituent.inorder:
			for i, target in enumerate(node.lexeme.realization['outline']):
				pitches.append(target + node.lexeme.realization['lens'][i])
		pitchReference = deepcopy(pitches)
		shift = 0
		print(pitchReference)
		while min(pitchReference) > floor+12:
			pitchReference = [pitch-12 for pitch in pitchReference]
			print(pitchReference)
			shift -= 12
		while min(pitchReference) < floor:
			pitchReference = [pitch+12 for pitch in pitchReference]
			print(pitchReference)
			shift += 12
		if max(pitchReference) > ceiling and constituent.left and constituent.right: # and constituent.left.min_leaf_depth>1 and constituent.right.min_leaf_depth>1:
			self.fitConstituent(constituent.left, floor, ceiling)
			self.fitConstituent(constituent.right, floor, ceiling)
		else:
			print(constituent)
			print(min(pitchReference), floor, max(pitchReference), ceiling)
			print(constituent.left)
			print(constituent.right)
			for node in constituent.inorder:
				node.lexeme.realization['shift'] = shift