2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1998--2015 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"
31 Pitch::Pitch (int o, int n, Rational a)
36 scale_ = default_global_scale;
40 /* FIXME: why is octave == 0 and default not middleC ? */
44 scale_ = default_global_scale;
46 alteration_ = (Rational)0;
50 Pitch::compare (Pitch const &m1, Pitch const &m2)
52 int o = m1.octave_ - m2.octave_;
53 int n = m1.notename_ - m2.notename_;
54 Rational a = m1.alteration_ - m2.alteration_;
61 return a > (Rational)0 ? 1 : -1;
69 return notename_ + octave_ * scale_->step_count ();
73 Pitch::tone_pitch () const
75 return scale_->tones_at_step (notename_, octave_) + alteration_;
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 (floor (double (tone_pitch () * Rational (2) + Rational (1, 2))));
87 Pitch::rounded_quartertone_pitch () const
89 return int (floor (double (tone_pitch () * Rational (4) + Rational (1, 2))));
93 Pitch::normalize_octave ()
95 int normalized_step = notename_ % scale_->step_count ();
96 if (normalized_step < 0)
97 normalized_step += scale_->step_count ();
99 octave_ += (notename_ - normalized_step) / scale_->step_count ();
100 notename_ = normalized_step;
104 Pitch::normalize_alteration ()
106 while (alteration_ > Rational (1))
108 alteration_ -= scale_->step_size (notename_);
111 while (alteration_ < Rational (-1))
114 alteration_ += scale_->step_size (notename_);
121 normalize_alteration ();
126 Pitch::transpose (Pitch delta)
128 Rational new_alter = tone_pitch () + delta.tone_pitch ();
130 octave_ += delta.octave_;
131 notename_ += delta.notename_;
132 alteration_ += new_alter - tone_pitch ();
138 pitch_interval (Pitch const &from, Pitch const &to)
140 Rational sound = to.tone_pitch () - from.tone_pitch ();
141 Pitch pt (to.get_octave () - from.get_octave (),
142 to.get_notename () - from.get_notename (),
144 to.get_alteration () - from.get_alteration ());
146 return pt.transposed (Pitch (0, 0, sound - pt.tone_pitch ()));
150 Merge with *pitch->text* funcs in chord-name.scm */
151 char const *accname[] = {"eses", "eseh", "es", "eh", "",
152 "ih", "is", "isih", "isis"
156 Pitch::to_string () const
158 int n = (notename_ + 2) % scale_->step_count ();
159 string s = ::to_string (char (n + 'a'));
160 Rational qtones = alteration_ * Rational (4, 1);
161 int qt = int (rint (Real (qtones)));
163 s += string (accname[qt + 4]);
170 else if (octave_ < 0)
172 int o = (-octave_) - 1;
174 s += ::to_string (',');
180 /* Change me to relative, counting from last pitch p
181 return copy of resulting pitch. */
183 Pitch::to_relative_octave (Pitch p) const
185 /* account for c' = octave 1 iso. 0 4 */
186 int oct_mod = octave_ + 1;
188 Pitch down_pitch (p);
190 up_pitch.alteration_ = alteration_;
191 down_pitch.alteration_ = alteration_;
194 up_pitch.up_to (notename_);
195 down_pitch.down_to (notename_);
198 if (abs (up_pitch.steps () - h) < abs (down_pitch.steps () - h))
203 n.octave_ += oct_mod;
208 Pitch::up_to (int notename)
210 if (notename_ > notename)
212 notename_ = notename;
216 Pitch::down_to (int notename)
218 if (notename_ < notename)
220 notename_ = notename;
223 const char Pitch::type_p_name_[] = "ly:pitch?";
226 Pitch::mark_smob () const
228 return scale_->self_scm ();
232 Pitch::print_smob (SCM port, scm_print_state *) const
234 scm_puts ("#<Pitch ", port);
235 scm_display (ly_string2scm (to_string ()), port);
236 scm_puts (" >", port);
241 Pitch::equal_p (SCM a, SCM b)
243 Pitch *p = (Pitch *) SCM_CELL_WORD_1 (a);
244 Pitch *q = (Pitch *) SCM_CELL_WORD_1 (b);
246 bool eq = p->notename_ == q->notename_
247 && p->octave_ == q->octave_
248 && p->alteration_ == q->alteration_;
250 return eq ? SCM_BOOL_T : SCM_BOOL_F;
253 MAKE_SCHEME_CALLBACK (Pitch, less_p, 2);
255 Pitch::less_p (SCM p1, SCM p2)
257 Pitch *a = unsmob<Pitch> (p1);
258 Pitch *b = unsmob<Pitch> (p2);
260 if (compare (*a, *b) < 0)
267 Pitch::get_octave () const
273 Pitch::get_notename () const
279 Pitch::get_alteration () const
285 Pitch::transposed (Pitch d) const
293 Pitch::normalized () const
300 Rational NATURAL_ALTERATION (0);
301 Rational FLAT_ALTERATION (-1, 2);
302 Rational DOUBLE_FLAT_ALTERATION (-1);
303 Rational SHARP_ALTERATION (1, 2);
306 Pitch::negated () const
308 return pitch_interval (*this, Pitch ());