diff musiccutter.py @ 27:87cf66e04ef9

Handle offsetting note pitch in a better way.
author Daniel O'Connor <darius@dons.net.au>
date Tue, 03 May 2016 08:31:28 +0930
parents f492b70f5e49
children 657bc32a0dfd
line wrap: on
line diff
--- a/musiccutter.py	Sat Apr 30 16:57:02 2016 +0930
+++ b/musiccutter.py	Tue May 03 08:31:28 2016 +0930
@@ -26,7 +26,7 @@
 
 class Midi2PDF(object):
     def __init__(self, notefile, pagewidth, pageheight, pitch, slotsize, heel, leadin, timemarks, drawrect, notenames, notelines, noteoffset, timescale, fontname, fontsize):
-        self.midi2note, self.note2midi = Midi2PDF.genmidi2note(noteoffset)
+        self.midi2note, self.note2midi = Midi2PDF.genmidi2note()
         self.note2slot, self.slot2note = Midi2PDF.loadnote2slot(notefile, self.note2midi)
         self.pagewidth = pagewidth # Dimensions are in millimetres
         self.pageheight = pageheight
@@ -38,6 +38,7 @@
         self.drawrect = drawrect # Draw rectangle around each page
         self.notenames = notenames # Draw note names on the right edge
         self.notelines = notelines # Draw line rulers
+        self.noteoffset = noteoffset # Amount to adjust note pitches by (+12 = 1 octave)
         self.timescale = timescale # Width per second
         self.fontname = fontname
         self.fontsize = fontsize # Points
@@ -61,6 +62,9 @@
         title = os.path.basename(midifile)
         title, ext = os.path.splitext(title)
         for ev in midi:
+            # Adjust pitch of note (if it's a note)
+            if hasattr(ev, 'note'):
+                ev.note += self.noteoffset
             if ev.type == 'text' and ctime == 0:
                 title = ev.text
 
@@ -135,9 +139,8 @@
 
     # http://newt.phys.unsw.edu.au/jw/notes.html
     @staticmethod
-    def genmidi2note(offset):
-        '''Create forward & reverse tables for midi number to note name (assuming 69 = A4 = A440)
-offset is amount to shift notes (+12 = +1 octave)'''
+    def genmidi2note():
+        '''Create forward & reverse tables for midi number to note name (assuming 69 = A4 = A440)'''
         names = ['C%d', 'C%d#', 'D%d', 'D%d#', 'E%d', 'F%d', 'F%d#', 'G%d', 'G%d#', 'A%d', 'A%d#', 'B%d']
         midi2note = {}
         note2midi = {}
@@ -145,8 +148,8 @@
             octave = midi / len(names) - 1
             index = midi % len(names)
             name = names[index] % (octave)
-            midi2note[midi - offset] = name
-            note2midi[name] = midi - offset
+            midi2note[midi] = name
+            note2midi[name] = midi
 
         return midi2note, note2midi