]> git.donarmstrong.com Git - lilypond.git/blob - lily/pitch-scheme.cc
Issue 1976: Handle full rational pitch differences in fingering calculations.
[lilypond.git] / lily / pitch-scheme.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2005--2012 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "pitch.hh"
21
22 LY_DEFINE (ly_pitch_transpose, "ly:pitch-transpose",
23            2, 0, 0, (SCM p, SCM delta),
24            "Transpose @var{p} by the amount @var{delta},"
25            " where @var{delta} is relative to middle@tie{}C.")
26 {
27   LY_ASSERT_SMOB (Pitch, p, 1);
28   LY_ASSERT_SMOB (Pitch, delta, 2);
29
30   Pitch *t = unsmob_pitch (p);
31   Pitch *d = unsmob_pitch (delta);
32   return t->transposed (*d).smobbed_copy ();
33 }
34
35 /* Should add optional args.  */
36 LY_DEFINE (ly_make_pitch, "ly:make-pitch",
37            2, 1, 0, (SCM octave, SCM note, SCM alter),
38            "@var{octave} is specified by an integer, zero for the octave"
39            " containing middle@tie{}C. @var{note} is a number indexing the"
40            " global default scale, with 0 corresponding to pitch@tie{}C"
41            " and 6 usually corresponding to pitch@tie{}B. @var{alter} is"
42            " a rational number of 200-cent whole tones for alteration.")
43
44 {
45   LY_ASSERT_TYPE (scm_is_integer, octave, 1);
46   LY_ASSERT_TYPE (scm_is_integer, note, 2);
47   LY_ASSERT_TYPE (scm_is_rational, alter, 3);
48
49   Pitch p (scm_to_int (octave), scm_to_int (note),
50            ly_scm2rational (alter));
51
52   return p.smobbed_copy ();
53 }
54
55 LY_DEFINE (ly_pitch_negate, "ly:pitch-negate", 1, 0, 0,
56            (SCM p),
57            "Negate @var{p}.")
58 {
59   LY_ASSERT_SMOB (Pitch, p, 1);
60   Pitch *pp = unsmob_pitch (p);
61   return pp->negated ().smobbed_copy ();
62 }
63
64 LY_DEFINE (ly_pitch_steps, "ly:pitch-steps", 1, 0, 0,
65            (SCM p),
66            "Number of steps counted from middle@tie{}C of the"
67            " pitch@tie{}@var{p}.")
68 {
69   LY_ASSERT_SMOB (Pitch, p, 1);
70   Pitch *pp = unsmob_pitch (p);
71   return scm_from_int (pp->steps ());
72 }
73
74 LY_DEFINE (ly_pitch_octave, "ly:pitch-octave",
75            1, 0, 0, (SCM pp),
76            "Extract the octave from pitch@tie{}@var{pp}.")
77 {
78   LY_ASSERT_SMOB (Pitch, pp, 1);
79   Pitch *p = unsmob_pitch (pp);
80   int q = p->get_octave ();
81   return scm_from_int (q);
82 }
83
84 LY_DEFINE (ly_pitch_alteration, "ly:pitch-alteration",
85            1, 0, 0, (SCM pp),
86            "Extract the alteration from pitch@tie{}@var{pp}.")
87 {
88   LY_ASSERT_SMOB (Pitch, pp, 1);
89   Pitch *p = unsmob_pitch (pp);
90   Rational q = p->get_alteration ();
91
92   return ly_rational2scm (q);
93 }
94
95 LY_DEFINE (ly_pitch_notename, "ly:pitch-notename",
96            1, 0, 0, (SCM pp),
97            "Extract the note name from pitch @var{pp}.")
98 {
99   LY_ASSERT_SMOB (Pitch, pp, 1);
100   Pitch *p = unsmob_pitch (pp);
101   int q = p->get_notename ();
102   return scm_from_int (q);
103 }
104
105 LY_DEFINE (ly_pitch_tones, "ly:pitch-tones",
106            1, 0, 0, (SCM pp),
107            "Calculate the number of tones of@tie{}@var{pp} from"
108            " middle@tie{}C as a rational number.")
109 {
110   LY_ASSERT_SMOB (Pitch, pp, 1);
111   return ly_rational2scm (unsmob_pitch (pp)->tone_pitch ());
112 }
113
114
115 LY_DEFINE (ly_pitch_quartertones, "ly:pitch-quartertones",
116            1, 0, 0, (SCM pp),
117            "Calculate the number of quarter tones of@tie{}@var{pp} from"
118            " middle@tie{}C.")
119 {
120   LY_ASSERT_SMOB (Pitch, pp, 1);
121   Pitch *p = unsmob_pitch (pp);
122   int q = p->rounded_quartertone_pitch ();
123   return scm_from_int (q);
124 }
125
126 LY_DEFINE (ly_pitch_semitones, "ly:pitch-semitones",
127            1, 0, 0, (SCM pp),
128            "Calculate the number of semitones of@tie{}@var{pp} from"
129            " middle@tie{}C.")
130 {
131   LY_ASSERT_SMOB (Pitch, pp, 1);
132   Pitch *p = unsmob_pitch (pp);
133   int q = p->rounded_semitone_pitch ();
134   return scm_from_int (q);
135 }
136
137 LY_DEFINE (ly_pitch_less_p, "ly:pitch<?",
138            2, 0, 0, (SCM p1, SCM p2),
139            "Is @var{p1} lexicographically smaller than @var{p2}?")
140 {
141   LY_ASSERT_SMOB (Pitch, p1, 1);
142   LY_ASSERT_SMOB (Pitch, p2, 2);
143
144   Pitch *a = unsmob_pitch (p1);
145   Pitch *b = unsmob_pitch (p2);
146
147   if (Pitch::compare (*a, *b) < 0)
148     return SCM_BOOL_T;
149   else
150     return SCM_BOOL_F;
151 }
152
153 LY_DEFINE (ly_pitch_diff, "ly:pitch-diff",
154            2, 0, 0, (SCM pitch, SCM root),
155            "Return pitch @var{delta} such that @var{pitch} transposed by"
156            " @var{delta} equals @var{root}.")
157 {
158
159   LY_ASSERT_SMOB (Pitch, pitch, 1);
160   LY_ASSERT_SMOB (Pitch, root, 2);
161
162   Pitch *p = unsmob_pitch (pitch);
163   Pitch *r = unsmob_pitch (root);
164
165   return pitch_interval (*r, *p).smobbed_copy ();
166 }
167
168 /* FIXME: probably isn't the right place for this function */
169 #include "context.hh"
170 LY_DEFINE (ly_set_middle_C_x, "ly:set-middle-C!",
171            1, 0, 0, (SCM context),
172            "Set the @code{middleCPosition} variable in @var{context}"
173            " based on the variables @code{middleCClefPosition} and"
174            " middleCOffset.")
175 {
176   LY_ASSERT_SMOB (Context, context, 1);
177
178   Context *c = unsmob_context (context);
179   int clef_pos = robust_scm2int (c->get_property ("middleCClefPosition"), 0);
180   int offset = robust_scm2int (c->get_property ("middleCOffset"), 0);
181   /* middleCCuePosition overrides the clef! */
182   SCM cue_pos = c->get_property ("middleCCuePosition");
183   if (scm_is_number (cue_pos))
184     clef_pos = robust_scm2int (cue_pos, 0);
185
186   c->set_property (ly_symbol2scm ("middleCPosition"), scm_from_int (clef_pos + offset));
187   return SCM_UNSPECIFIED;
188 }