2 musical-pitch.cc -- implement Pitch
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
12 #include "string-convert.hh"
15 #include "ly-smobs.icc"
17 Pitch::Pitch (int o, int n, int a)
25 /* FIXME: why is octave == 0 and default not middleC ? */
34 Pitch::compare (Pitch const &m1, Pitch const &m2)
36 int o = m1.octave_ - m2.octave_;
37 int n = m1.notename_ - m2.notename_;
38 int a = m1.alteration_ - m2.alteration_;
52 return notename_ + octave_ * 7;
55 /* Should be settable from input? */
56 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 ()));
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 IMPLEMENT_TYPE_P (Pitch, "ly:pitch?");
239 Pitch::mark_smob (SCM)
244 IMPLEMENT_SIMPLE_SMOBS (Pitch);
246 Pitch::print_smob (SCM s, SCM port, scm_print_state *)
248 Pitch *r = (Pitch *) SCM_CELL_WORD_1 (s);
249 scm_puts ("#<Pitch ", port);
250 scm_display (scm_makfrom0str (r->to_string ().c_str ()), port);
251 scm_puts (" >", port);
256 Pitch::equal_p (SCM a, SCM b)
258 Pitch *p = (Pitch *) SCM_CELL_WORD_1 (a);
259 Pitch *q = (Pitch *) SCM_CELL_WORD_1 (b);
261 bool eq = p->notename_ == q->notename_
262 && p->octave_ == q->octave_
263 && p->alteration_ == q->alteration_;
265 return eq ? SCM_BOOL_T : SCM_BOOL_F;
268 MAKE_SCHEME_CALLBACK (Pitch, less_p, 2);
270 Pitch::less_p (SCM p1, SCM p2)
272 Pitch *a = unsmob_pitch (p1);
273 Pitch *b = unsmob_pitch (p2);
275 if (compare (*a, *b) < 0)
282 Pitch::get_octave () const
288 Pitch::get_notename () const
294 Pitch::get_alteration () const
300 Pitch::transposed (Pitch d) const