lilypond-1.3.12
[lilypond.git] / lily / key.cc
blob40e5c7de94293cb760871926cc5257cb38f365db
1 /*
2 key.cc -- implement Key, Octave_key
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 */
10 #include "key.hh"
11 #include "debug.hh"
12 #include "musical-pitch.hh"
14 const int NUMBER_OF_OCTAVES=14; // ugh..
15 const int ZEROOCTAVE=7;
18 void
19 Octave_key::print () const
21 for (int i= 0; i < 7 ; i++)
22 DEBUG_OUT << "note " << i << " acc: " << accidental_i_arr_[i] << " iforce: " << internal_forceacc_b_arr_[i] << '\n';
27 Octave_key::Octave_key()
29 accidental_i_arr_.set_size (7);
30 internal_forceacc_b_arr_.set_size(7);
31 clear ();
34 void
35 Octave_key::clear ()
37 for (int i= 0; i < 7 ; i++)
39 accidental_i_arr_[i] = 0;
40 internal_forceacc_b_arr_[i] = false;
44 Key::Key()
46 multi_octave_b_ = false;
47 octaves_.set_size (NUMBER_OF_OCTAVES);
50 int
51 Key::octave_to_index (int o) const
53 int i = o + ZEROOCTAVE;
54 if (i < 0)
56 warning (_f ("Don't have that many octaves (%s)", to_str (o)));
57 i = 0;
59 if (i >= NUMBER_OF_OCTAVES)
61 warning (_f ("Don't have that many octaves (%s)", to_str (o)));
62 i = NUMBER_OF_OCTAVES -1;
64 return i;
67 Octave_key const&
68 Key::oct (int i) const
70 return octaves_[octave_to_index (i)];
74 void
75 Octave_key::set (int i, int a)
77 if (a <= -3)
79 warning (_f ("underdone accidentals (%s)", to_str (a)));
80 a = -2;
82 if (a >= 3)
84 warning (_f ("overdone accidentals (%s)", to_str (a)));
85 a = 2;
87 accidental_i_arr_[i]=a;
90 void
91 Key::set (Musical_pitch p)
93 int i = octave_to_index (p.octave_i_);
94 octaves_[i].set (p.notename_i_,p.accidental_i_);
97 void
98 Key::set_internal_forceacc (Musical_pitch p)
100 int i = octave_to_index (p.octave_i_);
101 octaves_[i].internal_forceacc_b_arr_[p.notename_i_] = true;
104 void
105 Key::clear_internal_forceacc (Musical_pitch p)
107 int i = octave_to_index (p.octave_i_);
108 octaves_[i].internal_forceacc_b_arr_[p.notename_i_] = false;
111 void
112 Key::set (int n, int a)
114 for (int i= 0; i < NUMBER_OF_OCTAVES ; i++)
115 octaves_[i].set (n,a);
117 void
118 Key::clear ()
120 for (int i= 0; i < NUMBER_OF_OCTAVES ; i++)
121 octaves_[i].clear ();
123 void
124 Key::print () const
126 for (int i= 0; i < NUMBER_OF_OCTAVES ; i++)
128 DEBUG_OUT << "octave " << i - ZEROOCTAVE << " Octave_key { ";
129 octaves_[i].print ();
130 DEBUG_OUT << "}\n";
134 bool
135 Key::different_acc (Musical_pitch p)const
137 return oct (p.octave_i_).acc (p.notename_i_) == p.accidental_i_;
141 bool
142 Key::internal_forceacc (Musical_pitch p)const
144 return oct (p.octave_i_).internal_forceacc_b_arr_[p.notename_i_];
148 bool
149 Key::double_to_single_acc (Musical_pitch p) const
151 return ((oct (p.octave_i_).acc (p.notename_i_) == -2
152 && p.accidental_i_ == -1)
154 || (oct (p.octave_i_).acc (p.notename_i_) == 2
155 && p.accidental_i_ == 1));