1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3 
4   Copyright (C) 1998--2021 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 "scale.hh"
23 #include "string-convert.hh"
24 #include "warn.hh"
25 
26 #include <cmath>
27 
28 using std::string;
29 
Pitch(int o,int n,Rational a)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 ? */
Pitch()40 Pitch::Pitch ()
41 {
42   notename_ = 0;
43   scale_ = default_global_scale;
44   octave_ = 0;
45   alteration_ = 0;
46 }
47 
48 int
compare(Pitch const & m1,Pitch const & m2)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 > 0) ? 1 : -1;
61 
62   return 0;
63 }
64 
65 int
steps() const66 Pitch::steps () const
67 {
68   return notename_ + octave_ * scale_->step_count ();
69 }
70 
71 Rational
tone_pitch() const72 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
rounded_semitone_pitch() const80 Pitch::rounded_semitone_pitch () const
81 {
82   return int (floor (double (tone_pitch () * Rational (2) + Rational (1, 2))));
83 }
84 
85 int
rounded_quartertone_pitch() const86 Pitch::rounded_quartertone_pitch () const
87 {
88   return int (floor (double (tone_pitch () * Rational (4) + Rational (1, 2))));
89 }
90 
91 void
normalize_octave()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
normalize_alteration()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
normalize()118 Pitch::normalize ()
119 {
120   normalize_alteration ();
121   normalize_octave ();
122 }
123 
124 void
transpose(Pitch delta)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
pitch_interval(Pitch const & from,Pitch const & to)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 *note-name->string* in chord-name.scm  */
150 char const *accname[] = {"eses", "eseh", "es", "eh", "",
151                          "ih", "is", "isih", "isis"
152                         };
153 
154 string
to_string() const155 Pitch::to_string () const
156 {
157   int n = (notename_ + 2) % scale_->step_count ();
158   string s (1, static_cast<char> (n + 'a'));
159   Rational qtones = alteration_ * Rational (4, 1);
160   size_t qt = size_t (rint (static_cast<Real> (qtones) + 4.0));
161   if (qt < sizeof (accname) / sizeof (accname[0]))
162     {
163       s += string (accname[qt]);
164     }
165   else
166     {
167       s += "??";
168     }
169 
170   if (octave_ >= 0)
171     {
172       int o = octave_ + 1;
173       while (o--)
174         s += "'";
175     }
176   else if (octave_ < 0)
177     {
178       int o = (-octave_) - 1;
179       while (o--)
180         s += ',';
181     }
182 
183   return s;
184 }
185 
186 /* Change me to relative, counting from last pitch p
187    return copy of resulting pitch.  */
188 Pitch
to_relative_octave(Pitch p) const189 Pitch::to_relative_octave (Pitch p) const
190 {
191   /* account for c' = octave 1 iso. 0 4 */
192   int oct_mod = octave_ + 1;
193   Pitch up_pitch (p);
194   Pitch down_pitch (p);
195 
196   up_pitch.alteration_ = alteration_;
197   down_pitch.alteration_ = alteration_;
198 
199   Pitch n = *this;
200   up_pitch.up_to (notename_);
201   down_pitch.down_to (notename_);
202 
203   int h = p.steps ();
204   if (abs (up_pitch.steps () - h) < abs (down_pitch.steps () - h))
205     n = up_pitch;
206   else
207     n = down_pitch;
208 
209   n.octave_ += oct_mod;
210   return n;
211 }
212 
213 void
up_to(int notename)214 Pitch::up_to (int notename)
215 {
216   if (notename_ > notename)
217     octave_++;
218   notename_ = notename;
219 }
220 
221 void
down_to(int notename)222 Pitch::down_to (int notename)
223 {
224   if (notename_ < notename)
225     octave_--;
226   notename_ = notename;
227 }
228 
229 const char *const Pitch::type_p_name_ = "ly:pitch?";
230 
231 SCM
mark_smob() const232 Pitch::mark_smob () const
233 {
234   return scale_->self_scm ();
235 }
236 
237 int
print_smob(SCM port,scm_print_state *) const238 Pitch::print_smob (SCM port, scm_print_state *) const
239 {
240   scm_puts ("#<Pitch ", port);
241   scm_display (ly_string2scm (to_string ()), port);
242   scm_puts (" >", port);
243   return 1;
244 }
245 
246 SCM
equal_p(SCM a,SCM b)247 Pitch::equal_p (SCM a, SCM b)
248 {
249   Pitch *p = unsmob<Pitch> (a);
250   Pitch *q = unsmob<Pitch> (b);
251 
252   bool eq = p->notename_ == q->notename_
253             && p->octave_ == q->octave_
254             && p->alteration_ == q->alteration_;
255 
256   return eq ? SCM_BOOL_T : SCM_BOOL_F;
257 }
258 
259 MAKE_SCHEME_CALLBACK (Pitch, less_p, 2);
260 SCM
less_p(SCM p1,SCM p2)261 Pitch::less_p (SCM p1, SCM p2)
262 {
263   Pitch *a = unsmob<Pitch> (p1);
264   Pitch *b = unsmob<Pitch> (p2);
265 
266   if (compare (*a, *b) < 0)
267     return SCM_BOOL_T;
268   else
269     return SCM_BOOL_F;
270 }
271 
272 int
get_octave() const273 Pitch::get_octave () const
274 {
275   return octave_;
276 }
277 
278 int
get_notename() const279 Pitch::get_notename () const
280 {
281   return notename_;
282 }
283 
284 Rational
get_alteration() const285 Pitch::get_alteration () const
286 {
287   return alteration_;
288 }
289 
290 Pitch
transposed(Pitch d) const291 Pitch::transposed (Pitch d) const
292 {
293   Pitch p = *this;
294   p.transpose (d);
295   return p;
296 }
297 
298 Pitch
normalized() const299 Pitch::normalized () const
300 {
301   Pitch p = *this;
302   p.normalize ();
303   return p;
304 }
305 
306 Rational NATURAL_ALTERATION (0);
307 Rational FLAT_ALTERATION (-1, 2);
308 Rational DOUBLE_FLAT_ALTERATION (-1);
309 Rational SHARP_ALTERATION (1, 2);
310 
311 Pitch
negated() const312 Pitch::negated () const
313 {
314   return pitch_interval (*this, Pitch ());
315 }
316 
317 /* TODO: find a good place for this function */
318 void
set_middle_C(Context * c)319 set_middle_C (Context *c)
320 {
321   int clef_pos = from_scm (get_property (c, "middleCClefPosition"), 0);
322   int offset = from_scm (get_property (c, "middleCOffset"), 0);
323   /* middleCCuePosition overrides the clef! */
324   SCM cue_pos = get_property (c, "middleCCuePosition");
325   if (scm_is_number (cue_pos))
326     clef_pos = from_scm (cue_pos, 0);
327 
328   set_property (c, ly_symbol2scm ("middleCPosition"), to_scm (clef_pos + offset));
329 }
330