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++)
23 int r = Musical_pitch::compare ((*left)[i], (*right)[i]);
35 Musical_pitch::to_scm ()const
37 return gh_list (gh_int2scm (octave_i_),
38 gh_int2scm (notename_i_),
39 gh_int2scm (accidental_i_),
44 Musical_pitch::Musical_pitch (SCM s)
46 octave_i_ = gh_scm2int (gh_car (s));
47 notename_i_ = gh_scm2int (gh_cadr (s));
48 accidental_i_ = gh_scm2int (gh_caddr (s));
51 Musical_pitch::Musical_pitch (int n, int a, int o)
60 Musical_pitch::compare (Musical_pitch const &m1, Musical_pitch const &m2)
62 int o= m1.octave_i_ - m2.octave_i_;
63 int n = m1.notename_i_ - m2.notename_i_;
64 int a = m1.accidental_i_ - m2.accidental_i_;
76 Musical_pitch::steps () const
78 return notename_i_ + octave_i_*7;
82 should be settable from input to allow "viola"-mode
84 static Byte pitch_byte_a[ ] = { 0, 2, 4, 5, 7, 9, 11 };
87 Musical_pitch::semitone_pitch () const
89 return pitch_byte_a[ notename_i_ % 7 ] + accidental_i_ + octave_i_ * 12;
93 Musical_pitch::transpose (Musical_pitch delta)
95 int old_pitch = semitone_pitch ();
96 int delta_pitch = delta.semitone_pitch ();
97 octave_i_ += delta.octave_i_;
98 notename_i_ += delta.notename_i_;
101 while (notename_i_ >= 7)
107 int new_pitch = semitone_pitch ();
108 int delta_acc = new_pitch - old_pitch - delta_pitch;
109 accidental_i_ -= delta_acc;
114 // nice test for internationalisation strings
115 char const *accname[] = {"double flat", "flat", "natural",
116 "sharp" , "double sharp"};
118 char const *accname[] = {"eses", "es", "", "is" , "isis"};
122 Musical_pitch::str () const
124 int n = (notename_i_ + 2) % 7;
125 String s = to_str (char(n + 'a'));
127 s += String (accname[accidental_i_ + 2]);
131 int o = octave_i_ + 1;
135 else if (octave_i_ <0)
137 int o = (-octave_i_) - 1;
147 change me to relative, counting from last pitch p
148 return copy of resulting pitch
151 Musical_pitch::to_relative_octave (Musical_pitch p)
153 int oct_mod = octave_i_ + 1; // account for c' = octave 1 iso. 0 4
154 Musical_pitch up_pitch (p);
155 Musical_pitch down_pitch (p);
157 up_pitch.accidental_i_ = accidental_i_;
158 down_pitch.accidental_i_ = accidental_i_;
160 Musical_pitch n = *this;
161 up_pitch.up_to (notename_i_);
162 down_pitch.down_to (notename_i_);
165 if (abs (up_pitch.steps () - h) < abs (down_pitch.steps () - h))
170 n.octave_i_ += oct_mod;
177 Musical_pitch::up_to (int notename)
179 if (notename_i_ > notename)
183 notename_i_ = notename;
187 Musical_pitch::down_to (int notename)
189 if (notename_i_ < notename)
193 notename_i_ = notename;