]> git.donarmstrong.com Git - lilypond.git/commitdiff
MusicXML: Implement pitch names for different languages
authorReinhold Kainhofer <reinhold@kainhofer.com>
Tue, 13 Nov 2007 22:00:12 +0000 (23:00 +0100)
committerReinhold Kainhofer <reinhold@kainhofer.com>
Tue, 13 Nov 2007 22:00:12 +0000 (23:00 +0100)
Added a --language (-l) command line switch for the musicxml2ly
script that allows you to specify the name of a language.ly file,
so that localized pitch names (e.g. do/re/mi/fa/sol/la/ti for
espanol) are used and \include "language.ly" is automatically
inserted into the lilypond file.

Unfortunately, I had to use a global variable in the musicxml.py
file, which holds the function to convert pitches to strings.
However, I found this much cleaner than passing the language or
the function through all functions calls...

python/musicexp.py
scripts/musicxml2ly.py

index c70b396815939d4284ed384f51662f3f57e78027..168be1a33077a20bb639e37f15f0f1a0d1ecc208 100644 (file)
@@ -127,8 +127,8 @@ class Output_printer:
         self.newline ()
         self._file.close ()
         self._file = None
-        
-        
+
+
 class Duration:
     def __init__ (self):
         self.duration_log = 0
@@ -183,7 +183,71 @@ class Duration:
 
         return base * dot_fact * self.factor
 
-    
+
+# Implement the different note names for the various languages
+def pitch_generic (pitch, notenames, accidentals):
+    str = notenames[pitch.step]
+    if pitch.alteration < 0:
+        str += accidentals[0] * (-pitch.alteration)
+    elif pitch.alteration > 0:
+        str += accidentals[3] * (pitch.alteration)
+    return str
+
+def pitch_general (pitch):
+    str = pitch_generic (pitch, ['c', 'd', 'e', 'f', 'g', 'a', 'b'], ['es', 'eh', 'ih', 'is'])
+    return str.replace ('aes', 'as').replace ('ees', 'es')
+
+def pitch_nederlands (pitch):
+    return pitch_general (pitch)
+
+def pitch_english (pitch):
+    str = pitch_generic (pitch, ['c', 'd', 'e', 'f', 'g', 'a', 'b'], ['f', 'qf', 'qs', 's'])
+    return str.replace ('aes', 'as').replace ('ees', 'es')
+
+def pitch_deutsch (pitch):
+    str = pitch_generic (pitch, ['c', 'd', 'e', 'f', 'g', 'a', 'h'], ['es', 'eh', 'ih', 'is'])
+    return str.replace ('hes', 'b').replace ('aes', 'as').replace ('ees', 'es')
+
+def pitch_norsk (pitch):
+    return pitch_deutsch (pitch)
+
+def pitch_svenska (pitch):
+    str = pitch_generic (pitch, ['c', 'd', 'e', 'f', 'g', 'a', 'h'], ['ess', '', '', 'iss'])
+    return str.replace ('hess', 'b').replace ('aes', 'as').replace ('ees', 'es')
+
+def pitch_italiano (pitch):
+    str = pitch_generic (pitch, ['do', 're', 'mi', 'fa', 'sol', 'la', 'si'], ['b', 'sb', 'sd', 'd'])
+    return str
+
+def pitch_catalan (pitch):
+    return pitch_italiano (pitch)
+
+def pitch_espanol (pitch):
+    str = pitch_generic (pitch, ['do', 're', 'mi', 'fa', 'sol', 'la', 'si'], ['b', '', '', 's'])
+    return str
+
+def pitch_vlaams (pitch):
+    str = pitch_generic (pitch, ['do', 're', 'mi', 'fa', 'sol', 'la', 'si'], ['b', '', '', 'k'])
+    return str
+
+def set_pitch_language (language):
+    global pitch_generating_function
+    function_dict = {
+        "nederlands": pitch_nederlands,
+        "english": pitch_english,
+        "deutsch": pitch_deutsch,
+        "norsk": pitch_norsk,
+        "svenska": pitch_svenska,
+        "italiano": pitch_italiano,
+        "catalan": pitch_catalan,
+        "espanol": pitch_espanol,
+        "vlaams": pitch_vlaams}
+    pitch_generating_function = function_dict.get (language, pitch_general)
+
+# global variable to hold the formatting function.
+pitch_generating_function = pitch_general
+
+
 class Pitch:
     def __init__ (self):
         self.alteration = 0
@@ -230,13 +294,7 @@ class Pitch:
         return self.octave * 12 + [0,2,4,5,7,9,11][self.step] + self.alteration
     
     def ly_step_expression (self): 
-        str = 'cdefgab'[self.step]
-        if self.alteration > 0:
-            str += 'is'* (self.alteration)
-        elif self.alteration < 0:
-            str += 'es'* (-self.alteration)
-
-        return str.replace ('aes', 'as').replace ('ees', 'es')
+        return pitch_generating_function (self)
     
     def ly_expression (self):
         str = self.ly_step_expression ()
index 2c553a94d07215fb038b2b7886d843c94907134c..e54dd440d16da9e5a2780ea4d453b00308dae498 100644 (file)
@@ -1619,7 +1619,11 @@ Copyright (c) 2005--2007 by
                   default=False,
                   dest="use_lxml",
                   help=_ ("Use lxml.etree; uses less memory and cpu time."))
-    
+
+    p.add_option ('-l', '--language',
+                  action = "store",
+                  help = _ ("Use a different language file, e.g. 'deutsch' for deutsch.ly."))
+
     p.add_option ('-o', '--output',
                   metavar=_ ("FILE"),
                   action="store",
@@ -1718,6 +1722,7 @@ def print_ly_additional_definitions (printer, filename):
         printer.newline ()
     for a in set(needed_additional_definitions):
         printer.print_verbatim (additional_definitions.get (a, ''))
+    printer.newline ()
 
 
 def read_musicxml (filename, use_lxml):
@@ -1808,7 +1813,12 @@ def main ():
     if not args:
         opt_parser.print_usage()
         sys.exit (2)
-    
+
+    if options.language:
+        musicexp.set_pitch_language (options.language)
+        needed_additional_definitions.append (options.language)
+        additional_definitions[options.language] = "\\include \"%s.ly\"\n" % options.language
+
     # Allow the user to leave out the .xml or xml on the filename
     filename = get_existing_filename_with_extension (args[0], "xml")
     if filename and os.path.exists (filename):