]> git.donarmstrong.com Git - lilypond.git/blob - lily/pitch.cc
Web-ja: update introduction
[lilypond.git] / lily / pitch.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1998--2015 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 <cmath>
28
29 Pitch::Pitch (int o, int n, Rational a)
30 {
31   notename_ = n;
32   alteration_ = a;
33   octave_ = o;
34   scale_ = default_global_scale;
35   normalize_octave ();
36 }
37
38 /* FIXME: why is octave == 0 and default not middleC ? */
39 Pitch::Pitch ()
40 {
41   notename_ = 0;
42   scale_ = default_global_scale;
43   octave_ = 0;
44   alteration_ = (Rational)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 ? 1 : -1;
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 (floor (double (tone_pitch () * Rational (2) + Rational (1, 2))));
82 }
83
84 int
85 Pitch::rounded_quartertone_pitch () const
86 {
87   return int (floor (double (tone_pitch () * Rational (4) + Rational (1, 2))));
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
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 const char * const Pitch::type_p_name_ = "ly:pitch?";
222
223 SCM
224 Pitch::mark_smob () const
225 {
226   return scale_->self_scm ();
227 }
228
229 int
230 Pitch::print_smob (SCM port, scm_print_state *) const
231 {
232   scm_puts ("#<Pitch ", port);
233   scm_display (ly_string2scm (to_string ()), port);
234   scm_puts (" >", port);
235   return 1;
236 }
237
238 SCM
239 Pitch::equal_p (SCM a, SCM b)
240 {
241   Pitch *p = unsmob<Pitch> (a);
242   Pitch *q = unsmob<Pitch> (b);
243
244   bool eq = p->notename_ == q->notename_
245             && p->octave_ == q->octave_
246             && p->alteration_ == q->alteration_;
247
248   return eq ? SCM_BOOL_T : SCM_BOOL_F;
249 }
250
251 MAKE_SCHEME_CALLBACK (Pitch, less_p, 2);
252 SCM
253 Pitch::less_p (SCM p1, SCM p2)
254 {
255   Pitch *a = unsmob<Pitch> (p1);
256   Pitch *b = unsmob<Pitch> (p2);
257
258   if (compare (*a, *b) < 0)
259     return SCM_BOOL_T;
260   else
261     return SCM_BOOL_F;
262 }
263
264 int
265 Pitch::get_octave () const
266 {
267   return octave_;
268 }
269
270 int
271 Pitch::get_notename () const
272 {
273   return notename_;
274 }
275
276 Rational
277 Pitch::get_alteration () const
278 {
279   return alteration_;
280 }
281
282 Pitch
283 Pitch::transposed (Pitch d) const
284 {
285   Pitch p = *this;
286   p.transpose (d);
287   return p;
288 }
289
290 Pitch
291 Pitch::normalized () const
292 {
293   Pitch p = *this;
294   p.normalize ();
295   return p;
296 }
297
298 Rational NATURAL_ALTERATION (0);
299 Rational FLAT_ALTERATION (-1, 2);
300 Rational DOUBLE_FLAT_ALTERATION (-1);
301 Rational SHARP_ALTERATION (1, 2);
302
303 Pitch
304 Pitch::negated () const
305 {
306   return pitch_interval (*this, Pitch ());
307 }