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