1
# This file is part of the Frescobaldi project, http://www.frescobaldi.org/
3
# Copyright (c) 2008, 2009, 2010 by Wilbert Berendsen
5
# This program is free software; you can redistribute it and/or
6
# modify it under the terms of the GNU General Public License
7
# as published by the Free Software Foundation; either version 2
8
# of the License, or (at your option) any later version.
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
# See http://www.gnu.org/licenses/ for more information.
20
from __future__ import unicode_literals
22
""" LilyPond information and logic concerning durations """
27
durations = ['\\maxima', '\\longa', '\\breve',
28
'1', '2', '4', '8', '16', '32', '64', '128', '256', '512', '1024', '2048']
32
Decorator to handle functions that are the callback for the regexp.
36
return m.group('duration') and func(m) or m.group()
37
return ly.rx.chord_rest.sub(repl, text)
41
def doubleDurations(m):
42
chord, dur, dots, scale = m.group('chord', 'dur', 'dots', 'scale')
44
i = durations.index(dur)
46
dur = durations[i - 1]
47
return ''.join(i or '' for i in (chord, dur, dots, scale))
50
def halveDurations(m):
51
chord, dur, dots, scale = m.group('chord', 'dur', 'dots', 'scale')
53
i = durations.index(dur)
54
if i < len(durations) - 1:
55
dur = durations[i + 1]
56
return ''.join(i or '' for i in (chord, dur, dots, scale))
60
chord, dur, dots, scale = m.group('chord', 'dur', 'dots', 'scale')
61
dots = (dots or '') + '.'
62
return ''.join(i or '' for i in (chord, dur, dots, scale))
65
def undotDurations(m):
66
chord, dur, dots, scale = m.group('chord', 'dur', 'dots', 'scale')
69
return ''.join(i or '' for i in (chord, dur, dots, scale))
73
return ''.join(i or '' for i in m.group('chord', 'dur', 'dots'))
76
def removeDurations(m):
77
return m.group('chord')
79
def makeImplicit(text):
82
chord, duration = m.group('chord', 'duration')
84
if not duration or duration == old[0]:
88
return chord + duration
90
return ly.rx.chord_rest.sub(repl, text)
92
def makeImplicitPerLine(text):
93
return '\n'.join(makeImplicit(t) for t in makeExplicit(text).split('\n'))
95
def makeExplicit(text):
98
chord, duration = m.group('chord', 'duration')
101
return chord + old[0]
104
return chord + duration
106
return ly.rx.chord_rest.sub(repl, text)
108
def applyRhythm(text, rhythm):
109
""" Adds the entered rhythm to the selected music."""
110
durs = [m.group() for m in ly.rx.finddurs.finditer(rhythm)]
117
yield i != old and i or ''
122
return m.group('chord') + next(durations)
124
return ly.rx.chord_rest.sub(repl, text)
126
def extractRhythm(text):
127
""" Iterate over a rhythm from text, returning only the durations """
129
for m in ly.rx.chord_rest.finditer(text):
131
if m.group('duration'):
132
duration = m.group('duration')