diff musiccutter.py @ 5:af683606184e

Paginate with a view port, a lot slower and wastes space but handling notes between pages is trivial.
author Daniel O'Connor <darius@dons.net.au>
date Tue, 08 Mar 2016 00:43:30 +1030
parents 9f4fa5f231e6
children 31db42ce72b8
line wrap: on
line diff
--- a/musiccutter.py	Mon Mar 07 21:23:59 2016 +1030
+++ b/musiccutter.py	Tue Mar 08 00:43:30 2016 +1030
@@ -49,21 +49,12 @@
 
     return note2slot
 
-def emitnote(pages, outpat, slot, start, notelen, pagewidth, pageheight, pitch, offset, timescale):
+def emitnote(svg, slot, start, notelen, pagewidth, pageheight, pitch, offset, timescale):
     x = start / timescale
-    pageidx = int(x / pagewidth)
-    x = x % pagewidth
     y = pageheight - (offset + slot * pitch)
     w = notelen / timescale
     h = pitch
-    print 'pageidx = %d x = %.3f y = %.3f w = %.3f h = %.3f' % (pageidx, x, y, w, h)
-    if len(pages) <= pageidx:
-        pages.extend(itertools.repeat(None, len(pages) - pageidx + 1))
-    if not pages[pageidx]:
-        svg = svgwrite.Drawing(outpat % (pageidx), size = ('%.3fcm' % (pagewidth), '%.3fcm' % (pageheight)))
-        pages[pageidx] = svg
-    else:
-        svg = pages[pageidx]
+    print 'x = %.3f y = %.3f w = %.3f h = %.3f' % (x, y, w, h)
     svg.add(svgwrite.shapes.Rect(insert = ('%.3fcm' % (x), '%.3fcm' % (y)),
                                  size = ('%.3fcm' % (w), '%.3fcm' % (h)),
                                 stroke = 'red', stroke_width = '1px', fill = 'red'))
@@ -74,6 +65,7 @@
     midi = mido.MidiFile(inf)
     ctime = 0
     channels = []
+    svg = svgwrite.Drawing(outpat % (0), size = ('%.3fcm' % (pagewidth), '%.3fcm' % (pageheight)))
     for i in range(16):
         channels.append({})
 
@@ -99,7 +91,7 @@
                         notelen = ctime - start
                         slot = note2slot[note]
                         #print 'Note %s (%d) at %d length %d' % (note, slot, start, notelen)
-                        emitnote(pages,outpat,  slot, start, notelen, pagewidth, pageheight, pitch, offset, timescale)
+                        emitnote(svg, slot, start, notelen, pagewidth, pageheight, pitch, offset, timescale)
                         playablecount += 1
                     del channels[ev.channel][ev.note]
         elif ev.type == 'end_of_track':
@@ -108,11 +100,11 @@
                 for ev in chan:
                     print ev
 
-    for svg in pages:
-        if not svg:
-            asdasd
-            continue
-        svg.save()
+    npages = int(midi.length / timescale / pagewidth + 0.5)
+    print 'npages', npages
+    for i in range(npages):
+        svg.viewbox(pagewidth / 2.54 * 96.0 * i, 0, pagewidth / 2.54 * 96.0, pageheight / 2.54 * 96.0)
+        svg.saveas(outpat % i)
 
     print 'Playable count:', playablecount
     print 'Unplayable count:', unplayablecount