comparison 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
comparison
equal deleted inserted replaced
27:87cf66e04ef9 28:657bc32a0dfd
44 self.fontsize = fontsize # Points 44 self.fontsize = fontsize # Points
45 45
46 def processMidi(self, midifile, outpat): 46 def processMidi(self, midifile, outpat):
47 playablecount = 0 47 playablecount = 0
48 unplayablecount = 0 48 unplayablecount = 0
49 transposeupcount = 0
50 transposedowncount = 0
49 midi = mido.MidiFile(midifile) 51 midi = mido.MidiFile(midifile)
50 ctime = 0 52 ctime = 0
51 channels = [] 53 channels = []
52 for i in range(16): 54 for i in range(16):
53 channels.append({}) 55 channels.append({})
79 channels[ev.channel][ev.note] = ctime 81 channels[ev.channel][ev.note] = ctime
80 elif ev.type == 'note_off' or (ev.type == 'note_on' and ev.velocity == 0): 82 elif ev.type == 'note_off' or (ev.type == 'note_on' and ev.velocity == 0):
81 if ev.note not in channels[ev.channel]: 83 if ev.note not in channels[ev.channel]:
82 print 'note_off with no corresponding note_on for channel %d note %d' % (ev.channel, ev.note) 84 print 'note_off with no corresponding note_on for channel %d note %d' % (ev.channel, ev.note)
83 else: 85 else:
84 if note not in self.note2slot: 86 start = channels[ev.channel][ev.note]
85 print 'Skipping unplayable note %d (%s)' % (ev.note, note) 87 notelen = ctime - start
88 playable = True
89
90 if note in self.note2slot:
91 slot = self.note2slot[note]
92 elif ev.note - 12 in self.midi2note and self.note2slot[self.midi2note[ev.note - 12]]:
93 print 'Transposing note %d (%s) down' % (ev.note, note)
94 slot = self.note2slot[self.midi2note[ev.note - 12]]
95 transposedowncount += 1
96 elif ev.note + 12 in self.midi2note and self.note2slot[self.midi2note[ev.note + 12]]:
97 print 'Transposing note %d (%s) up' % (ev.note, note)
98 slot = self.note2slot[self.midi2note[ev.note + 12]]
99 transposeupcount += 1
100 else:
86 unplayablecount += 1 101 unplayablecount += 1
87 else: 102 playable = False
88 start = channels[ev.channel][ev.note] 103
89 notelen = ctime - start 104 if playable:
90 slot = self.note2slot[note] 105 #print 'Note %s (%d) at %.2f length %.2f' % (note, slot, start, notelen)
91 print 'Note %s (%d) at %.2f length %.2f' % (note, slot, start, notelen)
92 self.emitnote(pdfs, slot, start, notelen) 106 self.emitnote(pdfs, slot, start, notelen)
93 playablecount += 1 107 playablecount += 1
108
94 del channels[ev.channel][ev.note] 109 del channels[ev.channel][ev.note]
95 elif ev.type == 'end_of_track': 110 elif ev.type == 'end_of_track':
96 print 'EOT, not flushing, check for missed notes' 111 print 'EOT, not flushing, check for missed notes'
97 for chan in channels: 112 for chan in channels:
98 for ev in chan: 113 for ev in chan:
99 print ev 114 print ev
100 115
101 print 'Playable count:', playablecount 116 print 'Playable count:', playablecount
102 print 'Unplayable count:', unplayablecount 117 print 'Unplayable count:', unplayablecount
118 print 'Transpose down:', transposedowncount
119 print 'Transpose up:', transposeupcount
103 120
104 for pindx in range(len(pdfs)): 121 for pindx in range(len(pdfs)):
105 pdf = pdfs[pindx] 122 pdf = pdfs[pindx]
106 # Add title and page number 123 # Add title and page number
107 Midi2PDF.textHelper(pdf, 0 * mm, 1 * mm, ENGRAVE_COLOUR, True, self.fontname, self.fontsize, '%s (%d / %d)' % (title, pindx + 1, npages)) 124 Midi2PDF.textHelper(pdf, 0 * mm, 1 * mm, ENGRAVE_COLOUR, True, self.fontname, self.fontsize, '%s (%d / %d)' % (title, pindx + 1, npages))