diff options
| -rw-r--r-- | cadence.py | 2 | ||||
| -rw-r--r-- | grammar.py | 2 | ||||
| -rw-r--r-- | passage.py | 33 | ||||
| -rw-r--r-- | path.py | 53 | ||||
| -rw-r--r-- | render.py | 17 | ||||
| -rw-r--r-- | spec.py | 17 | ||||
| -rw-r--r-- | utils.py | 2 |
7 files changed, 96 insertions, 30 deletions
@@ -88,7 +88,7 @@ cad.function = { cad.path = { 'range': 2, 'figure': 'rampUp', - 'tilt': 'float', + 'direction': 'float', } cad.print() # = pprint(cad.features) @@ -103,7 +103,7 @@ class Phonology(object): path = {} path['range'] = choice(pathRule['range']) path['figure'] = choice(pathRule['figure']) - path['tilt'] = choice(pathRule['tilt']) + path['direction'] = choice(pathRule['direction']) return path @@ -1,9 +1,11 @@ -import rhythm 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)""" @@ -80,19 +82,33 @@ class Passage(object): spellout = [] # now read out nodes into list, with prolonged features replacing lexical features for node in self.tree.inorder: - next_lexeme = deepcopy(node.lexeme) + next_lexeme = node.lexeme # deepcopy(node.lexeme) 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','tilt']: + if feat in ['range','figure','direction']: next_lexeme.cadence.path[feat] = node.unify[feat] spellout.append(next_lexeme) self.spelling = spellout self.setMeter() + self.setFigure() + # self.setHarmony() return spellout + def setFigure(self): + print("\nsetFigure\n---------") + 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): durations = self.getDurations(self.spelling) @@ -165,10 +181,6 @@ class Passage(object): return bars, removedIndices, prolongedIndices - 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): @@ -210,10 +222,11 @@ class Passage(object): return newBars - def getDurations(self, realization): + + def getDurations(self, spelling): durations = [] anacruses = [] - for lexeme in realization: + for lexeme in spelling: # special extra bit before barline anacrusis = 0.0 boundaryEvents = [] @@ -286,4 +299,4 @@ class Passage(object): >>> root.postorder [Node(4), Node(5), Node(2), Node(3), Node(1)] -''' +'''
\ No newline at end of file @@ -0,0 +1,53 @@ +import math +from pprint import pprint +from utils import * + +figures = { + # sample continuous functions, apply scaling factor + 'flat': lambda x: 1, + 'rampUp': lambda x: x, + 'rampDown': lambda x: -x, + 'accUp': lambda x: math.sin(5*(x-1)/math.pi)+1, + 'accDown': lambda x: -(math.sin(5*(x-1)/math.pi)+1), + 'decUp': lambda x: math.sin(5*x/math.pi), + 'decDown': lambda x: -math.sin(5*x/math.pi), + 'triangleUp': lambda x: abs(((((x/2)+0.25)%(0.5))*4)-1), + 'triangleDown': lambda x: -abs(((((x/2)+0.25)%(0.5))*4)-1), + 'saddleUp': lambda x: math.sin(10*(x-0.5)/math.pi), + 'saddleDown': lambda x: -math.sin(10*x/math.pi), + 'spikeUp': lambda x: -(abs(math.sin(10*(x-0.5)/math.pi))-1), + 'spikeDown': lambda x: abs(math.sin(10*(x-0.5)/math.pi))-1, + 'zigZagDown': lambda x: (abs((((x/2)%(0.3333)+0.1667)*3)-1.0)-0.5)*2, + 'zigZagUp': lambda x: -(abs((((x/2)%(0.3333)+0.1667)*3)-1.0)-0.5)*2, + 'sawtooth1Up': lambda x: x%(1.0), # same as ramp except f(1.0)==0 + 'sawtooth1Down': lambda x: -x%(1.0), + 'sawtooth2Up': lambda x: (x%(0.5))*2, + 'sawtooth2Down': lambda x: -(x%(0.5))*2, + 'sawtooth3Up': lambda x: (x%(0.3333))*3, + 'sawtooth3Down': lambda x: -(x%(0.3333))*3, + # 'stepUp': just use if/then + # 'stepDn': just use if/then +# etc +} + +def makeOutline(pathFeatures, realizationFeatures): + outline = [] + print("realizationFeatures['feet']", flatten(realizationFeatures['feet'])) + cardinality = len(flatten(realizationFeatures['feet'])) + for i in range(cardinality): + positionRatio = i / cardinality + # calculate relative positions + pitch = figures[pathFeatures['figure']](positionRatio) + # scale by height + pitch = round(pitch * pathFeatures['range']) + # shift by direction + pitch += realizationFeatures['height'] + outline.append(pitch) + return outline + + # print('"feet"', feet) + # span = voice['span'][1] - voice['span'][0] + # proportional - also try sampling function evenly by # notes. + # for note in voice['notes']: + # position_ratio = (note[1] - voice['span'][0]) / span + @@ -22,12 +22,6 @@ class Renderer(object): for pair in incrementPairs: increments.extend(pair) - # print('increments =', increments) - # print("\nincrementPairs =", incrementPairs) - # print('passage.bars', passage.bars) - # print('passage.durations', passage.durations) - # print('passage.stations', passage.stations) - # split each lexeme in anacrusis and station, put them in a flat list splitLexemes = [] for i, lexeme in enumerate(passage.spelling): @@ -45,6 +39,8 @@ class Renderer(object): '_duration': incrementPairs[i][1], 'parent': lexeme, } + pickup['_pitches'] = lexeme.realization['outline'][:len(pickup['_offsets'])] + station['_pitches'] = lexeme.realization['outline'][len(pickup['_offsets']):] # for ligatures with multiple feet, add remaining feet, with internal offset if len(lexeme.realization['feet']) > 1: for index in range(1, len(lexeme.realization['feet'])): @@ -94,9 +90,9 @@ class Renderer(object): for k, duration in enumerate(durations): n = m21.note.Note() n.quarterLength = duration - n.pitch = m21.pitch.Pitch('C4') + n.pitch = m21.pitch.Pitch() + n.pitch.ps = 72.0 + increment['_pitches'][k] noteOffset = internalOffset+increment['_offsets'][k] - print(i,j,k, noteOffset) # measure.append(n) measure.insert(noteOffset, n) # if duration is positive, is station @@ -109,14 +105,13 @@ class Renderer(object): for k, duration in enumerate(durations): n = m21.note.Note() n.quarterLength = duration - n.pitch = m21.pitch.Pitch('C4') + n.pitch = m21.pitch.Pitch() + n.pitch.ps = 72.0 + increment['_pitches'][k] noteOffset = internalOffset+increment['_offsets'][k] - print(str(i)+" "+str(j)+" "+str(k)+" "+str(noteOffset)) # measure.append(n) measure.insert(noteOffset, n) internalOffset += abs(increment['_duration']) if i == 0: - # doesn't work in MuseScore at least... measure.padAsAnacrusis() measure.makeRests(fillGaps=False, timeRangeFromBarDuration=True) measures.show() @@ -61,22 +61,27 @@ functionRules = [ pathRules = [ { 'range': [2,3,4], - 'figure': ['rampUp', 'rampDown'], - 'tilt': ['float', 'dive'], + 'figure': ['zigZagUp', 'zigZagDown'], + 'direction': [-1, 0, 1], }, { 'range': [5,6,7], - 'figure': ['stepUp', 'stepDown'], - 'tilt': ['rise', 'fall'], + 'figure': ['rampUp'], + 'direction': [-4,-5,-6], + }, + { + 'range': [5,6,7], + 'figure': ['rampDown'], + 'direction': [4,5,6], }, ] anticipationRules = [ # ['ligature'], - # ['ligature','tilt'], + # ['ligature','direction'], # ['ligature','figure'], [], - ['tilt'], + ['direction'], ['figure'], ] @@ -20,7 +20,7 @@ def flatten(pool): if isinstance(v, list): res += flatten(v) else: - if isinstance(v, int) or isinstance(v, str): + if isinstance(v, int) or isinstance(v, str) or isinstance(v, float): res.append(v) return res |