2 musical-pitch.cc -- implement Musical_pitch
4 source file of the GNU LilyPond music typesetter
6 (c) 1998 Han-Wen Nienhuys <hanwen@cs.ruu.nl>
9 #include "musical-pitch.hh"
12 Musical_pitch::Musical_pitch ()
18 Musical_pitch::init ()
26 Musical_pitch::print () const
34 Musical_pitch::compare (Musical_pitch const &m1, Musical_pitch const &m2)
36 int o= m1.octave_i_ - m2.octave_i_;
37 int n = m1.notename_i_ - m2.notename_i_;
38 int a = m1.accidental_i_ - m2.accidental_i_;
50 Musical_pitch::steps () const
52 return notename_i_ + octave_i_*7;
56 should be settable from input to allow "viola"-mode
58 static Byte pitch_byte_a[ ] = { 0, 2, 4, 5, 7, 9, 11 };
61 Musical_pitch::semitone_pitch () const
63 return pitch_byte_a[ notename_i_ % 7 ] + accidental_i_ + octave_i_ * 12;
67 Musical_pitch::transpose (Musical_pitch delta)
69 int old_pitch = semitone_pitch ();
70 int delta_pitch = delta.semitone_pitch ();
71 octave_i_ += delta.octave_i_;
72 notename_i_ += delta.notename_i_;
75 while (notename_i_ >= 7)
81 int new_pitch = semitone_pitch ();
82 int delta_acc = new_pitch - old_pitch - delta_pitch;
83 accidental_i_ -= delta_acc;
87 char const *accname[] = {"double flat", "flat", "natural",
88 "sharp" , "double sharp"};
91 Musical_pitch::str () const
93 int n = (notename_i_ + 2) % 7;
94 String s (char(n + 'a'));
96 s += " " + String (accname[accidental_i_ + 2]);
99 s += String ((octave_i_> 0)? "^": "_") + String(octave_i_);
106 Musical_pitch::to_relative_octave (Musical_pitch p)
108 int oct_mod = octave_i_ + 1; // account for c' = octave 1 iso. 0 4
109 Musical_pitch up_pitch (p);
110 Musical_pitch down_pitch (p);
112 up_pitch.accidental_i_ = accidental_i_;
113 down_pitch.accidental_i_ = accidental_i_;
115 up_pitch.up_to (notename_i_);
116 down_pitch.down_to (notename_i_);
118 if (abs (up_pitch.steps () - h) < abs (down_pitch.steps () - h))
122 if (oct_mod > 0) // ugh
132 octave_i_ += oct_mod;
137 Musical_pitch::up_to (int notename)
139 if (notename_i_ > notename)
143 notename_i_ = notename;
147 Musical_pitch::down_to (int notename)
149 if (notename_i_ < notename)
153 notename_i_ = notename;
159 My_lily_parser::get_melodic_req (Musical_pitch p, int quotes)
161 if (relative_octave_mode_b_)
163 set_nearest (melodic);
164 int d = melodic->pitch () - last_melodic_->pitch ();
166 if (quotes && (sign (d) == sign (quotes)))
167 shift -= sign (quotes);
168 if (!quotes && (abs (d) == 6))
170 String str = _("Octave ambiguity; assuming ");
175 If the distance is exactly*) half an octave, there is
176 no nearest pitch. In that case, we'll try to guess what
177 composer/ typist meant.
178 Firstly, we'll do this by comparing the 'notename distance':
180 f b' % name-distance: f g a b: 3
182 is surely a shorter notename distance than
184 f 'b % name-distance: b c d e f: 4
186 (should we give a warning at all, or can we safely assume
187 this is a positive interval up?)
189 *) It is conceivable that, musically speaking, the interval
190 with the greater pitch-distance is thought to be smaller?
194 int name_delta = melodic->notename_i_ - last_melodic_->notename_i_;
195 int name_near = abs (name_delta) % 7;
196 int name_wrap = (7 - abs (name_delta)) % 7;
197 if (name_near != name_wrap)
198 shift = name_near < name_wrap ? sign (name_delta) : -sign (name_delta);
199 else if (sign (last_melodic_->accidental_i_)
200 != sign (melodic->accidental_i_))
201 shift = last_melodic_->accidental_i_ - melodic->accidental_i_;
204 String name_str = notename_str (melodic);
205 str += shift > 0 ? name_str + "'" : "'" + name_str;
206 if (sign (d) == sign (shift))
208 melodic->warning (str);
210 melodic->octave_i_ += quotes + shift;
214 Melodic_req nearest (*melodic);
215 set_nearest (&nearest);
216 melodic->octave_i_ += quotes;
218 if (find_quarts_global_b)
220 int e = melodic->pitch () - nearest.pitch ();
223 int d = melodic->pitch () - last_melodic_->pitch ();
224 String str = _("Interval greater than quart");
225 int n = 1 + (abs (d) - 1) / 12;
226 String quote_str ('\'', n);
227 str += _(", relative: ");
228 String name_str = notename_str (melodic);
229 str += d < 0 ? quote_str + name_str : name_str + quote_str;
230 melodic->warning (str);
234 delete last_melodic_;
235 last_melodic_ = melodic->clone ()->musical ()->melodic ();