]> git.donarmstrong.com Git - lilypond.git/blob - lily/pitch.cc
*** empty log message ***
[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--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "pitch.hh"
10 #include "warn.hh"
11 #include "main.hh"
12
13 #include "ly-smobs.icc"
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 pitch_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 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 ().to_str0 ()), 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
269 MAKE_SCHEME_CALLBACK (Pitch, less_p, 2);
270 SCM
271 Pitch::less_p (SCM p1, SCM p2)
272 {
273   Pitch *a = unsmob_pitch (p1);
274   Pitch *b = unsmob_pitch (p2);
275
276   if (compare (*a, *b) < 0)
277     return SCM_BOOL_T;
278   else
279     return SCM_BOOL_F;
280 }
281
282
283 int
284 Pitch::get_octave () const
285 {
286   return octave_;
287 }
288
289 int
290 Pitch::get_notename () const
291 {
292   return notename_;
293 }
294
295 int
296 Pitch::get_alteration () const
297 {
298   return alteration_;
299 }
300
301 Pitch
302 Pitch::transposed (Pitch d) const
303 {
304   Pitch p = *this;
305   p.transpose (d);
306   return p;
307 }