diff Syncopation models/parameter_setter.py @ 9:c2843ef4de2c

changing filenames to Python conventions
author csong
date Fri, 03 Apr 2015 11:41:01 +0100
parents
children 9030967a05f8
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Syncopation models/parameter_setter.py	Fri Apr 03 11:41:01 2015 +0100
@@ -0,0 +1,90 @@
+'''
+Author: Chunyang Song
+Institution: Centre for Digital Music, Queen Mary University of London
+'''
+
+# 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 = {
+	'2/2': [[1,2,2,2,2],1],
+	'3/2': [[1,3,2,2,2],1],
+	'4/2': [[1,2,2,2,2],1],
+	'2/4': [[1,2,2,2,2],1],
+	'3/4': [[1,3,2,2,2],1],
+	'4/4': [[1,2,2,2,2],2],
+	'5/4': [[1,5,2,2,2],1],
+	'7/4': [[1,7,2,2,2],1],
+	'3/8': [[1,3,2,2,2],1],
+	'5/8': [[1,5,2,2,2],1],
+	'6/8': [[1,2,3,2,2],1],
+	'9/8': [[1,3,3,2,2],1],
+	'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:
+			print 'This time-signature is existed already.'
+		else:
+			timesigBase[timesig] = [subdivision_seq, beat_level]
+			writeTimesig()
+
+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 isTSValid(timesig, subdivision_seq, beat_level):
+	isValid = False
+	if ('/' not in timesig) or (not timesig.split('/')[0].isdigit()) or (not timesig.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)]:
+		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):
+		print 'Error: beat-level exceeds the range of subdivision sequence list.'
+	else:
+		isValid = True
+	return isValid
+
+def writeTimesig():
+	import cPickle as pickle
+	timesigFile = open('TimeSignature.pkl', 'wb')
+	pickle.dump(timesigBase, timesigFile)
+	timesigFile.close()
+
+def readTimesig():
+	import cPickle as pickle
+	timesigFile = open('TimeSignature.pkl','rb')
+	data = pickle.load(timesigFile)
+	return data
+	timesigFile.close()
+
+def viewTimesigBase():
+	data = readTimesig()
+	for timesig, settings in data.items():
+		print timesig, settings
+
+def set_L_max(number):
+	L_max = number
+
+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