comparison 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
comparison
equal deleted inserted replaced
26:f492b70f5e49 27:87cf66e04ef9
24 base = os.path.basename(base) 24 base = os.path.basename(base)
25 m.processMidi(filename, base + '-%02d.pdf') 25 m.processMidi(filename, base + '-%02d.pdf')
26 26
27 class Midi2PDF(object): 27 class Midi2PDF(object):
28 def __init__(self, notefile, pagewidth, pageheight, pitch, slotsize, heel, leadin, timemarks, drawrect, notenames, notelines, noteoffset, timescale, fontname, fontsize): 28 def __init__(self, notefile, pagewidth, pageheight, pitch, slotsize, heel, leadin, timemarks, drawrect, notenames, notelines, noteoffset, timescale, fontname, fontsize):
29 self.midi2note, self.note2midi = Midi2PDF.genmidi2note(noteoffset) 29 self.midi2note, self.note2midi = Midi2PDF.genmidi2note()
30 self.note2slot, self.slot2note = Midi2PDF.loadnote2slot(notefile, self.note2midi) 30 self.note2slot, self.slot2note = Midi2PDF.loadnote2slot(notefile, self.note2midi)
31 self.pagewidth = pagewidth # Dimensions are in millimetres 31 self.pagewidth = pagewidth # Dimensions are in millimetres
32 self.pageheight = pageheight 32 self.pageheight = pageheight
33 self.pitch = pitch # Distance between each slot 33 self.pitch = pitch # Distance between each slot
34 self.slotsize = slotsize # Size of each slot cut out 34 self.slotsize = slotsize # Size of each slot cut out
36 self.leadin = leadin # Extra at the start 36 self.leadin = leadin # Extra at the start
37 self.timemarks = timemarks # Draw vertical time lines 37 self.timemarks = timemarks # Draw vertical time lines
38 self.drawrect = drawrect # Draw rectangle around each page 38 self.drawrect = drawrect # Draw rectangle around each page
39 self.notenames = notenames # Draw note names on the right edge 39 self.notenames = notenames # Draw note names on the right edge
40 self.notelines = notelines # Draw line rulers 40 self.notelines = notelines # Draw line rulers
41 self.noteoffset = noteoffset # Amount to adjust note pitches by (+12 = 1 octave)
41 self.timescale = timescale # Width per second 42 self.timescale = timescale # Width per second
42 self.fontname = fontname 43 self.fontname = fontname
43 self.fontsize = fontsize # Points 44 self.fontsize = fontsize # Points
44 45
45 def processMidi(self, midifile, outpat): 46 def processMidi(self, midifile, outpat):
59 pdfs.append(pdf) 60 pdfs.append(pdf)
60 61
61 title = os.path.basename(midifile) 62 title = os.path.basename(midifile)
62 title, ext = os.path.splitext(title) 63 title, ext = os.path.splitext(title)
63 for ev in midi: 64 for ev in midi:
65 # Adjust pitch of note (if it's a note)
66 if hasattr(ev, 'note'):
67 ev.note += self.noteoffset
64 if ev.type == 'text' and ctime == 0: 68 if ev.type == 'text' and ctime == 0:
65 title = ev.text 69 title = ev.text
66 70
67 ctime += ev.time 71 ctime += ev.time
68 if ev.type == 'note_on' or ev.type == 'note_off': 72 if ev.type == 'note_on' or ev.type == 'note_off':
133 # Save PDF 137 # Save PDF
134 pdf.save() 138 pdf.save()
135 139
136 # http://newt.phys.unsw.edu.au/jw/notes.html 140 # http://newt.phys.unsw.edu.au/jw/notes.html
137 @staticmethod 141 @staticmethod
138 def genmidi2note(offset): 142 def genmidi2note():
139 '''Create forward & reverse tables for midi number to note name (assuming 69 = A4 = A440) 143 '''Create forward & reverse tables for midi number to note name (assuming 69 = A4 = A440)'''
140 offset is amount to shift notes (+12 = +1 octave)'''
141 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'] 144 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']
142 midi2note = {} 145 midi2note = {}
143 note2midi = {} 146 note2midi = {}
144 for midi in range(21, 128): 147 for midi in range(21, 128):
145 octave = midi / len(names) - 1 148 octave = midi / len(names) - 1
146 index = midi % len(names) 149 index = midi % len(names)
147 name = names[index] % (octave) 150 name = names[index] % (octave)
148 midi2note[midi - offset] = name 151 midi2note[midi] = name
149 note2midi[name] = midi - offset 152 note2midi[name] = midi
150 153
151 return midi2note, note2midi 154 return midi2note, note2midi
152 155
153 @staticmethod 156 @staticmethod
154 def loadnote2slot(fname, note2midi): 157 def loadnote2slot(fname, note2midi):