annotate musiccutter.py @ 1:123b42d95ab8

Use cm directly
author Daniel O'Connor <darius@dons.net.au>
date Sat, 05 Mar 2016 22:47:56 +1030
parents 0773354c7428
children b1aac55d2864
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
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
8 def test():
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
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
10 conf = { 'notefile' : 'notes', 'pitch' : 5.5 }
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)
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
13 midi2svg('test.midi', 'test%d.svg', conf['pitch'], midi2note, note2midi, note2slot)
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
14
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
15 # 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
16 def genmidi2note():
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
17 '''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
18 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
19 midi2note = {}
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
20 note2midi = {}
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
21 for midi in range(128):
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
22 octave = midi / len(names)
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
23 index = midi % len(names)
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
24 name = names[index] % (octave)
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
25 midi2note[midi] = name
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
26 note2midi[name] = midi
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
27
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
28 return midi2note, note2midi
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 def loadnote2slot(fname, note2midi):
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
31 svg = svgwrite.Drawing(fname)
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 note2slot = {}
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
34 index = 0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
35
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
36 for note in file(fname):
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
37 note = note.strip()
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
38 if note[0] == '#':
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
39 continue
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
40 if note not in note2midi:
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
41 raise exceptions.ValueError('Note \'%s\' not valid' % note)
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
42 note2slot[note] = index
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
43 index += 1
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
44
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
45 return note2slot
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 def emitnote(svg, slot, start, notelen):
1
123b42d95ab8 Use cm directly
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
48 # 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
49 tickscale = 185.0 / 0.5
123b42d95ab8 Use cm directly
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
50 pitch = 0.55
123b42d95ab8 Use cm directly
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
51 offset = 0.60
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
52 x = start / tickscale
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
53 y = offset + slot * pitch
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
54 w = notelen / tickscale
1
123b42d95ab8 Use cm directly
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
55 h = pitch / 10.0
123b42d95ab8 Use cm directly
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
56 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
57 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
58 size = ('%.3fcm' % (w), '%.3fcm' % (h)),
123b42d95ab8 Use cm directly
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
59 stroke = 'red', stroke_width = '1px', fill = 'red'))
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
60
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
61 def midi2svg(inf, outpat, pitch, midi2note, note2midi, note2slot):
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
62 midi = mido.MidiFile(inf)
1
123b42d95ab8 Use cm directly
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
63 svg = svgwrite.Drawing(outpat % (0), size = ('20.0cm', '15.5cm'))
0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
64 tnum = 0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
65 for t in midi.tracks:
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
66 print "Track", tnum
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
67 tnum += 1
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
68 channels = []
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
69 for i in range(16):
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
70 channels.append({})
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
71 ctime = 0
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
72 for ev in t:
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
73 ctime += ev.time
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
74 if ev.type == 'note_on' and ev.velocity > 0:
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
75 if ev.note in channels[ev.channel]:
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
76 print "Duplicate channel on message"
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
77 else:
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
78 channels[ev.channel][ev.note] = ctime
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
79 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
80 if ev.note not in channels[ev.channel]:
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
81 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
82 else:
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
83 note = midi2note[ev.note]
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
84 if note not in note2slot:
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
85 print 'Skipping unplayable note %s' % (note)
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
86 else:
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
87 notelen = ctime - channels[ev.channel][ev.note]
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
88 slot = note2slot[note]
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
89 print 'Note %s (%d) at %d length %d' % (note, slot, ctime, notelen)
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
90 emitnote(svg, slot, ctime, notelen)
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
91 del channels[ev.channel][ev.note]
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
92 svg.save()
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
93
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
94 if __name__ == '__main__':
0773354c7428 Initial commit, WIP.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
95 main()