lilypond-1.1.21
[lilypond.git] / lily / musical-pitch.cc
blob13a4e5de0877436c93559a473e17353c57c99c6f
1 /*
2 musical-pitch.cc -- implement Musical_pitch
4 source file of the GNU LilyPond music typesetter
6 (c) 1998 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 */
9 #include "musical-pitch.hh"
10 #include "debug.hh"
11 #include "main.hh"
13 Musical_pitch::Musical_pitch (int n, int a, int o, bool c)
15 notename_i_ = n;
16 accidental_i_ = a;
17 octave_i_ = o;
18 cautionary_b_ = c;
21 void
22 Musical_pitch::print () const
24 #ifndef NPRINT
25 DOUT << str ();
26 #endif
29 int
30 Musical_pitch::compare (Musical_pitch const &m1, Musical_pitch const &m2)
32 int o= m1.octave_i_ - m2.octave_i_;
33 int n = m1.notename_i_ - m2.notename_i_;
34 int a = m1.accidental_i_ - m2.accidental_i_;
36 if (o)
37 return o;
38 if (n)
39 return n;
40 if (a)
41 return a;
42 return 0;
45 int
46 Musical_pitch::steps () const
48 return notename_i_ + octave_i_*7;
52 should be settable from input to allow "viola"-mode
54 static Byte pitch_byte_a[ ] = { 0, 2, 4, 5, 7, 9, 11 };
56 int
57 Musical_pitch::semitone_pitch () const
59 return pitch_byte_a[ notename_i_ % 7 ] + accidental_i_ + octave_i_ * 12;
62 void
63 Musical_pitch::transpose (Musical_pitch delta)
65 int old_pitch = semitone_pitch ();
66 int delta_pitch = delta.semitone_pitch ();
67 octave_i_ += delta.octave_i_;
68 notename_i_ += delta.notename_i_;
71 while (notename_i_ >= 7)
73 notename_i_ -= 7;
74 octave_i_ ++;
77 int new_pitch = semitone_pitch ();
78 int delta_acc = new_pitch - old_pitch - delta_pitch;
79 accidental_i_ -= delta_acc;
83 #if 0
84 // nice test for internationalisation strings
85 char const *accname[] = {"double flat", "flat", "natural",
86 "sharp" , "double sharp"};
87 #else
88 char const *accname[] = {"eses", "es", "", "is" , "isis"};
89 #endif
91 String
92 Musical_pitch::str () const
94 int n = (notename_i_ + 2) % 7;
95 String s = to_str (char(n + 'a'));
96 if (accidental_i_)
97 s += String (accname[accidental_i_ + 2]);
99 if (octave_i_ > 0)
101 int o = octave_i_ + 1;
102 while (o--)
103 s += to_str ('\'');
105 else if (octave_i_ <0)
107 int o = (-octave_i_) - 1;
108 while (o--)
109 s += to_str (',');
111 #if 0
112 if (octave_i_)
113 s += String ((octave_i_> 0)? "^": "_") + to_str (octave_i_);
114 #endif
116 return s;
120 change me to relative, counting from last pitch p
121 return copy of resulting pitch
123 Musical_pitch
124 Musical_pitch::to_relative_octave (Musical_pitch p)
126 int oct_mod = octave_i_ + 1; // account for c' = octave 1 iso. 0 4
127 Musical_pitch up_pitch (p);
128 Musical_pitch down_pitch (p);
130 up_pitch.accidental_i_ = accidental_i_;
131 down_pitch.accidental_i_ = accidental_i_;
133 Musical_pitch n = *this;
134 up_pitch.up_to (notename_i_);
135 down_pitch.down_to (notename_i_);
137 int h = p.steps ();
138 if (abs (up_pitch.steps () - h) < abs (down_pitch.steps () - h))
139 n = up_pitch;
140 else
141 n = down_pitch;
143 n.octave_i_ += oct_mod;
145 *this = n;
146 return *this;
149 void
150 Musical_pitch::up_to (int notename)
152 if (notename_i_ > notename)
154 octave_i_ ++;
156 notename_i_ = notename;
159 void
160 Musical_pitch::down_to (int notename)
162 if (notename_i_ < notename)
164 octave_i_ --;
166 notename_i_ = notename;