comparison musiccutter.py @ 2:b1aac55d2864

Parameterise page size add some more debugging. Seems to work OK for rests etc
author Daniel O'Connor <darius@dons.net.au>
date Sun, 06 Mar 2016 22:43:28 +1030
parents 123b42d95ab8
children 49a33c431b45
comparison
equal deleted inserted replaced
1:123b42d95ab8 2:b1aac55d2864
5 import svgwrite 5 import svgwrite
6 import sys 6 import sys
7 7
8 def test(): 8 def test():
9 # http://www.orgues-de-barbarie.com/wp-content/uploads/2014/09/format-cartons.pdf 9 # http://www.orgues-de-barbarie.com/wp-content/uploads/2014/09/format-cartons.pdf
10 conf = { 'notefile' : 'notes', 'pitch' : 5.5 } 10 conf = { 'notefile' : 'notes', 'pitch' : 5.5 , 'pagewidth' : 20, 'pageheight' : 15.5 }
11 midi2note, note2midi = genmidi2note() 11 midi2note, note2midi = genmidi2note()
12 note2slot = loadnote2slot(conf['notefile'], note2midi) 12 note2slot = loadnote2slot(conf['notefile'], note2midi)
13 midi2svg('test.midi', 'test%d.svg', conf['pitch'], midi2note, note2midi, note2slot) 13 midi2svg('test.midi', 'test%d.svg', conf['pitch'], midi2note, note2midi, note2slot, conf['pagewidth'], conf['pageheight'])
14 14
15 # http://www.electronics.dit.ie/staff/tscarff/Music_technology/midi/midi_note_numbers_for_octaves.htm 15 # http://www.electronics.dit.ie/staff/tscarff/Music_technology/midi/midi_note_numbers_for_octaves.htm
16 def genmidi2note(): 16 def genmidi2note():
17 '''Create forward & reverse tables for midi number to note name (assuming 69 == A440)''' 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'] 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']
42 note2slot[note] = index 42 note2slot[note] = index
43 index += 1 43 index += 1
44 44
45 return note2slot 45 return note2slot
46 46
47 def emitnote(svg, slot, start, notelen): 47 def emitnote(svg, slot, start, notelen, pageheight):
48 # Let us say 185 ticks for a quaver and that is 0.5cm long 48 # Let us say 185 ticks for a quaver and that is 0.5cm long
49 tickscale = 185.0 / 0.5 49 tickscale = 185.0 / 0.5
50 pitch = 0.55 50 pitch = 0.55
51 offset = 0.60 51 offset = 0.60
52 x = start / tickscale 52 x = start / tickscale
53 y = offset + slot * pitch 53 y = pageheight - (offset + slot * pitch)
54 w = notelen / tickscale 54 w = notelen / tickscale
55 h = pitch / 10.0 55 h = pitch
56 print 'x = %.3f y = %.3f w = %.3f h = %.3f' % (x, y, w, h) 56 print 'x = %.3f y = %.3f w = %.3f h = %.3f' % (x, y, w, h)
57 svg.add(svgwrite.shapes.Rect(insert = ('%.3fcm' % (x), '%.3fcm' % (y)), 57 svg.add(svgwrite.shapes.Rect(insert = ('%.3fcm' % (x), '%.3fcm' % (y)),
58 size = ('%.3fcm' % (w), '%.3fcm' % (h)), 58 size = ('%.3fcm' % (w), '%.3fcm' % (h)),
59 stroke = 'red', stroke_width = '1px', fill = 'red')) 59 stroke = 'red', stroke_width = '1px', fill = 'red'))
60 60
61 def midi2svg(inf, outpat, pitch, midi2note, note2midi, note2slot): 61 def midi2svg(inf, outpat, pitch, midi2note, note2midi, note2slot, pagewidth, pageheight):
62 playablecount = 0
63 unplayablecount = 0
62 midi = mido.MidiFile(inf) 64 midi = mido.MidiFile(inf)
63 svg = svgwrite.Drawing(outpat % (0), size = ('20.0cm', '15.5cm')) 65 svg = svgwrite.Drawing(outpat % (0), size = ('%.3fcm' % (pagewidth), '%.3fcm' % (pageheight)))
64 tnum = 0 66 tnum = 0
67
65 for t in midi.tracks: 68 for t in midi.tracks:
66 print "Track", tnum 69 print "Track", tnum
67 tnum += 1 70 tnum += 1
68 channels = [] 71 channels = []
69 for i in range(16): 72 for i in range(16):
70 channels.append({}) 73 channels.append({})
71 ctime = 0 74 ctime = 0
72 for ev in t: 75 for ev in t:
73 ctime += ev.time 76 ctime += ev.time
77 print ctime, ev
74 if ev.type == 'note_on' and ev.velocity > 0: 78 if ev.type == 'note_on' and ev.velocity > 0:
75 if ev.note in channels[ev.channel]: 79 if ev.note in channels[ev.channel]:
76 print "Duplicate channel on message" 80 print "Duplicate channel on message"
77 else: 81 else:
78 channels[ev.channel][ev.note] = ctime 82 channels[ev.channel][ev.note] = ctime
81 print 'Note_off with no corresponding note_on for track %d channel %d note %d' % (tnum, ev.channel, ev.note) 85 print 'Note_off with no corresponding note_on for track %d channel %d note %d' % (tnum, ev.channel, ev.note)
82 else: 86 else:
83 note = midi2note[ev.note] 87 note = midi2note[ev.note]
84 if note not in note2slot: 88 if note not in note2slot:
85 print 'Skipping unplayable note %s' % (note) 89 print 'Skipping unplayable note %s' % (note)
90 unplayablecount += 1
86 else: 91 else:
87 notelen = ctime - channels[ev.channel][ev.note] 92 start = channels[ev.channel][ev.note]
93 notelen = ctime - start
88 slot = note2slot[note] 94 slot = note2slot[note]
89 print 'Note %s (%d) at %d length %d' % (note, slot, ctime, notelen) 95 print 'Note %s (%d) at %d length %d' % (note, slot, start, notelen)
90 emitnote(svg, slot, ctime, notelen) 96 emitnote(svg, slot, start, notelen, pageheight)
97 print
98 playablecount += 1
91 del channels[ev.channel][ev.note] 99 del channels[ev.channel][ev.note]
92 svg.save() 100 elif ev.type == 'end_of_track':
101 print 'EOT, flushing'
102 for chan in channels:
103 for ev in chan:
104 print ev
105 print
106 print
107 print
108 svg.save()
109 print 'Playable count:', playablecount
110 print 'Unplayable count:', unplayablecount
93 111
94 if __name__ == '__main__': 112 if __name__ == '__main__':
95 main() 113 main()