2 musical-pitch.cc -- implement Pitch
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
12 #include "ly-smobs.icc"
16 Pitch::Pitch (int o, int n, int a)
22 if (n < 0 || n >= 7 ||
25 String s = _ ("Pitch arguments out of range");
26 s += ": alteration = " + to_string (a);
27 s += ", notename = " + to_string (n);
41 Pitch::compare (Pitch const &m1, Pitch const &m2)
43 int o= m1.octave_ - m2.octave_;
44 int n = m1.notename_ - m2.notename_;
45 int a = m1.alteration_ - m2.alteration_;
59 return notename_ + octave_*7;
63 should be settable from input?
65 static Byte pitch_byte_a[ ] = { 0, 2, 4, 5, 7, 9, 11 };
68 /* Calculate pitch height in 12th octave steps. Don't assume
69 normalised pitch as this function is used to normalise the pitch. */
71 Pitch::semitone_pitch () const
80 return (o + n / 7) * 12 + pitch_byte_a[n % 7] + alteration_;
86 int pitch = semitone_pitch ();
87 while (notename_ >= 7)
91 alteration_ -= semitone_pitch () - pitch;
97 alteration_ -= semitone_pitch () - pitch;
99 while (alteration_ >= 3)
110 alteration_ -= semitone_pitch () - pitch;
112 while (alteration_ <= -3)
123 alteration_ -= semitone_pitch () - pitch;
127 /* WHugh, wat een intervaas */
129 Pitch::transpose (Pitch delta)
131 int new_semi = semitone_pitch () +delta.semitone_pitch();
132 octave_ += delta.octave_;
133 notename_ += delta.notename_;
134 alteration_ += new_semi - semitone_pitch();
140 interval (Pitch const & from , Pitch const & to )
142 int sound = to.semitone_pitch() - from.semitone_pitch ();
143 Pitch pt (to.get_octave () - from.get_octave (),
144 to.get_notename() - from.get_notename(),
145 to.get_alteration() - from.get_alteration());
147 return pt.transposed (Pitch(0,0,sound - pt.semitone_pitch()));
152 Merge with *pitch->text* funcs in chord-name.scm
154 char const *accname[] = {"eses", "es", "", "is" , "isis"};
157 Pitch::string () const
159 int n = (notename_ + 2) % 7;
160 String s = to_string (char (n + 'a'));
162 s += String (accname[alteration_ + 2]);
170 else if (octave_ < 0)
172 int o = (-octave_) - 1;
174 s += to_string (',');
181 change me to relative, counting from last pitch p
182 return copy of resulting pitch
185 Pitch::to_relative_octave (Pitch p) const
187 int oct_mod = octave_ + 1; // account for c' = octave 1 iso. 0 4
189 Pitch down_pitch (p);
191 up_pitch.alteration_ = alteration_;
192 down_pitch.alteration_ = alteration_;
195 up_pitch.up_to (notename_);
196 down_pitch.down_to (notename_);
199 if (abs (up_pitch.steps () - h) < abs (down_pitch.steps () - h))
204 n.octave_ += oct_mod;
209 Pitch::up_to (int notename)
211 if (notename_ > notename)
215 notename_ = notename;
219 Pitch::down_to (int notename)
221 if (notename_ < notename)
225 notename_ = notename;
228 LY_DEFINE(ly_pitch_transpose,
229 "ly:pitch-transpose", 2, 0, 0,
231 "Transpose @var{p} by the amount @var{delta}, where @var{delta} is the "
232 " pitch that central C is transposed to.")
234 Pitch* t = unsmob_pitch (p);
235 Pitch *d = unsmob_pitch (delta);
236 SCM_ASSERT_TYPE(t, p, SCM_ARG1, __FUNCTION__, "pitch") ;
237 SCM_ASSERT_TYPE(d, delta, SCM_ARG1, __FUNCTION__, "pitch") ;
239 return t->transposed (*d).smobbed_copy ();
242 /****************************************************************/
245 IMPLEMENT_TYPE_P (Pitch, "ly:pitch?");
248 Pitch::mark_smob (SCM)
253 IMPLEMENT_SIMPLE_SMOBS (Pitch);
255 Pitch::print_smob (SCM s, SCM port, scm_print_state *)
257 Pitch *r = (Pitch *) ly_cdr (s);
259 scm_puts ("#<Pitch ", port);
260 scm_display (scm_makfrom0str (r->string ().to_str0 ()), port);
261 scm_puts (" >", port);
267 Pitch::equal_p (SCM a , SCM b)
269 Pitch *p = (Pitch *) ly_cdr (a);
270 Pitch *q = (Pitch *) ly_cdr (b);
272 bool eq = p->notename_ == q->notename_
273 && p->octave_ == q->octave_
274 && p->alteration_ == q->alteration_;
276 return eq ? SCM_BOOL_T : SCM_BOOL_F;
279 MAKE_SCHEME_CALLBACK (Pitch, less_p, 2);
281 Pitch::less_p (SCM p1, SCM p2)
283 Pitch *a = unsmob_pitch (p1);
284 Pitch *b = unsmob_pitch (p2);
286 if (compare (*a, *b) < 0)
293 should add optional args
296 LY_DEFINE(make_pitch, "ly:make-pitch", 3, 0, 0,
297 (SCM o, SCM n, SCM a),
298 "@var{octave} is specified by an integer, zero for the octave containing "
299 "middle C. @var{note} is a number from 0 to 6, with 0 corresponding to C "
300 "and 6 corresponding to B. The shift is zero for a natural, negative for "
301 "flats, or positive for sharps. ")
303 SCM_ASSERT_TYPE(gh_number_p (o), o, SCM_ARG1, __FUNCTION__, "number");
304 SCM_ASSERT_TYPE(gh_number_p (n), n, SCM_ARG2, __FUNCTION__, "number");
305 SCM_ASSERT_TYPE(gh_number_p (a), a, SCM_ARG3, __FUNCTION__, "number");
307 Pitch p (gh_scm2int (o), gh_scm2int (n), gh_scm2int (a));
308 return p.smobbed_copy ();
311 LY_DEFINE(pitch_steps, "ly:pitch-steps", 1, 0,0,
313 "Number of steps counted from central C of the pitch @var{p}.")
315 Pitch *pp = unsmob_pitch (p);
316 SCM_ASSERT_TYPE(pp, p, SCM_ARG1, __FUNCTION__, "Pitch");
318 return gh_int2scm (pp->steps());
321 LY_DEFINE(pitch_octave, "ly:pitch-octave", 1, 0, 0,
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 ();
329 return gh_int2scm (q);
332 LY_DEFINE(pitch_alteration, "ly:pitch-alteration", 1, 0, 0,
334 "extract the alteration from pitch @var{p}.")
336 Pitch *p = unsmob_pitch (pp);
337 SCM_ASSERT_TYPE(p, pp, SCM_ARG1, __FUNCTION__, "Pitch");
338 int q = p->get_alteration ();
340 return gh_int2scm (q);
343 LY_DEFINE(pitch_notename, "ly:pitch-notename", 1, 0, 0,
345 "extract the note name from pitch @var{pp}.")
347 Pitch *p = unsmob_pitch (pp);
348 SCM_ASSERT_TYPE(p, pp, SCM_ARG1, __FUNCTION__, "Pitch");
349 int q = p->get_notename ();
351 return gh_int2scm (q);
354 LY_DEFINE(pitch_semitones, "ly:pitch-semitones", 1, 0, 0,
356 "calculate the number of semitones of @var{p} from central C.")
358 Pitch *p = unsmob_pitch (pp);
359 SCM_ASSERT_TYPE(p, pp, SCM_ARG1, __FUNCTION__, "Pitch");
361 int q = p->semitone_pitch ();
363 return gh_int2scm (q);
366 LY_DEFINE(pitch_less, "ly:pitch<?", 2,0,0, (SCM p1, SCM p2),
367 "Is @var{p1} lower than @var{p2}? This uses lexicographic ordening.")
369 return Pitch::less_p (ly_car (p1), ly_car (p2));
373 LY_DEFINE(ly_pitch_diff, "ly:pitch-diff", 2 ,0 ,0,
374 (SCM pitch, SCM root),
375 "Return pitch with value DELTA = PITCH - ROOT, ie,
376 ROOT == (ly:pitch-transpose root delta).")
378 Pitch *p = unsmob_pitch (pitch);
379 Pitch *r = unsmob_pitch (root);
380 SCM_ASSERT_TYPE(p, pitch, SCM_ARG1, __FUNCTION__, "Pitch");
381 SCM_ASSERT_TYPE(r, root, SCM_ARG2, __FUNCTION__, "Pitch");
383 return interval (*p, *r ).smobbed_copy();
391 Pitch::smobbed_copy ()const
393 Pitch * p = new Pitch (*this);
394 return p->smobbed_self ();
398 Pitch::get_octave ()const
404 Pitch::get_notename () const
410 Pitch::get_alteration () const
416 Pitch::transposed (Pitch d) const