annotate musiccutter.py @ 3:49a33c431b45

Accept filename for test function
author Daniel O'Connor <darius@dons.net.au>
date Mon, 07 Mar 2016 14:46:37 +1030
parents b1aac55d2864
children 9f4fa5f231e6
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
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
4 import mido
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
5 import svgwrite
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
6 import sys
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
7
3
49a33c431b45 Accept filename for test function
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
8 def test(filename = None):
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
9 # http://www.orgues-de-barbarie.com/wp-content/uploads/2014/09/format-cartons.pdf
2
b1aac55d2864 Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
10 conf = { 'notefile' : 'notes', 'pitch' : 5.5 , 'pagewidth' : 20, 'pageheight' : 15.5 }
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
11 midi2note, note2midi = genmidi2note()
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
12 note2slot = loadnote2slot(conf['notefile'], note2midi)
3
49a33c431b45 Accept filename for test function
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
13 if filename == None:
49a33c431b45 Accept filename for test function
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
14 filename = 'test.midi'
49a33c431b45 Accept filename for test function
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
15 midi2svg(filename, 'test%d.svg', conf['pitch'], midi2note, note2midi, note2slot, conf['pagewidth'], conf['pageheight'])
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
16
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
17 # 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
18 def genmidi2note():
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
19 '''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
20 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
21 midi2note = {}
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
22 note2midi = {}
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
23 for midi in range(128):
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
24 octave = midi / len(names)
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
25 index = midi % len(names)
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
26 name = names[index] % (octave)
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
27 midi2note[midi] = name
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
28 note2midi[name] = midi
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
29
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
30 return midi2note, note2midi
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
31
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
32 def loadnote2slot(fname, note2midi):
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
33 svg = svgwrite.Drawing(fname)
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 note2slot = {}
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
36 index = 0
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 for note in file(fname):
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
39 note = note.strip()
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
40 if note[0] == '#':
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
41 continue
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
42 if note not in note2midi:
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
43 raise exceptions.ValueError('Note \'%s\' not valid' % note)
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
44 note2slot[note] = index
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
45 index += 1
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
46
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
47 return note2slot
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
48
2
b1aac55d2864 Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
49 def emitnote(svg, slot, start, notelen, pageheight):
1
123b42d95ab8 Use cm directly
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
50 # Let us say 185 ticks for a quaver and that is 0.5cm long
123b42d95ab8 Use cm directly
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
51 tickscale = 185.0 / 0.5
123b42d95ab8 Use cm directly
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
52 pitch = 0.55
123b42d95ab8 Use cm directly
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
53 offset = 0.60
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
54 x = start / tickscale
2
b1aac55d2864 Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
55 y = pageheight - (offset + slot * pitch)
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
56 w = notelen / tickscale
2
b1aac55d2864 Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
57 h = pitch
1
123b42d95ab8 Use cm directly
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
58 print 'x = %.3f y = %.3f w = %.3f h = %.3f' % (x, y, w, h)
123b42d95ab8 Use cm directly
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
59 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
60 size = ('%.3fcm' % (w), '%.3fcm' % (h)),
123b42d95ab8 Use cm directly
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
61 stroke = 'red', stroke_width = '1px', fill = 'red'))
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
62
2
b1aac55d2864 Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
63 def midi2svg(inf, outpat, pitch, midi2note, note2midi, note2slot, pagewidth, pageheight):
b1aac55d2864 Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
64 playablecount = 0
b1aac55d2864 Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
65 unplayablecount = 0
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
66 midi = mido.MidiFile(inf)
2
b1aac55d2864 Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
67 svg = svgwrite.Drawing(outpat % (0), size = ('%.3fcm' % (pagewidth), '%.3fcm' % (pageheight)))
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
68 tnum = 0
2
b1aac55d2864 Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
69
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
70 for t in midi.tracks:
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
71 print "Track", tnum
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
72 tnum += 1
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
73 channels = []
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
74 for i in range(16):
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
75 channels.append({})
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
76 ctime = 0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
77 for ev in t:
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
78 ctime += ev.time
2
b1aac55d2864 Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
79 print ctime, ev
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
80 if ev.type == 'note_on' and ev.velocity > 0:
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
81 if ev.note in channels[ev.channel]:
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
82 print "Duplicate channel on message"
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
83 else:
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
84 channels[ev.channel][ev.note] = ctime
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
85 elif ev.type == 'note_off' or (ev.type == 'note_on' and ev.velocity == 0):
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
86 if ev.note not in channels[ev.channel]:
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
87 print 'Note_off with no corresponding note_on for track %d channel %d note %d' % (tnum, ev.channel, ev.note)
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
88 else:
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
89 note = midi2note[ev.note]
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
90 if note not in note2slot:
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
91 print 'Skipping unplayable note %s' % (note)
2
b1aac55d2864 Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
92 unplayablecount += 1
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
93 else:
2
b1aac55d2864 Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
94 start = channels[ev.channel][ev.note]
b1aac55d2864 Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
95 notelen = ctime - start
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
96 slot = note2slot[note]
2
b1aac55d2864 Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
97 print 'Note %s (%d) at %d length %d' % (note, slot, start, notelen)
b1aac55d2864 Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
98 emitnote(svg, slot, start, notelen, pageheight)
b1aac55d2864 Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
99 print
b1aac55d2864 Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
100 playablecount += 1
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
101 del channels[ev.channel][ev.note]
2
b1aac55d2864 Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
102 elif ev.type == 'end_of_track':
b1aac55d2864 Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
103 print 'EOT, flushing'
b1aac55d2864 Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
104 for chan in channels:
b1aac55d2864 Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
105 for ev in chan:
b1aac55d2864 Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
106 print ev
b1aac55d2864 Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
107 print
b1aac55d2864 Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
108 print
b1aac55d2864 Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
109 print
b1aac55d2864 Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
110 svg.save()
b1aac55d2864 Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
111 print 'Playable count:', playablecount
b1aac55d2864 Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents: 1
diff changeset
112 print 'Unplayable count:', unplayablecount
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
113
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
114 if __name__ == '__main__':
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
115 main()