]> git.donarmstrong.com Git - lilypond.git/blob - lily/pitch.cc
* input/test/dpncnt.ly: New file.
[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--2002 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
16 Pitch::Pitch (int o, int n, int a)
17 {
18   notename_ = n;
19   alteration_ = a;
20   octave_ = o;
21
22   if (n < 0 || n >= 7 ||
23       a < -2 || a > 2)
24     {
25       String s = _ ("Pitch arguments out of range");
26       s += ": alteration = " + to_string (a);
27       s += ", notename = " + to_string (n);
28       warning (s);
29     }
30   normalise ();
31 }
32
33 Pitch::Pitch ()
34 {
35   notename_ = 0;
36   alteration_ = 0;
37   octave_ = 0;
38 }
39
40 int
41 Pitch::compare (Pitch const &m1, Pitch const &m2)
42 {
43   int o=  m1.octave_ - m2.octave_;
44   int n = m1.notename_ - m2.notename_;
45   int a = m1.alteration_ - m2.alteration_;
46
47   if (o)
48         return o;
49   if (n)
50         return n;
51   if (a)
52         return a;
53   return 0;
54 }
55
56 int
57 Pitch::steps () const
58 {
59   return  notename_ + octave_*7;
60 }
61
62 /*
63   should be settable from input?
64  */
65 static Byte pitch_byte_a[  ] = { 0, 2, 4, 5, 7, 9, 11 };
66
67
68 /* Calculate pitch height in 12th octave steps.  Don't assume
69    normalised pitch as this function is used to normalise the pitch.  */
70 int
71 Pitch::semitone_pitch () const
72 {
73   int o = octave_;
74   int n = notename_;
75   while (n < 0)
76     {
77       n += 7;
78       o --;
79     }
80   return (o + n / 7) * 12 + pitch_byte_a[n % 7] + alteration_;
81 }
82
83 void
84 Pitch::normalise ()
85 {
86   int pitch = semitone_pitch ();
87   while (notename_ >= 7)
88     {
89       notename_ -= 7;
90       octave_++;
91       alteration_ -= semitone_pitch () - pitch;
92     }
93   while (notename_ < 0)
94     {
95       notename_ += 7;
96       octave_--;
97       alteration_ -= semitone_pitch () - pitch;
98     }
99   while (alteration_ >= 3)
100     {
101       if (notename_ == 6)
102         {
103           notename_ = 0;
104           octave_++;
105         }
106       else
107         notename_++;
108
109       alteration_ = 0;
110       alteration_ -= semitone_pitch () - pitch;
111     }
112   while (alteration_ <= -3)
113     {
114       if (notename_ == 0)
115         {
116           notename_ = 6;
117           octave_--;
118         }
119       else
120         notename_--;
121
122       alteration_ = 0;
123       alteration_ -= semitone_pitch () - pitch;
124     }
125 }
126
127 /* WHugh, wat een intervaas */
128 void
129 Pitch::transpose (Pitch delta)
130 {
131   int new_semi = semitone_pitch ()  +delta.semitone_pitch();
132   octave_ += delta.octave_;
133   notename_ += delta.notename_;
134   alteration_ += new_semi - semitone_pitch();
135
136   normalise ();
137 }
138
139 Pitch
140 interval (Pitch const & from , Pitch const & to )
141 {
142   int sound = to.semitone_pitch()  - from.semitone_pitch ();
143   Pitch pt ;
144   pt.octave_ = to.octave_ - from.octave_;
145   pt.notename_ = to.notename_ - from.notename_;
146   pt.alteration_ = to.alteration_ - from.alteration_;
147   pt.alteration_ += sound - pt.semitone_pitch();
148
149   return pt;
150 }
151
152
153 /* FIXME
154    Merge with *pitch->text* funcs in chord-name.scm
155  */
156 char const *accname[] = {"eses", "es", "", "is" , "isis"};
157
158 String
159 Pitch::string () const
160 {
161   int n = (notename_ + 2) % 7;
162   String s = to_string (char (n + 'a'));
163   if (alteration_)
164     s += String (accname[alteration_ + 2]);
165
166   if (octave_ >= 0)
167     {
168       int o = octave_ + 1;
169       while (o--)
170         s += "'";
171     }
172   else if (octave_ < 0)
173     {
174       int o = (-octave_) - 1;
175       while (o--)
176         s += to_string (',');
177     }
178
179   return s;
180 }
181
182 /*
183   change me to relative, counting from last pitch p
184   return copy of resulting pitch
185  */
186 Pitch
187 Pitch::to_relative_octave (Pitch p)
188 {
189   int oct_mod = octave_  + 1;   // account for c' = octave 1 iso. 0 4
190   Pitch up_pitch (p);
191   Pitch down_pitch (p);
192
193   up_pitch.alteration_ = alteration_;
194   down_pitch.alteration_ = alteration_;
195   
196   Pitch n = *this;
197   up_pitch.up_to (notename_);
198   down_pitch.down_to (notename_);
199
200   int h = p.steps ();
201   if (abs (up_pitch.steps () - h) < abs (down_pitch.steps () - h))
202     n = up_pitch;
203   else
204     n = down_pitch;
205   
206   n.octave_ += oct_mod;
207
208   *this = n;
209   return *this;
210 }
211
212 void
213 Pitch::up_to (int notename)
214 {
215   if (notename_  > notename)
216     {
217       octave_ ++;
218     }
219   notename_  = notename;
220 }
221
222 void
223 Pitch::down_to (int notename)
224 {
225   if (notename_ < notename)
226     {
227       octave_ --;
228     }
229   notename_ = notename;
230 }
231  
232 LY_DEFINE(ly_pitch_transpose,
233           "ly:pitch-transpose", 2, 0, 0,
234           (SCM p, SCM delta),
235           "Transpose @var{p} by the amount @var{delta}, where @var{delta} is the "
236 " pitch that central C is transposed to.")
237 {
238   Pitch* t = unsmob_pitch (p);
239   Pitch *d = unsmob_pitch (delta);
240   SCM_ASSERT_TYPE(t, p, SCM_ARG1, __FUNCTION__, "pitch")  ;
241   SCM_ASSERT_TYPE(d, delta, SCM_ARG1, __FUNCTION__, "pitch")  ;
242
243   Pitch tp =*t;
244   tp.transpose (*d);
245   return tp.smobbed_copy ();
246 }
247
248 /****************************************************************/
249
250
251 IMPLEMENT_TYPE_P (Pitch, "ly:pitch?");
252
253 SCM
254 Pitch::mark_smob (SCM)
255 {
256   return SCM_EOL;
257 }
258
259 IMPLEMENT_SIMPLE_SMOBS (Pitch);
260 int
261 Pitch::print_smob (SCM s, SCM port, scm_print_state *)
262 {
263   Pitch  *r = (Pitch *) ly_cdr (s);
264      
265   scm_puts ("#<Pitch ", port);
266   scm_display (scm_makfrom0str (r->string ().to_str0 ()), port);
267   scm_puts (" >", port);
268   
269   return 1;
270 }
271
272 SCM
273 Pitch::equal_p (SCM a , SCM b)
274 {
275   Pitch  *p = (Pitch *) ly_cdr (a);
276   Pitch  *q = (Pitch *) ly_cdr (b);  
277
278   bool eq = p->notename_ == q->notename_
279     && p->octave_ == q->octave_
280     && p->alteration_ == q->alteration_;
281
282   return eq ? SCM_BOOL_T : SCM_BOOL_F;
283 }
284
285 MAKE_SCHEME_CALLBACK (Pitch, less_p, 2);
286 SCM
287 Pitch::less_p (SCM p1, SCM p2)
288 {
289   Pitch *a = unsmob_pitch (p1);
290   Pitch *b = unsmob_pitch (p2);
291
292   if (compare (*a, *b) < 0)
293     return SCM_BOOL_T;
294   else
295     return SCM_BOOL_F;
296 }
297
298 /*
299   should add optional args
300  */
301
302 LY_DEFINE(make_pitch, "ly:make-pitch", 3, 0, 0, 
303           (SCM o, SCM n, SCM a),
304           "@var{octave} is specified by an integer, zero for the octave containing "
305           "middle C.  @var{note} is a number from 0 to 6, with 0 corresponding to C "
306           "and 6 corresponding to B.  The shift is zero for a natural, negative for "
307           "flats, or positive for sharps. ")
308 {
309   SCM_ASSERT_TYPE(gh_number_p (o), o, SCM_ARG1, __FUNCTION__, "number");
310   SCM_ASSERT_TYPE(gh_number_p (n), n, SCM_ARG2, __FUNCTION__, "number");
311   SCM_ASSERT_TYPE(gh_number_p (a), a, SCM_ARG3, __FUNCTION__, "number");
312
313   Pitch p (gh_scm2int (o), gh_scm2int (n), gh_scm2int (a));
314   return p.smobbed_copy ();
315 }
316
317
318 LY_DEFINE(pitch_octave, "ly:pitch-octave", 1, 0, 0, 
319           (SCM pp),
320           "extract the octave from pitch @var{p}.")
321 {
322   Pitch *p = unsmob_pitch (pp);
323    SCM_ASSERT_TYPE(p, pp, SCM_ARG1, __FUNCTION__, "Pitch");
324   int q = p->get_octave ();
325
326   return gh_int2scm (q);
327 }
328
329 LY_DEFINE(pitch_alteration, "ly:pitch-alteration", 1, 0, 0, 
330           (SCM pp),
331           "extract the alteration from pitch  @var{p}.")
332 {
333   Pitch *p = unsmob_pitch (pp);
334   SCM_ASSERT_TYPE(p, pp, SCM_ARG1, __FUNCTION__, "Pitch");
335   int     q = p->get_alteration ();
336
337   return gh_int2scm (q);
338 }
339
340 LY_DEFINE(pitch_notename, "ly:pitch-notename", 1, 0, 0, 
341           (SCM pp),
342           "extract the note name from pitch  @var{pp}.")
343 {
344   Pitch *p = unsmob_pitch (pp);
345   SCM_ASSERT_TYPE(p, pp, SCM_ARG1, __FUNCTION__, "Pitch");
346   int q  = p->get_notename ();
347
348   return gh_int2scm (q);
349 }
350
351 LY_DEFINE(pitch_semitones,  "ly:pitch-semitones", 1, 0, 0, 
352           (SCM pp),
353           "calculate the number of semitones of @var{p} from central C.")
354 {
355   Pitch *p = unsmob_pitch (pp);
356   SCM_ASSERT_TYPE(p, pp, SCM_ARG1, __FUNCTION__, "Pitch");
357  
358   int q = p->semitone_pitch ();
359   
360   // Was :
361   //
362   //int q = p->steps ();
363   //
364   // As the function is called "pitch_semitones", I assume it was a mistake !
365   // Jiba
366
367   return gh_int2scm (q);
368 }
369
370 LY_DEFINE(pitch_less, "ly:pitch<?", 2,0,0, (SCM p1, SCM p2),
371           "Is @var{p1} lower than @var{p2}? This uses lexicographic ordening.")
372 {
373   return Pitch::less_p (ly_car (p1),  ly_car (p2));
374 }
375
376 SCM
377 Pitch::smobbed_copy ()const
378 {
379   Pitch *  p = new Pitch (*this);
380   return p->smobbed_self ();
381 }
382
383 int
384 Pitch::get_octave ()const
385 {
386   return octave_;
387 }
388
389 int
390 Pitch::get_notename () const
391 {
392   return notename_;
393 }
394
395 int
396 Pitch::get_alteration () const
397 {
398   return alteration_;
399 }
400