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>
12 #include "ly-smobs.icc"
15 Pitch::Pitch (int o, int n, int a)
23 /* FIXME: why is octave == 0 and default not middleC ? */
32 Pitch::compare (Pitch const &m1, Pitch const &m2)
34 int o= m1.octave_ - m2.octave_;
35 int n = m1.notename_ - m2.notename_;
36 int a = m1.alteration_ - m2.alteration_;
50 return notename_ + octave_*7;
53 /* Should be settable from input? */
54 static Byte diatonic_scale_semitones[ ] = { 0, 2, 4, 5, 7, 9, 11 };
57 /* Calculate pitch height in 12th octave steps. Don't assume
58 normalised pitch as this function is used to normalise the pitch. */
60 Pitch::semitone_pitch () const
71 programming_error ("semitone_pitch () called on quarter tone alteration.");
73 return ((o + n / 7) * 12
74 + diatonic_scale_semitones[n % 7]
79 Pitch::quartertone_pitch () const
89 return ((o + n / 7) * 24
90 + 2 * diatonic_scale_semitones[n % 7]
97 int pitch = quartertone_pitch ();
98 while (notename_ >= 7)
102 alteration_ -= quartertone_pitch () - pitch;
104 while (notename_ < 0)
108 alteration_ -= quartertone_pitch () - pitch;
110 while (alteration_ > DOUBLE_SHARP)
121 alteration_ -= quartertone_pitch () - pitch;
124 while (alteration_ < DOUBLE_FLAT)
135 alteration_ -= quartertone_pitch () - pitch;
139 /* WHugh, wat een intervaas */
141 Pitch::transpose (Pitch delta)
143 int new_semi = quartertone_pitch () +delta.quartertone_pitch ();
144 octave_ += delta.octave_;
145 notename_ += delta.notename_;
146 alteration_ += new_semi - quartertone_pitch ();
152 interval (Pitch const & from , Pitch const & to )
154 int sound = to.quartertone_pitch () - from.quartertone_pitch ();
155 Pitch pt (to.get_octave () - from.get_octave (),
156 to.get_notename () - from.get_notename (),
158 to.get_alteration () - from.get_alteration ());
160 return pt.transposed (Pitch (0,0,sound - pt.quartertone_pitch ()));
165 Merge with *pitch->text* funcs in chord-name.scm */
166 char const *accname[] = {"eses", "eseh", "es", "eh", "",
167 "ih", "is" , "isih", "isis"};
170 Pitch::to_string () const
172 int n = (notename_ + 2) % 7;
173 String s = ::to_string (char (n + 'a'));
175 s += String (accname[alteration_ - DOUBLE_FLAT]);
183 else if (octave_ < 0)
185 int o = (-octave_) - 1;
187 s += ::to_string (',');
193 /* Change me to relative, counting from last pitch p
194 return copy of resulting pitch. */
196 Pitch::to_relative_octave (Pitch p) const
198 /* account for c' = octave 1 iso. 0 4 */
199 int oct_mod = octave_ + 1;
201 Pitch down_pitch (p);
203 up_pitch.alteration_ = alteration_;
204 down_pitch.alteration_ = alteration_;
207 up_pitch.up_to (notename_);
208 down_pitch.down_to (notename_);
211 if (abs (up_pitch.steps () - h) < abs (down_pitch.steps () - h))
216 n.octave_ += oct_mod;
221 Pitch::up_to (int notename)
223 if (notename_ > notename)
225 notename_ = notename;
229 Pitch::down_to (int notename)
231 if (notename_ < notename)
233 notename_ = notename;
236 LY_DEFINE (ly_pitch_transpose, "ly:pitch-transpose",
237 2, 0, 0, (SCM p, SCM delta),
238 "Transpose @var{p} by the amount @var{delta}, "
239 "where @var{delta} is relative to middle C.")
241 Pitch* t = unsmob_pitch (p);
242 Pitch *d = unsmob_pitch (delta);
243 SCM_ASSERT_TYPE (t, p, SCM_ARG1, __FUNCTION__, "pitch");
244 SCM_ASSERT_TYPE (d, delta, SCM_ARG1, __FUNCTION__, "pitch");
245 return t->transposed (*d).smobbed_copy ();
248 IMPLEMENT_TYPE_P (Pitch, "ly:pitch?");
251 Pitch::mark_smob (SCM)
256 IMPLEMENT_SIMPLE_SMOBS (Pitch);
258 Pitch::print_smob (SCM s, SCM port, scm_print_state *)
260 Pitch *r = (Pitch *) scm_cdr (s);
261 scm_puts ("#<Pitch ", port);
262 scm_display (scm_makfrom0str (r->to_string ().to_str0 ()), port);
263 scm_puts (" >", port);
268 Pitch::equal_p (SCM a , SCM b)
270 Pitch *p = (Pitch *) scm_cdr (a);
271 Pitch *q = (Pitch *) scm_cdr (b);
273 bool eq = p->notename_ == q->notename_
274 && p->octave_ == q->octave_
275 && p->alteration_ == q->alteration_;
277 return eq ? SCM_BOOL_T : SCM_BOOL_F;
280 MAKE_SCHEME_CALLBACK (Pitch, less_p, 2);
282 Pitch::less_p (SCM p1, SCM p2)
284 Pitch *a = unsmob_pitch (p1);
285 Pitch *b = unsmob_pitch (p2);
287 if (compare (*a, *b) < 0)
293 /* Should add optional args. */
294 LY_DEFINE (ly_make_pitch, "ly:make-pitch",
295 3, 0, 0, (SCM octave, SCM note, SCM alter),
296 "@var{octave} is specified by an integer, "
297 "zero for the octave containing middle C. "
298 "@var{note} is a number from 0 to 6, "
299 "with 0 corresponding to C and 6 corresponding to B. "
300 "The @var{alter} is zero for a natural, negative for "
301 "flats, or positive for sharps. ")
303 SCM_ASSERT_TYPE (scm_integer_p (octave)== SCM_BOOL_T , octave, SCM_ARG1, __FUNCTION__, "integer");
304 SCM_ASSERT_TYPE (scm_integer_p (note)== SCM_BOOL_T, note, SCM_ARG2, __FUNCTION__, "integer");
305 SCM_ASSERT_TYPE (scm_integer_p (alter)== SCM_BOOL_T, alter, SCM_ARG3, __FUNCTION__, "integer");
307 Pitch p (scm_to_int (octave), scm_to_int (note), scm_to_int (alter));
308 return p.smobbed_copy ();
311 LY_DEFINE (ly_pitch_steps, "ly:pitch-steps", 1, 0, 0,
313 "Number of steps counted from middle C of the pitch @var{p}.")
315 Pitch *pp = unsmob_pitch (p);
316 SCM_ASSERT_TYPE (pp, p, SCM_ARG1, __FUNCTION__, "Pitch");
317 return scm_int2num (pp->steps ());
320 LY_DEFINE (ly_pitch_octave, "ly:pitch-octave",
322 "Extract the octave from pitch @var{p}.")
324 Pitch *p = unsmob_pitch (pp);
325 SCM_ASSERT_TYPE (p, pp, SCM_ARG1, __FUNCTION__, "Pitch");
326 int q = p->get_octave ();
327 return scm_int2num (q);
330 LY_DEFINE (ly_pitch_alteration, "ly:pitch-alteration",
332 "Extract the alteration from pitch @var{p}.")
334 Pitch *p = unsmob_pitch (pp);
335 SCM_ASSERT_TYPE (p, pp, SCM_ARG1, __FUNCTION__, "Pitch");
336 int q = p->get_alteration ();
338 return scm_int2num (q);
341 LY_DEFINE (pitch_notename, "ly:pitch-notename",
343 "Extract the note name from pitch @var{pp}.")
345 Pitch *p = unsmob_pitch (pp);
346 SCM_ASSERT_TYPE (p, pp, SCM_ARG1, __FUNCTION__, "Pitch");
347 int q = p->get_notename ();
348 return scm_int2num (q);
351 LY_DEFINE (ly_pitch_quartertones, "ly:pitch-quartertones",
353 "Calculate the number of quarter tones of @var{p} from middle C.")
355 Pitch *p = unsmob_pitch (pp);
356 SCM_ASSERT_TYPE (p, pp, SCM_ARG1, __FUNCTION__, "Pitch");
357 int q = p->quartertone_pitch ();
358 return scm_int2num (q);
361 LY_DEFINE (ly_pitch_semitones, "ly:pitch-semitones",
363 "calculate the number of semitones of @var{p} from middle C.")
365 Pitch *p = unsmob_pitch (pp);
366 SCM_ASSERT_TYPE (p, pp, SCM_ARG1, __FUNCTION__, "Pitch");
367 int q = p->semitone_pitch ();
368 return scm_int2num (q);
371 LY_DEFINE (ly_pitch_less_p, "ly:pitch<?",
372 2, 0, 0, (SCM p1, SCM p2),
373 "Is @var{p1} lexicographically smaller than @var{p2}?")
375 Pitch *a = unsmob_pitch (p1);
376 Pitch *b = unsmob_pitch (p2);
378 SCM_ASSERT_TYPE (a, p1, SCM_ARG1, __FUNCTION__, "Pitch");
379 SCM_ASSERT_TYPE (b, p2, SCM_ARG2, __FUNCTION__, "Pitch");
381 if (Pitch::compare (*a, *b) < 0)
387 LY_DEFINE (ly_pitch_diff, "ly:pitch-diff",
388 2 ,0, 0, (SCM pitch, SCM root),
389 "Return pitch @var{delta} such that @code{pitch} transposed by "
390 "@var{delta} equals @var{root}" )
392 Pitch *p = unsmob_pitch (pitch);
393 Pitch *r = unsmob_pitch (root);
394 SCM_ASSERT_TYPE (p, pitch, SCM_ARG1, __FUNCTION__, "Pitch");
395 SCM_ASSERT_TYPE (r, root, SCM_ARG2, __FUNCTION__, "Pitch");
397 return interval (*r, *p).smobbed_copy ();
401 Pitch::get_octave () const
407 Pitch::get_notename () const
413 Pitch::get_alteration () const
419 Pitch::transposed (Pitch d) const