]> git.donarmstrong.com Git - lilypond.git/blob - lily/pitch.cc
Run grand-replace (issue 3765)
[lilypond.git] / lily / pitch.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1998--2014 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
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.
10
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.
15
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/>.
18 */
19
20 #include "pitch.hh"
21
22 #include "main.hh"
23 #include "scale.hh"
24 #include "string-convert.hh"
25 #include "warn.hh"
26
27 #include "ly-smobs.icc"
28 #include <cmath>
29
30 Pitch::Pitch (int o, int n, Rational a)
31 {
32   notename_ = n;
33   alteration_ = a;
34   octave_ = o;
35   scale_ = default_global_scale;
36   normalize_octave ();
37 }
38
39 /* FIXME: why is octave == 0 and default not middleC ? */
40 Pitch::Pitch ()
41 {
42   notename_ = 0;
43   scale_ = default_global_scale;
44   octave_ = 0;
45   alteration_ = (Rational)0;
46 }
47
48 int
49 Pitch::compare (Pitch const &m1, Pitch const &m2)
50 {
51   int o = m1.octave_ - m2.octave_;
52   int n = m1.notename_ - m2.notename_;
53   Rational a = m1.alteration_ - m2.alteration_;
54
55   if (o)
56     return o;
57   if (n)
58     return n;
59   if (a)
60     return a > (Rational)0 ? 1 : -1;
61
62   return 0;
63 }
64
65 int
66 Pitch::steps () const
67 {
68   return notename_ + octave_ * scale_->step_count ();
69 }
70
71 Rational
72 Pitch::tone_pitch () const
73 {
74   return scale_->tones_at_step (notename_, octave_) + alteration_;
75 }
76
77 /* Calculate pitch height in 12th octave steps.  Don't assume
78    normalized pitch as this function is used to normalize the pitch.  */
79 int
80 Pitch::rounded_semitone_pitch () const
81 {
82   return int (floor (double (tone_pitch () * Rational (2) + Rational (1, 2))));
83 }
84
85 int
86 Pitch::rounded_quartertone_pitch () const
87 {
88   return int (floor (double (tone_pitch () * Rational (4) + Rational (1, 2))));
89 }
90
91 void
92 Pitch::normalize_octave ()
93 {
94   int normalized_step = notename_ % scale_->step_count ();
95   if (normalized_step < 0)
96     normalized_step += scale_->step_count ();
97
98   octave_ += (notename_ - normalized_step) / scale_->step_count ();
99   notename_ = normalized_step;
100 }
101
102 void
103 Pitch::normalize_alteration ()
104 {
105   while (alteration_ > Rational (1))
106     {
107       alteration_ -= scale_->step_size (notename_);
108       notename_++;
109     }
110   while (alteration_ < Rational (-1))
111     {
112       notename_--;
113       alteration_ += scale_->step_size (notename_);
114     }
115 }
116
117 void
118 Pitch::normalize ()
119 {
120   normalize_alteration ();
121   normalize_octave ();
122 }
123
124 void
125 Pitch::transpose (Pitch delta)
126 {
127   Rational new_alter = tone_pitch () + delta.tone_pitch ();
128
129   octave_ += delta.octave_;
130   notename_ += delta.notename_;
131   alteration_ += new_alter - tone_pitch ();
132
133   normalize_octave ();
134 }
135
136 Pitch
137 pitch_interval (Pitch const &from, Pitch const &to)
138 {
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 (),
142
143             to.get_alteration () - from.get_alteration ());
144
145   return pt.transposed (Pitch (0, 0, sound - pt.tone_pitch ()));
146 }
147
148 /* FIXME
149    Merge with *pitch->text* funcs in chord-name.scm  */
150 char const *accname[] = {"eses", "eseh", "es", "eh", "",
151                          "ih", "is", "isih", "isis"
152                         };
153
154 string
155 Pitch::to_string () const
156 {
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)));
161
162   s += string (accname[qt + 4]);
163   if (octave_ >= 0)
164     {
165       int o = octave_ + 1;
166       while (o--)
167         s += "'";
168     }
169   else if (octave_ < 0)
170     {
171       int o = (-octave_) - 1;
172       while (o--)
173         s += ::to_string (',');
174     }
175
176   return s;
177 }
178
179 /* Change me to relative, counting from last pitch p
180    return copy of resulting pitch.  */
181 Pitch
182 Pitch::to_relative_octave (Pitch p) const
183 {
184   /* account for c' = octave 1 iso. 0 4 */
185   int oct_mod = octave_ + 1;
186   Pitch up_pitch (p);
187   Pitch down_pitch (p);
188
189   up_pitch.alteration_ = alteration_;
190   down_pitch.alteration_ = alteration_;
191
192   Pitch n = *this;
193   up_pitch.up_to (notename_);
194   down_pitch.down_to (notename_);
195
196   int h = p.steps ();
197   if (abs (up_pitch.steps () - h) < abs (down_pitch.steps () - h))
198     n = up_pitch;
199   else
200     n = down_pitch;
201
202   n.octave_ += oct_mod;
203   return n;
204 }
205
206 void
207 Pitch::up_to (int notename)
208 {
209   if (notename_ > notename)
210     octave_++;
211   notename_ = notename;
212 }
213
214 void
215 Pitch::down_to (int notename)
216 {
217   if (notename_ < notename)
218     octave_--;
219   notename_ = notename;
220 }
221
222 IMPLEMENT_TYPE_P (Pitch, "ly:pitch?");
223 SCM
224 Pitch::mark_smob (SCM x)
225 {
226   Pitch *p = (Pitch *) SCM_CELL_WORD_1 (x);
227   return p->scale_->self_scm ();
228 }
229
230 IMPLEMENT_SIMPLE_SMOBS (Pitch);
231 int
232 Pitch::print_smob (SCM s, SCM port, scm_print_state *)
233 {
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);
238   return 1;
239 }
240
241 SCM
242 Pitch::equal_p (SCM a, SCM b)
243 {
244   Pitch *p = (Pitch *) SCM_CELL_WORD_1 (a);
245   Pitch *q = (Pitch *) SCM_CELL_WORD_1 (b);
246
247   bool eq = p->notename_ == q->notename_
248             && p->octave_ == q->octave_
249             && p->alteration_ == q->alteration_;
250
251   return eq ? SCM_BOOL_T : SCM_BOOL_F;
252 }
253
254 MAKE_SCHEME_CALLBACK (Pitch, less_p, 2);
255 SCM
256 Pitch::less_p (SCM p1, SCM p2)
257 {
258   Pitch *a = unsmob_pitch (p1);
259   Pitch *b = unsmob_pitch (p2);
260
261   if (compare (*a, *b) < 0)
262     return SCM_BOOL_T;
263   else
264     return SCM_BOOL_F;
265 }
266
267 int
268 Pitch::get_octave () const
269 {
270   return octave_;
271 }
272
273 int
274 Pitch::get_notename () const
275 {
276   return notename_;
277 }
278
279 Rational
280 Pitch::get_alteration () const
281 {
282   return alteration_;
283 }
284
285 Pitch
286 Pitch::transposed (Pitch d) const
287 {
288   Pitch p = *this;
289   p.transpose (d);
290   return p;
291 }
292
293 Pitch
294 Pitch::normalized () const
295 {
296   Pitch p = *this;
297   p.normalize ();
298   return p;
299 }
300
301 Rational NATURAL_ALTERATION (0);
302 Rational FLAT_ALTERATION (-1, 2);
303 Rational DOUBLE_FLAT_ALTERATION (-1);
304 Rational SHARP_ALTERATION (1, 2);
305
306 Pitch
307 Pitch::negated () const
308 {
309   return pitch_interval (*this, Pitch ());
310 }