1# This file is part of python-ly, https://pypi.python.org/pypi/python-ly
2#
3# Copyright (c) 2011 - 2015 by Wilbert Berendsen
4#
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.
9#
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.
14#
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.
19
20"""
21Transforming music by pitch manipulation.
22"""
23
24from __future__ import unicode_literals
25
26import ly.lex.lilypond
27
28
29def retrograde(cursor, language="nederlands"):
30    """Reverses pitches."""
31    source = ly.document.Source(cursor, True, tokens_with_position=True)
32
33    pitches = ly.pitch.PitchIterator(source, language)
34    psource = pitches.pitches()
35
36    plist = [p for p in psource if isinstance(p, ly.pitch.Pitch)]
37    rlist = [r.copy() for r in reversed(plist)]
38
39    with cursor.document as d:
40        for p, r in zip(plist, rlist):
41            p.note = r.note
42            p.alter = r.alter
43            p.octave = r.octave
44            pitches.write(p)
45
46def inversion(cursor, language="nederlands"):
47    """Inversion of the intervals between pitches."""
48    import ly.pitch.transpose
49
50    source = ly.document.Source(cursor, True, tokens_with_position=True)
51
52    pitches = ly.pitch.PitchIterator(source, language)
53    psource = pitches.pitches()
54
55    prev_note = None
56
57    with cursor.document as d:
58        for p in psource:
59            if isinstance(p, ly.pitch.Pitch):
60                if prev_note is None:
61                    prev_note = refp = p
62                    continue
63                transposer = ly.pitch.transpose.Transposer(p, prev_note)
64                prev_note = p.copy()
65                p.note = refp.note
66                p.alter = refp.alter
67                p.octave = refp.octave
68                transposer.transpose(p)
69                refp = p
70                pitches.write(p)
71
72
73