Mercurial > ~darius > hgwebdir.cgi > musiccutter
annotate 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 |
rev | line source |
---|---|
0 | 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 | |
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 | 11 midi2note, note2midi = genmidi2note() |
12 note2slot = loadnote2slot(conf['notefile'], note2midi) | |
2
b1aac55d2864
Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
13 midi2svg('test.midi', 'test%d.svg', conf['pitch'], midi2note, note2midi, note2slot, conf['pagewidth'], conf['pageheight']) |
0 | 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 | |
2
b1aac55d2864
Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
47 def emitnote(svg, slot, start, notelen, pageheight): |
1 | 48 # Let us say 185 ticks for a quaver and that is 0.5cm long |
49 tickscale = 185.0 / 0.5 | |
50 pitch = 0.55 | |
51 offset = 0.60 | |
0 | 52 x = start / tickscale |
2
b1aac55d2864
Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
53 y = pageheight - (offset + slot * pitch) |
0 | 54 w = notelen / tickscale |
2
b1aac55d2864
Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
55 h = pitch |
1 | 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)), | |
58 size = ('%.3fcm' % (w), '%.3fcm' % (h)), | |
59 stroke = 'red', stroke_width = '1px', fill = 'red')) | |
0 | 60 |
2
b1aac55d2864
Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
61 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
|
62 playablecount = 0 |
b1aac55d2864
Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
63 unplayablecount = 0 |
0 | 64 midi = mido.MidiFile(inf) |
2
b1aac55d2864
Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
65 svg = svgwrite.Drawing(outpat % (0), size = ('%.3fcm' % (pagewidth), '%.3fcm' % (pageheight))) |
0 | 66 tnum = 0 |
2
b1aac55d2864
Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
67 |
0 | 68 for t in midi.tracks: |
69 print "Track", tnum | |
70 tnum += 1 | |
71 channels = [] | |
72 for i in range(16): | |
73 channels.append({}) | |
74 ctime = 0 | |
75 for ev in t: | |
76 ctime += ev.time | |
2
b1aac55d2864
Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
77 print ctime, ev |
0 | 78 if ev.type == 'note_on' and ev.velocity > 0: |
79 if ev.note in channels[ev.channel]: | |
80 print "Duplicate channel on message" | |
81 else: | |
82 channels[ev.channel][ev.note] = ctime | |
83 elif ev.type == 'note_off' or (ev.type == 'note_on' and ev.velocity == 0): | |
84 if ev.note not in channels[ev.channel]: | |
85 print 'Note_off with no corresponding note_on for track %d channel %d note %d' % (tnum, ev.channel, ev.note) | |
86 else: | |
87 note = midi2note[ev.note] | |
88 if note not in note2slot: | |
89 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
|
90 unplayablecount += 1 |
0 | 91 else: |
2
b1aac55d2864
Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
92 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
|
93 notelen = ctime - start |
0 | 94 slot = note2slot[note] |
2
b1aac55d2864
Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
95 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
|
96 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
|
97 print |
b1aac55d2864
Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
98 playablecount += 1 |
0 | 99 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
|
100 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
|
101 print 'EOT, flushing' |
b1aac55d2864
Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
102 for chan in channels: |
b1aac55d2864
Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
103 for ev in chan: |
b1aac55d2864
Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
104 print ev |
b1aac55d2864
Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
105 print |
b1aac55d2864
Parameterise page size add some more debugging.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
106 print |
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 svg.save() |
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 | 111 |
112 if __name__ == '__main__': | |
113 main() |