# HG changeset patch # User Daniel O'Connor # Date 1457360010 -37800 # Node ID af683606184ee0a1d5a80fa5fa7075bfcc044be9 # Parent 9f4fa5f231e6e833e7b0dac83eed6301d7bcdb7e Paginate with a view port, a lot slower and wastes space but handling notes between pages is trivial. diff -r 9f4fa5f231e6 -r af683606184e musiccutter.py --- 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