2 musical-pitch.cc -- implement Pitch
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
14 #include "ly-smobs.icc"
16 Pitch::Pitch (int o, int n, int a)
24 /* FIXME: why is octave == 0 and default not middleC ? */
33 Pitch::compare (Pitch const &m1, Pitch const &m2)
35 int o = m1.octave_ - m2.octave_;
36 int n = m1.notename_ - m2.notename_;
37 int a = m1.alteration_ - m2.alteration_;
51 return notename_ + octave_*7;
54 /* Should be settable from input? */
55 static Byte diatonic_scale_semitones[ ] = { 0, 2, 4, 5, 7, 9, 11 };
58 /* Calculate pitch height in 12th octave steps. Don't assume
59 normalised pitch as this function is used to normalise the pitch. */
61 Pitch::semitone_pitch () const
72 programming_error ("semitone_pitch () called on quarter tone alteration.");
74 return ((o + n / 7) * 12
75 + diatonic_scale_semitones[n % 7]
80 Pitch::quartertone_pitch () const
90 return ((o + n / 7) * 24
91 + 2 * diatonic_scale_semitones[n % 7]
98 int pitch = quartertone_pitch ();
99 while (notename_ >= 7)
103 alteration_ -= quartertone_pitch () - pitch;
105 while (notename_ < 0)
109 alteration_ -= quartertone_pitch () - pitch;
111 while (alteration_ > DOUBLE_SHARP)
122 alteration_ -= quartertone_pitch () - pitch;
125 while (alteration_ < DOUBLE_FLAT)
136 alteration_ -= quartertone_pitch () - pitch;
140 /* WHugh, wat een intervaas */
142 Pitch::transpose (Pitch delta)
144 int new_semi = quartertone_pitch () +delta.quartertone_pitch ();
145 octave_ += delta.octave_;
146 notename_ += delta.notename_;
147 alteration_ += new_semi - quartertone_pitch ();
153 pitch_interval (Pitch const & from , Pitch const & to )
155 int sound = to.quartertone_pitch () - from.quartertone_pitch ();
156 Pitch pt (to.get_octave () - from.get_octave (),
157 to.get_notename () - from.get_notename (),
159 to.get_alteration () - from.get_alteration ());
161 return pt.transposed (Pitch (0,0,sound - pt.quartertone_pitch ()));
166 Merge with *pitch->text* funcs in chord-name.scm */
167 char const *accname[] = {"eses", "eseh", "es", "eh", "",
168 "ih", "is" , "isih", "isis"};
171 Pitch::to_string () const
173 int n = (notename_ + 2) % 7;
174 String s = ::to_string (char (n + 'a'));
176 s += String (accname[alteration_ - DOUBLE_FLAT]);
184 else if (octave_ < 0)
186 int o = (-octave_) - 1;
188 s += ::to_string (',');
194 /* Change me to relative, counting from last pitch p
195 return copy of resulting pitch. */
197 Pitch::to_relative_octave (Pitch p) const
199 /* account for c' = octave 1 iso. 0 4 */
200 int oct_mod = octave_ + 1;
202 Pitch down_pitch (p);
204 up_pitch.alteration_ = alteration_;
205 down_pitch.alteration_ = alteration_;
208 up_pitch.up_to (notename_);
209 down_pitch.down_to (notename_);
212 if (abs (up_pitch.steps () - h) < abs (down_pitch.steps () - h))
217 n.octave_ += oct_mod;
222 Pitch::up_to (int notename)
224 if (notename_ > notename)
226 notename_ = notename;
230 Pitch::down_to (int notename)
232 if (notename_ < notename)
234 notename_ = notename;
237 LY_DEFINE (ly_pitch_transpose, "ly:pitch-transpose",
238 2, 0, 0, (SCM p, SCM delta),
239 "Transpose @var{p} by the amount @var{delta}, "
240 "where @var{delta} is relative to middle C.")
242 Pitch* t = unsmob_pitch (p);
243 Pitch *d = unsmob_pitch (delta);
244 SCM_ASSERT_TYPE (t, p, SCM_ARG1, __FUNCTION__, "pitch");
245 SCM_ASSERT_TYPE (d, delta, SCM_ARG1, __FUNCTION__, "pitch");
246 return t->transposed (*d).smobbed_copy ();
249 IMPLEMENT_TYPE_P (Pitch, "ly:pitch?");
252 Pitch::mark_smob (SCM)
257 IMPLEMENT_SIMPLE_SMOBS (Pitch);
259 Pitch::print_smob (SCM s, SCM port, scm_print_state *)
261 Pitch *r = (Pitch *) SCM_CELL_WORD_1 (s);
262 scm_puts ("#<Pitch ", port);
263 scm_display (scm_makfrom0str (r->to_string ().to_str0 ()), port);
264 scm_puts (" >", port);
269 Pitch::equal_p (SCM a , SCM b)
271 Pitch *p = (Pitch *) SCM_CELL_WORD_1 (a);
272 Pitch *q = (Pitch *) SCM_CELL_WORD_1 (b);
274 bool eq = p->notename_ == q->notename_
275 && p->octave_ == q->octave_
276 && p->alteration_ == q->alteration_;
278 return eq ? SCM_BOOL_T : SCM_BOOL_F;
281 MAKE_SCHEME_CALLBACK (Pitch, less_p, 2);
283 Pitch::less_p (SCM p1, SCM p2)
285 Pitch *a = unsmob_pitch (p1);
286 Pitch *b = unsmob_pitch (p2);
288 if (compare (*a, *b) < 0)
294 /* Should add optional args. */
295 LY_DEFINE (ly_make_pitch, "ly:make-pitch",
296 3, 0, 0, (SCM octave, SCM note, SCM alter),
297 "@var{octave} is specified by an integer, "
298 "zero for the octave containing middle C. "
299 "@var{note} is a number from 0 to 6, "
300 "with 0 corresponding to C and 6 corresponding to B. "
301 "The @var{alter} is zero for a natural, negative for "
302 "flats, or positive for sharps. ")
304 SCM_ASSERT_TYPE (scm_integer_p (octave)== SCM_BOOL_T , octave, SCM_ARG1, __FUNCTION__, "integer");
305 SCM_ASSERT_TYPE (scm_integer_p (note)== SCM_BOOL_T, note, SCM_ARG2, __FUNCTION__, "integer");
306 SCM_ASSERT_TYPE (scm_integer_p (alter)== SCM_BOOL_T, alter, SCM_ARG3, __FUNCTION__, "integer");
308 Pitch p (scm_to_int (octave), scm_to_int (note), scm_to_int (alter));
309 return p.smobbed_copy ();
312 LY_DEFINE (ly_pitch_steps, "ly:pitch-steps", 1, 0, 0,
314 "Number of steps counted from middle C of the pitch @var{p}.")
316 Pitch *pp = unsmob_pitch (p);
317 SCM_ASSERT_TYPE (pp, p, SCM_ARG1, __FUNCTION__, "Pitch");
318 return scm_int2num (pp->steps ());
321 LY_DEFINE (ly_pitch_octave, "ly:pitch-octave",
323 "Extract the octave from pitch @var{p}.")
325 Pitch *p = unsmob_pitch (pp);
326 SCM_ASSERT_TYPE (p, pp, SCM_ARG1, __FUNCTION__, "Pitch");
327 int q = p->get_octave ();
328 return scm_int2num (q);
331 LY_DEFINE (ly_pitch_alteration, "ly:pitch-alteration",
333 "Extract the alteration from pitch @var{p}.")
335 Pitch *p = unsmob_pitch (pp);
336 SCM_ASSERT_TYPE (p, pp, SCM_ARG1, __FUNCTION__, "Pitch");
337 int q = p->get_alteration ();
339 return scm_int2num (q);
342 LY_DEFINE (pitch_notename, "ly:pitch-notename",
344 "Extract the note name from pitch @var{pp}.")
346 Pitch *p = unsmob_pitch (pp);
347 SCM_ASSERT_TYPE (p, pp, SCM_ARG1, __FUNCTION__, "Pitch");
348 int q = p->get_notename ();
349 return scm_int2num (q);
352 LY_DEFINE (ly_pitch_quartertones, "ly:pitch-quartertones",
354 "Calculate the number of quarter tones of @var{p} from middle C.")
356 Pitch *p = unsmob_pitch (pp);
357 SCM_ASSERT_TYPE (p, pp, SCM_ARG1, __FUNCTION__, "Pitch");
358 int q = p->quartertone_pitch ();
359 return scm_int2num (q);
362 LY_DEFINE (ly_pitch_semitones, "ly:pitch-semitones",
364 "calculate the number of semitones of @var{p} from middle C.")
366 Pitch *p = unsmob_pitch (pp);
367 SCM_ASSERT_TYPE (p, pp, SCM_ARG1, __FUNCTION__, "Pitch");
368 int q = p->semitone_pitch ();
369 return scm_int2num (q);
372 LY_DEFINE (ly_pitch_less_p, "ly:pitch<?",
373 2, 0, 0, (SCM p1, SCM p2),
374 "Is @var{p1} lexicographically smaller than @var{p2}?")
376 Pitch *a = unsmob_pitch (p1);
377 Pitch *b = unsmob_pitch (p2);
379 SCM_ASSERT_TYPE (a, p1, SCM_ARG1, __FUNCTION__, "Pitch");
380 SCM_ASSERT_TYPE (b, p2, SCM_ARG2, __FUNCTION__, "Pitch");
382 if (Pitch::compare (*a, *b) < 0)
388 LY_DEFINE (ly_pitch_diff, "ly:pitch-diff",
389 2 ,0, 0, (SCM pitch, SCM root),
390 "Return pitch @var{delta} such that @code{pitch} transposed by "
391 "@var{delta} equals @var{root}" )
393 Pitch *p = unsmob_pitch (pitch);
394 Pitch *r = unsmob_pitch (root);
395 SCM_ASSERT_TYPE (p, pitch, SCM_ARG1, __FUNCTION__, "Pitch");
396 SCM_ASSERT_TYPE (r, root, SCM_ARG2, __FUNCTION__, "Pitch");
398 return pitch_interval (*r, *p).smobbed_copy ();
402 Pitch::get_octave () const
408 Pitch::get_notename () const
414 Pitch::get_alteration () const
420 Pitch::transposed (Pitch d) const