]> git.donarmstrong.com Git - lilypond.git/blob - lily/pitch.cc
Merge branch 'master' of git://git.savannah.gnu.org/lilypond.git
[lilypond.git] / lily / pitch.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1998--2011 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   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;
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 (double (tone_pitch () * Rational (2)));
83 }
84
85 int
86 Pitch::rounded_quartertone_pitch () const
87 {
88   return int (double (tone_pitch () * Rational (4)));
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 string
154 Pitch::to_string () const
155 {
156   int n = (notename_ + 2) % scale_->step_count ();
157   string s = ::to_string (char (n + 'a'));
158   Rational qtones = alteration_ * Rational (4,1);
159   int qt = int (rint (Real (qtones)));
160
161   s += string (accname[qt + 4]);
162   if (octave_ >= 0)
163     {
164       int o = octave_ + 1;
165       while (o--)
166         s += "'";
167     }
168   else if (octave_ < 0)
169     {
170       int o = (-octave_) - 1;
171       while (o--)
172         s += ::to_string (',');
173     }
174
175   return s;
176 }
177
178 /* Change me to relative, counting from last pitch p
179    return copy of resulting pitch.  */
180 Pitch
181 Pitch::to_relative_octave (Pitch p) const
182 {
183   /* account for c' = octave 1 iso. 0 4 */
184   int oct_mod = octave_ + 1;
185   Pitch up_pitch (p);
186   Pitch down_pitch (p);
187
188   up_pitch.alteration_ = alteration_;
189   down_pitch.alteration_ = alteration_;
190
191   Pitch n = *this;
192   up_pitch.up_to (notename_);
193   down_pitch.down_to (notename_);
194
195   int h = p.steps ();
196   if (abs (up_pitch.steps () - h) < abs (down_pitch.steps () - h))
197     n = up_pitch;
198   else
199     n = down_pitch;
200
201   n.octave_ += oct_mod;
202   return n;
203 }
204
205 void
206 Pitch::up_to (int notename)
207 {
208   if (notename_ > notename)
209     octave_++;
210   notename_ = notename;
211 }
212
213 void
214 Pitch::down_to (int notename)
215 {
216   if (notename_ < notename)
217     octave_--;
218   notename_ = notename;
219 }
220
221 IMPLEMENT_TYPE_P (Pitch, "ly:pitch?");
222 SCM
223 Pitch::mark_smob (SCM x)
224 {
225   Pitch *p = (Pitch*) SCM_CELL_WORD_1 (x);
226   return p->scale_->self_scm ();
227 }
228
229 IMPLEMENT_SIMPLE_SMOBS (Pitch);
230 int
231 Pitch::print_smob (SCM s, SCM port, scm_print_state *)
232 {
233   Pitch *r = (Pitch *) SCM_CELL_WORD_1 (s);
234   scm_puts ("#<Pitch ", port);
235   scm_display (ly_string2scm (r->to_string ()), port);
236   scm_puts (" >", port);
237   return 1;
238 }
239
240 SCM
241 Pitch::equal_p (SCM a, SCM b)
242 {
243   Pitch *p = (Pitch *) SCM_CELL_WORD_1 (a);
244   Pitch *q = (Pitch *) SCM_CELL_WORD_1 (b);
245
246   bool eq = p->notename_ == q->notename_
247     && p->octave_ == q->octave_
248     && p->alteration_ == q->alteration_;
249
250   return eq ? SCM_BOOL_T : SCM_BOOL_F;
251 }
252
253 MAKE_SCHEME_CALLBACK (Pitch, less_p, 2);
254 SCM
255 Pitch::less_p (SCM p1, SCM p2)
256 {
257   Pitch *a = unsmob_pitch (p1);
258   Pitch *b = unsmob_pitch (p2);
259
260   if (compare (*a, *b) < 0)
261     return SCM_BOOL_T;
262   else
263     return SCM_BOOL_F;
264 }
265
266 int
267 Pitch::get_octave () const
268 {
269   return octave_;
270 }
271
272 int
273 Pitch::get_notename () const
274 {
275   return notename_;
276 }
277
278 Rational
279 Pitch::get_alteration () const
280 {
281   return alteration_;
282 }
283
284 Pitch
285 Pitch::transposed (Pitch d) const
286 {
287   Pitch p = *this;
288   p.transpose (d);
289   return p;
290 }
291
292 Pitch
293 Pitch::normalized () const
294 {
295   Pitch p = *this;
296   p.normalize ();
297   return p;
298 }
299
300 Rational NATURAL_ALTERATION (0);
301 Rational FLAT_ALTERATION (-1, 2);
302 Rational DOUBLE_FLAT_ALTERATION (-1);
303 Rational SHARP_ALTERATION (1, 2);
304
305 Pitch
306 Pitch::negated () const
307 {
308   return pitch_interval (*this, Pitch ());
309 }