2 musical-pitch.cc -- implement Pitch
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2002 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 ();
144 pt.octave_ = to.octave_ - from.octave_;
145 pt.notename_ = to.notename_ - from.notename_;
146 pt.alteration_ = to.alteration_ - from.alteration_;
147 pt.alteration_ += sound - pt.semitone_pitch();
154 Merge with *pitch->text* funcs in chord-name.scm
156 char const *accname[] = {"eses", "es", "", "is" , "isis"};
159 Pitch::string () const
161 int n = (notename_ + 2) % 7;
162 String s = to_string (char (n + 'a'));
164 s += String (accname[alteration_ + 2]);
172 else if (octave_ < 0)
174 int o = (-octave_) - 1;
176 s += to_string (',');
183 change me to relative, counting from last pitch p
184 return copy of resulting pitch
187 Pitch::to_relative_octave (Pitch p)
189 int oct_mod = octave_ + 1; // account for c' = octave 1 iso. 0 4
191 Pitch down_pitch (p);
193 up_pitch.alteration_ = alteration_;
194 down_pitch.alteration_ = alteration_;
197 up_pitch.up_to (notename_);
198 down_pitch.down_to (notename_);
201 if (abs (up_pitch.steps () - h) < abs (down_pitch.steps () - h))
206 n.octave_ += oct_mod;
213 Pitch::up_to (int notename)
215 if (notename_ > notename)
219 notename_ = notename;
223 Pitch::down_to (int notename)
225 if (notename_ < notename)
229 notename_ = notename;
232 LY_DEFINE(ly_pitch_transpose,
233 "ly:transpose-pitch", 2, 0, 0,
235 "Transpose @var{p} by the amount @var{delta}, where @var{delta} is the "
236 " pitch that central C is transposed to.")
238 Pitch* t = unsmob_pitch (p);
239 Pitch *d = unsmob_pitch (delta);
240 SCM_ASSERT_TYPE(t, p, SCM_ARG1, __FUNCTION__, "pitch") ;
241 SCM_ASSERT_TYPE(d, delta, SCM_ARG1, __FUNCTION__, "pitch") ;
245 return tp.smobbed_copy ();
248 /****************************************************************/
251 IMPLEMENT_TYPE_P (Pitch, "ly:pitch?");
254 Pitch::mark_smob (SCM)
259 IMPLEMENT_SIMPLE_SMOBS (Pitch);
261 Pitch::print_smob (SCM s, SCM port, scm_print_state *)
263 Pitch *r = (Pitch *) ly_cdr (s);
265 scm_puts ("#<Pitch ", port);
266 scm_display (scm_makfrom0str (r->string ().to_str0 ()), port);
267 scm_puts (" >", port);
273 Pitch::equal_p (SCM a , SCM b)
275 Pitch *p = (Pitch *) ly_cdr (a);
276 Pitch *q = (Pitch *) ly_cdr (b);
278 bool eq = p->notename_ == q->notename_
279 && p->octave_ == q->octave_
280 && p->alteration_ == q->alteration_;
282 return eq ? SCM_BOOL_T : SCM_BOOL_F;
285 MAKE_SCHEME_CALLBACK (Pitch, less_p, 2);
287 Pitch::less_p (SCM p1, SCM p2)
289 Pitch *a = unsmob_pitch (p1);
290 Pitch *b = unsmob_pitch (p2);
292 if (compare (*a, *b) < 0)
299 should add optional args
302 LY_DEFINE(make_pitch, "ly:make-pitch", 3, 0, 0,
303 (SCM o, SCM n, SCM a),
304 "@var{octave} is specified by an integer, zero for the octave containing "
305 "middle C. @var{note} is a number from 0 to 6, with 0 corresponding to C "
306 "and 6 corresponding to B. The shift is zero for a natural, negative for "
307 "flats, or positive for sharps. ")
309 SCM_ASSERT_TYPE(gh_number_p (o), o, SCM_ARG1, __FUNCTION__, "number");
310 SCM_ASSERT_TYPE(gh_number_p (n), n, SCM_ARG2, __FUNCTION__, "number");
311 SCM_ASSERT_TYPE(gh_number_p (a), a, SCM_ARG3, __FUNCTION__, "number");
313 Pitch p (gh_scm2int (o), gh_scm2int (n), gh_scm2int (a));
314 return p.smobbed_copy ();
318 LY_DEFINE(pitch_octave, "ly:pitch-octave", 1, 0, 0,
320 "extract the octave from pitch @var{p}.")
322 Pitch *p = unsmob_pitch (pp);
323 SCM_ASSERT_TYPE(p, pp, SCM_ARG1, __FUNCTION__, "Pitch");
324 int q = p->get_octave ();
326 return gh_int2scm (q);
329 LY_DEFINE(pitch_alteration, "ly:pitch-alteration", 1, 0, 0,
331 "extract the alteration from pitch @var{p}.")
333 Pitch *p = unsmob_pitch (pp);
334 SCM_ASSERT_TYPE(p, pp, SCM_ARG1, __FUNCTION__, "Pitch");
335 int q = p->get_alteration ();
337 return gh_int2scm (q);
340 LY_DEFINE(pitch_notename, "ly:pitch-notename", 1, 0, 0,
342 "extract the note name from pitch @var{pp}.")
344 Pitch *p = unsmob_pitch (pp);
345 SCM_ASSERT_TYPE(p, pp, SCM_ARG1, __FUNCTION__, "Pitch");
346 int q = p->get_notename ();
348 return gh_int2scm (q);
351 LY_DEFINE(pitch_semitones, "ly:pitch-semitones", 1, 0, 0,
353 "calculate the number of semitones of @var{p} from central C.")
355 Pitch *p = unsmob_pitch (pp);
356 SCM_ASSERT_TYPE(p, pp, SCM_ARG1, __FUNCTION__, "Pitch");
358 int q = p->semitone_pitch ();
362 //int q = p->steps ();
364 // As the function is called "pitch_semitones", I assume it was a mistake !
367 return gh_int2scm (q);
370 LY_DEFINE(pitch_less, "ly:pitch<?", 2,0,0, (SCM p1, SCM p2),
371 "Is @var{p1} lower than @var{p2}? This uses lexicographic ordening.")
373 return Pitch::less_p (ly_car (p1), ly_car (p2));
377 Pitch::smobbed_copy ()const
379 Pitch * p = new Pitch (*this);
380 return p->smobbed_self ();
384 Pitch::get_octave ()const
390 Pitch::get_notename () const
396 Pitch::get_alteration () const