2 pitch-scheme.cc -- implement scheme functions for Pitch
4 source file of the GNU LilyPond music typesetter
6 (c) 2005--2006 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 C.")
16 Pitch *t = unsmob_pitch (p);
17 Pitch *d = unsmob_pitch (delta);
18 SCM_ASSERT_TYPE (t, p, SCM_ARG1, __FUNCTION__, "pitch");
19 SCM_ASSERT_TYPE (d, delta, SCM_ARG1, __FUNCTION__, "pitch");
20 return t->transposed (*d).smobbed_copy ();
23 /* Should add optional args. */
24 LY_DEFINE (ly_make_pitch, "ly:make-pitch",
25 3, 0, 0, (SCM octave, SCM note, SCM alter),
26 "@var{octave} is specified by an integer, "
27 "zero for the octave containing middle C. "
28 "@var{note} is a number from 0 to 6, "
29 "with 0 corresponding to C and 6 corresponding to B. "
30 "The @var{alter} is zero for a natural, negative for "
31 "flats, or positive for sharps. ")
33 SCM_ASSERT_TYPE (scm_integer_p (octave) == SCM_BOOL_T, octave, SCM_ARG1, __FUNCTION__, "integer");
34 SCM_ASSERT_TYPE (scm_integer_p (note) == SCM_BOOL_T, note, SCM_ARG2, __FUNCTION__, "integer");
35 SCM_ASSERT_TYPE (scm_integer_p (alter) == SCM_BOOL_T, alter, SCM_ARG3, __FUNCTION__, "integer");
37 Pitch p (scm_to_int (octave), scm_to_int (note), scm_to_int (alter));
38 return p.smobbed_copy ();
41 LY_DEFINE (ly_pitch_steps, "ly:pitch-steps", 1, 0, 0,
43 "Number of steps counted from middle C of the pitch @var{p}.")
45 Pitch *pp = unsmob_pitch (p);
46 SCM_ASSERT_TYPE (pp, p, SCM_ARG1, __FUNCTION__, "Pitch");
47 return scm_from_int (pp->steps ());
50 LY_DEFINE (ly_pitch_octave, "ly:pitch-octave",
52 "Extract the octave from pitch @var{p}.")
54 Pitch *p = unsmob_pitch (pp);
55 SCM_ASSERT_TYPE (p, pp, SCM_ARG1, __FUNCTION__, "Pitch");
56 int q = p->get_octave ();
57 return scm_from_int (q);
60 LY_DEFINE (ly_pitch_alteration, "ly:pitch-alteration",
62 "Extract the alteration from pitch @var{p}.")
64 Pitch *p = unsmob_pitch (pp);
65 SCM_ASSERT_TYPE (p, pp, SCM_ARG1, __FUNCTION__, "Pitch");
66 int q = p->get_alteration ();
68 return scm_from_int (q);
71 LY_DEFINE (pitch_notename, "ly:pitch-notename",
73 "Extract the note name from pitch @var{pp}.")
75 Pitch *p = unsmob_pitch (pp);
76 SCM_ASSERT_TYPE (p, pp, SCM_ARG1, __FUNCTION__, "Pitch");
77 int q = p->get_notename ();
78 return scm_from_int (q);
81 LY_DEFINE (ly_pitch_quartertones, "ly:pitch-quartertones",
83 "Calculate the number of quarter tones of @var{p} from middle C.")
85 Pitch *p = unsmob_pitch (pp);
86 SCM_ASSERT_TYPE (p, pp, SCM_ARG1, __FUNCTION__, "Pitch");
87 int q = p->quartertone_pitch ();
88 return scm_from_int (q);
91 LY_DEFINE (ly_pitch_semitones, "ly:pitch-semitones",
93 "calculate the number of semitones of @var{p} from middle C.")
95 Pitch *p = unsmob_pitch (pp);
96 SCM_ASSERT_TYPE (p, pp, SCM_ARG1, __FUNCTION__, "Pitch");
97 int q = p->semitone_pitch ();
98 return scm_from_int (q);
101 LY_DEFINE (ly_pitch_less_p, "ly:pitch<?",
102 2, 0, 0, (SCM p1, SCM p2),
103 "Is @var{p1} lexicographically smaller than @var{p2}?")
105 Pitch *a = unsmob_pitch (p1);
106 Pitch *b = unsmob_pitch (p2);
108 SCM_ASSERT_TYPE (a, p1, SCM_ARG1, __FUNCTION__, "Pitch");
109 SCM_ASSERT_TYPE (b, p2, SCM_ARG2, __FUNCTION__, "Pitch");
111 if (Pitch::compare (*a, *b) < 0)
117 LY_DEFINE (ly_pitch_diff, "ly:pitch-diff",
118 2, 0, 0, (SCM pitch, SCM root),
119 "Return pitch @var{delta} such that @code{pitch} transposed by "
120 "@var{delta} equals @var{root}")
122 Pitch *p = unsmob_pitch (pitch);
123 Pitch *r = unsmob_pitch (root);
124 SCM_ASSERT_TYPE (p, pitch, SCM_ARG1, __FUNCTION__, "Pitch");
125 SCM_ASSERT_TYPE (r, root, SCM_ARG2, __FUNCTION__, "Pitch");
127 return pitch_interval (*r, *p).smobbed_copy ();