lilypond-1.3.67
[lilypond.git] / lily / musical-pitch.cc
blob0ebd91dfe4139d94bcc2c02dbb60c610ed0878fb
1 /*
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>
8 */
9 #include "musical-pitch.hh"
10 #include "debug.hh"
11 #include "main.hh"
13 SCM
14 Musical_pitch::to_scm ()const
16 return gh_list (gh_int2scm (octave_i_),
17 gh_int2scm (notename_i_),
18 gh_int2scm (accidental_i_),
19 SCM_UNDEFINED);
23 Musical_pitch::Musical_pitch (SCM s)
25 octave_i_ = gh_scm2int (gh_car (s));
26 notename_i_ = gh_scm2int (gh_cadr (s));
27 accidental_i_ = gh_scm2int (gh_caddr (s));
30 Musical_pitch::Musical_pitch (int n, int a, int o)
32 notename_i_ = n;
33 accidental_i_ = a;
34 octave_i_ = o;
37 void
38 Musical_pitch::print () const
40 #ifndef NPRINT
41 DEBUG_OUT << str ();
42 #endif
45 int
46 Musical_pitch::compare (Musical_pitch const &m1, Musical_pitch const &m2)
48 int o= m1.octave_i_ - m2.octave_i_;
49 int n = m1.notename_i_ - m2.notename_i_;
50 int a = m1.accidental_i_ - m2.accidental_i_;
52 if (o)
53 return o;
54 if (n)
55 return n;
56 if (a)
57 return a;
58 return 0;
61 int
62 Musical_pitch::steps () const
64 return notename_i_ + octave_i_*7;
68 should be settable from input to allow "viola"-mode
70 static Byte pitch_byte_a[ ] = { 0, 2, 4, 5, 7, 9, 11 };
72 int
73 Musical_pitch::semitone_pitch () const
75 return pitch_byte_a[ notename_i_ % 7 ] + accidental_i_ + octave_i_ * 12;
78 void
79 Musical_pitch::transpose (Musical_pitch delta)
81 int old_pitch = semitone_pitch ();
82 int delta_pitch = delta.semitone_pitch ();
83 octave_i_ += delta.octave_i_;
84 notename_i_ += delta.notename_i_;
87 while (notename_i_ >= 7)
89 notename_i_ -= 7;
90 octave_i_ ++;
93 int new_pitch = semitone_pitch ();
94 int delta_acc = new_pitch - old_pitch - delta_pitch;
95 accidental_i_ -= delta_acc;
99 #if 0
100 // nice test for internationalisation strings
101 char const *accname[] = {"double flat", "flat", "natural",
102 "sharp" , "double sharp"};
103 #else
104 char const *accname[] = {"eses", "es", "", "is" , "isis"};
105 #endif
107 String
108 Musical_pitch::str () const
110 int n = (notename_i_ + 2) % 7;
111 String s = to_str (char(n + 'a'));
112 if (accidental_i_)
113 s += String (accname[accidental_i_ + 2]);
115 if (octave_i_ > 0)
117 int o = octave_i_ + 1;
118 while (o--)
119 s += "'";
121 else if (octave_i_ <0)
123 int o = (-octave_i_) - 1;
124 while (o--)
125 s += to_str (',');
129 return s;
133 change me to relative, counting from last pitch p
134 return copy of resulting pitch
136 Musical_pitch
137 Musical_pitch::to_relative_octave (Musical_pitch p)
139 int oct_mod = octave_i_ + 1; // account for c' = octave 1 iso. 0 4
140 Musical_pitch up_pitch (p);
141 Musical_pitch down_pitch (p);
143 up_pitch.accidental_i_ = accidental_i_;
144 down_pitch.accidental_i_ = accidental_i_;
146 Musical_pitch n = *this;
147 up_pitch.up_to (notename_i_);
148 down_pitch.down_to (notename_i_);
150 int h = p.steps ();
151 if (abs (up_pitch.steps () - h) < abs (down_pitch.steps () - h))
152 n = up_pitch;
153 else
154 n = down_pitch;
156 n.octave_i_ += oct_mod;
158 *this = n;
159 return *this;
162 void
163 Musical_pitch::up_to (int notename)
165 if (notename_i_ > notename)
167 octave_i_ ++;
169 notename_i_ = notename;
172 void
173 Musical_pitch::down_to (int notename)
175 if (notename_i_ < notename)
177 octave_i_ --;
179 notename_i_ = notename;