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)
22 scale_ = default_global_scale;
26 /* FIXME: why is octave == 0 and default not middleC ? */
31 scale_ = default_global_scale;
36 Pitch::compare (Pitch const &m1, Pitch const &m2)
38 int o = m1.octave_ - m2.octave_;
39 int n = m1.notename_ - m2.notename_;
40 int a = m1.alteration_ - m2.alteration_;
54 return notename_ + octave_ * scale_->step_semitones_.size ();
57 /* Should be settable from input? */
58 // static Byte diatonic_scale_semitones[ ] = { 0, 2, 4, 5, 7, 9, 11 };
62 /* Calculate pitch height in 12th octave steps. Don't assume
63 normalised pitch as this function is used to normalise the pitch. */
65 Pitch::semitone_pitch () const
71 n += scale_->step_semitones_.size ();
76 programming_error ("semitone_pitch () called on quarter tone alteration.");
78 return ((o + n / scale_->step_semitones_.size ()) * 12
79 + scale_->step_semitones_[n % scale_->step_semitones_.size ()]
84 Pitch::quartertone_pitch () const
90 n += scale_->step_semitones_.size ();
94 return ((o + n / scale_->step_semitones_.size ()) * 24
95 + 2 * scale_->step_semitones_[n % scale_->step_semitones_.size ()]
102 int pitch = quartertone_pitch ();
103 while (notename_ >= (int) scale_->step_semitones_.size ())
105 notename_ -= scale_->step_semitones_.size ();
107 alteration_ -= quartertone_pitch () - pitch;
109 while (notename_ < 0)
111 notename_ += scale_->step_semitones_.size ();
113 alteration_ -= quartertone_pitch () - pitch;
115 while (alteration_ > DOUBLE_SHARP)
126 alteration_ -= quartertone_pitch () - pitch;
129 while (alteration_ < DOUBLE_FLAT)
140 alteration_ -= quartertone_pitch () - pitch;
144 /* WHugh, wat een intervaas */
146 Pitch::transpose (Pitch delta)
148 int new_semi = quartertone_pitch () +delta.quartertone_pitch ();
149 octave_ += delta.octave_;
150 notename_ += delta.notename_;
151 alteration_ += new_semi - quartertone_pitch ();
157 pitch_interval (Pitch const &from, Pitch const &to)
159 int sound = to.quartertone_pitch () - from.quartertone_pitch ();
160 Pitch pt (to.get_octave () - from.get_octave (),
161 to.get_notename () - from.get_notename (),
163 to.get_alteration () - from.get_alteration ());
165 return pt.transposed (Pitch (0, 0, sound - pt.quartertone_pitch ()));
169 Merge with *pitch->text* funcs in chord-name.scm */
170 char const *accname[] = {"eses", "eseh", "es", "eh", "",
171 "ih", "is", "isih", "isis"};
174 Pitch::to_string () const
176 int n = (notename_ + 2) % scale_->step_semitones_.size ();
177 string s = ::to_string (char (n + 'a'));
179 s += string (accname[alteration_ - DOUBLE_FLAT]);
187 else if (octave_ < 0)
189 int o = (-octave_) - 1;
191 s += ::to_string (',');
197 /* Change me to relative, counting from last pitch p
198 return copy of resulting pitch. */
200 Pitch::to_relative_octave (Pitch p) const
202 /* account for c' = octave 1 iso. 0 4 */
203 int oct_mod = octave_ + 1;
205 Pitch down_pitch (p);
207 up_pitch.alteration_ = alteration_;
208 down_pitch.alteration_ = alteration_;
211 up_pitch.up_to (notename_);
212 down_pitch.down_to (notename_);
215 if (abs (up_pitch.steps () - h) < abs (down_pitch.steps () - h))
220 n.octave_ += oct_mod;
225 Pitch::up_to (int notename)
227 if (notename_ > notename)
229 notename_ = notename;
233 Pitch::down_to (int notename)
235 if (notename_ < notename)
237 notename_ = notename;
240 IMPLEMENT_TYPE_P (Pitch, "ly:pitch?");
242 Pitch::mark_smob (SCM x)
244 Pitch *p = (Pitch*) SCM_CELL_WORD_1 (x);
245 return p->scale_->self_scm ();
248 IMPLEMENT_SIMPLE_SMOBS (Pitch);
250 Pitch::print_smob (SCM s, SCM port, scm_print_state *)
252 Pitch *r = (Pitch *) SCM_CELL_WORD_1 (s);
253 scm_puts ("#<Pitch ", port);
254 scm_display (scm_makfrom0str (r->to_string ().c_str ()), port);
255 scm_puts (" >", port);
260 Pitch::equal_p (SCM a, SCM b)
262 Pitch *p = (Pitch *) SCM_CELL_WORD_1 (a);
263 Pitch *q = (Pitch *) SCM_CELL_WORD_1 (b);
265 bool eq = p->notename_ == q->notename_
266 && p->octave_ == q->octave_
267 && p->alteration_ == q->alteration_;
269 return eq ? SCM_BOOL_T : SCM_BOOL_F;
272 MAKE_SCHEME_CALLBACK (Pitch, less_p, 2);
274 Pitch::less_p (SCM p1, SCM p2)
276 Pitch *a = unsmob_pitch (p1);
277 Pitch *b = unsmob_pitch (p2);
279 if (compare (*a, *b) < 0)
286 Pitch::get_octave () const
292 Pitch::get_notename () const
298 Pitch::get_alteration () const
304 Pitch::transposed (Pitch d) const