]> git.donarmstrong.com Git - lilypond.git/blob - python/utilities.py
Web-ja: update introduction
[lilypond.git] / python / utilities.py
1 # -*- coding: utf-8 -*-
2 import re
3 import string
4
5 def string_to_number(s):
6     try:
7         return int(s)
8     except ValueError:
9         return float(s)
10
11 def string_to_integer(s):
12     num = string_to_number(s)
13     if isinstance(num, int):
14         return num
15     else:
16         return int(num)
17
18 def escape_ly_output_string (input_string):
19     return_string = input_string
20     needs_quotes = not re.match (u"^[a-zA-ZäöüÜÄÖßñ]*$", return_string);
21     if needs_quotes:
22         return_string = "\"" + string.replace (return_string, "\"", "\\\"") + "\""
23     return return_string
24
25 def interpret_alter_element (alter_elm):
26     alter = 0
27     if alter_elm:
28         val = eval(alter_elm.get_text ())
29         if type (val) in (int, float):
30             alter = val
31     return alter
32
33 def musicxml_duration_to_log (dur):
34     return  {'256th': 8,
35              '128th': 7,
36              '64th': 6,
37              '32nd': 5,
38              '16th': 4,
39              'eighth': 3,
40              'quarter': 2,
41              'half': 1,
42              'whole': 0,
43              'breve':-1,
44              'longa':-2,
45              'long':-2}.get (dur, 0)
46
47 def hexcolorval_to_nr (hex_val):
48     try:
49         v = int (hex_val, 16)
50         if v == 255:
51             v = 256
52         return v / 256.
53     except ValueError:
54         return 0.
55
56 def hex_to_color(hex_val):
57     res = re.match (
58         r'#([0-9a-f][0-9a-f]|)([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])$',
59         hex_val,
60         re.IGNORECASE)
61     if res:
62         return map(lambda x: hexcolorval_to_nr (x), res.group (2, 3, 4))
63     else:
64         return None
65
66 def split_string_and_preserve_doublequoted_substrings(value):
67     import shlex
68     lex = shlex.shlex(value)
69     lex.quotes = '"'
70     lex.whitespace_split = True
71     lex.commenters = ''
72     return list(lex)
73
74 def musicxml_sound_to_lilypond_midi_instrument(sound):
75     sounds = {
76         "brass.french-horn": 'french horn',
77         "brass.group": 'brass section',
78         "brass.group.synth": 'synthbrass 1',
79         "brass.trombone": 'trombone',
80         "brass.trombone.alto": 'trombone',
81         "brass.trombone.bass": 'trombone',
82         "brass.trombone.contrabass": 'trombone',
83         "brass.trombone.tenor": 'trombone',
84         "brass.trumpet": 'trumpet',
85         "brass.trumpet.baroque": 'trumpet',
86         "brass.trumpet.bass": 'trumpet',
87         "brass.trumpet.bflat": 'trumpet',
88         "brass.trumpet.c": 'trumpet',
89         "brass.trumpet.d": 'trumpet',
90         "brass.trumpet.piccolo": 'trumpet',
91         "brass.trumpet.pocket": 'trumpet',
92         "brass.trumpet.slide": 'trumpet',
93         "brass.trumpet.tenor": 'trumpet',
94         "brass.tuba": 'tuba',
95         "brass.tuba.bass": 'tuba',
96         "brass.tuba.subcontrabass": 'tuba',
97         "brass.wagner-tuba": 'french horn',
98         "drum.timpani": 'timpani',
99         "drum.tom-tom": 'melodic tom',
100         "drum.tom-tom.synth": 'synth drum',
101         "effect.applause": 'applause',
102         "effect.bass-string-slap": 'slap bass 1',
103         "effect.bird": 'bird tweet',
104         "effect.bird.tweet": 'bird tweet',
105         "effect.breath": 'breath noise',
106         "effect.guitar-fret": 'guitar fret noise',
107         "effect.gunshot": 'gunshot',
108         "effect.helicopter": 'helicopter',
109         "effect.metronome-click": 'woodblock',
110         "effect.rain": 'fx1 (rain)',
111         "effect.seashore": 'seashore',
112         "effect.telephone-ring": 'telephone ring',
113         "keyboard.accordion": 'accordion',
114         "keyboard.bandoneon": 'accordion',
115         "keyboard.celesta": 'celesta',
116         "keyboard.clavichord": 'clav',
117         "keyboard.concertina": 'concertina',
118         "keyboard.harpsichord": 'harpsichord',
119         "keyboard.ondes-martenot": 'ocarina',
120         "keyboard.organ": 'church organ',
121         "keyboard.organ.drawbar": 'drawbar organ',
122         "keyboard.organ.percussive": 'percussive organ',
123         "keyboard.organ.pipe": 'church organ',
124         "keyboard.organ.reed": 'reed organ',
125         "keyboard.piano": 'acoustic grand',
126         "keyboard.piano.electric": 'electric piano 1',
127         "keyboard.piano.grand": 'acoustic grand',
128         "keyboard.piano.honky-tonk": 'honky-tonk',
129         "metal.bells.agogo": 'agogo',
130         "metal.bells.tinklebell": 'tinkle bell',
131         "metal.cymbal.reverse": 'reverse cymbal',
132         "pitched-percussion.glockenspiel": 'glockenspiel',
133         "pitched-percussion.glockenspiel.alto": 'glockenspiel',
134         "pitched-percussion.glockenspiel.soprano": 'glockenspiel',
135         "pitched-percussion.hammer-dulcimer": 'dulcimer',
136         "pitched-percussion.kalimba": 'kalimba',
137         "pitched-percussion.marimba": 'marimba',
138         "pitched-percussion.marimba.bass": 'marimba',
139         "pitched-percussion.music-box": 'music box',
140         "pitched-percussion.tubular-bells": 'tubular bells',
141         "pitched-percussion.vibraphone": 'vibraphone',
142         "pitched-percussion.xylophone": 'xylophone',
143         "pitched-percussion.xylophone.alto": 'xylophone',
144         "pitched-percussion.xylophone.bass": 'xylophone',
145         "pitched-percussion.xylophone.soprano": 'xylophone',
146         "pitched-percussion.xylorimba": 'xylophone',
147         "pluck.banjo": 'banjo',
148         "pluck.banjo.tenor": 'banjo',
149         "pluck.bass": 'acoustic bass',
150         "pluck.bass.acoustic": 'acoustic bass',
151         "pluck.bass.electric": 'electric bass',
152         "pluck.bass.fretless": 'fretless bass',
153         "pluck.bass.synth": 'synth bass 1',
154         "pluck.dulcimer": 'dulcimer',
155         "pluck.guitar": 'acoustic guitar (nylon)',
156         "pluck.guitar.acoustic": 'acoustic guitar (nylon)',
157         "pluck.guitar.electric": 'electric guitar (jazz)',
158         "pluck.guitar.nylon-string": 'acoustic guitar (nylon)',
159         "pluck.guitar.steel-string": 'acoustic guitar (steel)',
160         "pluck.harp": 'orchestral harp',
161         "pluck.lute": 'acoustic guitar (nylon)',
162         "pluck.shamisen": 'shamisen',
163         "pluck.sitar": 'sitar',
164         "strings.cello": 'cello',
165         "strings.cello.piccolo": 'cello',
166         "strings.contrabass": 'contrabass',
167         "strings.fiddle": 'fiddle',
168         "strings.group.synth": 'synth strings 1',
169         "strings.viola": 'viola',
170         "strings.violin": 'violin',
171         "synth.effects.atmosphere": 'fx 4 (atmosphere)',
172         "synth.effects.brightness": 'fx 5 (brightness)',
173         "synth.effects.crystal": 'fx 3 (crystal)',
174         "synth.effects.echoes": 'fx 7 echoes',
175         "synth.effects.goblins": 'fx 6 goblins',
176         "synth.effects.rain": 'fx 1 rain',
177         "synth.effects.sci-fi": 'fx 8 sci-fi',
178         "synth.effects.soundtrack": 'fx 2 (soundtrack)',
179         "synth.pad.bowed": 'pad 5 bowed',
180         "synth.pad.choir": 'pad 4 choir',
181         "synth.pad.halo": 'pad 7 halo',
182         "synth.pad.metallic": 'pad 6 metallic',
183         "synth.pad.polysynth": 'pad 3 polysynth',
184         "synth.pad.sweep": 'pad 8 sweep',
185         "synth.pad.warm": 'pad 2 warm',
186         "synth.tone.sawtooth": 'lead 1 (square)',
187         "synth.tone.square": 'lead 2 (sawtooth)',
188         "voice.aa": 'choir aahs',
189         "voice.alto": 'choir aahs',
190         "voice.aw": 'choir aahs',
191         "voice.baritone": 'choir aahs',
192         "voice.bass": 'choir aahs',
193         "voice.child": 'choir aahs',
194         "voice.countertenor": 'choir aahs',
195         "voice.doo": 'choir aahs',
196         "voice.ee": 'choir aahs',
197         "voice.female": 'choir aahs',
198         "voice.kazoo": 'choir aahs',
199         "voice.male": 'choir aahs',
200         "voice.mezzo-soprano": 'choir aahs',
201         "voice.mm": 'choir aahs',
202         "voice.oo": 'voice oohs',
203         "voice.soprano": 'choir aahs',
204         "voice.synth": 'synth voice',
205         "wind.flutes.blown-bottle": 'blown bottle',
206         "wind.flutes.calliope": 'lead 3 (calliope)',
207         "wind.flutes.flute": 'flute',
208         "wind.flutes.flute.alto": 'flute',
209         "wind.flutes.flute.bass": 'flute',
210         "wind.flutes.flute.contra-alto": 'flute',
211         "wind.flutes.flute.contrabass": 'flute',
212         "wind.flutes.flute.double-contrabass": 'flute',
213         "wind.flutes.flute.piccolo": 'flute',
214         "wind.flutes.flute.subcontrabass": 'flute',
215         "wind.flutes.ocarina": 'ocarina',
216         "wind.flutes.recorder": 'recorder',
217         "wind.flutes.recorder.alto": 'recorder',
218         "wind.flutes.recorder.bass": 'recorder',
219         "wind.flutes.recorder.contrabass": 'recorder',
220         "wind.flutes.recorder.descant": 'recorder',
221         "wind.flutes.recorder.garklein": 'recorder',
222         "wind.flutes.recorder.great-bass": 'recorder',
223         "wind.flutes.recorder.sopranino": 'recorder',
224         "wind.flutes.recorder.soprano": 'recorder',
225         "wind.flutes.recorder.tenor": 'recorder',
226         "wind.flutes.shakuhachi": 'shakuhachi',
227         "wind.flutes.whistle": 'whistle',
228         "wind.flutes.whistle.alto": 'whistle',
229         "wind.pipes.bagpipes": 'bagpipe',
230         "wind.reed.basset-horn": 'clarinet',
231         "wind.reed.bassoon": 'bassoon',
232         "wind.reed.clarinet": 'clarinet',
233         "wind.reed.clarinet.a": 'clarinet',
234         "wind.reed.clarinet.alto": 'clarinet',
235         "wind.reed.clarinet.bass": 'clarinet',
236         "wind.reed.clarinet.basset": 'clarinet',
237         "wind.reed.clarinet.bflat": 'clarinet',
238         "wind.reed.clarinet.contra-alto": 'clarinet',
239         "wind.reed.clarinet.contrabass": 'clarinet',
240         "wind.reed.clarinet.eflat": 'clarinet',
241         "wind.reed.clarinet.piccolo.aflat": 'clarinet',
242         "wind.reed.contrabass": 'contrabass',
243         "wind.reed.contrabassoon": 'bassoon',
244         "wind.reed.english-horn": 'oboe',
245         "wind.reed.harmonica": 'harmonica',
246         "wind.reed.harmonica.bass": 'harmonica',
247         "wind.reed.oboe": 'oboe',
248         "wind.reed.oboe.bass": 'oboe',
249         "wind.reed.oboe.piccolo": 'oboe',
250         "wind.reed.oboe-da-caccia": 'oboe',
251         "wind.reed.oboe-damore": 'oboe',
252         "wind.reed.saxophone": 'alto sax',
253         "wind.reed.saxophone.alto": 'alto sax',
254         "wind.reed.saxophone.baritone": 'baritone sax',
255         "wind.reed.saxophone.bass": 'baritone sax',
256         "wind.reed.saxophone.contrabass": 'baritone sax',
257         "wind.reed.saxophone.melody": 'soprano sax',
258         "wind.reed.saxophone.mezzo-soprano": 'soprano sax',
259         "wind.reed.saxophone.sopranino": 'soprano sax',
260         "wind.reed.saxophone.sopranissimo": 'soprano sax',
261         "wind.reed.saxophone.soprano": 'soprano, sax',
262         "wind.reed.saxophone.subcontrabass": 'baritone sax',
263         "wind.reed.saxophone.tenor": 'tenor sax',
264         "wind.reed.shenai": 'shanai',
265         "wood.temple-block": 'wood block',
266         "wood.wood-block": 'wood block',
267         }
268     return sounds.get(sound, 'acoustic grand')