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