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"
16 Pitch::Pitch (int o, int n, int a)
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;
54 should be settable from input?
56 static Byte diatonic_scale_semitones[ ] = { 0, 2, 4, 5, 7, 9, 11 };
59 /* Calculate pitch height in 12th octave steps. Don't assume
60 normalised pitch as this function is used to normalise the pitch. */
62 Pitch::semitone_pitch () const
74 programming_error ("Calling semitone_pitch() for quarter tone alterations.");
78 return (o + n / 7) * 12 + diatonic_scale_semitones[n % 7] + (alteration_/2);
82 Pitch::quartertone_pitch () const
92 return (o + n / 7) * 24 + 2* diatonic_scale_semitones[n % 7] + (alteration_);
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 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
168 char const *accname[] = {"eses", "eseh", "es", "eh", "",
169 "ih", "is" , "isih", "isis"};
172 Pitch::to_string () const
174 int n = (notename_ + 2) % 7;
175 String s = ::to_string (char (n + 'a'));
177 s += String (accname[alteration_ - DOUBLE_FLAT]);
185 else if (octave_ < 0)
187 int o = (-octave_) - 1;
189 s += ::to_string (',');
196 change me to relative, counting from last pitch p
197 return copy of resulting pitch
200 Pitch::to_relative_octave (Pitch p) const
202 int oct_mod = octave_ + 1; // account for c' = octave 1 iso. 0 4
204 Pitch down_pitch (p);
206 up_pitch.alteration_ = alteration_;
207 down_pitch.alteration_ = alteration_;
210 up_pitch.up_to (notename_);
211 down_pitch.down_to (notename_);
214 if (abs (up_pitch.steps () - h) < abs (down_pitch.steps () - h))
219 n.octave_ += oct_mod;
224 Pitch::up_to (int notename)
226 if (notename_ > notename)
230 notename_ = notename;
234 Pitch::down_to (int notename)
236 if (notename_ < notename)
240 notename_ = notename;
243 LY_DEFINE(ly_pitch_transpose,
244 "ly:pitch-transpose", 2, 0, 0,
246 "Transpose @var{p} by the amount @var{delta}, where @var{delta} is the "
247 " pitch that central C is transposed to.")
249 Pitch* t = unsmob_pitch (p);
250 Pitch *d = unsmob_pitch (delta);
251 SCM_ASSERT_TYPE(t, p, SCM_ARG1, __FUNCTION__, "pitch") ;
252 SCM_ASSERT_TYPE(d, delta, SCM_ARG1, __FUNCTION__, "pitch") ;
254 return t->transposed (*d).smobbed_copy ();
257 /****************************************************************/
260 IMPLEMENT_TYPE_P (Pitch, "ly:pitch?");
263 Pitch::mark_smob (SCM)
268 IMPLEMENT_SIMPLE_SMOBS (Pitch);
270 Pitch::print_smob (SCM s, SCM port, scm_print_state *)
272 Pitch *r = (Pitch *) ly_cdr (s);
274 scm_puts ("#<Pitch ", port);
275 scm_display (scm_makfrom0str (r->to_string ().to_str0 ()), port);
276 scm_puts (" >", port);
282 Pitch::equal_p (SCM a , SCM b)
284 Pitch *p = (Pitch *) ly_cdr (a);
285 Pitch *q = (Pitch *) ly_cdr (b);
287 bool eq = p->notename_ == q->notename_
288 && p->octave_ == q->octave_
289 && p->alteration_ == q->alteration_;
291 return eq ? SCM_BOOL_T : SCM_BOOL_F;
294 MAKE_SCHEME_CALLBACK (Pitch, less_p, 2);
296 Pitch::less_p (SCM p1, SCM p2)
298 Pitch *a = unsmob_pitch (p1);
299 Pitch *b = unsmob_pitch (p2);
301 if (compare (*a, *b) < 0)
308 should add optional args
311 LY_DEFINE(make_pitch, "ly:make-pitch", 3, 0, 0,
312 (SCM octave, SCM note, SCM alter),
313 "@var{octave} is specified by an integer, zero for the octave containing "
314 "middle C. @var{note} is a number from 0 to 6, with 0 corresponding to C "
315 "and 6 corresponding to B. The @var{alter} is zero for a natural, negative for "
316 "flats, or positive for sharps. ")
318 SCM_ASSERT_TYPE(scm_integer_p (octave)== SCM_BOOL_T , octave, SCM_ARG1, __FUNCTION__, "integer");
319 SCM_ASSERT_TYPE(scm_integer_p (note)== SCM_BOOL_T, note, SCM_ARG2, __FUNCTION__, "integer");
320 SCM_ASSERT_TYPE(scm_integer_p (alter)== SCM_BOOL_T, alter, SCM_ARG3, __FUNCTION__, "integer");
322 Pitch p (gh_scm2int (octave), gh_scm2int (note), gh_scm2int (alter));
323 return p.smobbed_copy ();
326 LY_DEFINE(pitch_steps, "ly:pitch-steps", 1, 0,0,
328 "Number of steps counted from central C of the pitch @var{p}.")
330 Pitch *pp = unsmob_pitch (p);
331 SCM_ASSERT_TYPE(pp, p, SCM_ARG1, __FUNCTION__, "Pitch");
333 return gh_int2scm (pp->steps());
336 LY_DEFINE(pitch_octave, "ly:pitch-octave", 1, 0, 0,
338 "extract the octave from pitch @var{p}.")
340 Pitch *p = unsmob_pitch (pp);
341 SCM_ASSERT_TYPE(p, pp, SCM_ARG1, __FUNCTION__, "Pitch");
342 int q = p->get_octave ();
344 return gh_int2scm (q);
347 LY_DEFINE(pitch_alteration, "ly:pitch-alteration", 1, 0, 0,
349 "extract the alteration from pitch @var{p}.")
351 Pitch *p = unsmob_pitch (pp);
352 SCM_ASSERT_TYPE(p, pp, SCM_ARG1, __FUNCTION__, "Pitch");
353 int q = p->get_alteration ();
355 return gh_int2scm (q);
358 LY_DEFINE(pitch_notename, "ly:pitch-notename", 1, 0, 0,
360 "extract the note name from pitch @var{pp}.")
362 Pitch *p = unsmob_pitch (pp);
363 SCM_ASSERT_TYPE(p, pp, SCM_ARG1, __FUNCTION__, "Pitch");
364 int q = p->get_notename ();
366 return gh_int2scm (q);
369 LY_DEFINE(ly_pitch_quartertones, "ly:pitch-quartertones", 1, 0, 0,
371 "calculate the number of semitones of @var{p} from central C.")
373 Pitch *p = unsmob_pitch (pp);
374 SCM_ASSERT_TYPE(p, pp, SCM_ARG1, __FUNCTION__, "Pitch");
376 int q = p->quartertone_pitch ();
378 return gh_int2scm (q);
381 LY_DEFINE(ly_pitch_semitones, "ly:pitch-semitones", 1, 0, 0,
383 "calculate the number of semitones of @var{p} from central C.")
385 Pitch *p = unsmob_pitch (pp);
386 SCM_ASSERT_TYPE(p, pp, SCM_ARG1, __FUNCTION__, "Pitch");
388 int q = p->semitone_pitch ();
390 return gh_int2scm (q);
393 LY_DEFINE(pitch_less, "ly:pitch<?", 2,0,0, (SCM p1, SCM p2),
394 "Is @var{p1} lower than @var{p2}? This uses lexicographic ordening.")
396 Pitch *a = unsmob_pitch (p1);
397 Pitch *b = unsmob_pitch (p2);
399 SCM_ASSERT_TYPE(a, p1, SCM_ARG1, __FUNCTION__, "Pitch");
400 SCM_ASSERT_TYPE(b, p2, SCM_ARG2, __FUNCTION__, "Pitch");
402 if (Pitch::compare (*a, *b) < 0)
408 LY_DEFINE(ly_pitch_diff, "ly:pitch-diff", 2 ,0 ,0,
409 (SCM pitch, SCM root),
410 "Return pitch @var{delta} such that @code{pitch} transposed by "
411 "@var{delta} equals @var{root}"
414 Pitch *p = unsmob_pitch (pitch);
415 Pitch *r = unsmob_pitch (root);
416 SCM_ASSERT_TYPE(p, pitch, SCM_ARG1, __FUNCTION__, "Pitch");
417 SCM_ASSERT_TYPE(r, root, SCM_ARG2, __FUNCTION__, "Pitch");
419 return interval (*r, *p).smobbed_copy();
423 Pitch::smobbed_copy ()const
425 Pitch * p = new Pitch (*this);
426 return p->smobbed_self ();
430 Pitch::get_octave ()const
436 Pitch::get_notename () const
442 Pitch::get_alteration () const
448 Pitch::transposed (Pitch d) const