comparison 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
comparison
equal deleted inserted replaced
8:2c5df6a4a22f 9:c2843ef4de2c
1 '''
2 Author: Chunyang Song
3 Institution: Centre for Digital Music, Queen Mary University of London
4 '''
5
6 # Parse the rhythm file and return a list of Bar objects
7 Piece = []
8
9 from ParameterSetter import timesigBase
10
11
12 comment_sign = '#'
13 def discardComments(line):
14 if comment_sign in line:
15 line = line[0:line.find(comment_sign)]
16 return line
17
18 def discardSpaces(line):
19 line = line.replace(" ", '').replace("\t", '')
20 return line
21
22 def extractInfo(line):
23 try:
24 if '{' not in line and '}' not in line:
25 raise RhythmSyntaxError(line)
26 else:
27 return line[line.find('{')+1 : line.find('}')]
28 except RhythmSyntaxError:
29 print 'Rhythmic information needs to be enclosed by "{" and "}"'
30
31
32 current_timesig = ''
33 current_timesigValue = []
34 def process(line):
35 try:
36 if 't' in line:
37 current_timesig = extractInfo(line)
38 if current_timesig in timesigBase:
39 current_timesigValue = timesigBase[current_timesig]
40 else:
41 raise NoTimesigError(current_timesig)
42
43 elif 'v' in line:
44 if current_timesig == '':
45 raise InitialTimesigError(line)
46 else:
47 rhythmString = extractInfo(line)
48 rhythm_seq = map(int,rhythmString.split(','))
49 Piece.append(Bar(rhythm_seq, current_timesigValue[0], current_timesigValue[1]))
50
51 else:
52 raise SymbolError(line)
53
54 except InitialTimesigError:
55 print 'The initial time-signature is not given.'
56 except NoTimesigError:
57 print 'The time-signature is not recognised.'
58 except SymbolError:
59 print 'Unrecognised symbol.'
60
61
62 def readRhythm(fileName):
63 try:
64 f = file(fileName)
65
66 # Clean up each line by discarding comments and spaces; extract time-signature or sequence information
67 isfinished = False
68 while(not isfinished):
69 line = f.readline()
70 if len(line) == 0:
71 isfinished = True
72 else:
73 cleanline = discardSpaces(discardComments(line))
74 process(cleanline)
75
76 # Extract time-signature and rhythm sequence information
77
78 except IOError as e:
79 print "I/O error({0}): {1}".format(e.errno, e.strerror)
80 except:
81 print 'Unexpected error.'#, sys.exc_info()[0]
82
83 # To check the
84 def isInfoValid(info):
85 # If two parts of information is not seperated by ;
86 # if 't' in info and 'q' in info:
87 # print 'Error: time-signature and rhythm sequence needs ";" to seperate. Please check this rhythm file: %s.' %fileName
88 # Piece = None
89 # break
90 # elif '{' not in info and '}' not in info:
91 # print 'Error: information needs to be enclosed by "{ }". Please check this rhythm file: %s.' %fileName
92 # Piece = None
93 # break
94 # else:
95 return True
96
97 # To check whether the given time-signature is existed; if not then requires user to add in the new time-signature
98 #def checkTimesig(input):
99
100
101 # TESTING
102 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'
103 # print discardSpaces(discardComments(line))
104 print line[line.find('{')+1:line.find('}')]
105 #print readRhythm("rhythmbase/testrhythm.txt")
106