Mercurial > ~darius > hgwebdir.cgi > musiccutter
annotate musiccutter.py @ 43:c69e53b8917c
Read in per-job options from an ini file and add main funciton so you
can do..
env PYTHONPATH=$HOME/lib/python python ./musiccutter.py music/Rosy\'s\ Angel\ Lullaby.mid music/Rosy\'s\ Angel\ Lullaby.ini orgues-de-barbarie-27.ini music
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Tue, 28 Jun 2016 17:58:32 +0930 |
parents | 3925ac56d99e |
children |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python |
2 | |
37
c490fecec0ef
Have another crack at fixing transposition.
Daniel O'Connor <darius@dons.net.au>
parents:
36
diff
changeset
|
3 from IPython.core.debugger import Tracer |
41
21da8af1cdd2
Use ini file to hold organ specific details.
Daniel O'Connor <darius@dons.net.au>
parents:
40
diff
changeset
|
4 import ConfigParser |
0 | 5 import exceptions |
4
9f4fa5f231e6
Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents:
3
diff
changeset
|
6 import itertools |
7 | 7 import math |
0 | 8 import mido |
22
65e8298f5800
Remove extension and path name from title.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
9 import os.path |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
10 import reportlab.lib.colors |
12
6e46ceee57a7
Use correct MIDI note generation, previous version did not agree with
Daniel O'Connor <darius@dons.net.au>
parents:
11
diff
changeset
|
11 import reportlab.pdfgen.canvas |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
12 from reportlab.lib.units import mm |
0 | 13 import sys |
14 | |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
15 CUT_COLOUR = reportlab.lib.colors.red |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
16 ENGRAVE_COLOUR = reportlab.lib.colors.black |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
17 |
37
c490fecec0ef
Have another crack at fixing transposition.
Daniel O'Connor <darius@dons.net.au>
parents:
36
diff
changeset
|
18 class Stats(object): |
c490fecec0ef
Have another crack at fixing transposition.
Daniel O'Connor <darius@dons.net.au>
parents:
36
diff
changeset
|
19 pass |
c490fecec0ef
Have another crack at fixing transposition.
Daniel O'Connor <darius@dons.net.au>
parents:
36
diff
changeset
|
20 |
c490fecec0ef
Have another crack at fixing transposition.
Daniel O'Connor <darius@dons.net.au>
parents:
36
diff
changeset
|
21 class EVWrap(object): |
c490fecec0ef
Have another crack at fixing transposition.
Daniel O'Connor <darius@dons.net.au>
parents:
36
diff
changeset
|
22 def __init__(self, ev): |
c490fecec0ef
Have another crack at fixing transposition.
Daniel O'Connor <darius@dons.net.au>
parents:
36
diff
changeset
|
23 self.ev = ev |
c490fecec0ef
Have another crack at fixing transposition.
Daniel O'Connor <darius@dons.net.au>
parents:
36
diff
changeset
|
24 |
43
c69e53b8917c
Read in per-job options from an ini file and add main funciton so you
Daniel O'Connor <darius@dons.net.au>
parents:
42
diff
changeset
|
25 optionlist = { 'leadin' : int, 'timemarks' : bool, 'trytranspose' : bool, 'drawrect' : bool, 'notenames' : bool, |
c69e53b8917c
Read in per-job options from an ini file and add main funciton so you
Daniel O'Connor <darius@dons.net.au>
parents:
42
diff
changeset
|
26 'notelines' : bool, 'noteoffset' : int, 'pagesperpdf' : int, 'timescale' : float, |
c69e53b8917c
Read in per-job options from an ini file and add main funciton so you
Daniel O'Connor <darius@dons.net.au>
parents:
42
diff
changeset
|
27 'notescale' : float, 'fontname' : str, 'fontsize' : float } |
c69e53b8917c
Read in per-job options from an ini file and add main funciton so you
Daniel O'Connor <darius@dons.net.au>
parents:
42
diff
changeset
|
28 |
c69e53b8917c
Read in per-job options from an ini file and add main funciton so you
Daniel O'Connor <darius@dons.net.au>
parents:
42
diff
changeset
|
29 def main(): |
c69e53b8917c
Read in per-job options from an ini file and add main funciton so you
Daniel O'Connor <darius@dons.net.au>
parents:
42
diff
changeset
|
30 if len(sys.argv) != 5: |
c69e53b8917c
Read in per-job options from an ini file and add main funciton so you
Daniel O'Connor <darius@dons.net.au>
parents:
42
diff
changeset
|
31 print('Bad usage') |
c69e53b8917c
Read in per-job options from an ini file and add main funciton so you
Daniel O'Connor <darius@dons.net.au>
parents:
42
diff
changeset
|
32 print(' %s music.midi config.ini organconf.ini destdir' % (sys.argv[0])) |
c69e53b8917c
Read in per-job options from an ini file and add main funciton so you
Daniel O'Connor <darius@dons.net.au>
parents:
42
diff
changeset
|
33 sys.exit(1) |
c69e53b8917c
Read in per-job options from an ini file and add main funciton so you
Daniel O'Connor <darius@dons.net.au>
parents:
42
diff
changeset
|
34 |
c69e53b8917c
Read in per-job options from an ini file and add main funciton so you
Daniel O'Connor <darius@dons.net.au>
parents:
42
diff
changeset
|
35 filename, config, organconf, destdir = sys.argv[1:] |
c69e53b8917c
Read in per-job options from an ini file and add main funciton so you
Daniel O'Connor <darius@dons.net.au>
parents:
42
diff
changeset
|
36 cp = ConfigParser.ConfigParser() |
c69e53b8917c
Read in per-job options from an ini file and add main funciton so you
Daniel O'Connor <darius@dons.net.au>
parents:
42
diff
changeset
|
37 cp.read(config) |
c69e53b8917c
Read in per-job options from an ini file and add main funciton so you
Daniel O'Connor <darius@dons.net.au>
parents:
42
diff
changeset
|
38 args = {} |
c69e53b8917c
Read in per-job options from an ini file and add main funciton so you
Daniel O'Connor <darius@dons.net.au>
parents:
42
diff
changeset
|
39 for opt in optionlist: |
c69e53b8917c
Read in per-job options from an ini file and add main funciton so you
Daniel O'Connor <darius@dons.net.au>
parents:
42
diff
changeset
|
40 print opt |
c69e53b8917c
Read in per-job options from an ini file and add main funciton so you
Daniel O'Connor <darius@dons.net.au>
parents:
42
diff
changeset
|
41 if not cp.has_option('default', opt): |
c69e53b8917c
Read in per-job options from an ini file and add main funciton so you
Daniel O'Connor <darius@dons.net.au>
parents:
42
diff
changeset
|
42 print('Missing option', opt) |
c69e53b8917c
Read in per-job options from an ini file and add main funciton so you
Daniel O'Connor <darius@dons.net.au>
parents:
42
diff
changeset
|
43 sys.exit(1) |
c69e53b8917c
Read in per-job options from an ini file and add main funciton so you
Daniel O'Connor <darius@dons.net.au>
parents:
42
diff
changeset
|
44 if optionlist[opt] == int: |
c69e53b8917c
Read in per-job options from an ini file and add main funciton so you
Daniel O'Connor <darius@dons.net.au>
parents:
42
diff
changeset
|
45 fn = cp.getint |
c69e53b8917c
Read in per-job options from an ini file and add main funciton so you
Daniel O'Connor <darius@dons.net.au>
parents:
42
diff
changeset
|
46 elif optionlist[opt] == float: |
c69e53b8917c
Read in per-job options from an ini file and add main funciton so you
Daniel O'Connor <darius@dons.net.au>
parents:
42
diff
changeset
|
47 fn = cp.getfloat |
c69e53b8917c
Read in per-job options from an ini file and add main funciton so you
Daniel O'Connor <darius@dons.net.au>
parents:
42
diff
changeset
|
48 elif optionlist[opt] == bool: |
c69e53b8917c
Read in per-job options from an ini file and add main funciton so you
Daniel O'Connor <darius@dons.net.au>
parents:
42
diff
changeset
|
49 fn = cp.getboolean |
c69e53b8917c
Read in per-job options from an ini file and add main funciton so you
Daniel O'Connor <darius@dons.net.au>
parents:
42
diff
changeset
|
50 elif optionlist[opt] == str: |
c69e53b8917c
Read in per-job options from an ini file and add main funciton so you
Daniel O'Connor <darius@dons.net.au>
parents:
42
diff
changeset
|
51 fn = cp.get |
c69e53b8917c
Read in per-job options from an ini file and add main funciton so you
Daniel O'Connor <darius@dons.net.au>
parents:
42
diff
changeset
|
52 else: |
c69e53b8917c
Read in per-job options from an ini file and add main funciton so you
Daniel O'Connor <darius@dons.net.au>
parents:
42
diff
changeset
|
53 print('unknown type') |
c69e53b8917c
Read in per-job options from an ini file and add main funciton so you
Daniel O'Connor <darius@dons.net.au>
parents:
42
diff
changeset
|
54 sys.exit(1) |
c69e53b8917c
Read in per-job options from an ini file and add main funciton so you
Daniel O'Connor <darius@dons.net.au>
parents:
42
diff
changeset
|
55 try: |
c69e53b8917c
Read in per-job options from an ini file and add main funciton so you
Daniel O'Connor <darius@dons.net.au>
parents:
42
diff
changeset
|
56 args[opt] = fn('default', opt) |
c69e53b8917c
Read in per-job options from an ini file and add main funciton so you
Daniel O'Connor <darius@dons.net.au>
parents:
42
diff
changeset
|
57 except ValueError: |
c69e53b8917c
Read in per-job options from an ini file and add main funciton so you
Daniel O'Connor <darius@dons.net.au>
parents:
42
diff
changeset
|
58 print('Error parsing', opt) |
c69e53b8917c
Read in per-job options from an ini file and add main funciton so you
Daniel O'Connor <darius@dons.net.au>
parents:
42
diff
changeset
|
59 sys.exit(1) |
c69e53b8917c
Read in per-job options from an ini file and add main funciton so you
Daniel O'Connor <darius@dons.net.au>
parents:
42
diff
changeset
|
60 args['organconf'] = organconf |
c69e53b8917c
Read in per-job options from an ini file and add main funciton so you
Daniel O'Connor <darius@dons.net.au>
parents:
42
diff
changeset
|
61 m = Midi2PDF(**args) |
c69e53b8917c
Read in per-job options from an ini file and add main funciton so you
Daniel O'Connor <darius@dons.net.au>
parents:
42
diff
changeset
|
62 base, ext = os.path.splitext(filename) |
c69e53b8917c
Read in per-job options from an ini file and add main funciton so you
Daniel O'Connor <darius@dons.net.au>
parents:
42
diff
changeset
|
63 base = os.path.basename(base) |
c69e53b8917c
Read in per-job options from an ini file and add main funciton so you
Daniel O'Connor <darius@dons.net.au>
parents:
42
diff
changeset
|
64 m.processMidi(filename, destdir + '/' + base + '-%02d.pdf') |
c69e53b8917c
Read in per-job options from an ini file and add main funciton so you
Daniel O'Connor <darius@dons.net.au>
parents:
42
diff
changeset
|
65 |
37
c490fecec0ef
Have another crack at fixing transposition.
Daniel O'Connor <darius@dons.net.au>
parents:
36
diff
changeset
|
66 def test(filename = None, shift = 0): |
3
49a33c431b45
Accept filename for test function
Daniel O'Connor <darius@dons.net.au>
parents:
2
diff
changeset
|
67 if filename == None: |
49a33c431b45
Accept filename for test function
Daniel O'Connor <darius@dons.net.au>
parents:
2
diff
changeset
|
68 filename = 'test.midi' |
7 | 69 # Card layout from http://www.orgues-de-barbarie.com/wp-content/uploads/2014/09/format-cartons.pdf |
19
ffaf97818ce3
- Music needs to run right to left.
Daniel O'Connor <darius@dons.net.au>
parents:
18
diff
changeset
|
70 # Notes are read from right to left |
24
71b78f06ff03
Highest notes are at the bottom not the top.
Daniel O'Connor <darius@dons.net.au>
parents:
23
diff
changeset
|
71 # Highest note is at the bottom (closest to the crank) |
32
f053dd6e9e68
Add l33t ASCII diagram to show how folds should be
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
72 # fold fold |
f053dd6e9e68
Add l33t ASCII diagram to show how folds should be
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
73 # in out |
f053dd6e9e68
Add l33t ASCII diagram to show how folds should be
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
74 # V ^ |
f053dd6e9e68
Add l33t ASCII diagram to show how folds should be
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
75 # +---+---+---+ lowest note |
f053dd6e9e68
Add l33t ASCII diagram to show how folds should be
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
76 # | | | | |
f053dd6e9e68
Add l33t ASCII diagram to show how folds should be
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
77 # +---+---+---+ highest note |
f053dd6e9e68
Add l33t ASCII diagram to show how folds should be
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
78 # |
40
5c47f9361d93
Use unpacked argument lists to make it easier to work out which
Daniel O'Connor <darius@dons.net.au>
parents:
39
diff
changeset
|
79 m = Midi2PDF(**{ |
43
c69e53b8917c
Read in per-job options from an ini file and add main funciton so you
Daniel O'Connor <darius@dons.net.au>
parents:
42
diff
changeset
|
80 'organconf' : 'orgues-de-barbarie-27.ini', |
40
5c47f9361d93
Use unpacked argument lists to make it easier to work out which
Daniel O'Connor <darius@dons.net.au>
parents:
39
diff
changeset
|
81 'leadin' : 50, |
5c47f9361d93
Use unpacked argument lists to make it easier to work out which
Daniel O'Connor <darius@dons.net.au>
parents:
39
diff
changeset
|
82 'timemarks' : False, |
5c47f9361d93
Use unpacked argument lists to make it easier to work out which
Daniel O'Connor <darius@dons.net.au>
parents:
39
diff
changeset
|
83 'trytranspose' : True, |
5c47f9361d93
Use unpacked argument lists to make it easier to work out which
Daniel O'Connor <darius@dons.net.au>
parents:
39
diff
changeset
|
84 'drawrect' : False, |
5c47f9361d93
Use unpacked argument lists to make it easier to work out which
Daniel O'Connor <darius@dons.net.au>
parents:
39
diff
changeset
|
85 'notenames' : False, |
5c47f9361d93
Use unpacked argument lists to make it easier to work out which
Daniel O'Connor <darius@dons.net.au>
parents:
39
diff
changeset
|
86 'notelines' : False, |
5c47f9361d93
Use unpacked argument lists to make it easier to work out which
Daniel O'Connor <darius@dons.net.au>
parents:
39
diff
changeset
|
87 'noteoffset' : shift, |
5c47f9361d93
Use unpacked argument lists to make it easier to work out which
Daniel O'Connor <darius@dons.net.au>
parents:
39
diff
changeset
|
88 'pagesperpdf' : 1, |
43
c69e53b8917c
Read in per-job options from an ini file and add main funciton so you
Daniel O'Connor <darius@dons.net.au>
parents:
42
diff
changeset
|
89 'timescale' : 35, |
40
5c47f9361d93
Use unpacked argument lists to make it easier to work out which
Daniel O'Connor <darius@dons.net.au>
parents:
39
diff
changeset
|
90 'notescale' : 0.9, |
5c47f9361d93
Use unpacked argument lists to make it easier to work out which
Daniel O'Connor <darius@dons.net.au>
parents:
39
diff
changeset
|
91 'fontname' : 'Helvetica', |
5c47f9361d93
Use unpacked argument lists to make it easier to work out which
Daniel O'Connor <darius@dons.net.au>
parents:
39
diff
changeset
|
92 'fontsize' : 12, |
5c47f9361d93
Use unpacked argument lists to make it easier to work out which
Daniel O'Connor <darius@dons.net.au>
parents:
39
diff
changeset
|
93 }) |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
94 base, ext = os.path.splitext(filename) |
22
65e8298f5800
Remove extension and path name from title.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
95 base = os.path.basename(base) |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
96 m.processMidi(filename, base + '-%02d.pdf') |
0 | 97 |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
98 class Midi2PDF(object): |
43
c69e53b8917c
Read in per-job options from an ini file and add main funciton so you
Daniel O'Connor <darius@dons.net.au>
parents:
42
diff
changeset
|
99 def __init__(self, organconf, leadin, timemarks, trytranspose, drawrect, notenames, notelines, noteoffset, pagesperpdf, timescale, notescale, fontname, fontsize): |
41
21da8af1cdd2
Use ini file to hold organ specific details.
Daniel O'Connor <darius@dons.net.au>
parents:
40
diff
changeset
|
100 cp = ConfigParser.ConfigParser() |
43
c69e53b8917c
Read in per-job options from an ini file and add main funciton so you
Daniel O'Connor <darius@dons.net.au>
parents:
42
diff
changeset
|
101 cp.read(organconf) |
41
21da8af1cdd2
Use ini file to hold organ specific details.
Daniel O'Connor <darius@dons.net.au>
parents:
40
diff
changeset
|
102 self.pagewidth = cp.getfloat('default', 'pagewidth') |
21da8af1cdd2
Use ini file to hold organ specific details.
Daniel O'Connor <darius@dons.net.au>
parents:
40
diff
changeset
|
103 self.pageheight = cp.getfloat('default', 'pageheight') |
21da8af1cdd2
Use ini file to hold organ specific details.
Daniel O'Connor <darius@dons.net.au>
parents:
40
diff
changeset
|
104 self.pitch = cp.getfloat('default', 'pitch') |
21da8af1cdd2
Use ini file to hold organ specific details.
Daniel O'Connor <darius@dons.net.au>
parents:
40
diff
changeset
|
105 self.slotsize = cp.getfloat('default', 'slotsize') |
21da8af1cdd2
Use ini file to hold organ specific details.
Daniel O'Connor <darius@dons.net.au>
parents:
40
diff
changeset
|
106 self.heel = cp.getfloat('default', 'heel') |
19
ffaf97818ce3
- Music needs to run right to left.
Daniel O'Connor <darius@dons.net.au>
parents:
18
diff
changeset
|
107 self.leadin = leadin # Extra at the start |
ffaf97818ce3
- Music needs to run right to left.
Daniel O'Connor <darius@dons.net.au>
parents:
18
diff
changeset
|
108 self.timemarks = timemarks # Draw vertical time lines |
30
f46cc9401e79
Make transposition optional and add note scale parameter (to get rearticulation)
Daniel O'Connor <darius@dons.net.au>
parents:
29
diff
changeset
|
109 self.trytranspose = trytranspose # Attempt to tranpose unplayable notes |
19
ffaf97818ce3
- Music needs to run right to left.
Daniel O'Connor <darius@dons.net.au>
parents:
18
diff
changeset
|
110 self.drawrect = drawrect # Draw rectangle around each page |
20
9bb72ae63651
- Make various things optional.
Daniel O'Connor <darius@dons.net.au>
parents:
19
diff
changeset
|
111 self.notenames = notenames # Draw note names on the right edge |
9bb72ae63651
- Make various things optional.
Daniel O'Connor <darius@dons.net.au>
parents:
19
diff
changeset
|
112 self.notelines = notelines # Draw line rulers |
27
87cf66e04ef9
Handle offsetting note pitch in a better way.
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
113 self.noteoffset = noteoffset # Amount to adjust note pitches by (+12 = 1 octave) |
31
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
114 self.pagesperpdf = pagesperpdf # Number of pages to emit per PDF |
16
20337b22977d
Add left margin and slot size support.
Daniel O'Connor <darius@dons.net.au>
parents:
14
diff
changeset
|
115 self.timescale = timescale # Width per second |
30
f46cc9401e79
Make transposition optional and add note scale parameter (to get rearticulation)
Daniel O'Connor <darius@dons.net.au>
parents:
29
diff
changeset
|
116 self.notescale = notescale # Multiply all note lengths by this (to get rearticulation) |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
117 self.fontname = fontname |
16
20337b22977d
Add left margin and slot size support.
Daniel O'Connor <darius@dons.net.au>
parents:
14
diff
changeset
|
118 self.fontsize = fontsize # Points |
0 | 119 |
31
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
120 self.pdfwidth = self.pagewidth * self.pagesperpdf |
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
121 |
41
21da8af1cdd2
Use ini file to hold organ specific details.
Daniel O'Connor <darius@dons.net.au>
parents:
40
diff
changeset
|
122 self.midi2note, self.note2midi = Midi2PDF.genmidi2note() |
21da8af1cdd2
Use ini file to hold organ specific details.
Daniel O'Connor <darius@dons.net.au>
parents:
40
diff
changeset
|
123 self.note2slot, self.slot2note = Midi2PDF.loadnote2slot(cp.get('default', 'notes').split(), self.note2midi) |
21da8af1cdd2
Use ini file to hold organ specific details.
Daniel O'Connor <darius@dons.net.au>
parents:
40
diff
changeset
|
124 |
7 | 125 def processMidi(self, midifile, outpat): |
42 | 126 self.stats = Stats() |
127 self.stats.playablecount = 0 | |
128 self.stats.unplayablecount = 0 | |
129 self.stats.transposeupcount = 0 | |
130 self.stats.transposedowncount = 0 | |
7 | 131 midi = mido.MidiFile(midifile) |
42 | 132 self.ctime = 0 |
133 self.channels = [] | |
134 for i in range(16): # 16 is the maximum number of MIDI channels | |
135 self.channels.append({}) | |
0 | 136 |
19
ffaf97818ce3
- Music needs to run right to left.
Daniel O'Connor <darius@dons.net.au>
parents:
18
diff
changeset
|
137 npages = int(math.ceil(((midi.length * self.timescale) + self.leadin) / self.pagewidth)) |
31
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
138 npdfs = int(math.ceil(float(npages) / self.pagesperpdf)) |
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
139 print 'npages %d, npdfs %d' % (npages, npdfs) |
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
140 |
42 | 141 self.pdfs = [] |
31
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
142 for i in range(npdfs): |
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
143 pdf = reportlab.pdfgen.canvas.Canvas(file(outpat % (i + 1), 'w'), |
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
144 pagesize = (self.pdfwidth * mm, self.pageheight * mm)) |
42 | 145 self.pdfs.append(pdf) |
0 | 146 |
22
65e8298f5800
Remove extension and path name from title.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
147 title = os.path.basename(midifile) |
65e8298f5800
Remove extension and path name from title.
Daniel O'Connor <darius@dons.net.au>
parents:
21
diff
changeset
|
148 title, ext = os.path.splitext(title) |
7 | 149 for ev in midi: |
38
9e8ed92b477c
Re-jig note translation to only happen when we are going to emit a
Daniel O'Connor <darius@dons.net.au>
parents:
37
diff
changeset
|
150 # Adjust pitch |
9e8ed92b477c
Re-jig note translation to only happen when we are going to emit a
Daniel O'Connor <darius@dons.net.au>
parents:
37
diff
changeset
|
151 if hasattr(ev, 'note'): |
9e8ed92b477c
Re-jig note translation to only happen when we are going to emit a
Daniel O'Connor <darius@dons.net.au>
parents:
37
diff
changeset
|
152 ev.note += self.noteoffset |
42 | 153 self.ctime += ev.time |
154 print '%.03f: %s' % (self.ctime, str(ev)) | |
38
9e8ed92b477c
Re-jig note translation to only happen when we are going to emit a
Daniel O'Connor <darius@dons.net.au>
parents:
37
diff
changeset
|
155 #Tracer()() |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
156 |
42 | 157 if ev.type == 'text' and self.ctime == 0: |
38
9e8ed92b477c
Re-jig note translation to only happen when we are going to emit a
Daniel O'Connor <darius@dons.net.au>
parents:
37
diff
changeset
|
158 title = ev.text |
9e8ed92b477c
Re-jig note translation to only happen when we are going to emit a
Daniel O'Connor <darius@dons.net.au>
parents:
37
diff
changeset
|
159 if ev.type == 'note_on' and ev.velocity > 0: |
42 | 160 if ev.note in self.channels[ev.channel]: |
161 print 'Duplicate note_on message at %.1f sec channel %d note %d' % (self.ctime, ev.channel, ev.note) | |
38
9e8ed92b477c
Re-jig note translation to only happen when we are going to emit a
Daniel O'Connor <darius@dons.net.au>
parents:
37
diff
changeset
|
162 else: |
42 | 163 # Store start time |
164 self.channels[ev.channel][ev.note] = self.ctime | |
165 # note_on with velocity of 0 is a note_off | |
38
9e8ed92b477c
Re-jig note translation to only happen when we are going to emit a
Daniel O'Connor <darius@dons.net.au>
parents:
37
diff
changeset
|
166 elif ev.type == 'note_off' or (ev.type == 'note_on' and ev.velocity == 0): |
42 | 167 if ev.note not in self.channels[ev.channel]: |
39 | 168 # These can be rests (iWriteMusic) |
42 | 169 print 'note_off with no corresponding note_on at %.1f sec for channel %d note %d' % (self.ctime, ev.channel, ev.note) |
38
9e8ed92b477c
Re-jig note translation to only happen when we are going to emit a
Daniel O'Connor <darius@dons.net.au>
parents:
37
diff
changeset
|
170 continue |
0 | 171 else: |
42 | 172 orignote = self.emitnote(ev) |
173 del self.channels[ev.channel][orignote] | |
38
9e8ed92b477c
Re-jig note translation to only happen when we are going to emit a
Daniel O'Connor <darius@dons.net.au>
parents:
37
diff
changeset
|
174 elif ev.type == 'end_of_track': |
42 | 175 for chan in self.channels: |
38
9e8ed92b477c
Re-jig note translation to only happen when we are going to emit a
Daniel O'Connor <darius@dons.net.au>
parents:
37
diff
changeset
|
176 for ev in chan: |
42 | 177 print 'Flushed', ev |
178 self.emitnote(ev) | |
38
9e8ed92b477c
Re-jig note translation to only happen when we are going to emit a
Daniel O'Connor <darius@dons.net.au>
parents:
37
diff
changeset
|
179 print ev |
7 | 180 |
42 | 181 print 'Playable count:', self.stats.playablecount |
182 print 'Unplayable count:', self.stats.unplayablecount | |
30
f46cc9401e79
Make transposition optional and add note scale parameter (to get rearticulation)
Daniel O'Connor <darius@dons.net.au>
parents:
29
diff
changeset
|
183 if self.trytranspose: |
42 | 184 print 'Transpose down:', self.stats.transposedowncount |
185 print 'Transpose up:', self.stats.transposeupcount | |
7 | 186 |
39 | 187 # Do per-page things |
31
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
188 for pindx in range(npages): |
42 | 189 pdf = self.pdfs[pindx / self.pagesperpdf] # PDF for this page |
31
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
190 # Offset into PDF where the page starts |
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
191 pageofs = self.pagewidth * (self.pagesperpdf - (pindx % self.pagesperpdf) - 1) |
13
5f4c21bb5140
Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
192 # Add title and page number |
31
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
193 Midi2PDF.textHelper(pdf, pageofs * mm, 1 * mm, |
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
194 ENGRAVE_COLOUR, True, self.fontname, self.fontsize, |
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
195 '%s (%d / %d)' % (title, pindx + 1, npages)) |
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
196 pdf.saveState() |
13
5f4c21bb5140
Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
197 pdf.setLineWidth(0) |
17
c50427f1da2d
- Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
198 |
31
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
199 # Draw time marks every 5 seconds |
19
ffaf97818ce3
- Music needs to run right to left.
Daniel O'Connor <darius@dons.net.au>
parents:
18
diff
changeset
|
200 if self.timemarks: |
31
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
201 pdfidx = pindx / self.pagesperpdf |
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
202 # Work out start and end times (pdf 1 is special due to leadin) |
19
ffaf97818ce3
- Music needs to run right to left.
Daniel O'Connor <darius@dons.net.au>
parents:
18
diff
changeset
|
203 tstart = self.leadin / self.timescale |
31
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
204 tend = (self.pagewidth * self.pagesperpdf) / self.timescale |
18
cf93a76eda92
The laser cutter software barfs on these lines and stops processing
Daniel O'Connor <darius@dons.net.au>
parents:
17
diff
changeset
|
205 if pindx > 0: |
31
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
206 tsize = self.pagewidth / self.timescale # Amount of time per pdf |
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
207 tstart = tend + tsize * pdfidx |
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
208 tend = tend + tsize * (pdfidx + 1) |
18
cf93a76eda92
The laser cutter software barfs on these lines and stops processing
Daniel O'Connor <darius@dons.net.au>
parents:
17
diff
changeset
|
209 for s in range(tstart, tend, 5): |
19
ffaf97818ce3
- Music needs to run right to left.
Daniel O'Connor <darius@dons.net.au>
parents:
18
diff
changeset
|
210 x = self.pagewidth - (float(s * self.timescale + self.leadin) % self.pagewidth) |
24
71b78f06ff03
Highest notes are at the bottom not the top.
Daniel O'Connor <darius@dons.net.au>
parents:
23
diff
changeset
|
211 pdf.line(x * mm, self.heel, x * mm, self.pageheight * mm) |
18
cf93a76eda92
The laser cutter software barfs on these lines and stops processing
Daniel O'Connor <darius@dons.net.au>
parents:
17
diff
changeset
|
212 Midi2PDF.textHelper(pdf, x * mm, 1 * mm, ENGRAVE_COLOUR, False, self.fontname, self.fontsize, str(s) + 's') |
cf93a76eda92
The laser cutter software barfs on these lines and stops processing
Daniel O'Connor <darius@dons.net.au>
parents:
17
diff
changeset
|
213 |
25 | 214 # Draw rectangle around page (upper and right hand ends don't seem to render though) |
19
ffaf97818ce3
- Music needs to run right to left.
Daniel O'Connor <darius@dons.net.au>
parents:
18
diff
changeset
|
215 if self.drawrect: |
31
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
216 pdf.rect((pindx % self.pagesperpdf) * self.pagewidth * mm, 0, |
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
217 self.pagewidth * mm, self.pageheight * mm, fill = False, stroke = True) |
18
cf93a76eda92
The laser cutter software barfs on these lines and stops processing
Daniel O'Connor <darius@dons.net.au>
parents:
17
diff
changeset
|
218 |
13
5f4c21bb5140
Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
219 # Draw lines per note |
5f4c21bb5140
Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
220 for slot in sorted(self.slot2note.keys()): |
26
f492b70f5e49
Fix notename/line drawing.
Daniel O'Connor <darius@dons.net.au>
parents:
25
diff
changeset
|
221 ofs = self.pageheight - (self.heel + slot * self.pitch) - self.slotsize / 2 |
20
9bb72ae63651
- Make various things optional.
Daniel O'Connor <darius@dons.net.au>
parents:
19
diff
changeset
|
222 if self.notelines: |
31
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
223 pdf.line(0, ofs * mm, self.pdfwidth * mm, ofs * mm) |
13
5f4c21bb5140
Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
224 # Note name |
20
9bb72ae63651
- Make various things optional.
Daniel O'Connor <darius@dons.net.au>
parents:
19
diff
changeset
|
225 if self.notenames: |
31
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
226 Midi2PDF.textHelper(pdf, (self.pdfwidth - 10) * mm, (ofs + 0.5) * mm, ENGRAVE_COLOUR, False, self.fontname, self.fontsize, self.slot2note[slot]) |
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
227 pdf.restoreState() |
42 | 228 for pdf in self.pdfs: |
26
f492b70f5e49
Fix notename/line drawing.
Daniel O'Connor <darius@dons.net.au>
parents:
25
diff
changeset
|
229 pdf.save() |
7 | 230 |
37
c490fecec0ef
Have another crack at fixing transposition.
Daniel O'Connor <darius@dons.net.au>
parents:
36
diff
changeset
|
231 def noteisplayable(self, midi): |
42 | 232 #Tracer()() |
37
c490fecec0ef
Have another crack at fixing transposition.
Daniel O'Connor <darius@dons.net.au>
parents:
36
diff
changeset
|
233 if midi in self.midi2note: |
c490fecec0ef
Have another crack at fixing transposition.
Daniel O'Connor <darius@dons.net.au>
parents:
36
diff
changeset
|
234 notename = self.midi2note[midi] |
c490fecec0ef
Have another crack at fixing transposition.
Daniel O'Connor <darius@dons.net.au>
parents:
36
diff
changeset
|
235 if notename in self.note2slot: |
42 | 236 return True |
37
c490fecec0ef
Have another crack at fixing transposition.
Daniel O'Connor <darius@dons.net.au>
parents:
36
diff
changeset
|
237 |
42 | 238 return False |
37
c490fecec0ef
Have another crack at fixing transposition.
Daniel O'Connor <darius@dons.net.au>
parents:
36
diff
changeset
|
239 |
39 | 240 # Check if the organ can play the note |
37
c490fecec0ef
Have another crack at fixing transposition.
Daniel O'Connor <darius@dons.net.au>
parents:
36
diff
changeset
|
241 def transposenote(self, evw, amount): |
c490fecec0ef
Have another crack at fixing transposition.
Daniel O'Connor <darius@dons.net.au>
parents:
36
diff
changeset
|
242 evw.ev.note += amount |
c490fecec0ef
Have another crack at fixing transposition.
Daniel O'Connor <darius@dons.net.au>
parents:
36
diff
changeset
|
243 evw.notename = self.midi2note[evw.ev.note] |
c490fecec0ef
Have another crack at fixing transposition.
Daniel O'Connor <darius@dons.net.au>
parents:
36
diff
changeset
|
244 evw.slot = self.note2slot[evw.notename] |
c490fecec0ef
Have another crack at fixing transposition.
Daniel O'Connor <darius@dons.net.au>
parents:
36
diff
changeset
|
245 |
39 | 246 # Work out which slot to use for the note, transpose if desired |
42 | 247 def getslotfornote(self, evw): |
37
c490fecec0ef
Have another crack at fixing transposition.
Daniel O'Connor <darius@dons.net.au>
parents:
36
diff
changeset
|
248 evw.slot = None |
c490fecec0ef
Have another crack at fixing transposition.
Daniel O'Connor <darius@dons.net.au>
parents:
36
diff
changeset
|
249 evw.notename = None |
c490fecec0ef
Have another crack at fixing transposition.
Daniel O'Connor <darius@dons.net.au>
parents:
36
diff
changeset
|
250 |
c490fecec0ef
Have another crack at fixing transposition.
Daniel O'Connor <darius@dons.net.au>
parents:
36
diff
changeset
|
251 # First off, is the note in our midi table? |
c490fecec0ef
Have another crack at fixing transposition.
Daniel O'Connor <darius@dons.net.au>
parents:
36
diff
changeset
|
252 if evw.ev.note in self.midi2note: |
c490fecec0ef
Have another crack at fixing transposition.
Daniel O'Connor <darius@dons.net.au>
parents:
36
diff
changeset
|
253 evw.notename = self.midi2note[evw.ev.note] |
c490fecec0ef
Have another crack at fixing transposition.
Daniel O'Connor <darius@dons.net.au>
parents:
36
diff
changeset
|
254 # Is it playable? |
42 | 255 if self.noteisplayable(evw.ev.note): |
37
c490fecec0ef
Have another crack at fixing transposition.
Daniel O'Connor <darius@dons.net.au>
parents:
36
diff
changeset
|
256 evw.slot = self.note2slot[evw.notename] |
c490fecec0ef
Have another crack at fixing transposition.
Daniel O'Connor <darius@dons.net.au>
parents:
36
diff
changeset
|
257 # Nope, maybe we can transpose? |
c490fecec0ef
Have another crack at fixing transposition.
Daniel O'Connor <darius@dons.net.au>
parents:
36
diff
changeset
|
258 elif self.trytranspose: |
c490fecec0ef
Have another crack at fixing transposition.
Daniel O'Connor <darius@dons.net.au>
parents:
36
diff
changeset
|
259 # Go for -3 to +3 octaves (going down first) |
c490fecec0ef
Have another crack at fixing transposition.
Daniel O'Connor <darius@dons.net.au>
parents:
36
diff
changeset
|
260 for i in [-12, -24, -36, 12, 24, 36]: |
42 | 261 if self.noteisplayable(evw.ev.note + i): |
37
c490fecec0ef
Have another crack at fixing transposition.
Daniel O'Connor <darius@dons.net.au>
parents:
36
diff
changeset
|
262 self.transposenote(evw, i) |
c490fecec0ef
Have another crack at fixing transposition.
Daniel O'Connor <darius@dons.net.au>
parents:
36
diff
changeset
|
263 if i < 0: |
42 | 264 self.stats.transposedowncount += 1 |
37
c490fecec0ef
Have another crack at fixing transposition.
Daniel O'Connor <darius@dons.net.au>
parents:
36
diff
changeset
|
265 tmp = 'down' |
c490fecec0ef
Have another crack at fixing transposition.
Daniel O'Connor <darius@dons.net.au>
parents:
36
diff
changeset
|
266 else: |
42 | 267 self.stats.transposeupcount += 1 |
37
c490fecec0ef
Have another crack at fixing transposition.
Daniel O'Connor <darius@dons.net.au>
parents:
36
diff
changeset
|
268 tmp = 'up' |
c490fecec0ef
Have another crack at fixing transposition.
Daniel O'Connor <darius@dons.net.au>
parents:
36
diff
changeset
|
269 print 'Transposed note at %.1f sec %s (%d) %s %d octave(s) to %s (%d)' % ( |
42 | 270 self.ctime, self.midi2note[evw.ev.note - i], evw.ev.note - i, tmp, |
37
c490fecec0ef
Have another crack at fixing transposition.
Daniel O'Connor <darius@dons.net.au>
parents:
36
diff
changeset
|
271 abs(i / 12), evw.notename, evw.ev.note) |
c490fecec0ef
Have another crack at fixing transposition.
Daniel O'Connor <darius@dons.net.au>
parents:
36
diff
changeset
|
272 break |
c490fecec0ef
Have another crack at fixing transposition.
Daniel O'Connor <darius@dons.net.au>
parents:
36
diff
changeset
|
273 if evw.slot != None: |
42 | 274 self.stats.playablecount += 1 |
37
c490fecec0ef
Have another crack at fixing transposition.
Daniel O'Connor <darius@dons.net.au>
parents:
36
diff
changeset
|
275 else: |
42 | 276 print 'Note at %.1f sec %d (%s) not playable' % (self.ctime, evw.ev.note, self.midi2note[evw.ev.note]) |
277 self.stats.unplayablecount += 1 | |
37
c490fecec0ef
Have another crack at fixing transposition.
Daniel O'Connor <darius@dons.net.au>
parents:
36
diff
changeset
|
278 else: |
42 | 279 print 'Note at %.1f sec, %d not in MIDI table' % (self.ctime, evw.ev.note) |
280 self.stats.unplayablecount += 1 | |
37
c490fecec0ef
Have another crack at fixing transposition.
Daniel O'Connor <darius@dons.net.au>
parents:
36
diff
changeset
|
281 |
12
6e46ceee57a7
Use correct MIDI note generation, previous version did not agree with
Daniel O'Connor <darius@dons.net.au>
parents:
11
diff
changeset
|
282 # http://newt.phys.unsw.edu.au/jw/notes.html |
38
9e8ed92b477c
Re-jig note translation to only happen when we are going to emit a
Daniel O'Connor <darius@dons.net.au>
parents:
37
diff
changeset
|
283 # But this seems dumb since the lowest MIDI note is 0 which would be C-1.. |
7 | 284 @staticmethod |
27
87cf66e04ef9
Handle offsetting note pitch in a better way.
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
285 def genmidi2note(): |
87cf66e04ef9
Handle offsetting note pitch in a better way.
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
286 '''Create forward & reverse tables for midi number to note name (assuming 69 = A4 = A440)''' |
7 | 287 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'] |
288 midi2note = {} | |
289 note2midi = {} | |
38
9e8ed92b477c
Re-jig note translation to only happen when we are going to emit a
Daniel O'Connor <darius@dons.net.au>
parents:
37
diff
changeset
|
290 for midi in range(12, 128): |
12
6e46ceee57a7
Use correct MIDI note generation, previous version did not agree with
Daniel O'Connor <darius@dons.net.au>
parents:
11
diff
changeset
|
291 octave = midi / len(names) - 1 |
7 | 292 index = midi % len(names) |
293 name = names[index] % (octave) | |
27
87cf66e04ef9
Handle offsetting note pitch in a better way.
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
294 midi2note[midi] = name |
87cf66e04ef9
Handle offsetting note pitch in a better way.
Daniel O'Connor <darius@dons.net.au>
parents:
26
diff
changeset
|
295 note2midi[name] = midi |
4
9f4fa5f231e6
Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents:
3
diff
changeset
|
296 |
7 | 297 return midi2note, note2midi |
298 | |
299 @staticmethod | |
41
21da8af1cdd2
Use ini file to hold organ specific details.
Daniel O'Connor <darius@dons.net.au>
parents:
40
diff
changeset
|
300 def loadnote2slot(notelist, note2midi): |
7 | 301 note2slot = {} |
13
5f4c21bb5140
Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
302 slot2note = {} |
7 | 303 index = 0 |
304 | |
41
21da8af1cdd2
Use ini file to hold organ specific details.
Daniel O'Connor <darius@dons.net.au>
parents:
40
diff
changeset
|
305 for note in notelist: |
7 | 306 note = note.strip() |
307 if note[0] == '#': | |
308 continue | |
309 if note not in note2midi: | |
310 raise exceptions.ValueError('Note \'%s\' not valid' % note) | |
311 note2slot[note] = index | |
13
5f4c21bb5140
Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
312 slot2note[index] = note |
7 | 313 index += 1 |
314 | |
13
5f4c21bb5140
Add lines and notes engraved on the paper.
Daniel O'Connor <darius@dons.net.au>
parents:
12
diff
changeset
|
315 return note2slot, slot2note |
4
9f4fa5f231e6
Rework to use mido iterator and paginate.
Daniel O'Connor <darius@dons.net.au>
parents:
3
diff
changeset
|
316 |
42 | 317 def emitnote(self, ev): |
318 orignote = ev.note | |
319 start = self.channels[ev.channel][orignote] | |
320 evw = EVWrap(ev) | |
321 # Find a slot (plus adjust pitch, attempt transposition etc) | |
322 if hasattr(ev, 'note'): | |
323 self.getslotfornote(evw) | |
324 # Check if it was unplayable | |
325 if evw.slot == None: | |
326 return orignote | |
327 notelen = self.ctime - start | |
328 print 'Note %s (%d) at %.2f length %.2f' % (evw.notename, evw.slot, start, notelen) | |
19
ffaf97818ce3
- Music needs to run right to left.
Daniel O'Connor <darius@dons.net.au>
parents:
18
diff
changeset
|
329 x = start * self.timescale + self.leadin # Convert start time to distance |
31
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
330 pdfidx = int(x / self.pdfwidth) # Determine which pdf |
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
331 x = x % self.pdfwidth # and where on that pdf |
16
20337b22977d
Add left margin and slot size support.
Daniel O'Connor <darius@dons.net.au>
parents:
14
diff
changeset
|
332 h = self.slotsize |
42 | 333 y = self.pageheight - (self.heel + evw.slot * self.pitch - self.slotsize / 2) - self.slotsize |
19
ffaf97818ce3
- Music needs to run right to left.
Daniel O'Connor <darius@dons.net.au>
parents:
18
diff
changeset
|
334 w = notelen * self.timescale # Convert note length in time to distance |
7 | 335 |
37
c490fecec0ef
Have another crack at fixing transposition.
Daniel O'Connor <darius@dons.net.au>
parents:
36
diff
changeset
|
336 #print 'pdf = %d x = %.3f y = %.3f w = %.3f h = %.3f' % (pdfidx, x, y, w, h) |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
337 w1 = w |
31
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
338 # Check if the note crosses a pdf |
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
339 if x + w > self.pdfwidth: |
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
340 w1 = self.pdfwidth - x # Crop first note |
19
ffaf97818ce3
- Music needs to run right to left.
Daniel O'Connor <darius@dons.net.au>
parents:
18
diff
changeset
|
341 w2 = w - w1 # Calculate length of second note |
31
ea98c9507f47
Preliminary multi-page support. Breaks time marks though.
Daniel O'Connor <darius@dons.net.au>
parents:
30
diff
changeset
|
342 assert w2 <= self.pdfwidth, 'note extends for more than a pdf' |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
343 # Emit second half of note |
37
c490fecec0ef
Have another crack at fixing transposition.
Daniel O'Connor <darius@dons.net.au>
parents:
36
diff
changeset
|
344 #print 'split note, pdf %d w2 = %.3f' % (pdfidx + 1, w2) |
42 | 345 Midi2PDF._emitnote(self.pdfs[pdfidx + 1], self.pdfwidth - w2, y, w2, h) |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
346 |
42 | 347 Midi2PDF._emitnote(self.pdfs[pdfidx], self.pdfwidth - x - w1, y, w1, h) |
348 return orignote | |
7 | 349 |
350 @staticmethod | |
11
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
351 def _emitnote(pdf, x, y, w, h): |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
352 pdf.saveState() |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
353 pdf.setStrokeColor(CUT_COLOUR) |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
354 pdf.setLineWidth(0) |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
355 pdf.rect(x * mm, y * mm, w * mm, h * mm, fill = False, stroke = True) |
9faad813e39e
switch to PDF to avoid dealing with CorelDraw crashing
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
356 pdf.restoreState() |
0 | 357 |
17
c50427f1da2d
- Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
358 @staticmethod |
c50427f1da2d
- Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
359 def textHelper(pdf, x, y, colour, fill, fontname, fontsize, text): |
c50427f1da2d
- Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
360 tobj = pdf.beginText() |
c50427f1da2d
- Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
361 tobj.setTextOrigin(x, y) |
c50427f1da2d
- Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
362 tobj.setFont(fontname, fontsize) |
c50427f1da2d
- Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
363 tobj.setStrokeColor(colour) |
c50427f1da2d
- Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
364 tobj.setFillColor(colour) |
c50427f1da2d
- Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
365 if fill: |
c50427f1da2d
- Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
366 tobj.setTextRenderMode(0) |
c50427f1da2d
- Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
367 else: |
c50427f1da2d
- Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
368 tobj.setTextRenderMode(1) |
c50427f1da2d
- Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
369 tobj.textLine(text) |
c50427f1da2d
- Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
370 pdf.drawText(tobj) |
c50427f1da2d
- Draw vertical time lines.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
371 |
0 | 372 if __name__ == '__main__': |
373 main() |