comparison musiccutter.py @ 0:0773354c7428

Initial commit, WIP.
author Daniel O'Connor <darius@dons.net.au>
date Sat, 05 Mar 2016 21:59:44 +1030
parents
children 123b42d95ab8
comparison
equal deleted inserted replaced
-1:000000000000 0:0773354c7428
1 #!/usr/bin/env python
2
3 import exceptions
4 import mido
5 import svgwrite
6 import sys
7
8 def test():
9 # http://www.orgues-de-barbarie.com/wp-content/uploads/2014/09/format-cartons.pdf
10 conf = { 'notefile' : 'notes', 'pitch' : 5.5 }
11 midi2note, note2midi = genmidi2note()
12 note2slot = loadnote2slot(conf['notefile'], note2midi)
13 midi2svg('test.midi', 'test%d.svg', conf['pitch'], midi2note, note2midi, note2slot)
14
15 # http://www.electronics.dit.ie/staff/tscarff/Music_technology/midi/midi_note_numbers_for_octaves.htm
16 def genmidi2note():
17 '''Create forward & reverse tables for midi number to note name (assuming 69 == A440)'''
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']
19 midi2note = {}
20 note2midi = {}
21 for midi in range(128):
22 octave = midi / len(names)
23 index = midi % len(names)
24 name = names[index] % (octave)
25 midi2note[midi] = name
26 note2midi[name] = midi
27
28 return midi2note, note2midi
29
30 def loadnote2slot(fname, note2midi):
31 svg = svgwrite.Drawing(fname)
32
33 note2slot = {}
34 index = 0
35
36 for note in file(fname):
37 note = note.strip()
38 if note[0] == '#':
39 continue
40 if note not in note2midi:
41 raise exceptions.ValueError('Note \'%s\' not valid' % note)
42 note2slot[note] = index
43 index += 1
44
45 return note2slot
46
47 def emitnote(svg, slot, start, notelen):
48 # Let us say 185 ticks for a quaver and that is 5mm long
49 tickscale = 185.0 / 5.0
50 pitch = 5.5
51 offset = 6
52 x = start / tickscale
53 y = offset + slot * pitch
54 w = notelen / tickscale
55 h = pitch
56 print x, y, w, h
57 svg.add(svgwrite.shapes.Rect(insert = ('%.3fcm' % (x / 10, '%.3fcm' % (y / 10)), size = (w, h), stroke = 'red', stroke_width = '1px', fill = 'red'))
58
59 def midi2svg(inf, outpat, pitch, midi2note, note2midi, note2slot):
60 midi = mido.MidiFile(inf)
61 svg = svgwrite.Drawing(outpat % (0))
62 tnum = 0
63 for t in midi.tracks:
64 print "Track", tnum
65 tnum += 1
66 channels = []
67 for i in range(16):
68 channels.append({})
69 ctime = 0
70 for ev in t:
71 ctime += ev.time
72 if ev.type == 'note_on' and ev.velocity > 0:
73 if ev.note in channels[ev.channel]:
74 print "Duplicate channel on message"
75 else:
76 channels[ev.channel][ev.note] = ctime
77 elif ev.type == 'note_off' or (ev.type == 'note_on' and ev.velocity == 0):
78 if ev.note not in channels[ev.channel]:
79 print 'Note_off with no corresponding note_on for track %d channel %d note %d' % (tnum, ev.channel, ev.note)
80 else:
81 note = midi2note[ev.note]
82 if note not in note2slot:
83 print 'Skipping unplayable note %s' % (note)
84 else:
85 notelen = ctime - channels[ev.channel][ev.note]
86 slot = note2slot[note]
87 print 'Note %s (%d) at %d length %d' % (note, slot, ctime, notelen)
88 emitnote(svg, slot, ctime, notelen)
89 del channels[ev.channel][ev.note]
90 svg.save()
91
92 if __name__ == '__main__':
93 main()