From 7cfc12332af2968351a14b8c8520da2d0b3dacad Mon Sep 17 00:00:00 2001 From: Reinhold Kainhofer Date: Tue, 13 Nov 2007 23:00:12 +0100 Subject: [PATCH] MusicXML: Implement pitch names for different languages 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 | 78 ++++++++++++++++++++++++++++++++++++------ scripts/musicxml2ly.py | 14 ++++++-- 2 files changed, 80 insertions(+), 12 deletions(-) diff --git a/python/musicexp.py b/python/musicexp.py index c70b396815..168be1a330 100644 --- a/python/musicexp.py +++ b/python/musicexp.py @@ -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 () diff --git a/scripts/musicxml2ly.py b/scripts/musicxml2ly.py index 2c553a94d0..e54dd440d1 100644 --- a/scripts/musicxml2ly.py +++ b/scripts/musicxml2ly.py @@ -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): -- 2.39.5