Anonymous avatar Anonymous committed c8df0c2 Draft

Refactor IR things into own ir module.

Comments (0)

Files changed (3)

src/mmd0tools/ir.py

+"""Intermediate representation of an MMD0 file under conversion.
+
+This module was written by Chris Pressey, who hereby places it into the
+public domain.
+
+"""
+
+
+from mmd0tools.mmd0 import lol, CMD_NAMES, NOTE_NAMES, MMD0Block
+
+
+class IRSong(object):
+    def __init__(self, mmd0):
+        """Turn the event data in the blocks in the song sequence
+        into one big block.
+
+        """
+        numtracks = None
+        numlines = None
+        for block in mmd0.blockarr:
+            if numtracks is None:
+                numtracks = block.numtracks
+            else:
+                assert numtracks == block.numtracks, \
+                    "blocks have differing numbers of tracks"
+            if numlines is None:
+                numlines = block.lines
+            else:
+                assert numlines == block.lines, \
+                    "blocks have differing numbers of lines"
+
+        # XXX this is sort of an abuse of MMD0Block
+        b = MMD0Block(None, 0)
+        b.clear(numtracks, 0)
+        for block_no in mmd0.song.playseq[:mmd0.song.songlen]:
+            block = mmd0.blockarr[block_no]
+
+            line_no = 0
+            while line_no < numlines:
+                track_no = 0
+                end_block_now = False
+                while track_no < numtracks:
+                    e = block.track[track_no][line_no]
+                    if e.command == 15 and e.databyte == 0:
+                        end_block_now = True
+                    b.track[track_no].append(e)
+                    track_no += 1
+                if end_block_now:
+                    break
+                line_no += 1
+
+        b.lines = len(b.track[0])
+        #b.dump(999)
+
+        self.numtracks = numtracks
+        self.mmd0_block = b
+        self.ir_track = None
+
+    def to_ir_events(self):
+        ir_track = lol(self.numtracks)
+        track_no = 0
+        while track_no < self.numtracks:
+            ir_track[track_no] = self.track_to_ir_events(self.mmd0_block.track[track_no])
+            track_no += 1
+        # XXX IRBlock?
+        self.ir_track = ir_track
+
+    def track_to_ir_events(self, track):
+        # XXX handle rests (at beginning of track, and after
+        # VOLM/00 commands.)
+        # XXX handle effects
+
+        ir_events = []
+
+        line_no = 0
+        instr = None
+        note = None
+        dur = 0
+
+        while line_no < len(track):
+            e = track[line_no]
+            if e.instr > 0:
+                if instr is not None:
+                    ir = IREvent(instr, note, dur, None)
+                    ir_events.append(ir)
+                instr = e.instr
+                note = e.note
+                dur = 1
+            else:
+                dur += 1
+            line_no += 1
+
+        return ir_events
+
+    def dump(self):
+        for track in self.ir_track:
+            print "IR TRACK"
+            print "--------"
+            for ir in track:
+                print ir
+            print
+
+
+class IREvent(object):
+    """An event in our intermediate representation.
+
+    """
+    def __init__(self, instr, note, dur, effects):
+        self.instr = instr
+        self.note = note
+        self.dur = dur
+        self.effects = effects
+
+    def __str__(self):
+        c = ""
+        if self.effects:
+            c = "/*"
+        return "[%02d/%s/%d%s]" % (
+            self.instr, NOTE_NAMES[self.note], self.dur, c
+        )

src/mmd0tools/main.py

 from mmd0tools.mmd0 import Buffer, MMD0
-
+from mmd0tools.ir import IRSong
 
 def load(filename):
     b = None
 
 def convert(args):
     m = load(args[1])
-    b = m.flatten()
-    for track in b.to_ir_events():
-        print "IR TRACK"
-        for ir in track:
-            print ir
+    ir_song = IRSong(m)
+    ir_song.to_ir_events()
+    ir_song.dump()
 
 
 def samples(args):

src/mmd0tools/mmd0.py

 This module was written by Chris Pressey, who hereby places it into the
 public domain.
 
-This module is also a mess and needs to be refactored.
-
 """
 
 
         for smpl_offset in self.smplarr_offsets:
             self.smplarr.append(InstrHdr(buffer, smpl_offset))
 
-    def flatten(self):
-        """Turn the event data in the blocks in the song sequence
-        into one big block.
-
-        """
-        numtracks = None
-        numlines = None
-        for block in self.blockarr:
-            if numtracks is None:
-                numtracks = block.numtracks
-            else:
-                assert numtracks == block.numtracks, \
-                    "blocks have differing numbers of tracks"
-            if numlines is None:
-                numlines = block.lines
-            else:
-                assert numlines == block.lines, \
-                    "blocks have differing numbers of lines"
-
-        b = MMD0Block(None, 0)
-        b.clear(numtracks, 0)
-        for block_no in self.song.playseq[:self.song.songlen]:
-            block = self.blockarr[block_no]
-
-            line_no = 0
-            while line_no < numlines:
-                track_no = 0
-                end_block_now = False
-                while track_no < numtracks:
-                    e = block.track[track_no][line_no]
-                    if e.command == 15 and e.databyte == 0:
-                        end_block_now = True
-                    b.track[track_no].append(e)
-                    track_no += 1
-                if end_block_now:
-                    break
-                line_no += 1
-
-        b.lines = len(b.track[0])
-        #b.dump(999)
-
-        return b
-
     def dump(self):
         print "MMD0 Header"
         print "-----------"
         self.lines = lines
         self.track = lol(self.numtracks)
 
-    def to_ir_events(self):
-        ir_track = lol(self.numtracks)
-        track_no = 0
-        while track_no < self.numtracks:
-            ir_track[track_no] = self.track_to_ir_events(track_no)
-            track_no += 1
-        # XXX IRBlock?
-        return ir_track
-
-    def track_to_ir_events(self, track_no):
-        # XXX handle rests (at beginning of track, and after
-        # VOLM/00 commands.)
-        track = self.track[track_no]
-
-        ir_events = []
-
-        line_no = 0
-        instr = None
-        note = None
-        dur = 0
-
-        while line_no < len(track):
-            e = track[line_no]
-            if e.instr > 0:
-                if instr is not None:
-                    ir = IREvent(instr, note, dur, None)
-                    ir_events.append(ir)
-                instr = e.instr
-                note = e.note
-                dur = 1
-            else:
-                dur += 1
-            line_no += 1
-
-        return ir_events
-
     def dump(self, num):
         print "Block %d:" % num
         for attr in ('numtracks', 'lines'):
         if self.command == 0 and self.databyte == 0:
             c = "----/--"
         return "[%02d/%s/%s]" % (self.instr, NOTE_NAMES[self.note], c)
-
-
-class IREvent(object):
-    """An event in our intermediate representation.
-
-    """
-    def __init__(self, instr, note, dur, effects):
-        self.instr = instr
-        self.note = note
-        self.dur = dur
-        self.effects = effects
-
-    def __str__(self):
-        c = ""
-        if self.effects:
-            c = "/*"
-        return "[%02d/%s/%d%s]" % (
-            self.instr, NOTE_NAMES[self.note], self.dur, c
-        )
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.