2 musical-pitch.cc -- implement Pitch
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
13 #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 pitch_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 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 ().to_str0 ()), 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;
269 MAKE_SCHEME_CALLBACK (Pitch, less_p, 2);
271 Pitch::less_p (SCM p1, SCM p2)
273 Pitch *a = unsmob_pitch (p1);
274 Pitch *b = unsmob_pitch (p2);
276 if (compare (*a, *b) < 0)
284 Pitch::get_octave () const
290 Pitch::get_notename () const
296 Pitch::get_alteration () const
302 Pitch::transposed (Pitch d) const