File: stemml.py

package info (click to toggle)
songwrite 0.14-10
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,536 kB
  • ctags: 1,193
  • sloc: python: 7,059; xml: 850; makefile: 11
file content (294 lines) | stat: -rw-r--r-- 10,516 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# SongWrite
# Copyright (C) 2001-2004 Jean-Baptiste LAMY -- jibalamy@free.fr
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

# This is the parser/reader for StemML.
# The writer is included in the song.py module.

import xml.sax as sax, xml.sax.handler as handler
from cStringIO import StringIO
import codecs

import song, songbook

def parse(file):
  if isinstance(file, unicode): file = file.encode("latin")
  
  parser = sax.make_parser()
  song_handler = SongHandler(parser, file)
  parser.setContentHandler(song_handler)
  parser.parse(file)

  if hasattr(song_handler, "songbook"): return song_handler.songbook
  else:                                 return song_handler.song
  
class SongHandler(handler.ContentHandler):
  def __init__(self, parser, file):
    self.parser = parser
    self.current    = ""
    self.need_strip = 0
    self.file = file
    
  def startElement(self, name, attrs):
    if   name == "bar":
      self.bar_attrs.update(attrs)
      rythm = self.bar_attrs["rythm"]
      i = rythm.index("/")
      if self.song.mesures:
        self.song.mesures.append(song.Mesure(self.song.mesures[-1].endtime(), int(self.bar_attrs["tempo"]), int(rythm[:i]), int(rythm[i + 1:]), int(self.bar_attrs["syncope"])))
      else:
        self.song.mesures.append(song.Mesure(0,                               int(self.bar_attrs["tempo"]), int(rythm[:i]), int(rythm[i + 1:]), int(self.bar_attrs["syncope"])))
        
    elif name == "play":
      self.song.playlist.playlist_items.append(song.PlaylistItem(self.song.playlist, int(attrs["from"]), int(attrs["to"])))
      
    elif name == "partition":
      partition_handler = PartitionHandler(self.parser, self, self.song, attrs)
      self.parser.setContentHandler(partition_handler)
      
    elif name == "lyrics":
      lyrics_handler = LyricsHandler(self.parser, self, self.song, attrs)
      self.parser.setContentHandler(lyrics_handler)
      
    elif name == "bars": pass
    elif name == "playlist": pass
    elif name == "song":
      self.bar_attrs = {"tempo" : 60, "syncope" : 0 , "rythm" : "4/4"}
      self.song = self.obj = song.Song()
      self.song.mesures *= 0
      self.song.version  = attrs.get("version", song.VERSION)
      self.song.lang     = attrs.get("lang", "en")[:2]
      self.song.filename = self.file
    elif name == "songfile":
      self.songbook.add_song(attrs.get("filename"), attrs.get("title"))
    elif name == "songbook":
      self.songbook = self.obj = songbook.Songbook(self.file, attrs.get("title", u""), attrs.get("authors", u""), attrs.get("comments", u""))
      self.songbook.version = attrs.get("version", song.VERSION)
    else: self.current = name
    
  def endElement(self, name):
    if   name == "song":
      self.song.playlist.analyse()
      
    if self.need_strip:
      setattr(self.obj, self.current, getattr(self.obj, self.current).strip())
      self.need_strip = 0
    self.current = ""
    
  def characters(self, content):
    if   self.current == "": pass
    else:
      setattr(self.obj, self.current, getattr(self.obj, self.current, u"") + content)
      self.need_strip = 1 # Will strip when the tag ends.

FX = {
  "normal"  : song.Note,
  "hammer"  : song.HammerNote,
  "legato"  : song.HammerNote,
  "slide"   : song.SlideNote,
  "bend"    : song.BendNote,
  "tremolo" : song.TremoloNote,
  "dead"    : song.DeadNote,
  "roll"    : song.RollNote,
  }
NOTATION_POS = {
  "top"  : 0,
  "down" : 1,
  }

class PartitionHandler(handler.ContentHandler):
  def __init__(self, parser, previous_handler, song_, attrs):
    self.parser               = parser
    self.previous_handler     = previous_handler
    self.partition            = song.Partition(song_)
    for attr, value in attrs.items():
      try: value = int(value)
      except:
        try: value = float(value)
        except: pass
      setattr(self.partition, attr, value)
    song_.partitions.append(self.partition)
    self.current     = ""
    self.id_to_notes = {}
    self.note_setter = {
      "time"        : None,
      "duration"    : None,
      "pitch"       : None,
      "volume"      : None,
      "linked_to"   : self.note_set_linked_to,
      "fx"          : self.note_set_fx,
      "id"          : self.note_set_id,
      "string"      : self.note_set_string,
      "bend_pitch"  : self.note_set_bend_pitch,
      }
    self.notes_linked_to = []
    self.roll_notes      = {}
    self.view_hidden     = 0
    self.need_strip      = 0
    
  def note_set_id(self, note, id): self.id_to_notes[id] = note
  
  def note_set_linked_to(self, note, id):
    # Cannot be done yet, since the following notes are not parsed yet !
    # We put this request somewhere and treat it later.
    if id:
      self.notes_linked_to.append((note, id))
      if note.__class__ is song.Note: note.__class__ = song.LinkedNote
    else: note.linked_to = None
    note.linked_from = None
    
  def note_set_fx(self, note, fx):
    note.__class__ = FX[fx]
    if fx == "roll":
      this_roll = self.roll_notes.get(note.time)
      if this_roll is None: self.roll_notes[note.time] = [note]
      else: this_roll.append(note)
      
  def note_set_string(self, note, stringid):
    note.stringid = int(stringid)
    
  def note_set_bend_pitch(self, note, bend_pitch):
    note.pitch = float(bend_pitch)
    
  def startElement(self, name, attrs):
    if   name == "note":
      note = song.Note(int(round(float(attrs["time"    ]) * 96.0)),
                       int(round(float(attrs["duration"]) * 96.0)),
                       int(attrs["pitch"]),
                       int(attrs.get("volume") or self.partition.notes[-1].volume),
                       )
      for attr in attrs.keys():
        setter = self.note_setter.get(attr, 1)
        if   setter is 1:
          value = attrs[attr]
          try: value = int(value)
          except:
            try: value = float(value)
            except: pass
          setattr(note, attr, value)
        elif setter: setter(note, attrs[attr])
        
      self.partition.notes.append(note)
      
    elif name == "notes"  : pass
    elif name == "strings": pass
    elif name == "string" :
      self.partition.view.strings.append(getattr(self.string_module, attrs.get("type") or "String")(int(attrs.get("pitch") or attrs.get("patch")), NOTATION_POS[attrs.get("notation", "top")]))
      
    elif name == "view":
      if   attrs["type"] == "tablature":
        import tablature
        
        self.partition.view = tablature.Tablature(self.partition.song, self.partition, [])
        self.string_module = tablature
        
      elif attrs["type"] == "drums":
        import drum
        
        self.partition.view = drum.DrumView(self.partition.song, self.partition, [])
        self.string_module = drum
        
      elif attrs["type"] == "staff":
        import staff
        
        if attrs.get("g8"): self.partition.setviewtype(staff.g8_view_type)
        else:               self.partition.setviewtype(staff.piano_view_type)
        self.string_module = staff
        
      if attrs.get("hidden"): self.view_hidden = 1
        
    else: self.current = name
    
  def endElement(self, name):
    if   name == "partition":
      # Finalize notes linked to another one
      for note, id in self.notes_linked_to:
        other = self.id_to_notes[id]
        note .linked_to   = other
        other.linked_from = note
        if other.__class__ is song.Note:
          other.__class__ = song.LinkedNote # other is linked !
          other.linked_to = None
          
      # Finalize rolls
      def sorter(b, a):
        try:    return cmp(a.stringid, b.stringid)
        except: return cmp(a.value, b.value)
      for roll in self.roll_notes.values():
        roll.sort(sorter)
        decal = 0
        for note in roll:
          note.decal = decal
          decal += 2
          
      self.parser.setContentHandler(self.previous_handler)
      
    elif name == "view":
      if self.view_hidden:
        import view
        
        self.partition.setviewtype(view.hidden_view_type)
        self.view_hidden     = 0
        
    elif name == self.current:
      if self.need_strip:
        setattr(self.partition, self.current, getattr(self.partition, self.current).strip())
        self.need_strip = 0
      self.current = ""
      
  def characters(self, content):
    if   self.current == ""    : pass
    else:
      setattr(self.partition, self.current, getattr(self.partition, self.current, u"") + content)
      self.need_strip = 1 # Will strip when the tag ends.
      
class LyricsHandler(handler.ContentHandler):
  def __init__(self, parser, previous_handler, song_, attrs):
    self.parser           = parser
    self.previous_handler = previous_handler
    self.lyrics           = song.Lyrics2(song_)
    song_.partitions.append(self.lyrics)
    self.texts      = []
    self.current    = ""
    self.need_strip = 0
    
  def startElement(self, name, attrs):
    if   name == "br":       self.texts.append("\\\\")
    elif name == "br-verse": self.texts.append("\n")
    else: self.current = name
    
  def endElement(self, name):
    if   name == "lyrics":
      import lyric2
      self.lyrics.text = "".join(self.texts).rstrip()
      self.lyrics.setviewtype(lyric2.lyric_view_type)
      self.parser.setContentHandler(self.previous_handler)
      
    elif name == self.current:
      if self.need_strip:
        setattr(self.lyrics, self.current, getattr(self.lyrics, self.current).strip())
        self.need_strip = 0
      self.current = ""
      
  def characters(self, content):
    if   self.current == ""    : pass
    elif self.current == "text":
      self.texts.append(content.replace("\n", ""))
    else:
      setattr(self.lyrics, self.current, getattr(self.lyrics, self.current, u"") + content)
      self.need_strip = 1 # Will strip when the tag ends.