diff musiccutter.py @ 28:657bc32a0dfd

Try transposing unplayable notes down or up an octave (in that order).
author Daniel O'Connor <darius@dons.net.au>
date Tue, 03 May 2016 08:34:39 +0930
parents 87cf66e04ef9
children 767ba8ec90e6
line wrap: on
line diff
--- a/musiccutter.py	Tue May 03 08:31:28 2016 +0930
+++ b/musiccutter.py	Tue May 03 08:34:39 2016 +0930
@@ -46,6 +46,8 @@
     def processMidi(self, midifile, outpat):
         playablecount = 0
         unplayablecount = 0
+        transposeupcount = 0
+        transposedowncount = 0
         midi = mido.MidiFile(midifile)
         ctime = 0
         channels = []
@@ -81,16 +83,29 @@
                     if ev.note not in channels[ev.channel]:
                         print 'note_off with no corresponding note_on for channel %d note %d' % (ev.channel, ev.note)
                     else:
-                        if note not in self.note2slot:
-                            print 'Skipping unplayable note %d (%s)' % (ev.note, note)
-                            unplayablecount += 1
+                        start = channels[ev.channel][ev.note]
+                        notelen = ctime - start
+                        playable = True
+
+                        if note in self.note2slot:
+                            slot = self.note2slot[note]
+                        elif ev.note - 12 in self.midi2note and self.note2slot[self.midi2note[ev.note - 12]]:
+                            print 'Transposing note %d (%s) down' % (ev.note, note)
+                            slot = self.note2slot[self.midi2note[ev.note - 12]]
+                            transposedowncount += 1
+                        elif ev.note + 12 in self.midi2note and self.note2slot[self.midi2note[ev.note + 12]]:
+                            print 'Transposing note %d (%s) up' % (ev.note, note)
+                            slot = self.note2slot[self.midi2note[ev.note + 12]]
+                            transposeupcount += 1
                         else:
-                            start = channels[ev.channel][ev.note]
-                            notelen = ctime - start
-                            slot = self.note2slot[note]
-                            print 'Note %s (%d) at %.2f length %.2f' % (note, slot, start, notelen)
+                            unplayablecount += 1
+                            playable = False
+
+                        if playable:
+                            #print 'Note %s (%d) at %.2f length %.2f' % (note, slot, start, notelen)
                             self.emitnote(pdfs, slot, start, notelen)
                             playablecount += 1
+
                         del channels[ev.channel][ev.note]
             elif ev.type == 'end_of_track':
                 print 'EOT, not flushing, check for missed notes'
@@ -100,6 +115,8 @@
 
         print 'Playable count:', playablecount
         print 'Unplayable count:', unplayablecount
+        print 'Transpose down:', transposedowncount
+        print 'Transpose up:', transposeupcount
 
         for pindx in range(len(pdfs)):
             pdf = pdfs[pindx]