]> git.donarmstrong.com Git - lilypond.git/blob - lily/key.cc
release: 1.3.8
[lilypond.git] / lily / key.cc
1 /*
2   key.cc -- implement Key, Octave_key
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7
8 */
9
10 #include "key.hh"
11 #include "debug.hh"
12 #include "musical-pitch.hh"
13
14 const int NUMBER_OF_OCTAVES=14;         // ugh..
15 const int ZEROOCTAVE=7;
16
17
18 void
19 Octave_key::print () const
20 {
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';
23 }
24
25
26
27 Octave_key::Octave_key()
28 {
29   accidental_i_arr_.set_size (7);
30   internal_forceacc_b_arr_.set_size(7);
31   clear ();
32 }
33
34 void
35 Octave_key::clear ()
36 {
37   for (int i= 0; i < 7 ; i++)
38   {
39     accidental_i_arr_[i] = 0;
40     internal_forceacc_b_arr_[i] = false;
41   }
42 }
43
44 Key::Key()
45 {
46   multi_octave_b_ = false;
47   octaves_.set_size (NUMBER_OF_OCTAVES);
48 }
49
50 int 
51 Key::octave_to_index (int o) const
52 {
53   int i = o + ZEROOCTAVE;
54   if (i < 0)
55     {
56       warning (_f ("Don't have that many octaves (%s)", to_str (o)));
57       i = 0;
58     }
59   if (i >= NUMBER_OF_OCTAVES)
60     {
61       warning (_f ("Don't have that many octaves (%s)", to_str (o)));
62       i = NUMBER_OF_OCTAVES -1;
63     }
64   return i;
65 }
66
67 Octave_key const&
68 Key::oct (int i) const
69 {
70   return octaves_[octave_to_index (i)];    
71 }
72
73
74 void
75 Octave_key::set (int i, int a)
76 {
77   if (a <= -3)
78     {
79       warning (_f ("underdone accidentals (%s)", to_str (a)));
80       a = -2;
81     }
82   if (a >= 3)
83     {
84       warning (_f ("overdone accidentals (%s)", to_str (a)));
85       a = 2;
86     }
87   accidental_i_arr_[i]=a;
88 }
89
90 void
91 Key::set (Musical_pitch p)
92 {
93   int   i = octave_to_index (p.octave_i_);
94   octaves_[i].set (p.notename_i_,p.accidental_i_);
95 }
96
97 void
98 Key::set_internal_forceacc (Musical_pitch p)
99 {
100   int   i = octave_to_index (p.octave_i_);
101   octaves_[i].internal_forceacc_b_arr_[p.notename_i_] = true;
102 }
103
104 void
105 Key::clear_internal_forceacc (Musical_pitch p)
106 {
107   int   i = octave_to_index (p.octave_i_);
108   octaves_[i].internal_forceacc_b_arr_[p.notename_i_] = false;
109 }
110
111 void
112 Key::set (int n, int a)
113 {
114   for (int i= 0; i < NUMBER_OF_OCTAVES ; i++)
115     octaves_[i].set (n,a);
116 }
117 void
118 Key::clear ()
119 {
120   for (int i= 0; i < NUMBER_OF_OCTAVES ; i++)
121     octaves_[i].clear ();
122 }
123 void
124 Key::print () const
125 {
126   for (int i= 0; i < NUMBER_OF_OCTAVES ; i++)
127     {
128       DEBUG_OUT << "octave " << i - ZEROOCTAVE << " Octave_key { ";
129       octaves_[i].print ();
130       DEBUG_OUT << "}\n";
131     }
132 }
133
134 bool
135 Key::different_acc (Musical_pitch p)const
136 {
137   return oct (p.octave_i_).acc (p.notename_i_) == p.accidental_i_;
138 }
139
140
141 bool
142 Key::internal_forceacc (Musical_pitch p)const
143 {
144   return oct (p.octave_i_).internal_forceacc_b_arr_[p.notename_i_];
145 }
146
147
148 bool
149 Key::double_to_single_acc (Musical_pitch p) const
150 {
151   return ((oct (p.octave_i_).acc (p.notename_i_) == -2
152       && p.accidental_i_ == -1)
153           
154          || (oct (p.octave_i_).acc (p.notename_i_) == 2
155             && p.accidental_i_ == 1));
156 }