annotate Syncopation models/parameter_setter.py @ 42:121d0e1f1748

Added the R plots
author csong <csong@eecs.qmul.ac.uk>
date Thu, 23 Apr 2015 22:08:48 +0100
parents f5abd2e8cafe
children
rev   line source
csong@9 1 '''
csong@9 2 Author: Chunyang Song
csong@9 3 Institution: Centre for Digital Music, Queen Mary University of London
csong@9 4 '''
csong@9 5
csong@28 6 # Set the parameters: time-signature, subdivision-sequence, strong-beat-level; Lmax; weight-sequence
csong@28 7 # Important condition: Lmax needs to be no less than the length of subdivision-sequence and the length of weight-sequence
csong@28 8
csong@9 9
csong@9 10 # {'key': time-signature} :
csong@28 11 # {'value': [subdivision-sequence, theoretical beat-level represented by index in the subdivision-sequence list]}
csong@19 12 timeSignatureBase = {
csong@20 13 '2/2': [[1,2,2,2,2,2],1],
csong@20 14 '3/2': [[1,3,2,2,2,2],1],
csong@20 15 '4/2': [[1,2,2,2,2,2],1],
csong@20 16 '2/4': [[1,2,2,2,2,2],1],
csong@20 17 '3/4': [[1,3,2,2,2,2],1],
csong@20 18 '4/4': [[1,2,2,2,2,2],2],
csong@20 19 '5/4': [[1,5,2,2,2,2],1],
csong@20 20 '7/4': [[1,7,2,2,2,2],1],
csong@20 21 '3/8': [[1,3,2,2,2,2],1],
csong@20 22 '5/8': [[1,5,2,2,2,2],1],
csong@20 23 '6/8': [[1,2,3,2,2,2],1],
csong@20 24 '9/8': [[1,3,3,2,2,2],1],
csong@20 25 '12/8':[[1,2,2,3,2,2],2],
csong@9 26 }
csong@9 27
csong@28 28
csong@19 29 def add_time_signature(timeSignature, subdivisionSequence, beatLevel):
csong@19 30 if is_time_signature_valid(timeSignature,subdivisionSequence,beatLevel):
csong@19 31 if timeSignature in timesigBase:
csong@9 32 print 'This time-signature is existed already.'
csong@9 33 else:
csong@19 34 timeSignatureBase[timeSignature] = [subdivisionSequence, beatLevel]
csong@19 35 write_time_signature()
csong@9 36
csong@19 37 def update_time_signature(timeSignature, subdivisionSequence, beatLevel):
csong@19 38 if is_time_signature_valid(timeSignature,subdivisionSequence,beatLevel):
csong@19 39 if timeSignature in timeSignatureBase:
csong@19 40 print 'Original settings for ', timeSignature, ':',timeSignatureBase[timeSignature]
csong@19 41 timeSignatureBase[timeSignature] = [subdivisionSequence, beatLevel]
csong@19 42 print 'Changed into:',timeSignatureBase[timeSignature]
csong@19 43 write_time_signature()
csong@9 44
csong@19 45 def is_time_signature_valid(timeSignature, subdivisionSequence, beatLevel):
csong@9 46 isValid = False
csong@19 47 if ('/' not in timeSignature) or (not timeSignature.split('/')[0].isdigit()) or (not timeSignature.split('/')[1].isdigit()):
csong@9 48 print 'Error: invalid time-signature. Please indicate in the form of fraction, e.g. 4/4, 6/8 or 3/4.'
csong@19 49 elif subdivisionSequence != [s for s in subdivisionSequence if isinstance(s,int)]:
csong@9 50 print 'Error: invalid subdivision sequence. Please indicate in the form of list of numbers, e.g [1,2,2,2,2].'
csong@19 51 elif beatLevel >= len(subdivisionSequence):
csong@9 52 print 'Error: beat-level exceeds the range of subdivision sequence list.'
csong@9 53 else:
csong@9 54 isValid = True
csong@9 55 return isValid
csong@9 56
csong@19 57 def write_time_signature():
csong@9 58 import cPickle as pickle
csong@19 59 timeSigFile = open('TimeSignature.pkl', 'wb')
csong@19 60 pickle.dump(timeSignatureBase, timeSigFile)
csong@19 61 timeSigFile.close()
csong@9 62
csong@19 63 def read_time_signature():
csong@9 64 import cPickle as pickle
csong@19 65 timeSigFile = open('TimeSignature.pkl','rb')
csong@19 66 data = pickle.load(timeSigFile)
csong@9 67 return data
csong@19 68 timeSigFile.close()
csong@9 69
csong@19 70 def print_time_signature_base():
csong@19 71 data = read_time_signature()
csong@19 72 for timeSignature, settings in data.items():
csong@19 73 print timeSignature, settings
csong@9 74
csong@9 75
csong@28 76 def are_parameters_valid(Lmax, weightSequence, subdivisionSequence):
csong@28 77
csong@28 78 # is_Lmax_valid() checks:
csong@28 79 # 1. if Lmax is a non-negative integer
csong@28 80 # 2. if Lmax is higher than the length of weightSequence and subdivisionSequence
csong@28 81 def is_Lmax_valid():
csong@28 82 isValid = False
csong@28 83 if isinstance(Lmax,int) and Lmax > 0:
csong@33 84 if Lmax <= len(subdivisionSequence)-1:
csong@33 85 if Lmax <= len(weightSequence)-1:
csong@28 86 isValid = True
csong@28 87 else:
csong@33 88 print 'Error: Lmax exceeds the length of weight-sequence. Either reduce Lmax, or provide a new weight-sequence whose length is greater or equal to Lmax.'
csong@28 89 else:
csong@33 90 print 'Error: Lmax exceeds the length of subdivision-sequence. Either reduce Lmax, or extend subdivision-sequence through updating time-signature (refer to update_time_signature function).'
csong@28 91 else:
csong@28 92 print 'Error: Lmax needs to be a positive integer.'
csong@28 93 return isValid
csong@28 94
csong@28 95 # is_weight_sequence_valid() checks:
csong@28 96 # 1. weightSequence is a list of numbers
csong@28 97 # 2. the length of weightSequence is no less than Lmax
csong@28 98 def is_weight_sequence_valid():
csong@28 99 isValid = False
csong@28 100 if isinstance(weightSequence,list) and weightSequence == [i for i in weightSequence if isinstance(i,int)]:
csong@28 101 if len(weightSequence) >= Lmax:
csong@28 102 isValid = True
csong@28 103 else:
csong@28 104 print 'Error: the length of weight-sequence needs to be greater or equal to Lmax.'
csong@28 105 else:
csong@28 106 print 'Error: the weight-sequence needs to be a list of integers.'
csong@28 107 return isValid
csong@28 108
csong@28 109
csong@32 110 if is_weight_sequence_valid() and is_Lmax_valid():
csong@28 111 return True
csong@28 112 else:
csong@28 113 return False