]> git.donarmstrong.com Git - lilypond.git/blob - lily/pitch.cc
* lily/parser.yy (Music_list): add error-found to music with errors.
[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--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "pitch.hh"
10 #include "warn.hh"
11 #include "main.hh"
12 #include "ly-smobs.icc"
13
14
15 Pitch::Pitch (int o, int n, int a)
16 {
17   notename_ = n;
18   alteration_ = a;
19   octave_ = o;
20   normalise ();
21 }
22
23 /* FIXME: why is octave == 0 and default not middleC ? */
24 Pitch::Pitch ()
25 {
26   notename_ = 0;
27   alteration_ = 0;
28   octave_ = 0;
29 }
30
31 int
32 Pitch::compare (Pitch const &m1, Pitch const &m2)
33 {
34   int o=  m1.octave_ - m2.octave_;
35   int n = m1.notename_ - m2.notename_;
36   int a = m1.alteration_ - m2.alteration_;
37
38   if (o)
39     return o;
40   if (n)
41     return n;
42   if (a)
43     return a;
44   return 0;
45 }
46
47 int
48 Pitch::steps () const
49 {
50   return  notename_ + octave_*7;
51 }
52
53 /* Should be settable from input?  */
54 static Byte diatonic_scale_semitones[  ] = { 0, 2, 4, 5, 7, 9, 11 };
55
56
57 /* Calculate pitch height in 12th octave steps.  Don't assume
58    normalised pitch as this function is used to normalise the pitch.  */
59 int
60 Pitch::semitone_pitch () const
61 {
62   int o = octave_;
63   int n = notename_;
64   while (n < 0)
65     {
66       n += 7;
67       o --;
68     }
69
70   if (alteration_ % 2)
71     programming_error ("semitone_pitch () called on quarter tone alteration.");
72
73   return ((o + n / 7) * 12
74           + diatonic_scale_semitones[n % 7]
75           + (alteration_ / 2));
76 }
77
78 int
79 Pitch::quartertone_pitch () const
80 {
81   int o = octave_;
82   int n = notename_;
83   while (n < 0)
84     {
85       n += 7;
86       o --;
87     }
88
89   return ((o + n / 7) * 24
90           + 2 * diatonic_scale_semitones[n % 7]
91           + alteration_);
92 }
93
94 void
95 Pitch::normalise ()
96 {
97   int pitch = quartertone_pitch ();
98   while (notename_ >= 7)
99     {
100       notename_ -= 7;
101       octave_++;
102       alteration_ -= quartertone_pitch () - pitch;
103     }
104   while (notename_ < 0)
105     {
106       notename_ += 7;
107       octave_--;
108       alteration_ -= quartertone_pitch () - pitch;
109     }
110   while (alteration_ > DOUBLE_SHARP)
111     {
112       if (notename_ == 6)
113         {
114           notename_ = 0;
115           octave_++;
116         }
117       else
118         notename_++;
119
120       alteration_ = 0;
121       alteration_ -= quartertone_pitch () - pitch;
122     }
123
124   while (alteration_ < DOUBLE_FLAT)
125     {
126       if (notename_ == 0)
127         {
128           notename_ = 6;
129           octave_--;
130         }
131       else
132         notename_--;
133
134       alteration_ = 0;
135       alteration_ -= quartertone_pitch () - pitch;
136     }
137 }
138
139 /* WHugh, wat een intervaas */
140 void
141 Pitch::transpose (Pitch delta)
142 {
143   int new_semi = quartertone_pitch ()  +delta.quartertone_pitch ();
144   octave_ += delta.octave_;
145   notename_ += delta.notename_;
146   alteration_ += new_semi - quartertone_pitch ();
147
148   normalise ();
149 }
150
151 Pitch
152 interval (Pitch const & from , Pitch const & to )
153 {
154   int sound = to.quartertone_pitch ()  - from.quartertone_pitch ();
155   Pitch pt (to.get_octave () - from.get_octave (),
156             to.get_notename () - from.get_notename (),
157
158             to.get_alteration () - from.get_alteration ());
159
160   return pt.transposed (Pitch (0,0,sound - pt.quartertone_pitch ()));
161 }
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 LY_DEFINE (ly_pitch_transpose, "ly:pitch-transpose",
237            2, 0, 0, (SCM p, SCM delta),
238            "Transpose @var{p} by the amount @var{delta}, "
239            "where @var{delta} is relative to middle C.")
240 {
241   Pitch* t = unsmob_pitch (p);
242   Pitch *d = unsmob_pitch (delta);
243   SCM_ASSERT_TYPE (t, p, SCM_ARG1, __FUNCTION__, "pitch");
244   SCM_ASSERT_TYPE (d, delta, SCM_ARG1, __FUNCTION__, "pitch");
245   return t->transposed (*d).smobbed_copy ();
246 }
247
248 IMPLEMENT_TYPE_P (Pitch, "ly:pitch?");
249
250 SCM
251 Pitch::mark_smob (SCM)
252 {
253   return SCM_EOL;
254 }
255
256 IMPLEMENT_SIMPLE_SMOBS (Pitch);
257 int
258 Pitch::print_smob (SCM s, SCM port, scm_print_state *)
259 {
260   Pitch *r = (Pitch *) scm_cdr (s);
261   scm_puts ("#<Pitch ", port);
262   scm_display (scm_makfrom0str (r->to_string ().to_str0 ()), port);
263   scm_puts (" >", port);
264   return 1;
265 }
266
267 SCM
268 Pitch::equal_p (SCM a , SCM b)
269 {
270   Pitch *p = (Pitch *) scm_cdr (a);
271   Pitch *q = (Pitch *) scm_cdr (b);
272
273   bool eq = p->notename_ == q->notename_
274     && p->octave_ == q->octave_
275     && p->alteration_ == q->alteration_;
276
277   return eq ? SCM_BOOL_T : SCM_BOOL_F;
278 }
279
280 MAKE_SCHEME_CALLBACK (Pitch, less_p, 2);
281 SCM
282 Pitch::less_p (SCM p1, SCM p2)
283 {
284   Pitch *a = unsmob_pitch (p1);
285   Pitch *b = unsmob_pitch (p2);
286
287   if (compare (*a, *b) < 0)
288     return SCM_BOOL_T;
289   else
290     return SCM_BOOL_F;
291 }
292
293 /* Should add optional args.  */
294 LY_DEFINE (ly_make_pitch, "ly:make-pitch",
295            3, 0, 0, (SCM octave, SCM note, SCM alter),
296            "@var{octave} is specified by an integer, "
297            "zero for the octave containing middle C.  "
298            "@var{note} is a number from 0 to 6, "
299            "with 0 corresponding to C and 6 corresponding to B.  "
300            "The @var{alter} is zero for a natural, negative for "
301            "flats, or positive for sharps. ")
302 {
303   SCM_ASSERT_TYPE (scm_integer_p (octave)== SCM_BOOL_T , octave, SCM_ARG1, __FUNCTION__, "integer");
304   SCM_ASSERT_TYPE (scm_integer_p (note)== SCM_BOOL_T, note, SCM_ARG2, __FUNCTION__, "integer");
305   SCM_ASSERT_TYPE (scm_integer_p (alter)== SCM_BOOL_T, alter, SCM_ARG3, __FUNCTION__, "integer");
306
307   Pitch p (scm_to_int (octave), scm_to_int (note), scm_to_int (alter));
308   return p.smobbed_copy ();
309 }
310
311 LY_DEFINE (ly_pitch_steps, "ly:pitch-steps", 1, 0, 0,
312            (SCM p),
313            "Number of steps counted from middle C of the pitch @var{p}.")
314 {
315   Pitch *pp = unsmob_pitch (p);
316   SCM_ASSERT_TYPE (pp, p, SCM_ARG1, __FUNCTION__, "Pitch");
317   return scm_int2num (pp->steps ());
318 }
319
320 LY_DEFINE (ly_pitch_octave, "ly:pitch-octave",
321            1, 0, 0, (SCM pp),
322            "Extract the octave from pitch @var{p}.")
323 {
324   Pitch *p = unsmob_pitch (pp);
325   SCM_ASSERT_TYPE (p, pp, SCM_ARG1, __FUNCTION__, "Pitch");
326   int q = p->get_octave ();
327   return scm_int2num (q);
328 }
329
330 LY_DEFINE (ly_pitch_alteration, "ly:pitch-alteration",
331            1, 0, 0, (SCM pp),
332            "Extract the alteration from pitch  @var{p}.")
333 {
334   Pitch *p = unsmob_pitch (pp);
335   SCM_ASSERT_TYPE (p, pp, SCM_ARG1, __FUNCTION__, "Pitch");
336   int q = p->get_alteration ();
337
338   return scm_int2num (q);
339 }
340
341 LY_DEFINE (pitch_notename, "ly:pitch-notename",
342            1, 0, 0, (SCM pp),
343            "Extract the note name from pitch  @var{pp}.")
344 {
345   Pitch *p = unsmob_pitch (pp);
346   SCM_ASSERT_TYPE (p, pp, SCM_ARG1, __FUNCTION__, "Pitch");
347   int q = p->get_notename ();
348   return scm_int2num (q);
349 }
350
351 LY_DEFINE (ly_pitch_quartertones, "ly:pitch-quartertones",
352            1, 0, 0, (SCM pp),
353            "Calculate the number of quarter tones of @var{p} from middle C.")
354 {
355   Pitch *p = unsmob_pitch (pp);
356   SCM_ASSERT_TYPE (p, pp, SCM_ARG1, __FUNCTION__, "Pitch");
357   int q = p->quartertone_pitch ();
358   return scm_int2num (q);
359 }
360
361 LY_DEFINE (ly_pitch_semitones, "ly:pitch-semitones",
362            1, 0, 0, (SCM pp),
363            "calculate the number of semitones of @var{p} from middle C.")
364 {
365   Pitch *p = unsmob_pitch (pp);
366   SCM_ASSERT_TYPE (p, pp, SCM_ARG1, __FUNCTION__, "Pitch");
367   int q = p->semitone_pitch ();
368   return scm_int2num (q);
369 }
370
371 LY_DEFINE (ly_pitch_less_p, "ly:pitch<?",
372            2, 0, 0, (SCM p1, SCM p2),
373            "Is @var{p1} lexicographically smaller than @var{p2}?")
374 {
375   Pitch *a = unsmob_pitch (p1);
376   Pitch *b = unsmob_pitch (p2);
377
378   SCM_ASSERT_TYPE (a, p1, SCM_ARG1, __FUNCTION__, "Pitch");
379   SCM_ASSERT_TYPE (b, p2, SCM_ARG2, __FUNCTION__, "Pitch");
380
381   if (Pitch::compare (*a, *b) < 0)
382     return SCM_BOOL_T;
383   else
384     return SCM_BOOL_F;
385 }
386
387 LY_DEFINE (ly_pitch_diff, "ly:pitch-diff",
388            2 ,0, 0, (SCM pitch, SCM  root),
389            "Return pitch @var{delta} such that @code{pitch} transposed by "
390            "@var{delta} equals @var{root}" )
391 {
392   Pitch *p = unsmob_pitch (pitch);
393   Pitch *r = unsmob_pitch (root);
394   SCM_ASSERT_TYPE (p, pitch, SCM_ARG1, __FUNCTION__, "Pitch");
395   SCM_ASSERT_TYPE (r, root, SCM_ARG2, __FUNCTION__, "Pitch");
396
397   return interval (*r, *p).smobbed_copy ();
398 }
399
400 int
401 Pitch::get_octave () const
402 {
403   return octave_;
404 }
405
406 int
407 Pitch::get_notename () const
408 {
409   return notename_;
410 }
411
412 int
413 Pitch::get_alteration () const
414 {
415   return alteration_;
416 }
417
418 Pitch
419 Pitch::transposed (Pitch d) const
420 {
421   Pitch p = *this;
422   p.transpose (d);
423   return p;
424 }