annotate musiccutter.py @ 4:9f4fa5f231e6

Rework to use mido iterator and paginate. Pagination doesn't handle notes extending over a page yet.
author Daniel O'Connor <darius@dons.net.au>
date Mon, 07 Mar 2016 21:23:59 +1030
parents 49a33c431b45
children af683606184e
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
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
5 import mido
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
6 import svgwrite
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
7 import sys
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
8
3
49a33c431b45 Accept filename for test function
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
9 def test(filename = None):
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
10 # http://www.orgues-de-barbarie.com/wp-content/uploads/2014/09/format-cartons.pdf
4
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
11 conf = { 'notefile' : 'notes', 'pagewidth' : 20, 'pageheight' : 15.5,
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
12 'pitch' : 0.55, 'offset' : 0.60, 'timescale' : 1.0 }
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
13 midi2note, note2midi = genmidi2note()
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
14 note2slot = loadnote2slot(conf['notefile'], note2midi)
3
49a33c431b45 Accept filename for test function
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
15 if filename == None:
49a33c431b45 Accept filename for test function
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
16 filename = 'test.midi'
4
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
17 midi2svg(filename, 'test%02d.svg', midi2note, note2midi, note2slot, conf['pagewidth'], conf['pageheight'],
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
18 conf['pitch'], conf['offset'], conf['timescale'])
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
19
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
20 # http://www.electronics.dit.ie/staff/tscarff/Music_technology/midi/midi_note_numbers_for_octaves.htm
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
21 def genmidi2note():
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
22 '''Create forward & reverse tables for midi number to note name (assuming 69 == A440)'''
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
23 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']
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
24 midi2note = {}
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
25 note2midi = {}
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
26 for midi in range(128):
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
27 octave = midi / len(names)
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
28 index = midi % len(names)
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
29 name = names[index] % (octave)
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
30 midi2note[midi] = name
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
31 note2midi[name] = midi
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
32
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
33 return midi2note, note2midi
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
34
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
35 def loadnote2slot(fname, note2midi):
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
36 svg = svgwrite.Drawing(fname)
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
37
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
38 note2slot = {}
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
39 index = 0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
40
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
41 for note in file(fname):
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
42 note = note.strip()
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
43 if note[0] == '#':
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
44 continue
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
45 if note not in note2midi:
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
46 raise exceptions.ValueError('Note \'%s\' not valid' % note)
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
47 note2slot[note] = index
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
48 index += 1
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
49
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
50 return note2slot
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
51
4
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
52 def emitnote(pages, outpat, slot, start, notelen, pagewidth, pageheight, pitch, offset, timescale):
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
53 x = start / timescale
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
54 pageidx = int(x / pagewidth)
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
55 x = x % pagewidth
2
b1aac55d2864 Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
56 y = pageheight - (offset + slot * pitch)
4
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
57 w = notelen / timescale
2
b1aac55d2864 Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
58 h = pitch
4
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
59 print 'pageidx = %d x = %.3f y = %.3f w = %.3f h = %.3f' % (pageidx, x, y, w, h)
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
60 if len(pages) <= pageidx:
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
61 pages.extend(itertools.repeat(None, len(pages) - pageidx + 1))
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
62 if not pages[pageidx]:
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
63 svg = svgwrite.Drawing(outpat % (pageidx), size = ('%.3fcm' % (pagewidth), '%.3fcm' % (pageheight)))
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
64 pages[pageidx] = svg
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
65 else:
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
66 svg = pages[pageidx]
1
123b42d95ab8 Use cm directly
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
67 svg.add(svgwrite.shapes.Rect(insert = ('%.3fcm' % (x), '%.3fcm' % (y)),
123b42d95ab8 Use cm directly
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
68 size = ('%.3fcm' % (w), '%.3fcm' % (h)),
123b42d95ab8 Use cm directly
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
69 stroke = 'red', stroke_width = '1px', fill = 'red'))
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
70
4
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
71 def midi2svg(inf, outpat, midi2note, note2midi, note2slot, pagewidth, pageheight, pitch, offset, timescale):
2
b1aac55d2864 Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
72 playablecount = 0
b1aac55d2864 Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
73 unplayablecount = 0
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
74 midi = mido.MidiFile(inf)
4
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
75 ctime = 0
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
76 channels = []
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
77 for i in range(16):
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
78 channels.append({})
2
b1aac55d2864 Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
79
4
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
80 pages = []
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
81 for ev in midi:
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
82 ctime += ev.time
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
83 #print ctime, ev
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
84 if ev.type == 'note_on' and ev.velocity > 0:
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
85 if ev.note in channels[ev.channel]:
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
86 print 'Duplicate note_on message %d (%s)' % (ev.note, midi2note[ev.note])
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
87 else:
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
88 channels[ev.channel][ev.note] = ctime
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
89 elif ev.type == 'note_off' or (ev.type == 'note_on' and ev.velocity == 0):
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
90 if ev.note not in channels[ev.channel]:
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
91 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
92 else:
4
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
93 note = midi2note[ev.note]
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
94 if note not in note2slot:
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
95 print 'Skipping unplayable note %s' % (note)
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
96 unplayablecount += 1
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
97 else:
4
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
98 start = channels[ev.channel][ev.note]
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
99 notelen = ctime - start
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
100 slot = note2slot[note]
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
101 #print 'Note %s (%d) at %d length %d' % (note, slot, start, notelen)
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
102 emitnote(pages,outpat, slot, start, notelen, pagewidth, pageheight, pitch, offset, timescale)
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
103 playablecount += 1
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
104 del channels[ev.channel][ev.note]
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
105 elif ev.type == 'end_of_track':
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
106 print 'EOT, not flushing, check for missed notes'
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
107 for chan in channels:
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
108 for ev in chan:
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
109 print ev
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
110
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
111 for svg in pages:
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
112 if not svg:
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
113 asdasd
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
114 continue
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
115 svg.save()
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
116
2
b1aac55d2864 Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
117 print 'Playable count:', playablecount
b1aac55d2864 Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
118 print 'Unplayable count:', unplayablecount
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
119
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
120 if __name__ == '__main__':
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
121 main()