diff options
| -rw-r--r-- | cadence.py | 6 | ||||
| -rw-r--r-- | grammar.py | 8 | ||||
| -rw-r--r-- | lexicon.py | 1 | ||||
| -rw-r--r-- | main.py | 20 | ||||
| -rw-r--r-- | passage.py | 217 | ||||
| -rw-r--r-- | spec.py | 57 |
6 files changed, 277 insertions, 32 deletions
@@ -67,17 +67,17 @@ cad.footprint = [{ 'span': 2, 'gait': 'double', # Gait.double or Gait['double']... 'foot': [-1/2, -1/4, 0, 1/2], - 'step': [0, 0, 0, 1/2], + 'sync': [0, 0, 0, 1/2], },{ 'span': 1, 'gait': 'double', 'foot': [None], - 'step': [None], + 'sync': [None], },{ 'span': 1, 'gait': 'double', 'foot': [-1/2, 0, 1/4, 1/2], - 'step': [1/2, 0, 0, 0], + 'sync': [1/2, 0, 0, 0], }] cad.function = { @@ -47,7 +47,7 @@ class Phonology(object): def makeFootprint(self): # select footprint rule - weights = array([rule[1] for rule in self.functions]) + weights = array([rule[1] for rule in self.footprints]) normalized_weights = weights.astype(float) / weights.sum() footprintChoices = array(self.footprints) index = choice(len(footprintChoices), p=normalized_weights) @@ -65,9 +65,9 @@ class Phonology(object): foot['span'] = choice(footRule['span']) foot['gait'] = choice(footRule['gait']) # timing options are per note: - foot['step'] = [] - for steps in footRule['step']: - foot['step'].append( choice(steps) ) + foot['sync'] = [] + for steps in footRule['sync']: + foot['sync'].append( choice(steps) ) footprint.append(foot) return footprint @@ -61,6 +61,7 @@ class Lexeme(object): self.label = 'unlabeled' + def print(self): print("\n"+str(self.label)) self.cadence.print() @@ -1,6 +1,7 @@ from grammar import * from lexicon import * from passage import * +from render import * from spec import * ''' @@ -15,27 +16,32 @@ phonology.py distribution of cadential features for given grammar lexicon.py repository of lexemes: (cadence, dependencies, prolongation) passage.py lexemes unified over binary tree spellout() sequence of unified cadences in passage (for each voice) -score.py list of n passages rendered to music21 score object +render.py list of n passages rendered to music21 score object export() to xml, midi, ly' ''' # lexicon.addCategory() -g.phonology.print() -g.morphology.print() +# g.phonology.print() +# g.morphology.print() lexicon = Lexicon() lexicon.populate(g, 4) lexicon.print() -passage = Passage(size=12) +passage = Passage(height=5, size=23) realization = passage.spellout(lexicon) -passage.printSyntax() +# passage.printSyntax() + +# for lex in realization: +# lex.print() + +passage.setMeter() +print(passage.tree) +print(passage.bars) -for lex in realization: - lex.print() # next: go through spellout, create musical data, append to music21 score # then: add good lexemes, dependency of good lexeme pairs to lexicon pre-population, iteratively... @@ -1,4 +1,7 @@ +import rhythm from binarytree import * +from utils import * +from math import ceil, floor from copy import copy, deepcopy class Passage(object): @@ -17,6 +20,10 @@ class Passage(object): node.value = i node.unify = {} self.unified = [] + self.durations = [] + self.anacruses = [] + self.landings = [] + self.bars = [] def printSyntax(self): print(self.tree) @@ -64,6 +71,208 @@ class Passage(object): return spellout + def setMeter(self): + durations = self.getDurations(self.unified) + pickupBar = max([fl(ceil(-self.anacruses[0])),2.0]) + bars = [] + for i in range(len(self.durations)-1): + bars.append(self.landings[i]-self.anacruses[i+1]) + bars.append(self.landings[-1]) + # partitionedBars = self.partition(bars, self.tree) + # depthScores = (flatten(depth(partitionedBars,0))) + # depthTransitions = [abs(depthScores[i]-depthScores[i+1]) for i in range(len(depthScores)-1) ] + + print('bars',bars) + # print('depthScores',depthScores) + removedIndices = [] + while isOversliced(bars): + bars, removedIndices = self.desliceBars(bars, removedIndices) + + print('bars',bars) + removedIndices.sort() + removedIndices.reverse() + print('removedIndices',removedIndices) + for index in removedIndices: + bars.pop(index) + print('bars',bars) + + bars.insert(0, pickupBar) + self.bars = bars + return bars + + + def desliceBars(self, bars, removedIndices): + # targets = [bar < 2.5 for bar in bars] + # i = targets.index(True) + targets = [] + for b, bar in enumerate(bars): + if bar < 2.5: + 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 + + print('targets', targets) + print('max(targets)', max(targets), 'targets.index(max(targets))', targets.index(max(targets))) + scores = [node.size for node in self.tree.inorder] + print('scores',scores) + if bars[i] < 2.5: + 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] < 6 and bars[i]+bars[prev] > 6: + right = True + elif bars[i]+bars[next] > 6 and bars[i]+bars[prev] < 6: + right = False + else: + if scores[i] > scores[prev]: + right = True + else: + right = False + + if right == True: + bars[i] += bars[next] + # bars.pop(next) + removedIndices.append(next) + else: + bars[i] += bars[prev] + # bars.pop(prev) + removedIndices.append(prev) + return bars, removedIndices + + def consolidate(self, bars): + # [[[3.0, [2.5, 2.5, [5.0, 4.5]]], [2.5, [5.0, 7.0], [7.0, 4.5]]], [3.0, [5.0, [4.5, 5.0, 7.0, 5.0]], [5.0, [[4.5, 5.0], [7.0, 7.0, 4.5]], 2.0]]] + for component in bars: + pass + + 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 incrementalSetMeter(self, durations, anacruses, landings): + firstChunk = -anacruses[0] + firstBar = ceil(firstChunk) + bars = [firstBar] + load = 0 + partitions = [] + nextPartition = [] + for i, duration in enumerate(durations): + nextChunk = landings[i] + if (i+1) < len(landings): + nextChunk -= anacruses[i+1] + load += nextChunk + nextPartition.append(duration) + if load >= 3: + bars.append(load) + partitions.append(nextPartition) + load = 0 + nextPartition = [] + return bars, partitions + + def getDurations(self, unified): + durations = [] + anacruses = [] + for lexeme in unified: + # special extra bit before barline + anacrusis = 0.0 + boundaryEvents = [] + metricBoundaries = [] + subdurations = [] + for i, foot in enumerate(lexeme.cadence.footprint): + offsets = rhythm.getOffsets(foot) + firstAttack = offsets[0] + lastAttack = offsets[-1] + boundaryEvents.append( (firstAttack, offsets[-1]) ) + + leadingBoundary = 0 + if firstAttack < 0: + leadingBoundary = floor((firstAttack*2)) / 2 + # leadingBoundary = floor(firstAttack) # use this version for whole-beat measures (1 of 3) + + trailingBoundary = 0 + if lastAttack > 0: + trailingBoundary = ceil((lastAttack*2)) / 2 + # trailingBoundary = ceil(lastAttack) # use this version for whole-beat measures (2 of 3) + + # give last note some time to sustain + if lastAttack - floor(lastAttack) == 0: + trailingBoundary += 1 + # comment below out in case of whole-beat measures (3 of 3) + if lastAttack - floor(lastAttack) == 0.5: + trailingBoundary += 0.5 + + # 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(sum(subdurations)) + + # print('boundaryEvents', boundaryEvents) + # print('metricBoundaries', metricBoundaries) + # print('subdurations', subdurations) + + # print('durations', durations) + # print('anacruses', anacruses) + self.durations = durations + self.anacruses = anacruses + self.landings = [self.durations[i] + self.anacruses[i] for i in range(len(durations))] + return durations + + ''' >>> print(root) @@ -86,12 +295,4 @@ class Passage(object): >>> root.postorder [Node(4), Node(5), Node(2), Node(3), Node(1)] - - -{scaffold, configuration, shell, backbone, spine, projection} - -# example -dee = Passage() -dee.print() - ''' @@ -5,25 +5,57 @@ footprintRules = [ 'span': [2], 'gait': ['double'], 'foot': [[-1/2, 0, 1/2]], - 'step': [[0, 1/2], [0, 1/2], [0, 1/2]], + 'sync': [[0, 1/2], [0, 1/2], [0, 1/2]], }], [{ - 'span': [4], - 'gait': ['triple'], + 'span': [1], + 'gait': ['trp_ls'], 'foot': [[-1/2, 0, 1/2]], - 'step': [[0], [1/2], [0]], + 'sync': [[0], [0], [0]], + }], + [{ + 'span': [2], + 'gait': ['double'], + 'foot': [[-1/2, -1/4, 0, 1/2]], + 'sync': [[0, 1/4], [0, 1/4], [0, 1/4], [0, 1/4]], + }], + [{ + 'span': [1], + 'gait': ['trp_sl'], + 'foot': [[-1/2, -1/4, 0, 1/4]], + 'sync': [[0], [0], [0], [0]], + }], + [{ + 'span': [1], + 'gait': ['double'], + 'foot': [[-1/2, -1/4, 0, 1/2]], + 'sync': [[0], [0], [0], [0]], + }], + [{ + 'span': [2], + 'gait': ['trp_ll'], + 'foot': [[-1/2, -1/4, 0, 1/4]], + 'sync': [[0, 1/4], [0, 1/4], [0, 1/4], [0, 1/4]], }], ] functionRules = [ { 'address': ['A1','B1'], - 'transposition': [-1], + 'transposition': [-1,0], }, { 'address': ['C1','D1'], - 'transposition': [0], + 'transposition': [0,1], }, + # { + # 'address': ['D2','C2'], + # 'transposition': [0,1], + # }, + # { + # 'address': ['E1','E2'], + # 'transposition': [0,1], + # }, ] pathRules = [ @@ -40,17 +72,22 @@ pathRules = [ ] anticipationRules = [ - ['footprint'], - ['tilt'], + # ['footprint'], + # ['footprint','tilt'], + # ['footprint','figure'], [], + ['tilt'], + ['figure'], ] prolongationRules = [ - ['address','transposition'], + ['address','figure','range'], + ['address','figure'], + ['address','range'], ['address'], ['figure','range'], + ['figure'], ['range'], - ['range','tilt'], ] # assemble grammar object |