lilypond-1.3.19
[lilypond.git] / lily / musical-pitch.cc
blobd60a1fb832f2e043ce0aee6d82bb94c618465eb6
1 /*
2 musical-pitch.cc -- implement Musical_pitch
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 */
9 #include "musical-pitch.hh"
10 #include "debug.hh"
11 #include "main.hh"
13 SCM
14 to_scm (Musical_pitch p)
16 return gh_list (gh_int2scm (p.notename_i_),
17 gh_int2scm (p.accidental_i_),
18 gh_int2scm (p.octave_i_),
19 SCM_UNDEFINED);
23 TODO: check -- is_pitch () ?
25 void
26 scm_to (SCM s, Musical_pitch* p)
28 *p = Musical_pitch (gh_scm2int (gh_car (s)),
29 gh_scm2int (gh_cadr (s)),
30 gh_scm2int (gh_caddr (s)));
33 Musical_pitch::Musical_pitch (int n, int a, int o)
35 notename_i_ = n;
36 accidental_i_ = a;
37 octave_i_ = o;
40 void
41 Musical_pitch::print () const
43 #ifndef NPRINT
44 DEBUG_OUT << str ();
45 #endif
48 int
49 Musical_pitch::compare (Musical_pitch const &m1, Musical_pitch const &m2)
51 int o= m1.octave_i_ - m2.octave_i_;
52 int n = m1.notename_i_ - m2.notename_i_;
53 int a = m1.accidental_i_ - m2.accidental_i_;
55 if (o)
56 return o;
57 if (n)
58 return n;
59 if (a)
60 return a;
61 return 0;
64 int
65 Musical_pitch::steps () const
67 return notename_i_ + octave_i_*7;
71 should be settable from input to allow "viola"-mode
73 static Byte pitch_byte_a[ ] = { 0, 2, 4, 5, 7, 9, 11 };
75 int
76 Musical_pitch::semitone_pitch () const
78 return pitch_byte_a[ notename_i_ % 7 ] + accidental_i_ + octave_i_ * 12;
81 void
82 Musical_pitch::transpose (Musical_pitch delta)
84 int old_pitch = semitone_pitch ();
85 int delta_pitch = delta.semitone_pitch ();
86 octave_i_ += delta.octave_i_;
87 notename_i_ += delta.notename_i_;
90 while (notename_i_ >= 7)
92 notename_i_ -= 7;
93 octave_i_ ++;
96 int new_pitch = semitone_pitch ();
97 int delta_acc = new_pitch - old_pitch - delta_pitch;
98 accidental_i_ -= delta_acc;
102 #if 0
103 // nice test for internationalisation strings
104 char const *accname[] = {"double flat", "flat", "natural",
105 "sharp" , "double sharp"};
106 #else
107 char const *accname[] = {"eses", "es", "", "is" , "isis"};
108 #endif
110 String
111 Musical_pitch::str () const
113 int n = (notename_i_ + 2) % 7;
114 String s = to_str (char(n + 'a'));
115 if (accidental_i_)
116 s += String (accname[accidental_i_ + 2]);
118 if (octave_i_ > 0)
120 int o = octave_i_ + 1;
121 while (o--)
122 s += "'";
124 else if (octave_i_ <0)
126 int o = (-octave_i_) - 1;
127 while (o--)
128 s += to_str (',');
132 return s;
136 change me to relative, counting from last pitch p
137 return copy of resulting pitch
139 Musical_pitch
140 Musical_pitch::to_relative_octave (Musical_pitch p)
142 int oct_mod = octave_i_ + 1; // account for c' = octave 1 iso. 0 4
143 Musical_pitch up_pitch (p);
144 Musical_pitch down_pitch (p);
146 up_pitch.accidental_i_ = accidental_i_;
147 down_pitch.accidental_i_ = accidental_i_;
149 Musical_pitch n = *this;
150 up_pitch.up_to (notename_i_);
151 down_pitch.down_to (notename_i_);
153 int h = p.steps ();
154 if (abs (up_pitch.steps () - h) < abs (down_pitch.steps () - h))
155 n = up_pitch;
156 else
157 n = down_pitch;
159 n.octave_i_ += oct_mod;
161 *this = n;
162 return *this;
165 void
166 Musical_pitch::up_to (int notename)
168 if (notename_i_ > notename)
170 octave_i_ ++;
172 notename_i_ = notename;
175 void
176 Musical_pitch::down_to (int notename)
178 if (notename_i_ < notename)
180 octave_i_ --;
182 notename_i_ = notename;