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