2 pitch-scheme.cc -- implement scheme functions for Pitch
4 source file of the GNU LilyPond music typesetter
6 (c) 2005--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
11 LY_DEFINE (ly_pitch_transpose
, "ly:pitch-transpose",
12 2, 0, 0, (SCM p
, SCM delta
),
13 "Transpose @var{p} by the amount @var{delta},"
14 " where @var{delta} is relative to middle@tie{}C.")
16 LY_ASSERT_SMOB (Pitch
, p
, 1);
17 LY_ASSERT_SMOB (Pitch
, delta
, 2);
19 Pitch
*t
= unsmob_pitch (p
);
20 Pitch
*d
= unsmob_pitch (delta
);
21 return t
->transposed (*d
).smobbed_copy ();
24 /* Should add optional args. */
25 LY_DEFINE (ly_make_pitch
, "ly:make-pitch",
26 2, 1, 0, (SCM octave
, SCM note
, SCM alter
),
27 "@var{octave} is specified by an integer, zero for the octave"
28 " containing middle@tie{}C. @var{note} is a number from 0"
29 " to@tie{}6, with 0 corresponding to pitch@tie{}C and 6"
30 " corresponding to pitch@tie{}B. @var{alter} is a rational"
31 " number of whole tones for alteration.")
33 LY_ASSERT_TYPE (scm_is_integer
, octave
, 1);
34 LY_ASSERT_TYPE (scm_is_integer
, note
, 2);
35 LY_ASSERT_TYPE (scm_is_rational
, alter
, 3);
37 Pitch
p (scm_to_int (octave
), scm_to_int (note
),
38 ly_scm2rational (alter
));
40 return p
.smobbed_copy ();
43 LY_DEFINE (ly_pitch_negate
, "ly:pitch-negate", 1, 0, 0,
47 LY_ASSERT_SMOB (Pitch
, p
, 1);
48 Pitch
*pp
= unsmob_pitch (p
);
49 return pp
->negated ().smobbed_copy ();
52 LY_DEFINE (ly_pitch_steps
, "ly:pitch-steps", 1, 0, 0,
54 "Number of steps counted from middle@tie{}C of the"
55 " pitch@tie{}@var{p}.")
57 LY_ASSERT_SMOB (Pitch
, p
, 1);
58 Pitch
*pp
= unsmob_pitch (p
);
59 return scm_from_int (pp
->steps ());
62 LY_DEFINE (ly_pitch_octave
, "ly:pitch-octave",
64 "Extract the octave from pitch@tie{}@var{pp}.")
66 LY_ASSERT_SMOB (Pitch
, pp
, 1);
67 Pitch
*p
= unsmob_pitch (pp
);
68 int q
= p
->get_octave ();
69 return scm_from_int (q
);
72 LY_DEFINE (ly_pitch_alteration
, "ly:pitch-alteration",
74 "Extract the alteration from pitch@tie{}@var{pp}.")
76 LY_ASSERT_SMOB (Pitch
, pp
, 1);
77 Pitch
*p
= unsmob_pitch (pp
);
78 Rational q
= p
->get_alteration ();
80 return ly_rational2scm (q
);
83 LY_DEFINE (pitch_notename
, "ly:pitch-notename",
85 "Extract the note name from pitch @var{pp}.")
87 LY_ASSERT_SMOB (Pitch
, pp
, 1);
88 Pitch
*p
= unsmob_pitch (pp
);
89 int q
= p
->get_notename ();
90 return scm_from_int (q
);
93 LY_DEFINE (ly_pitch_quartertones
, "ly:pitch-quartertones",
95 "Calculate the number of quarter tones of@tie{}@var{pp} from"
98 LY_ASSERT_SMOB (Pitch
, pp
, 1);
99 Pitch
*p
= unsmob_pitch (pp
);
100 int q
= p
->rounded_quartertone_pitch ();
101 return scm_from_int (q
);
104 LY_DEFINE (ly_pitch_semitones
, "ly:pitch-semitones",
106 "Calculate the number of semitones of@tie{}@var{pp} from"
109 LY_ASSERT_SMOB (Pitch
, pp
, 1);
110 Pitch
*p
= unsmob_pitch (pp
);
111 int q
= p
->rounded_semitone_pitch ();
112 return scm_from_int (q
);
115 LY_DEFINE (ly_pitch_less_p
, "ly:pitch<?",
116 2, 0, 0, (SCM p1
, SCM p2
),
117 "Is @var{p1} lexicographically smaller than @var{p2}?")
119 LY_ASSERT_SMOB (Pitch
, p1
, 1);
120 LY_ASSERT_SMOB (Pitch
, p2
, 2);
122 Pitch
*a
= unsmob_pitch (p1
);
123 Pitch
*b
= unsmob_pitch (p2
);
125 if (Pitch::compare (*a
, *b
) < 0)
131 LY_DEFINE (ly_pitch_diff
, "ly:pitch-diff",
132 2, 0, 0, (SCM pitch
, SCM root
),
133 "Return pitch @var{delta} such that @var{pitch} transposed by"
134 " @var{delta} equals @var{root}.")
137 LY_ASSERT_SMOB (Pitch
, pitch
, 1);
138 LY_ASSERT_SMOB (Pitch
, root
, 2);
140 Pitch
*p
= unsmob_pitch (pitch
);
141 Pitch
*r
= unsmob_pitch (root
);
143 return pitch_interval (*r
, *p
).smobbed_copy ();
146 /* FIXME: probably isn't the right place for this function */
147 #include "context.hh"
148 LY_DEFINE (ly_set_middle_C_x
, "ly:set-middle-C!",
149 1, 0, 0, (SCM context
),
150 "Set the @code{middleCPosition} variable in @var{context}"
151 " based on the variables @code{middleCClefPosition} and"
154 LY_ASSERT_SMOB (Context
, context
, 1);
156 Context
*c
= unsmob_context (context
);
157 int clef_pos
= robust_scm2int (c
->get_property ("middleCClefPosition"), 0);
158 int offset
= robust_scm2int (c
->get_property ("middleCOffset"), 0);
160 c
->set_property (ly_symbol2scm ("middleCPosition"), scm_from_int (clef_pos
+ offset
));
161 return SCM_UNDEFINED
;