2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2005--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
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.
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.
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/>.
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.")
27 LY_ASSERT_SMOB (Pitch, p, 1);
28 LY_ASSERT_SMOB (Pitch, delta, 2);
30 Pitch *t = Pitch::unsmob (p);
31 Pitch *d = Pitch::unsmob (delta);
32 return t->transposed (*d).smobbed_copy ();
35 LY_DEFINE (ly_make_pitch, "ly:make-pitch",
36 2, 1, 0, (SCM octave, SCM note, SCM alter),
37 "@var{octave} is specified by an integer, zero for the octave"
38 " containing middle@tie{}C. @var{note} is a number indexing the"
39 " global default scale, with 0 corresponding to pitch@tie{}C"
40 " and 6 usually corresponding to pitch@tie{}B."
41 " Optional @var{alter} is"
42 " a rational number of 200-cent whole tones for alteration.")
45 LY_ASSERT_TYPE (scm_is_integer, octave, 1);
46 LY_ASSERT_TYPE (scm_is_integer, note, 2);
47 if (SCM_UNBNDP (alter))
49 LY_ASSERT_TYPE (scm_is_rational, alter, 3);
51 Pitch p (scm_to_int (octave), scm_to_int (note),
52 ly_scm2rational (alter));
54 return p.smobbed_copy ();
57 LY_DEFINE (ly_pitch_negate, "ly:pitch-negate", 1, 0, 0,
61 LY_ASSERT_SMOB (Pitch, p, 1);
62 Pitch *pp = Pitch::unsmob (p);
63 return pp->negated ().smobbed_copy ();
66 LY_DEFINE (ly_pitch_steps, "ly:pitch-steps", 1, 0, 0,
68 "Number of steps counted from middle@tie{}C of the"
69 " pitch@tie{}@var{p}.")
71 LY_ASSERT_SMOB (Pitch, p, 1);
72 Pitch *pp = Pitch::unsmob (p);
73 return scm_from_int (pp->steps ());
76 LY_DEFINE (ly_pitch_octave, "ly:pitch-octave",
78 "Extract the octave from pitch@tie{}@var{pp}.")
80 LY_ASSERT_SMOB (Pitch, pp, 1);
81 Pitch *p = Pitch::unsmob (pp);
82 int q = p->get_octave ();
83 return scm_from_int (q);
86 LY_DEFINE (ly_pitch_alteration, "ly:pitch-alteration",
88 "Extract the alteration from pitch@tie{}@var{pp}.")
90 LY_ASSERT_SMOB (Pitch, pp, 1);
91 Pitch *p = Pitch::unsmob (pp);
92 Rational q = p->get_alteration ();
94 return ly_rational2scm (q);
97 LY_DEFINE (ly_pitch_notename, "ly:pitch-notename",
99 "Extract the note name from pitch @var{pp}.")
101 LY_ASSERT_SMOB (Pitch, pp, 1);
102 Pitch *p = Pitch::unsmob (pp);
103 int q = p->get_notename ();
104 return scm_from_int (q);
107 LY_DEFINE (ly_pitch_tones, "ly:pitch-tones",
109 "Calculate the number of tones of@tie{}@var{pp} from"
110 " middle@tie{}C as a rational number.")
112 LY_ASSERT_SMOB (Pitch, pp, 1);
113 return ly_rational2scm (Pitch::unsmob (pp)->tone_pitch ());
116 LY_DEFINE (ly_pitch_quartertones, "ly:pitch-quartertones",
118 "Calculate the number of quarter tones of@tie{}@var{pp} from"
121 LY_ASSERT_SMOB (Pitch, pp, 1);
122 Pitch *p = Pitch::unsmob (pp);
123 int q = p->rounded_quartertone_pitch ();
124 return scm_from_int (q);
127 LY_DEFINE (ly_pitch_semitones, "ly:pitch-semitones",
129 "Calculate the number of semitones of@tie{}@var{pp} from"
132 LY_ASSERT_SMOB (Pitch, pp, 1);
133 Pitch *p = Pitch::unsmob (pp);
134 int q = p->rounded_semitone_pitch ();
135 return scm_from_int (q);
138 LY_DEFINE (ly_pitch_less_p, "ly:pitch<?",
139 2, 0, 0, (SCM p1, SCM p2),
140 "Is @var{p1} lexicographically smaller than @var{p2}?")
142 LY_ASSERT_SMOB (Pitch, p1, 1);
143 LY_ASSERT_SMOB (Pitch, p2, 2);
145 Pitch *a = Pitch::unsmob (p1);
146 Pitch *b = Pitch::unsmob (p2);
148 if (Pitch::compare (*a, *b) < 0)
154 LY_DEFINE (ly_pitch_diff, "ly:pitch-diff",
155 2, 0, 0, (SCM pitch, SCM root),
156 "Return pitch @var{delta} such that @var{root} transposed by"
157 " @var{delta} equals @var{pitch}.")
160 LY_ASSERT_SMOB (Pitch, pitch, 1);
161 LY_ASSERT_SMOB (Pitch, root, 2);
163 Pitch *p = Pitch::unsmob (pitch);
164 Pitch *r = Pitch::unsmob (root);
166 return pitch_interval (*r, *p).smobbed_copy ();
169 /* FIXME: probably isn't the right place for this function */
170 #include "context.hh"
171 LY_DEFINE (ly_set_middle_C_x, "ly:set-middle-C!",
172 1, 0, 0, (SCM context),
173 "Set the @code{middleCPosition} variable in @var{context}"
174 " based on the variables @code{middleCClefPosition} and"
175 " @code{middleCOffset}.")
177 LY_ASSERT_SMOB (Context, context, 1);
179 Context *c = Context::unsmob (context);
180 int clef_pos = robust_scm2int (c->get_property ("middleCClefPosition"), 0);
181 int offset = robust_scm2int (c->get_property ("middleCOffset"), 0);
182 /* middleCCuePosition overrides the clef! */
183 SCM cue_pos = c->get_property ("middleCCuePosition");
184 if (scm_is_number (cue_pos))
185 clef_pos = robust_scm2int (cue_pos, 0);
187 c->set_property (ly_symbol2scm ("middleCPosition"), scm_from_int (clef_pos + offset));
188 return SCM_UNSPECIFIED;