2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1998--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
24 #include "string-convert.hh"
27 #include "ly-smobs.icc"
30 Pitch::Pitch (int o, int n, Rational a)
35 scale_ = default_global_scale;
39 /* FIXME: why is octave == 0 and default not middleC ? */
43 scale_ = default_global_scale;
45 alteration_ = (Rational)0;
49 Pitch::compare (Pitch const &m1, Pitch const &m2)
51 int o = m1.octave_ - m2.octave_;
52 int n = m1.notename_ - m2.notename_;
53 Rational a = m1.alteration_ - m2.alteration_;
60 return a > (Rational)0;
68 return notename_ + octave_ * scale_->step_count ();
72 Pitch::tone_pitch () const
74 return scale_->tones_at_step (notename_, octave_) + alteration_;
77 /* Calculate pitch height in 12th octave steps. Don't assume
78 normalized pitch as this function is used to normalize the pitch. */
80 Pitch::rounded_semitone_pitch () const
82 return int (floor (double (tone_pitch () * Rational (2) + Rational (1, 2))));
86 Pitch::rounded_quartertone_pitch () const
88 return int (floor (double (tone_pitch () * Rational (4) + Rational (1, 2))));
92 Pitch::normalize_octave ()
94 int normalized_step = notename_ % scale_->step_count ();
95 if (normalized_step < 0)
96 normalized_step += scale_->step_count ();
98 octave_ += (notename_ - normalized_step) / scale_->step_count ();
99 notename_ = normalized_step;
103 Pitch::normalize_alteration ()
105 while (alteration_ > Rational (1))
107 alteration_ -= scale_->step_size (notename_);
110 while (alteration_ < Rational (-1))
113 alteration_ += scale_->step_size (notename_);
120 normalize_alteration ();
125 Pitch::transpose (Pitch delta)
127 Rational new_alter = tone_pitch () + delta.tone_pitch ();
129 octave_ += delta.octave_;
130 notename_ += delta.notename_;
131 alteration_ += new_alter - tone_pitch ();
137 pitch_interval (Pitch const &from, Pitch const &to)
139 Rational sound = to.tone_pitch () - from.tone_pitch ();
140 Pitch pt (to.get_octave () - from.get_octave (),
141 to.get_notename () - from.get_notename (),
143 to.get_alteration () - from.get_alteration ());
145 return pt.transposed (Pitch (0, 0, sound - pt.tone_pitch ()));
149 Merge with *pitch->text* funcs in chord-name.scm */
150 char const *accname[] = {"eses", "eseh", "es", "eh", "",
151 "ih", "is", "isih", "isis"
155 Pitch::to_string () const
157 int n = (notename_ + 2) % scale_->step_count ();
158 string s = ::to_string (char (n + 'a'));
159 Rational qtones = alteration_ * Rational (4, 1);
160 int qt = int (rint (Real (qtones)));
162 s += string (accname[qt + 4]);
169 else if (octave_ < 0)
171 int o = (-octave_) - 1;
173 s += ::to_string (',');
179 /* Change me to relative, counting from last pitch p
180 return copy of resulting pitch. */
182 Pitch::to_relative_octave (Pitch p) const
184 /* account for c' = octave 1 iso. 0 4 */
185 int oct_mod = octave_ + 1;
187 Pitch down_pitch (p);
189 up_pitch.alteration_ = alteration_;
190 down_pitch.alteration_ = alteration_;
193 up_pitch.up_to (notename_);
194 down_pitch.down_to (notename_);
197 if (abs (up_pitch.steps () - h) < abs (down_pitch.steps () - h))
202 n.octave_ += oct_mod;
207 Pitch::up_to (int notename)
209 if (notename_ > notename)
211 notename_ = notename;
215 Pitch::down_to (int notename)
217 if (notename_ < notename)
219 notename_ = notename;
222 IMPLEMENT_TYPE_P (Pitch, "ly:pitch?");
224 Pitch::mark_smob (SCM x)
226 Pitch *p = (Pitch *) SCM_CELL_WORD_1 (x);
227 return p->scale_->self_scm ();
230 IMPLEMENT_SIMPLE_SMOBS (Pitch);
232 Pitch::print_smob (SCM s, SCM port, scm_print_state *)
234 Pitch *r = (Pitch *) SCM_CELL_WORD_1 (s);
235 scm_puts ("#<Pitch ", port);
236 scm_display (ly_string2scm (r->to_string ()), port);
237 scm_puts (" >", port);
242 Pitch::equal_p (SCM a, SCM b)
244 Pitch *p = (Pitch *) SCM_CELL_WORD_1 (a);
245 Pitch *q = (Pitch *) SCM_CELL_WORD_1 (b);
247 bool eq = p->notename_ == q->notename_
248 && p->octave_ == q->octave_
249 && p->alteration_ == q->alteration_;
251 return eq ? SCM_BOOL_T : SCM_BOOL_F;
254 MAKE_SCHEME_CALLBACK (Pitch, less_p, 2);
256 Pitch::less_p (SCM p1, SCM p2)
258 Pitch *a = unsmob_pitch (p1);
259 Pitch *b = unsmob_pitch (p2);
261 if (compare (*a, *b) < 0)
268 Pitch::get_octave () const
274 Pitch::get_notename () const
280 Pitch::get_alteration () const
286 Pitch::transposed (Pitch d) const
294 Pitch::normalized () const
301 Rational NATURAL_ALTERATION (0);
302 Rational FLAT_ALTERATION (-1, 2);
303 Rational DOUBLE_FLAT_ALTERATION (-1);
304 Rational SHARP_ALTERATION (1, 2);
307 Pitch::negated () const
309 return pitch_interval (*this, Pitch ());