Mercurial > hg > syncopation-dataset
changeset 19:9030967a05f8
Refactored parameter_setter, basic_functions. Halfway fixing parameter argument in LHL model.
author | csong <csong@eecs.qmul.ac.uk> |
---|---|
date | Fri, 03 Apr 2015 22:57:27 +0100 |
parents | 43fa04812800 |
children | b959c2acb927 |
files | Syncopation models/LHL.py Syncopation models/PRS.py Syncopation models/basic_functions.py Syncopation models/parameter_setter.py |
diffstat | 4 files changed, 93 insertions(+), 88 deletions(-) [+] |
line wrap: on
line diff
--- a/Syncopation models/LHL.py Fri Apr 03 18:38:10 2015 +0100 +++ b/Syncopation models/LHL.py Fri Apr 03 22:57:27 2015 +0100 @@ -30,7 +30,11 @@ for a in range(len(subBinarySequences)): recursive_tree(subBinarySequences[a], subdivisionSequence, weightSequence, subWeightSequences[a], level+1) -def get_syncopation(bar, weightSequence = None, LMax = None): +def are_parameters_valid(parameters): + areValid = False + if 'Lmax' not in parameters : + +def get_syncopation(bar, parameters = None): ''' The get_syncopation function calculates syncopation value . ''' @@ -43,10 +47,17 @@ if get_rhythm_category(binarySequence, subdivisionSequence) == 'poly': print 'Warning: LHL model detects polyrhythms so returning None.' else: - if weightSequence == None: - if LMax == None: - LMax = 5 - weightSequence = range(0,-LMax,-1) + # If the parameters are not given, use the default settings + if parameters == None: + Lmax = 5 + weightSequence = range(0,-Lmax,-1) + else: + if are_parameters_valid(parameters): + Lmax = parameters['Lmax'] + weightSequence = parameters['W'] + else: + pass + #raise InvalidParameterError # If there is rhythm in previous bar, process its tree structure if bar.get_previous_bar() != None:
--- a/Syncopation models/PRS.py Fri Apr 03 18:38:10 2015 +0100 +++ b/Syncopation models/PRS.py Fri Apr 03 22:57:27 2015 +0100 @@ -33,7 +33,7 @@ return normalised #def get_syncopation(seq,subdivision_seq, postbar_seq, rhythm_category): -def get_syncopation(bar, weightSequence = None, LMax = None): +def get_syncopation(bar, parameters = None): ''' The get_syncopation function calculates the overall syncopation value for a bar of sequence. '''
--- a/Syncopation models/basic_functions.py Fri Apr 03 18:38:10 2015 +0100 +++ b/Syncopation models/basic_functions.py Fri Apr 03 22:57:27 2015 +0100 @@ -93,53 +93,53 @@ # The get_H returns a sequence of metrical weight for a certain metrical level (horizontal), # given the sequence of metrical weights in a hierarchy (vertical) and a sequence of subdivisions. -def get_H(weight_seq,subdivision_seq, level): +def get_H(weightSequence,subdivisionSequence, level): H = [] #print len(weight_seq), len(subdivision_seq), level - if (level <= len(subdivision_seq)-1) & (level <= len(weight_seq)-1): + if (level <= len(subdivisionSequence)-1) and (level <= len(weightSequence)-1): if level == 0: - H = repeat([weight_seq[0]],subdivision_seq[0]) + H = repeat([weightSequence[0]],subdivisionSequence[0]) else: - H_pre = get_H(weight_seq,subdivision_seq,level-1) + H_pre = get_H(weightSequence,subdivisionSequence,level-1) for h in H_pre: - H = concatenate(H, concatenate([h], repeat([weight_seq[level]],subdivision_seq[level]-1))) + H = concatenate(H, concatenate([h], repeat([weightSequence[level]],subdivisionSequence[level]-1))) else: print 'Error: a subdivision factor or metrical weight is not defined for the request metrical level.' return H -# The get_subdivision_seq function returns the subdivision sequence of several common time-signatures defined by GTTM, -# or ask for the top three level of subdivision_seq manually set by the user. -def get_subdivision_seq(timesig, L_max): - subdivision_seq = [] +# # The get_subdivision_seq function returns the subdivision sequence of several common time-signatures defined by GTTM, +# # or ask for the top three level of subdivision_seq manually set by the user. +# def get_subdivision_seq(timesig, L_max): +# subdivision_seq = [] - if timesig == '2/4' or timesig == '4/4': - subdivision_seq = [1,2,2] - elif timesig == '3/4' or timesig == '3/8': - subdivision_seq = [1,3,2] - elif timesig == '6/8': - subdivision_seq = [1,2,3] - elif timesig == '9/8': - subdivision_seq = [1,3,3] - elif timesig == '12/8': - subdivision_seq = [1,4,3] - elif timesig == '5/4' or timesig == '5/8': - subdivision_seq = [1,5,2] - elif timesig == '7/4' or timesig == '7/8': - subdivision_seq = [1,7,2] - elif timesig == '11/4' or timesig == '11/8': - subdivision_seq = [1,11,2] - else: - print 'Time-signature',timesig,'is undefined. Please indicate subdivision sequence for this requested time-signature, e.g. [1,2,2] for 4/4 meter.' - for i in range(3): - s = int(input('Enter the subdivision factor at metrical level '+str(i)+':')) - subdivision_seq.append(s) +# if timesig == '2/4' or timesig == '4/4': +# subdivision_seq = [1,2,2] +# elif timesig == '3/4' or timesig == '3/8': +# subdivision_seq = [1,3,2] +# elif timesig == '6/8': +# subdivision_seq = [1,2,3] +# elif timesig == '9/8': +# subdivision_seq = [1,3,3] +# elif timesig == '12/8': +# subdivision_seq = [1,4,3] +# elif timesig == '5/4' or timesig == '5/8': +# subdivision_seq = [1,5,2] +# elif timesig == '7/4' or timesig == '7/8': +# subdivision_seq = [1,7,2] +# elif timesig == '11/4' or timesig == '11/8': +# subdivision_seq = [1,11,2] +# else: +# print 'Time-signature',timesig,'is undefined. Please indicate subdivision sequence for this requested time-signature, e.g. [1,2,2] for 4/4 meter.' +# for i in range(3): +# s = int(input('Enter the subdivision factor at metrical level '+str(i)+':')) +# subdivision_seq.append(s) - if L_max > 2: - subdivision_seq = subdivision_seq + [2]*(L_max-2) - else: - subdivision_seq = subdivision_seq[0:L_max+1] +# if L_max > 2: +# subdivision_seq = subdivision_seq + [2]*(L_max-2) +# else: +# subdivision_seq = subdivision_seq[0:L_max+1] - return subdivision_seq +# return subdivision_seq def get_rhythm_category(velocitySequence, subdivisionSequence):
--- a/Syncopation models/parameter_setter.py Fri Apr 03 18:38:10 2015 +0100 +++ b/Syncopation models/parameter_setter.py Fri Apr 03 22:57:27 2015 +0100 @@ -5,11 +5,9 @@ # Set the parameters: subdivision_seq, weight_seq, L_max, strong_beat_level -L_max = 5 - # {'key': time-signature} : # {'value': [subdivision_seq, theoretical beat-level represented by index in the subdivision_seq list]} -timesigBase = { +timeSignatureBase = { '2/2': [[1,2,2,2,2],1], '3/2': [[1,3,2,2,2],1], '4/2': [[1,2,2,2,2],1], @@ -25,66 +23,62 @@ '12/8':[[1,2,2,3,2],2], } - -def addTimesig(timesig, subdivision_seq, beat_level): - if isTSValid(timesig,subdivision_seq,beat_level): - if timesig in timesigBase: +def add_time_signature(timeSignature, subdivisionSequence, beatLevel): + if is_time_signature_valid(timeSignature,subdivisionSequence,beatLevel): + if timeSignature in timesigBase: print 'This time-signature is existed already.' else: - timesigBase[timesig] = [subdivision_seq, beat_level] - writeTimesig() + timeSignatureBase[timeSignature] = [subdivisionSequence, beatLevel] + write_time_signature() -def updateTimesig(timesig, subdivision_seq, beat_level): - if isTSValid(timesig,subdivision_seq,beat_level): - if timesig in timesigBase: - print 'Original settings for', timesig, ':',timesigBase[timesig] - timesigBase[timesig] = [subdivision_seq, beat_level] - print 'Changed into:',timesigBase[timesig] - writeTimesig() +def update_time_signature(timeSignature, subdivisionSequence, beatLevel): + if is_time_signature_valid(timeSignature,subdivisionSequence,beatLevel): + if timeSignature in timeSignatureBase: + print 'Original settings for ', timeSignature, ':',timeSignatureBase[timeSignature] + timeSignatureBase[timeSignature] = [subdivisionSequence, beatLevel] + print 'Changed into:',timeSignatureBase[timeSignature] + write_time_signature() -def isTSValid(timesig, subdivision_seq, beat_level): +def is_time_signature_valid(timeSignature, subdivisionSequence, beatLevel): isValid = False - if ('/' not in timesig) or (not timesig.split('/')[0].isdigit()) or (not timesig.split('/')[1].isdigit()): + if ('/' not in timeSignature) or (not timeSignature.split('/')[0].isdigit()) or (not timeSignature.split('/')[1].isdigit()): print 'Error: invalid time-signature. Please indicate in the form of fraction, e.g. 4/4, 6/8 or 3/4.' - elif subdivision_seq != [s for s in subdivision_seq if isinstance(s,int)]: + elif subdivisionSequence != [s for s in subdivisionSequence if isinstance(s,int)]: print 'Error: invalid subdivision sequence. Please indicate in the form of list of numbers, e.g [1,2,2,2,2].' - elif beat_level >= len(subdivision_seq): + elif beatLevel >= len(subdivisionSequence): print 'Error: beat-level exceeds the range of subdivision sequence list.' else: isValid = True return isValid -def writeTimesig(): +def write_time_signature(): import cPickle as pickle - timesigFile = open('TimeSignature.pkl', 'wb') - pickle.dump(timesigBase, timesigFile) - timesigFile.close() + timeSigFile = open('TimeSignature.pkl', 'wb') + pickle.dump(timeSignatureBase, timeSigFile) + timeSigFile.close() -def readTimesig(): +def read_time_signature(): import cPickle as pickle - timesigFile = open('TimeSignature.pkl','rb') - data = pickle.load(timesigFile) + timeSigFile = open('TimeSignature.pkl','rb') + data = pickle.load(timeSigFile) return data - timesigFile.close() + timeSigFile.close() -def viewTimesigBase(): - data = readTimesig() - for timesig, settings in data.items(): - print timesig, settings +def print_time_signature_base(): + data = read_time_signature() + for timeSignature, settings in data.items(): + print timeSignature, settings -def set_L_max(number): - L_max = number +# def get_subdivision_seq(timeSignature): +# if timeSignature in readTimesig(): +# return timesigBase[timesig][0] +# else: +# print 'Error: the subdivision sequence for this time-signature is not defined.' +# return None -def get_subdivision_seq(timesig): - if timesig in readTimesig(): - return timesigBase[timesig][0] - else: - print 'Error: the subdivision sequence for this time-signature is not defined.' - return None - -def get_beat_level(timesig): - if timesig in readTimesig(): - return timesigBase[timesig][1] - else: - print 'Error: the subdivision sequence for this time-signature is not defined.' - return None +# def get_beat_level(timesig): +# if timesig in readTimesig(): +# return timesigBase[timesig][1] +# else: +# print 'Error: the subdivision sequence for this time-signature is not defined.' +# return None