2 musical-pitch.cc -- implement Pitch
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
13 #include "string-convert.hh"
16 #include "ly-smobs.icc"
19 Pitch::Pitch (int o, int n, Rational a)
24 scale_ = default_global_scale;
28 /* FIXME: why is octave == 0 and default not middleC ? */
32 scale_ = default_global_scale;
37 Pitch::compare (Pitch const &m1, Pitch const &m2)
39 int o = m1.octave_ - m2.octave_;
40 int n = m1.notename_ - m2.notename_;
41 Rational a = m1.alteration_ - m2.alteration_;
56 return notename_ + octave_ * scale_->step_tones_.size ();
60 Pitch::tone_pitch () const
66 n += scale_->step_tones_.size ();
70 Rational tones ((o + n / scale_->step_tones_.size ()) * 6, 1);
71 tones += scale_->step_tones_[n % scale_->step_tones_.size ()];
78 /* Calculate pitch height in 12th octave steps. Don't assume
79 normalized pitch as this function is used to normalize the pitch. */
81 Pitch::rounded_semitone_pitch () const
83 return int (double (tone_pitch () * Rational (2)));
87 Pitch::rounded_quartertone_pitch () const
89 return int (double (tone_pitch () * Rational (4)));
95 Rational pitch = tone_pitch ();
96 while (notename_ >= (int) scale_->step_tones_.size ())
98 notename_ -= scale_->step_tones_.size ();
100 alteration_ -= tone_pitch () - pitch;
102 while (notename_ < 0)
104 notename_ += scale_->step_tones_.size ();
106 alteration_ -= tone_pitch () - pitch;
109 while (alteration_ > Rational (1))
111 if (notename_ == int (scale_->step_tones_.size ()))
119 alteration_ = Rational (0);
120 alteration_ -= tone_pitch () - pitch;
122 while (alteration_ < Rational (-1))
126 notename_ = scale_->step_tones_.size ();
133 alteration_ -= tone_pitch () - pitch;
138 Pitch::transpose (Pitch delta)
140 Rational new_alter = tone_pitch () + delta.tone_pitch ();
142 octave_ += delta.octave_;
143 notename_ += delta.notename_;
144 alteration_ += new_alter - tone_pitch ();
150 pitch_interval (Pitch const &from, Pitch const &to)
152 Rational sound = to.tone_pitch () - from.tone_pitch ();
153 Pitch pt (to.get_octave () - from.get_octave (),
154 to.get_notename () - from.get_notename (),
156 to.get_alteration () - from.get_alteration ());
158 return pt.transposed (Pitch (0, 0, sound - pt.tone_pitch ()));
162 Merge with *pitch->text* funcs in chord-name.scm */
163 char const *accname[] = {"eses", "eseh", "es", "eh", "",
164 "ih", "is", "isih", "isis"};
167 Pitch::to_string () const
169 int n = (notename_ + 2) % scale_->step_tones_.size ();
170 string s = ::to_string (char (n + 'a'));
171 Rational qtones = alteration_ * Rational (4,1);
172 int qt = int (rint (Real (qtones)));
174 s += string (accname[qt + 4]);
181 else if (octave_ < 0)
183 int o = (-octave_) - 1;
185 s += ::to_string (',');
191 /* Change me to relative, counting from last pitch p
192 return copy of resulting pitch. */
194 Pitch::to_relative_octave (Pitch p) const
196 /* account for c' = octave 1 iso. 0 4 */
197 int oct_mod = octave_ + 1;
199 Pitch down_pitch (p);
201 up_pitch.alteration_ = alteration_;
202 down_pitch.alteration_ = alteration_;
205 up_pitch.up_to (notename_);
206 down_pitch.down_to (notename_);
209 if (abs (up_pitch.steps () - h) < abs (down_pitch.steps () - h))
214 n.octave_ += oct_mod;
219 Pitch::up_to (int notename)
221 if (notename_ > notename)
223 notename_ = notename;
227 Pitch::down_to (int notename)
229 if (notename_ < notename)
231 notename_ = notename;
234 IMPLEMENT_TYPE_P (Pitch, "ly:pitch?");
236 Pitch::mark_smob (SCM x)
238 Pitch *p = (Pitch*) SCM_CELL_WORD_1 (x);
239 return p->scale_->self_scm ();
242 IMPLEMENT_SIMPLE_SMOBS (Pitch);
244 Pitch::print_smob (SCM s, SCM port, scm_print_state *)
246 Pitch *r = (Pitch *) SCM_CELL_WORD_1 (s);
247 scm_puts ("#<Pitch ", port);
248 scm_display (ly_string2scm (r->to_string ()), port);
249 scm_puts (" >", port);
254 Pitch::equal_p (SCM a, SCM b)
256 Pitch *p = (Pitch *) SCM_CELL_WORD_1 (a);
257 Pitch *q = (Pitch *) SCM_CELL_WORD_1 (b);
259 bool eq = p->notename_ == q->notename_
260 && p->octave_ == q->octave_
261 && p->alteration_ == q->alteration_;
263 return eq ? SCM_BOOL_T : SCM_BOOL_F;
266 MAKE_SCHEME_CALLBACK (Pitch, less_p, 2);
268 Pitch::less_p (SCM p1, SCM p2)
270 Pitch *a = unsmob_pitch (p1);
271 Pitch *b = unsmob_pitch (p2);
273 if (compare (*a, *b) < 0)
280 Pitch::get_octave () const
286 Pitch::get_notename () const
292 Pitch::get_alteration () const
298 Pitch::transposed (Pitch d) const
305 Rational NATURAL_ALTERATION (0);
306 Rational FLAT_ALTERATION (-1, 2);
307 Rational DOUBLE_FLAT_ALTERATION (-1);
308 Rational SHARP_ALTERATION (1, 2);
311 Pitch::negated () const
313 return pitch_interval (*this, Pitch ());