annotate musiccutter.py @ 9:e9760224e992

Fill should be none.
author Daniel O'Connor <darius@dons.net.au>
date Sat, 02 Apr 2016 23:27:56 +1030
parents f07a997e9f79
children 9faad813e39e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1 #!/usr/bin/env python
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
3 import exceptions
4
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
4 import itertools
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
5 import math
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
6 import mido
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
7 import svgwrite
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
8 import sys
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
9
3
49a33c431b45 Accept filename for test function
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
10 def test(filename = None):
49a33c431b45 Accept filename for test function
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
11 if filename == None:
49a33c431b45 Accept filename for test function
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
12 filename = 'test.midi'
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
13 # Card layout from http://www.orgues-de-barbarie.com/wp-content/uploads/2014/09/format-cartons.pdf
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
14 m = Midi2SVG('notes', 20, 15.5, 0.55, 0.6, 1.0)
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
15 m.processMidi(filename, 'test%02d.svg')
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
16
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
17 class Midi2SVG(object):
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
18 def __init__(self, notefile, pagewidth, pageheight, pitch, offset, timescale):
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
19 self.midi2note, self.note2midi = Midi2SVG.genmidi2note()
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
20 self.note2slot = Midi2SVG.loadnote2slot(notefile, self.note2midi)
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
21 self.pagewidth = pagewidth
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
22 self.pageheight = pageheight
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
23 self.pitch = pitch
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
24 self.offset = offset
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
25 self.timescale = timescale
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
26
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
27 def processMidi(self, midifile, outpat):
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
28 playablecount = 0
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
29 unplayablecount = 0
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
30 midi = mido.MidiFile(midifile)
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
31 ctime = 0
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
32 channels = []
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
33 for i in range(16):
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
34 channels.append({})
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
35
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
36 npages = int(math.ceil(midi.length / self.timescale / self.pagewidth))
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
37 print 'npages', npages
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
38 svgs = []
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
39 for i in range(npages):
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
40 svg = svgwrite.Drawing(outpat % i, profile = 'full', size = ('%.3fcm' % (self.pagewidth), '%.3fcm' % (self.pageheight)))
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
41 svgs.append(svg)
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
42
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
43 for ev in midi:
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
44 ctime += ev.time
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
45 if ev.type == 'note_on' or ev.type == 'note_off':
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
46 note = self.midi2note[ev.note]
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
47 print ctime, ev
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
48 if ev.type == 'note_on' and ev.velocity > 0:
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
49 if ev.note in channels[ev.channel]:
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
50 print 'Duplicate note_on message %d (%s)' % (ev.note, note)
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
51 else:
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
52 channels[ev.channel][ev.note] = ctime
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
53 elif ev.type == 'note_off' or (ev.type == 'note_on' and ev.velocity == 0):
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
54 if ev.note not in channels[ev.channel]:
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
55 print 'note_off with no corresponding note_on for channel %d note %d' % (ev.channel, ev.note)
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
56 else:
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
57 if note not in self.note2slot:
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
58 print 'Skipping unplayable note %s' % (note)
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
59 unplayablecount += 1
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
60 else:
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
61 start = channels[ev.channel][ev.note]
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
62 notelen = ctime - start
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
63 slot = self.note2slot[note]
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
64 print 'Note %s (%d) at %.2f length %.2f' % (note, slot, start, notelen)
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
65 self.emitnote(svgs, slot, start, notelen)
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
66 playablecount += 1
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
67 del channels[ev.channel][ev.note]
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
68 elif ev.type == 'end_of_track':
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
69 print 'EOT, not flushing, check for missed notes'
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
70 for chan in channels:
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
71 for ev in chan:
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
72 print ev
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
73
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
74 print 'Playable count:', playablecount
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
75 print 'Unplayable count:', unplayablecount
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
76
8
f07a997e9f79 Engrave filename and page number.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
77 for i in range(len(svgs)):
f07a997e9f79 Engrave filename and page number.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
78 svg = svgs[i]
f07a997e9f79 Engrave filename and page number.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
79 svg.add(svg.text('%s (%d / %d)' % (midifile, i + 1, npages), fill = 'black', stroke = 'black',
f07a997e9f79 Engrave filename and page number.
Daniel O'Connor <darius@dons.net.au>
parents: 7
diff changeset
80 insert = ('%.3fcm' % (0), '%.3fcm' % (self.pageheight))))
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
81 svg.save()
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
82
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
83 # http://www.electronics.dit.ie/staff/tscarff/Music_technology/midi/midi_note_numbers_for_octaves.htm
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
84 @staticmethod
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
85 def genmidi2note():
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
86 '''Create forward & reverse tables for midi number to note name (assuming 69 == A440)'''
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
87 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']
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
88 midi2note = {}
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
89 note2midi = {}
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
90 for midi in range(128):
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
91 octave = midi / len(names)
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
92 index = midi % len(names)
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
93 name = names[index] % (octave)
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
94 midi2note[midi] = name
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
95 note2midi[name] = midi
4
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
96
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
97 return midi2note, note2midi
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
98
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
99 @staticmethod
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
100 def loadnote2slot(fname, note2midi):
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
101 note2slot = {}
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
102 index = 0
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
103
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
104 for note in file(fname):
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
105 note = note.strip()
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
106 if note[0] == '#':
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
107 continue
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
108 if note not in note2midi:
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
109 raise exceptions.ValueError('Note \'%s\' not valid' % note)
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
110 note2slot[note] = index
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
111 index += 1
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
112
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
113 return note2slot
4
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
114
7
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
115 def emitnote(self, svgs, slot, start, notelen):
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
116 startx = start / self.timescale
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
117 startpageidx = int(startx / self.pagewidth)
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
118 endx = (start + notelen) / self.timescale
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
119 endpageidx = int(endx / self.pagewidth)
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
120 startx = startx % self.pagewidth
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
121 y = self.pageheight - (self.offset + slot * self.pitch)
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
122 w = notelen / self.timescale
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
123 h = self.pitch
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
124
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
125 if startpageidx != endpageidx:
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
126 print 'page crossed from %d to %d' % (startpageidx, endpageidx)
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
127 print 'page = %d x = %.3f y = %.3f w = %.3f h = %.3f' % (startpageidx, startx, y, w, h)
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
128 Midi2SVG._emitnote(svgs[startpageidx], startx, y, w, h)
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
129
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
130 @staticmethod
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
131 def _emitnote(svg, x, y, w, h):
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
132 svg.add(svgwrite.shapes.Rect(insert = ('%.3fcm' % (x), '%.3fcm' % (y)),
31db42ce72b8 Rework to be class-y
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
133 size = ('%.3fcm' % (w), '%.3fcm' % (h)),
9
e9760224e992 Fill should be none.
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
134 stroke = 'red', stroke_width = '1px', fill = 'none'))
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
135
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
136 if __name__ == '__main__':
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
137 main()