changeset 3:1c99053c55cb

Working on the rhythm parser
author csong <csong@eecs.qmul.ac.uk>
date Tue, 31 Mar 2015 16:38:54 +0100
parents 031e2ccb1fb6
children 6f043542962a
files Syncopation models/ParameterSetter.pyc Syncopation models/Requirements.txt Syncopation models/RhythmParser.py Syncopation models/rhythmbase/testrhythm.txt
diffstat 4 files changed, 83 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
Binary file Syncopation models/ParameterSetter.pyc has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Syncopation models/Requirements.txt	Tue Mar 31 16:38:54 2015 +0100
@@ -0,0 +1,6 @@
+- Can read .txt file containing timesig and rhythm sequence bar by bar
+- Can detect the information is not given by the required format, and give alerts
+
+- Can read .midi file
+- User can add timesig if is not included in the time-signature dictionary
+- 
\ No newline at end of file
--- a/Syncopation models/RhythmParser.py	Fri Mar 20 18:28:08 2015 +0000
+++ b/Syncopation models/RhythmParser.py	Tue Mar 31 16:38:54 2015 +0100
@@ -4,8 +4,9 @@
 '''
 
 # Parse the rhythm file and return a list of Bar objects
+Piece = []
 
-import ParameterSetter
+from ParameterSetter import timesigBase
 
 class Bar:
 
@@ -14,46 +15,84 @@
 		self.subdivision_seq = subdivision_seq
 		self.beat_level = beat_level
 
-	def makeBar(self):
-		return [self.rhythm_seq, self.subdivision_seq, self.beat_level]
+
+
+
+comment_sign = '#'
+def discardComments(line):
+	if comment_sign in line:
+		line = line[0:line.find(comment_sign)]
+	return line
+
+spaces = [' ', '\t']
+def discardSpaces(line):
+	for charactor in spaces:
+		while (charactor in line):
+			line = line.replace(charactor, '')
+	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 = ''
-Piece = []
+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)
 
-		bar = f.readline()
+		# Extract time-signature and rhythm sequence information
 
-		# While the current line has contents
-		while len(bar) > 0:
-			rhythm_seq = []
-
-			info = bar.split(';')
-			
-			if isInfoValid(info):
-				for i in info:
-					if i[0] == 't':
-						current_timesig = i[i.find('{')+1:i.find('}')]
-					elif i[0] == 'q':
-						rhythmString = i[i.find('{')+1:i.find('}')]
-						rhythm_seq = map(int,rhythmString.split(','))
-					else:
-						print 'Error: %s is not recognisable. Please check this rhythm file: %s.' %(i[0], fileName)
-
-				#Piece.append(Bar(rhythm_seq, subdivision_seq, beat_level).makeBar())
-			else:
-				break
-
-			bar = f.readline()
-
-		return Piece
+	except IOError as e:
+		print "I/O error({0}): {1}".format(e.errno, e.strerror)
 	except:
 		print 'Unexpected error.'#, sys.exc_info()[0]
-		raise
 
+# To check the 
 def isInfoValid(info):
 	# If two parts of information is not seperated by ;
 	# if 't' in info and 'q' in info:
@@ -72,5 +111,8 @@
 
 
 # TESTING
-print readRhythm("rhythmbase/clave.txt")
+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")
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Syncopation models/rhythmbase/testrhythm.txt	Tue Mar 31 16:38:54 2015 +0100
@@ -0,0 +1,5 @@
+t{4/4} v{1,0,0,1,0,0,1,0,0,0,1,0,0.8,0,0,0} # This is a comment
+v{1,0,0,1,0,0,1,0,0,0,1,0,1,0,0,0}
+t{3/4} v{1,1,1}
+
+y{(), }
\ No newline at end of file