2 musical-pitch.cc -- implement Musical_pitch
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 #include "musical-pitch.hh"
14 compare (Array<Musical_pitch>* left, Array<Musical_pitch>* right)
19 if (left->size () == right->size ())
21 for (int i = 0; i < left->size (); i++)
22 if ((*left)[i] != (*right)[i])
29 Musical_pitch::to_scm ()const
31 return gh_list (gh_int2scm (octave_i_),
32 gh_int2scm (notename_i_),
33 gh_int2scm (accidental_i_),
38 Musical_pitch::Musical_pitch (SCM s)
40 octave_i_ = gh_scm2int (gh_car (s));
41 notename_i_ = gh_scm2int (gh_cadr (s));
42 accidental_i_ = gh_scm2int (gh_caddr (s));
45 Musical_pitch::Musical_pitch (int n, int a, int o)
53 Musical_pitch::print () const
61 Musical_pitch::compare (Musical_pitch const &m1, Musical_pitch const &m2)
63 int o= m1.octave_i_ - m2.octave_i_;
64 int n = m1.notename_i_ - m2.notename_i_;
65 int a = m1.accidental_i_ - m2.accidental_i_;
77 Musical_pitch::steps () const
79 return notename_i_ + octave_i_*7;
83 should be settable from input to allow "viola"-mode
85 static Byte pitch_byte_a[ ] = { 0, 2, 4, 5, 7, 9, 11 };
88 Musical_pitch::semitone_pitch () const
90 return pitch_byte_a[ notename_i_ % 7 ] + accidental_i_ + octave_i_ * 12;
94 Musical_pitch::transpose (Musical_pitch delta)
96 int old_pitch = semitone_pitch ();
97 int delta_pitch = delta.semitone_pitch ();
98 octave_i_ += delta.octave_i_;
99 notename_i_ += delta.notename_i_;
102 while (notename_i_ >= 7)
108 int new_pitch = semitone_pitch ();
109 int delta_acc = new_pitch - old_pitch - delta_pitch;
110 accidental_i_ -= delta_acc;
115 // nice test for internationalisation strings
116 char const *accname[] = {"double flat", "flat", "natural",
117 "sharp" , "double sharp"};
119 char const *accname[] = {"eses", "es", "", "is" , "isis"};
123 Musical_pitch::str () const
125 int n = (notename_i_ + 2) % 7;
126 String s = to_str (char(n + 'a'));
128 s += String (accname[accidental_i_ + 2]);
132 int o = octave_i_ + 1;
136 else if (octave_i_ <0)
138 int o = (-octave_i_) - 1;
148 change me to relative, counting from last pitch p
149 return copy of resulting pitch
152 Musical_pitch::to_relative_octave (Musical_pitch p)
154 int oct_mod = octave_i_ + 1; // account for c' = octave 1 iso. 0 4
155 Musical_pitch up_pitch (p);
156 Musical_pitch down_pitch (p);
158 up_pitch.accidental_i_ = accidental_i_;
159 down_pitch.accidental_i_ = accidental_i_;
161 Musical_pitch n = *this;
162 up_pitch.up_to (notename_i_);
163 down_pitch.down_to (notename_i_);
166 if (abs (up_pitch.steps () - h) < abs (down_pitch.steps () - h))
171 n.octave_i_ += oct_mod;
178 Musical_pitch::up_to (int notename)
180 if (notename_i_ > notename)
184 notename_i_ = notename;
188 Musical_pitch::down_to (int notename)
190 if (notename_i_ < notename)
194 notename_i_ = notename;