diff musiccutter.py @ 2:b1aac55d2864

Parameterise page size add some more debugging. Seems to work OK for rests etc
author Daniel O'Connor <darius@dons.net.au>
date Sun, 06 Mar 2016 22:43:28 +1030
parents 123b42d95ab8
children 49a33c431b45
line wrap: on
line diff
--- a/musiccutter.py	Sat Mar 05 22:47:56 2016 +1030
+++ b/musiccutter.py	Sun Mar 06 22:43:28 2016 +1030
@@ -7,10 +7,10 @@
 
 def test():
     # http://www.orgues-de-barbarie.com/wp-content/uploads/2014/09/format-cartons.pdf
-    conf = { 'notefile' : 'notes', 'pitch' : 5.5 }
+    conf = { 'notefile' : 'notes', 'pitch' : 5.5 , 'pagewidth' : 20, 'pageheight' : 15.5 }
     midi2note, note2midi = genmidi2note()
     note2slot = loadnote2slot(conf['notefile'], note2midi)
-    midi2svg('test.midi', 'test%d.svg', conf['pitch'], midi2note, note2midi, note2slot)
+    midi2svg('test.midi', 'test%d.svg', conf['pitch'], midi2note, note2midi, note2slot, conf['pagewidth'], conf['pageheight'])
 
 # http://www.electronics.dit.ie/staff/tscarff/Music_technology/midi/midi_note_numbers_for_octaves.htm
 def genmidi2note():
@@ -44,24 +44,27 @@
 
     return note2slot
 
-def emitnote(svg, slot, start, notelen):
+def emitnote(svg, slot, start, notelen, pageheight):
     # Let us say 185 ticks for a quaver and that is 0.5cm long
     tickscale = 185.0 / 0.5
     pitch = 0.55
     offset = 0.60
     x = start / tickscale
-    y = offset + slot * pitch
+    y = pageheight - (offset + slot * pitch)
     w = notelen / tickscale
-    h = pitch / 10.0
+    h = pitch
     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'))
 
-def midi2svg(inf, outpat, pitch, midi2note, note2midi, note2slot):
+def midi2svg(inf, outpat, pitch, midi2note, note2midi, note2slot, pagewidth, pageheight):
+    playablecount = 0
+    unplayablecount = 0
     midi = mido.MidiFile(inf)
-    svg = svgwrite.Drawing(outpat % (0), size = ('20.0cm', '15.5cm'))
+    svg = svgwrite.Drawing(outpat % (0), size = ('%.3fcm' % (pagewidth), '%.3fcm' % (pageheight)))
     tnum = 0
+
     for t in midi.tracks:
         print "Track", tnum
         tnum += 1
@@ -71,6 +74,7 @@
         ctime = 0
         for ev in t:
             ctime += ev.time
+            print ctime, ev
             if ev.type == 'note_on' and ev.velocity > 0:
                 if ev.note in channels[ev.channel]:
                     print "Duplicate channel on message"
@@ -83,13 +87,27 @@
                         note = midi2note[ev.note]
                         if note not in note2slot:
                             print 'Skipping unplayable note %s' % (note)
+                            unplayablecount += 1
                         else:
-                            notelen = ctime - channels[ev.channel][ev.note]
+                            start = channels[ev.channel][ev.note]
+                            notelen = ctime - start
                             slot = note2slot[note]
-                            print 'Note %s (%d) at %d length %d' % (note, slot, ctime, notelen)
-                            emitnote(svg, slot, ctime, notelen)
+                            print 'Note %s (%d) at %d length %d' % (note, slot, start, notelen)
+                            emitnote(svg, slot, start, notelen, pageheight)
+                            print
+                            playablecount += 1
                         del channels[ev.channel][ev.note]
-        svg.save()
+            elif ev.type == 'end_of_track':
+                print 'EOT, flushing'
+                for chan in channels:
+                    for ev in chan:
+                        print ev
+        print
+        print
+        print
+    svg.save()
+    print 'Playable count:', playablecount
+    print 'Unplayable count:', unplayablecount
 
 if __name__ == '__main__':
     main()