annotate musiccutter.py @ 5:af683606184e

Paginate with a view port, a lot slower and wastes space but handling notes between pages is trivial.
author Daniel O'Connor <darius@dons.net.au>
date Tue, 08 Mar 2016 00:43:30 +1030
parents 9f4fa5f231e6
children 31db42ce72b8
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
5
af683606184e Paginate with a view port, a lot slower and wastes space but handling
Daniel O'Connor <darius@dons.net.au>
parents: 4
diff changeset
52 def emitnote(svg, slot, start, notelen, pagewidth, pageheight, pitch, offset, timescale):
4
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
53 x = start / timescale
2
b1aac55d2864 Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
54 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
55 w = notelen / timescale
2
b1aac55d2864 Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
56 h = pitch
5
af683606184e Paginate with a view port, a lot slower and wastes space but handling
Daniel O'Connor <darius@dons.net.au>
parents: 4
diff changeset
57 print 'x = %.3f y = %.3f w = %.3f h = %.3f' % (x, y, w, h)
1
123b42d95ab8 Use cm directly
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
58 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
59 size = ('%.3fcm' % (w), '%.3fcm' % (h)),
123b42d95ab8 Use cm directly
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
60 stroke = 'red', stroke_width = '1px', fill = 'red'))
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
61
4
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
62 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
63 playablecount = 0
b1aac55d2864 Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
64 unplayablecount = 0
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
65 midi = mido.MidiFile(inf)
4
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
66 ctime = 0
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
67 channels = []
5
af683606184e Paginate with a view port, a lot slower and wastes space but handling
Daniel O'Connor <darius@dons.net.au>
parents: 4
diff changeset
68 svg = svgwrite.Drawing(outpat % (0), size = ('%.3fcm' % (pagewidth), '%.3fcm' % (pageheight)))
4
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
69 for i in range(16):
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
70 channels.append({})
2
b1aac55d2864 Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
71
4
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
72 pages = []
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
73 for ev in midi:
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
74 ctime += ev.time
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
75 #print ctime, ev
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
76 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
77 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
78 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
79 else:
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
80 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
81 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
82 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
83 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
84 else:
4
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
85 note = midi2note[ev.note]
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
86 if note not in note2slot:
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
87 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
88 unplayablecount += 1
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
89 else:
4
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
90 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
91 notelen = ctime - start
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
92 slot = note2slot[note]
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
93 #print 'Note %s (%d) at %d length %d' % (note, slot, start, notelen)
5
af683606184e Paginate with a view port, a lot slower and wastes space but handling
Daniel O'Connor <darius@dons.net.au>
parents: 4
diff changeset
94 emitnote(svg, slot, start, notelen, pagewidth, pageheight, pitch, offset, timescale)
4
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
95 playablecount += 1
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
96 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
97 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
98 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
99 for chan in channels:
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
100 for ev in chan:
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
101 print ev
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
102
5
af683606184e Paginate with a view port, a lot slower and wastes space but handling
Daniel O'Connor <darius@dons.net.au>
parents: 4
diff changeset
103 npages = int(midi.length / timescale / pagewidth + 0.5)
af683606184e Paginate with a view port, a lot slower and wastes space but handling
Daniel O'Connor <darius@dons.net.au>
parents: 4
diff changeset
104 print 'npages', npages
af683606184e Paginate with a view port, a lot slower and wastes space but handling
Daniel O'Connor <darius@dons.net.au>
parents: 4
diff changeset
105 for i in range(npages):
af683606184e Paginate with a view port, a lot slower and wastes space but handling
Daniel O'Connor <darius@dons.net.au>
parents: 4
diff changeset
106 svg.viewbox(pagewidth / 2.54 * 96.0 * i, 0, pagewidth / 2.54 * 96.0, pageheight / 2.54 * 96.0)
af683606184e Paginate with a view port, a lot slower and wastes space but handling
Daniel O'Connor <darius@dons.net.au>
parents: 4
diff changeset
107 svg.saveas(outpat % i)
4
9f4fa5f231e6 Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
108
2
b1aac55d2864 Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
109 print 'Playable count:', playablecount
b1aac55d2864 Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
110 print 'Unplayable count:', unplayablecount
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
111
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
112 if __name__ == '__main__':
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
113 main()