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