diff Syncopation models/rhythm_parser.py @ 9:c2843ef4de2c

changing filenames to Python conventions
author csong
date Fri, 03 Apr 2015 11:41:01 +0100
parents
children a3ed7d2b57d8
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Syncopation models/rhythm_parser.py	Fri Apr 03 11:41:01 2015 +0100
@@ -0,0 +1,106 @@
+'''
+Author: Chunyang Song
+Institution: Centre for Digital Music, Queen Mary University of London
+'''
+
+# Parse the rhythm file and return a list of Bar objects
+Piece = []
+
+from ParameterSetter import timesigBase
+
+
+comment_sign = '#'
+def discardComments(line):
+	if comment_sign in line:
+		line = line[0:line.find(comment_sign)]
+	return line
+
+def discardSpaces(line):
+	line = line.replace(" ", '').replace("\t", '')
+	return line
+
+def extractInfo(line):
+	try:
+		if '{' not in line and '}' not in line:
+			raise RhythmSyntaxError(line)
+		else:
+			return line[line.find('{')+1 : line.find('}')]
+	except RhythmSyntaxError:
+		print 'Rhythmic information needs to be enclosed by "{" and "}"'
+
+
+current_timesig = ''
+current_timesigValue = []
+def process(line):
+	try:
+		if 't' in line:
+			current_timesig = extractInfo(line)
+			if current_timesig in timesigBase:
+				current_timesigValue = timesigBase[current_timesig]
+			else:
+				raise NoTimesigError(current_timesig)
+		
+		elif 'v' in line:
+			if current_timesig == '':
+				raise InitialTimesigError(line)
+			else:
+				rhythmString = extractInfo(line)
+				rhythm_seq = map(int,rhythmString.split(','))
+				Piece.append(Bar(rhythm_seq, current_timesigValue[0], current_timesigValue[1]))
+		
+		else:
+			raise SymbolError(line)
+	
+	except InitialTimesigError:
+		print 'The initial time-signature is not given.'
+	except NoTimesigError:
+		print 'The time-signature is not recognised.'
+	except SymbolError:
+		print 'Unrecognised symbol.'
+
+
+def readRhythm(fileName):
+	try:
+		f = file(fileName)
+		
+		# Clean up each line by discarding comments and spaces; extract time-signature or sequence information
+		isfinished = False
+		while(not isfinished):
+			line = f.readline()
+			if len(line) == 0:
+				isfinished = True
+			else:
+				cleanline = discardSpaces(discardComments(line))
+				process(cleanline)
+
+		# Extract time-signature and rhythm sequence information
+
+	except IOError as e:
+		print "I/O error({0}): {1}".format(e.errno, e.strerror)
+	except:
+		print 'Unexpected error.'#, sys.exc_info()[0]
+
+# To check the 
+def isInfoValid(info):
+	# If two parts of information is not seperated by ;
+	# if 't' in info and 'q' in info:
+	# 	print 'Error: time-signature and rhythm sequence needs ";" to seperate. Please check this rhythm file: %s.' %fileName
+	# 	Piece = None
+	# 	break
+	# elif '{' not in info and '}' not in info:
+	# 	print 'Error: information needs to be enclosed by "{ }". Please check this rhythm file: %s.' %fileName
+	# 	Piece = None
+	# 	break				
+	# else:
+	return True			
+
+# To check whether the given time-signature is existed; if not then requires user to add in the new time-signature
+#def checkTimesig(input):
+
+
+# TESTING
+line = 't{4/4}	q{1,0,0,1,0,0,1,0,0,0,1,0,0.8,0,0,0} t{2/4} # This is a comment'
+# print discardSpaces(discardComments(line))
+print line[line.find('{')+1:line.find('}')]
+#print readRhythm("rhythmbase/testrhythm.txt")
+