Left: | ||
Right: |
OLD | NEW |
---|---|
1 # -*- coding: utf-8 -*- | 1 # -*- coding: utf-8 -*- |
2 import inspect | 2 import inspect |
3 import sys | 3 import sys |
4 import string | |
5 import re | 4 import re |
6 import math | 5 import math |
7 import lilylib as ly | 6 import lilylib as ly |
8 import warnings | 7 import warnings |
9 import utilities | 8 import utilities |
10 | 9 |
11 _ = ly._ | 10 _ = ly._ |
12 | 11 |
13 from rational import Rational | 12 from rational import Rational |
14 | 13 |
15 # Store previously converted pitch for \relative conversion as a global state va riable | 14 # Store previously converted pitch for \relative conversion as a global state va riable |
16 previous_pitch = None | 15 previous_pitch = None |
17 relative_pitches = False | 16 relative_pitches = False |
18 whatOrnament = "" | 17 whatOrnament = "" |
19 ly_dur = None # stores lilypond durations | 18 ly_dur = None # stores lilypond durations |
20 | 19 |
21 def escape_instrument_string (input_string): | 20 def escape_instrument_string (input_string): |
22 retstring = string.replace (input_string, "\"", "\\\"") | 21 retstring = input_string.replace ("\"", "\\\"") |
23 if re.match ('.*[\r\n]+.*', retstring): | 22 if re.match ('.*[\r\n]+.*', retstring): |
24 rx = re.compile (r'[\n\r]+') | 23 rx = re.compile (r'[\n\r]+') |
25 strings = rx.split (retstring) | 24 strings = rx.split (retstring) |
26 retstring = "\\markup { \\center-column { " | 25 retstring = "\\markup { \\center-column { " |
27 for s in strings: | 26 for s in strings: |
28 retstring += "\\line {\"" + s + "\"} " | 27 retstring += "\\line {\"" + s + "\"} " |
29 retstring += "} }" | 28 retstring += "} }" |
30 else: | 29 else: |
31 retstring = "\"" + retstring + "\"" | 30 retstring = "\"" + retstring + "\"" |
32 return retstring | 31 return retstring |
(...skipping 489 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
522 if not text: | 521 if not text: |
523 text = self.comment | 522 text = self.comment |
524 | 523 |
525 if not text: | 524 if not text: |
526 return | 525 return |
527 | 526 |
528 if text == '\n': | 527 if text == '\n': |
529 printer.newline () | 528 printer.newline () |
530 return | 529 return |
531 | 530 |
532 lines = string.split (text, '\n') | 531 lines = text.split ('\n') |
533 for l in lines: | 532 for l in lines: |
534 if l: | 533 if l: |
535 printer.unformatted_output ('% ' + l) | 534 printer.unformatted_output ('% ' + l) |
536 printer.newline () | 535 printer.newline () |
537 | 536 |
538 | 537 |
539 def print_with_identifier (self, printer): | 538 def print_with_identifier (self, printer): |
540 if self.identifier: | 539 if self.identifier: |
541 printer ("\\%s" % self.identifier) | 540 printer ("\\%s" % self.identifier) |
542 else: | 541 else: |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
685 if dir < 0: | 684 if dir < 0: |
686 idx = 0 | 685 idx = 0 |
687 elif dir > 0: | 686 elif dir > 0: |
688 idx = len (self.elements) | 687 idx = len (self.elements) |
689 | 688 |
690 self.elements.insert (idx, elt) | 689 self.elements.insert (idx, elt) |
691 elt.parent = self | 690 elt.parent = self |
692 | 691 |
693 def get_properties (self): | 692 def get_properties (self): |
694 return ("'elements (list %s)" | 693 return ("'elements (list %s)" |
695 % string.join (map (lambda x: x.lisp_expression(), | 694 % " ".join (map (lambda x: x.lisp_expression(), |
Malte Meyn
2019/10/23 21:22:16
Here you use " " …
| |
696 self.elements))) | 695 self.elements))) |
697 | 696 |
698 def get_subset_properties (self, predicate): | 697 def get_subset_properties (self, predicate): |
699 return ("'elements (list %s)" | 698 return ("'elements (list %s)" |
700 % string.join (map (lambda x: x.lisp_expression(), | 699 % " ".join (map (lambda x: x.lisp_expression(), |
701 filter (predicate, self.elements)))) | 700 filter (predicate, self.elements)))) |
702 def get_neighbor (self, music, dir): | 701 def get_neighbor (self, music, dir): |
703 assert music.parent == self | 702 assert music.parent == self |
704 idx = self.elements.index (music) | 703 idx = self.elements.index (music) |
705 idx += dir | 704 idx += dir |
706 idx = min (idx, len (self.elements) - 1) | 705 idx = min (idx, len (self.elements) - 1) |
707 idx = max (idx, 0) | 706 idx = max (idx, 0) |
708 | 707 |
709 return self.elements[idx] | 708 return self.elements[idx] |
710 | 709 |
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1031 for x in note_events: | 1030 for x in note_events: |
1032 if(x.associated_events): | 1031 if(x.associated_events): |
1033 for aev in x.associated_events: | 1032 for aev in x.associated_events: |
1034 if (isinstance(aev, StemEvent) and aev.value): | 1033 if (isinstance(aev, StemEvent) and aev.value): |
1035 stem = aev | 1034 stem = aev |
1036 pitches.append (x.chord_element_ly ()) | 1035 pitches.append (x.chord_element_ly ()) |
1037 if not basepitch: | 1036 if not basepitch: |
1038 basepitch = previous_pitch | 1037 basepitch = previous_pitch |
1039 if stem: | 1038 if stem: |
1040 printer (stem.ly_expression ()) | 1039 printer (stem.ly_expression ()) |
1041 printer ('<%s>' % string.join (pitches)) | 1040 printer ('<%s>' % ' '.join (pitches)) |
Malte Meyn
2019/10/23 21:22:16
… and here ' '.
| |
1042 previous_pitch = basepitch | 1041 previous_pitch = basepitch |
1043 duration = self.get_duration () | 1042 duration = self.get_duration () |
1044 if duration: | 1043 if duration: |
1045 duration.print_ly (printer) | 1044 duration.print_ly (printer) |
1046 else: | 1045 else: |
1047 pass | 1046 pass |
1048 | 1047 |
1049 for e in other_events: | 1048 for e in other_events: |
1050 e.print_ly (printer) | 1049 e.print_ly (printer) |
1051 | 1050 |
(...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1470 class FretBoardEvent (NestedMusic): | 1469 class FretBoardEvent (NestedMusic): |
1471 def __init__ (self): | 1470 def __init__ (self): |
1472 NestedMusic.__init__ (self) | 1471 NestedMusic.__init__ (self) |
1473 self.duration = None | 1472 self.duration = None |
1474 def print_ly (self, printer): | 1473 def print_ly (self, printer): |
1475 fretboard_notes = [n for n in self.elements if isinstance (n, FretBoardN ote)] | 1474 fretboard_notes = [n for n in self.elements if isinstance (n, FretBoardN ote)] |
1476 if fretboard_notes: | 1475 if fretboard_notes: |
1477 notes = [] | 1476 notes = [] |
1478 for n in fretboard_notes: | 1477 for n in fretboard_notes: |
1479 notes.append (n.ly_expression ()) | 1478 notes.append (n.ly_expression ()) |
1480 contents = string.join (notes) | 1479 contents = ' '.join (notes) |
1481 printer ('<%s>%s' % (contents,self.duration)) | 1480 printer ('<%s>%s' % (contents,self.duration)) |
1482 | 1481 |
1483 class FunctionWrapperEvent (Event): | 1482 class FunctionWrapperEvent (Event): |
1484 def __init__ (self, function_name=None): | 1483 def __init__ (self, function_name=None): |
1485 Event.__init__ (self) | 1484 Event.__init__ (self) |
1486 self.function_name = function_name | 1485 self.function_name = function_name |
1487 def pre_note_ly (self, is_chord_element): | 1486 def pre_note_ly (self, is_chord_element): |
1488 if self.function_name: | 1487 if self.function_name: |
1489 return "\\%s" % self.function_name | 1488 return "\\%s" % self.function_name |
1490 else: | 1489 else: |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1660 if ev: | 1659 if ev: |
1661 self.associated_events.append (ev) | 1660 self.associated_events.append (ev) |
1662 | 1661 |
1663 def pre_chord_ly (self): | 1662 def pre_chord_ly (self): |
1664 return [ev.pre_chord_ly () for ev in self.associated_events] | 1663 return [ev.pre_chord_ly () for ev in self.associated_events] |
1665 | 1664 |
1666 def pre_note_ly (self, is_chord_element): | 1665 def pre_note_ly (self, is_chord_element): |
1667 return [ev.pre_note_ly (is_chord_element) for ev in self.associated_even ts] | 1666 return [ev.pre_note_ly (is_chord_element) for ev in self.associated_even ts] |
1668 | 1667 |
1669 def ly_expression_pre_note (self, is_chord_element): | 1668 def ly_expression_pre_note (self, is_chord_element): |
1670 res = string.join (self.pre_note_ly (is_chord_element), ' ') | 1669 res = ' '.join (self.pre_note_ly (is_chord_element)) |
1671 if res != '': | 1670 if res != '': |
1672 res = res + ' ' | 1671 res = res + ' ' |
1673 return res | 1672 return res |
1674 | 1673 |
1675 def get_length (self): | 1674 def get_length (self): |
1676 return self.duration.get_length() | 1675 return self.duration.get_length() |
1677 | 1676 |
1678 def get_properties (self): | 1677 def get_properties (self): |
1679 return ("'duration %s" | 1678 return ("'duration %s" |
1680 % self.duration.lisp_expression ()) | 1679 % self.duration.lisp_expression ()) |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1808 else: | 1807 else: |
1809 return '' | 1808 return '' |
1810 | 1809 |
1811 def ly_expression (self): | 1810 def ly_expression (self): |
1812 if self.tonic: | 1811 if self.tonic: |
1813 return '\\key %s \\%s' % (self.tonic.ly_step_expression (), | 1812 return '\\key %s \\%s' % (self.tonic.ly_step_expression (), |
1814 self.mode) | 1813 self.mode) |
1815 elif self.non_standard_alterations: | 1814 elif self.non_standard_alterations: |
1816 alterations = [self.format_non_standard_alteration (a) for | 1815 alterations = [self.format_non_standard_alteration (a) for |
1817 a in self.non_standard_alterations] | 1816 a in self.non_standard_alterations] |
1818 return "\\set Staff.keyAlterations = #`(%s)" % string.join (alterati ons, " ") | 1817 return "\\set Staff.keyAlterations = #`(%s)" % " ".join (alterations ) |
1819 else: | 1818 else: |
1820 return '' | 1819 return '' |
1821 | 1820 |
1822 class ShiftDurations (MusicWrapper): | 1821 class ShiftDurations (MusicWrapper): |
1823 def __init__ (self): | 1822 def __init__ (self): |
1824 MusicWrapper.__init__ (self) | 1823 MusicWrapper.__init__ (self) |
1825 self.params = [0,0] | 1824 self.params = [0,0] |
1826 | 1825 |
1827 def set_shift_durations_parameters(self, timeSigChange): | 1826 def set_shift_durations_parameters(self, timeSigChange): |
1828 self.params = timeSigChange.get_shift_durations_parameters() | 1827 self.params = timeSigChange.get_shift_durations_parameters() |
(...skipping 23 matching lines...) Expand all Loading... | |
1852 | 1851 |
1853 def get_shift_durations_parameters (self): | 1852 def get_shift_durations_parameters (self): |
1854 dur = math.ceil(math.log(self.get_fractions_ratio(),2)) | 1853 dur = math.ceil(math.log(self.get_fractions_ratio(),2)) |
1855 dots = (1/self.get_fractions_ratio())/(math.pow(2,-dur)) | 1854 dots = (1/self.get_fractions_ratio())/(math.pow(2,-dur)) |
1856 dots = int(math.log(2-dots,0.5)) | 1855 dots = int(math.log(2-dots,0.5)) |
1857 return [dur, dots] | 1856 return [dur, dots] |
1858 | 1857 |
1859 def format_fraction (self, frac): | 1858 def format_fraction (self, frac): |
1860 if isinstance (frac, list): | 1859 if isinstance (frac, list): |
1861 l = [self.format_fraction (f) for f in frac] | 1860 l = [self.format_fraction (f) for f in frac] |
1862 return "(" + string.join (l, " ") + ")" | 1861 return "(" + " ".join (l) + ")" |
1863 else: | 1862 else: |
1864 return "%s" % frac | 1863 return "%s" % frac |
1865 | 1864 |
1866 def ly_expression (self): | 1865 def ly_expression (self): |
1867 st = '' | 1866 st = '' |
1868 # Print out the style if we have ome, but the '() should only be | 1867 # Print out the style if we have ome, but the '() should only be |
1869 # forced for 2/2 or 4/4, since in all other cases we'll get numeric | 1868 # forced for 2/2 or 4/4, since in all other cases we'll get numeric |
1870 # signatures anyway despite the default 'C signature style! | 1869 # signatures anyway despite the default 'C signature style! |
1871 is_common_signature = self.fractions in ([2, 2], [4, 4], [4, 2]) | 1870 is_common_signature = self.fractions in ([2, 2], [4, 4], [4, 2]) |
1872 if self.style and self.visible: | 1871 if self.style and self.visible: |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2069 def set_real_duration (self, dur): | 2068 def set_real_duration (self, dur): |
2070 self.real_duration = dur | 2069 self.real_duration = dur |
2071 | 2070 |
2072 def print_ly (self, printer): | 2071 def print_ly (self, printer): |
2073 figured_bass_events = [e for e in self.elements if | 2072 figured_bass_events = [e for e in self.elements if |
2074 isinstance (e, FiguredBassNote)] | 2073 isinstance (e, FiguredBassNote)] |
2075 if figured_bass_events: | 2074 if figured_bass_events: |
2076 notes = [] | 2075 notes = [] |
2077 for x in figured_bass_events: | 2076 for x in figured_bass_events: |
2078 notes.append (x.ly_expression ()) | 2077 notes.append (x.ly_expression ()) |
2079 contents = string.join (notes) | 2078 contents = ' '.join (notes) |
2080 if self.parentheses: | 2079 if self.parentheses: |
2081 contents = '[%s]' % contents | 2080 contents = '[%s]' % contents |
2082 printer ('<%s>' % contents) | 2081 printer ('<%s>' % contents) |
2083 self.duration.print_ly (printer) | 2082 self.duration.print_ly (printer) |
2084 | 2083 |
2085 | 2084 |
2086 class MultiMeasureRest(Music): | 2085 class MultiMeasureRest(Music): |
2087 | 2086 |
2088 def lisp_expression (self): | 2087 def lisp_expression (self): |
2089 return """ | 2088 return """ |
(...skipping 425 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2515 expr = test_expr() | 2514 expr = test_expr() |
2516 expr.set_start (Rational (0)) | 2515 expr.set_start (Rational (0)) |
2517 print expr.ly_expression() | 2516 print expr.ly_expression() |
2518 start = Rational (0, 4) | 2517 start = Rational (0, 4) |
2519 stop = Rational (4, 2) | 2518 stop = Rational (4, 2) |
2520 def sub(x, start=start, stop=stop): | 2519 def sub(x, start=start, stop=stop): |
2521 ok = x.start >= start and x.start + x.get_length() <= stop | 2520 ok = x.start >= start and x.start + x.get_length() <= stop |
2522 return ok | 2521 return ok |
2523 | 2522 |
2524 print expr.lisp_sub_expression(sub) | 2523 print expr.lisp_sub_expression(sub) |
OLD | NEW |