- def __init__ (self, name):
- self.measures = [None]
- self.chords = [None]
- self.frames = [None]
- self.tuplets = [None]
- self.staffs = [None]
- self.slurs = [None]
- self.articulations = [None]
- self.syllables = [None]
- self.verses = [None]
- self.articulation_defs = [None]
-
- ## do it
- self.parse (name)
-
- def get_global_measure (self, no):
- fill_list_to (self.measures, no)
- if self.measures[no] == None:
- self.measures [no] = Global_measure (no)
-
- return self.measures[no]
-
-
- def get_staff(self,staffno):
- fill_list_to (self.staffs, staffno)
- if self.staffs[staffno] == None:
- self.staffs[staffno] = Staff (staffno)
-
- return self.staffs[staffno]
-
- # staff-spec
- def try_IS (self, indices, contents):
- pass
-
- def try_BC (self, indices, contents):
- bn = indices[0]
- where = contents[0] / 1024.0
- def try_TP(self, indices, contents):
- (nil, num) = indices
-
- if self.tuplets[-1] == None or num <> self.tuplets[-1].start_note:
- self.tuplets.append (Tuplet (num))
-
- self.tuplets[-1].append_finale (contents)
-
- def try_IM (self, indices, contents):
- (a,b) = indices
- fin = contents
- self.articulations.append (Articulation (a,b,fin))
- def try_verse (self, indices, contents):
- a = indices[0]
- body = contents[0]
-
- body = re.sub (r"""\^[a-z]+\([^)]+\)""", "", body)
- body = re.sub ("\^[a-z]+", "", body)
- self.verses.append (Verse (a, body))
- def try_ve (self,indices, contents):
- (a,b) = indices
- self.syllables.append (Syllable (a,b,contents))
-
- def try_eE (self,indices, contents):
- no = indices[0]
- (prev, next, dur, pos, entryflag, extended, follow) = contents[:7]
-
- fill_list_to (self.chords, no)
- self.chords[no] =Chord (no, contents)
-
- def try_Sx(self,indices, contents):
- slurno = indices[0]
- fill_list_to (self.slurs, slurno)
- self.slurs[slurno] = Slur(slurno, contents)
-
- def try_IX (self, indices, contents):
- n = indices[0]
- a = contents[0]
- b = contents[1]
-
- ix= None
- try:
- ix = self.articulation_defs[n]
- except IndexError:
- ix = Articulation_def (n,a,b)
- self.articulation_defs.append (Articulation_def (n, a, b))
-
- def try_GF(self, indices, contents):
- (staffno,measno) = indices
-
- st = self.get_staff (staffno)
- meas = st.get_measure (measno)
- meas.finale = contents
-
- def try_FR(self, indices, contents):
- frameno = indices [0]
-
- startnote = contents[0]
- endnote = contents[1]
-
- fill_list_to (self.frames, frameno)
-
- self.frames[frameno] = Frame ((frameno, startnote, endnote))
-
- def try_MS (self, indices, contents):
- measno = indices[0]
- keynum = contents[1]
- meas =self. get_global_measure (measno)
- meas.set_keysig (keynum)
-
- beats = contents[2]
- beatlen = contents[3]
- meas.set_timesig ((beats, beatlen))
-
- meas_flag1 = contents[4]
- meas_flag2 = contents[5]
-
- meas.set_flags (meas_flag1, meas_flag2);
-
-
- routine_dict = {
- 'MS': try_MS,
- 'FR': try_FR,
- 'GF': try_GF,
- 'IX': try_IX,
- 'Sx' : try_Sx,
- 'eE' : try_eE,
- 'verse' : try_verse,
- 've' : try_ve,
- 'IM' : try_IM,
- 'TP' : try_TP,
- 'BC' : try_BC,
- 'IS' : try_IS,
- }
-
- def parse (self, etf_dict):
- sys.stderr.write ('reconstructing ...')
- sys.stderr.flush ()
-
- for (tag,routine) in Etf_file.routine_dict.items ():
- ks = etf_dict[tag].keys ()
- ks.sort ()
- for k in ks:
- routine (self, k, etf_dict[tag][k])
-
- sys.stderr.write ('processing ...')
- sys.stderr.flush ()
-
- self.unthread_entries ()
-
- for st in self.staffs[1:]:
- if not st:
- continue
- mno = 1
- for m in st.measures[1:]:
- if not m:
- continue
-
- m.calculate()
- try:
- m.global_measure = self.measures[mno]
- except IndexError:
- sys.stderr.write ("Non-existent global measure %d" % mno)
- continue
-
- frame_obj_list = [None]
- for frno in m.frames:
- try:
- fr = self.frames[frno]
- frame_obj_list.append (fr)
- except IndexError:
- sys.stderr.write ("\nNon-existent frame %d" % frno)
-
- m.frames = frame_obj_list
- for fr in frame_obj_list[1:]:
- if not fr:
- continue
-
- fr.set_measure (m)
-
- fr.chords = self.get_thread (fr.start, fr.end)
- for c in fr.chords:
- c.frame = fr
- mno = mno + 1
-
- for c in self.chords[1:]:
- if c:
- c.calculate()
-
- for f in self.frames[1:]:
- if f:
- f.calculate ()
-
- for t in self.tuplets[1:]:
- t.calculate (self.chords)
-
- for s in self.slurs[1:]:
- if s:
- s.calculate (self.chords)
-
- for s in self.articulations[1:]:
- s.calculate (self.chords, self.articulation_defs)
-
- def get_thread (self, startno, endno):
-
- thread = []
-
- c = None
- try:
- c = self.chords[startno]
- except IndexError:
- sys.stderr.write ("Huh? Frame has invalid bounds (%d,%d)\n" % (startno, endno))
- return []
-
-
- while c and c.number <> endno:
- thread.append (c)
- c = c.next
-
- if c:
- thread.append (c)
-
- return thread
-
- def dump (self):
- str = ''
- staffs = []
- for s in self.staffs[1:]:
- if s:
- str = str + '\n\n' + s.dump ()
- staffs.append ('\\' + s.staffid ())
-
-
- # should use \addlyrics ?
-
- for v in self.verses[1:]:
- str = str + v.dump()
-
- if len (self.verses) > 1:
- sys.stderr.write ("\nLyrics found; edit to use \\addlyrics to couple to a staff\n")
-
- if staffs:
- str = str + '\\score { < %s > } ' % string.join (staffs)
-
- return str
-
-
- def __str__ (self):
- return 'ETF FILE %s %s' % (self.measures, self.entries)
-
- def unthread_entries (self):
- for e in self.chords[1:]:
- if not e:
- continue
-
- e.prev = self.chords[e.finale[0]]
- e.next = self.chords[e.finale[1]]
+ def __init__ (self, name):
+ self.measures = [None]
+ self.chords = [None]
+ self.frames = [None]
+ self.tuplets = [None]
+ self.staffs = [None]
+ self.slurs = [None]
+ self.articulations = [None]
+ self.syllables = [None]
+ self.verses = [None]
+ self.articulation_defs = [None]
+
+ ## do it
+ self.parse (name)
+
+ def get_global_measure (self, no):
+ fill_list_to (self.measures, no)
+ if self.measures[no] == None:
+ self.measures [no] = Global_measure (no)
+
+ return self.measures[no]
+
+
+ def get_staff(self,staffno):
+ fill_list_to (self.staffs, staffno)
+ if self.staffs[staffno] == None:
+ self.staffs[staffno] = Staff (staffno)
+
+ return self.staffs[staffno]
+
+ # staff-spec
+ def try_IS (self, indices, contents):
+ pass
+
+ def try_BC (self, indices, contents):
+ bn = indices[0]
+ where = contents[0] / 1024.0
+ def try_TP(self, indices, contents):
+ (nil, num) = indices
+
+ if self.tuplets[-1] == None or num <> self.tuplets[-1].start_note:
+ self.tuplets.append (Tuplet (num))
+
+ self.tuplets[-1].append_finale (contents)
+
+ def try_IM (self, indices, contents):
+ (a,b) = indices
+ fin = contents
+ self.articulations.append (Articulation (a,b,fin))
+ def try_verse (self, indices, contents):
+ a = indices[0]
+ body = contents[0]
+
+ body = re.sub (r"""\^[a-z]+\([^)]+\)""", "", body)
+ body = re.sub ("\^[a-z]+", "", body)
+ self.verses.append (Verse (a, body))
+ def try_ve (self,indices, contents):
+ (a,b) = indices
+ self.syllables.append (Syllable (a,b,contents))
+
+ def try_eE (self,indices, contents):
+ no = indices[0]
+ (prev, next, dur, pos, entryflag, extended, follow) = contents[:7]
+
+ fill_list_to (self.chords, no)
+ self.chords[no] =Chord (no, contents)
+
+ def try_Sx(self,indices, contents):
+ slurno = indices[0]
+ fill_list_to (self.slurs, slurno)
+ self.slurs[slurno] = Slur(slurno, contents)
+
+ def try_IX (self, indices, contents):
+ n = indices[0]
+ a = contents[0]
+ b = contents[1]
+
+ ix= None
+ try:
+ ix = self.articulation_defs[n]
+ except IndexError:
+ ix = Articulation_def (n,a,b)
+ self.articulation_defs.append (Articulation_def (n, a, b))
+
+ def try_GF(self, indices, contents):
+ (staffno,measno) = indices
+
+ st = self.get_staff (staffno)
+ meas = st.get_measure (measno)
+ meas.finale = contents
+
+ def try_FR(self, indices, contents):
+ frameno = indices [0]
+
+ startnote = contents[0]
+ endnote = contents[1]
+
+ fill_list_to (self.frames, frameno)
+
+ self.frames[frameno] = Frame ((frameno, startnote, endnote))
+
+ def try_MS (self, indices, contents):
+ measno = indices[0]
+ keynum = contents[1]
+ meas =self. get_global_measure (measno)
+
+ meas.set_key_sig (keynum)
+
+ beats = contents[2]
+ beatlen = contents[3]
+ meas.set_timesig ((beats, beatlen))
+
+ meas_flag1 = contents[4]
+ meas_flag2 = contents[5]
+
+ meas.set_flags (meas_flag1, meas_flag2);
+
+
+ routine_dict = {
+ 'MS': try_MS,
+ 'FR': try_FR,
+ 'GF': try_GF,
+ 'IX': try_IX,
+ 'Sx' : try_Sx,
+ 'eE' : try_eE,
+ 'verse' : try_verse,
+ 've' : try_ve,
+ 'IM' : try_IM,
+ 'TP' : try_TP,
+ 'BC' : try_BC,
+ 'IS' : try_IS,
+ }
+
+ def parse (self, etf_dict):
+ sys.stderr.write ('reconstructing ...')
+ sys.stderr.flush ()
+
+ for (tag,routine) in Etf_file.routine_dict.items ():
+ ks = etf_dict[tag].keys ()
+ ks.sort ()
+ for k in ks:
+ routine (self, k, etf_dict[tag][k])
+
+ sys.stderr.write ('processing ...')
+ sys.stderr.flush ()
+
+ self.unthread_entries ()
+
+ for st in self.staffs[1:]:
+ if not st:
+ continue
+ mno = 1
+ for m in st.measures[1:]:
+ if not m:
+ continue
+
+ m.calculate()
+ try:
+ m.global_measure = self.measures[mno]
+ except IndexError:
+ sys.stderr.write ("Non-existent global measure %d" % mno)
+ continue
+
+ frame_obj_list = [None]
+ for frno in m.frames:
+ try:
+ fr = self.frames[frno]
+ frame_obj_list.append (fr)
+ except IndexError:
+ sys.stderr.write ("\nNon-existent frame %d" % frno)
+
+ m.frames = frame_obj_list
+ for fr in frame_obj_list[1:]:
+ if not fr:
+ continue
+
+ fr.set_measure (m)
+
+ fr.chords = self.get_thread (fr.start, fr.end)
+ for c in fr.chords:
+ c.frame = fr
+ mno = mno + 1
+
+ for c in self.chords[1:]:
+ if c:
+ c.calculate()
+
+ for f in self.frames[1:]:
+ if f:
+ f.calculate ()
+
+ for t in self.tuplets[1:]:
+ t.calculate (self.chords)
+
+ for s in self.slurs[1:]:
+ if s:
+ s.calculate (self.chords)
+
+ for s in self.articulations[1:]:
+ s.calculate (self.chords, self.articulation_defs)
+
+ def get_thread (self, startno, endno):
+
+ thread = []
+
+ c = None
+ try:
+ c = self.chords[startno]
+ except IndexError:
+ sys.stderr.write ("Huh? Frame has invalid bounds (%d,%d)\n" % (startno, endno))
+ return []
+
+
+ while c and c.number <> endno:
+ d = c # hack to avoid problem with scripts/build/grand-replace.py
+ thread.append (d)
+ c = c.next
+
+ if c:
+ d = c # hack to avoid problem with scripts/build/grand-replace.py
+ thread.append (d)
+
+ return thread
+
+ def dump (self):
+ str = ''
+ staffs = []
+ for s in self.staffs[1:]:
+ if s:
+ str = str + '\n\n' + s.dump ()
+ staffs.append ('\\' + s.staffid ())
+
+
+ # should use \addlyrics ?
+
+ for v in self.verses[1:]:
+ str = str + v.dump()
+
+ if len (self.verses) > 1:
+ sys.stderr.write ("\nLyrics found; edit to use \\addlyrics to couple to a staff\n")
+
+ if staffs:
+ str += '\\version "2.3.25"\n'
+ str = str + '<<\n %s\n>> } ' % ' '.join (staffs)
+
+ return str
+
+
+ def __str__ (self):
+ return 'ETF FILE %s %s' % (self.measures, self.entries)
+
+ def unthread_entries (self):
+ for e in self.chords[1:]:
+ if not e:
+ continue
+
+ e.prev = self.chords[e.finale[0]]
+ e.next = self.chords[e.finale[1]]