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