2 key-def.cc -- implement Key_def
4 source file of the GNU LilyPond music typesetter
6 (c) 1999 Jan Nieuwenhuizen <janneke@gnu.org>
16 ordinary_key_b_
= false;
20 Key_def::ordinary_accidentals_i () const
24 programming_error ("Accidentals requested for non-conventional key");
29 if (pitch_arr_
.size () < 1)
31 warning (_ ("No key name: assuming `C'"));
36 p
= pitch_arr_
[0].semitone_pitch ();
39 /* Solve the equation 7*accidentals_i mod 12 = p, -6 <= accidentals_i <= 5 */
40 int accidentals_i
= (7*p
) % 12;
41 accidentals_i
= (accidentals_i
+ 18) % 12 -6;
43 /* Correct from flats to sharps or vice versa */
44 if (accidentals_i
* pitch_arr_
[0].accidental_i_
< 0)
45 accidentals_i
+= 12 * sign (pitch_arr_
[0].accidental_i_
);
50 Key_def::flats_i () const
53 return 0 >? -ordinary_accidentals_i ();
55 for (int i
= 0; i
< pitch_arr_
.size (); i
++)
57 if (pitch_arr_
[i
].accidental_i_
< 0)
58 flats_i
-= pitch_arr_
[i
].accidental_i_
;
64 Key_def::minor_b () const
66 return modality_i_
== 3;
70 Key_def::sharps_i () const
73 return 0 >? ordinary_accidentals_i ();
75 for (int i
= 0; i
< pitch_arr_
.size (); i
++)
77 if (pitch_arr_
[i
].accidental_i_
> 0)
78 sharps_i
+= pitch_arr_
[i
].accidental_i_
;
84 Key_def::transpose (Musical_pitch d
)
88 if (pitch_arr_
.size () > 0)
89 pitch_arr_
[0].transpose (d
);
92 warning (_ ("don't know how handle empty keys")); // TODO
97 Array
<Musical_pitch
> old_pitch_arr_
;
98 for (int i
= 0; i
< pitch_arr_
.size (); i
++)
100 old_pitch_arr_
.push (pitch_arr_
[i
]);
102 // set accidentals for \key d (as in Key_engraver::read_req)
103 // (later called "new accidentals")
104 int p
= d
.semitone_pitch ();
105 /* Solve the equation 7*accidentals_i mod 12 = p, -6 <= accidentals_i <= 5 */
106 int accidentals_i
= (7*p
) % 12;
107 accidentals_i
= (accidentals_i
+ 18) % 12 -6;
109 /* Correct from flats to sharps or vice versa */
110 if (accidentals_i
* d
.accidental_i_
< 0)
111 accidentals_i
+= 12 * sign (d
.accidental_i_
);
114 if (accidentals_i
< 0)
116 int accidental
= 6 ; // First accidental: bes
117 for ( ; accidentals_i
< 0 ; accidentals_i
++ )
120 m
.accidental_i_
= -1;
121 m
.notename_i_
= accidental
;
124 accidental
= (accidental
+ 3) % 7 ;
129 int accidental
= 3 ; // First accidental: fis
130 for ( ; accidentals_i
> 0 ; accidentals_i
-- )
134 m
.notename_i_
= accidental
;
137 accidental
= (accidental
+ 4) % 7 ;
140 // Check if transposed old accidentals and the new ones coincide
141 accidentals_i
= pitch_arr_
.size ();
144 for (int i
=0; i
< old_pitch_arr_
.size (); i
++)
147 mm
= old_pitch_arr_
[i
];
149 for (int j
=0; ( (j
< accidentals_i
) && (acc_found
== 0)); j
++)
151 if (pitch_arr_
[j
].notename_i_
== mm
.notename_i_
)
153 if (mm
.accidental_i_
== 0)
155 // remove new accidental
162 // change new accidental
163 pitch_arr_
[j
].accidental_i_
= mm
.accidental_i_
;
170 // add transposed old accidental
171 pitch_arr_
.push (mm
);
178 Key_def::squash_octaves ()
180 for (int i
=0; i
< pitch_arr_
.size (); i
++)
182 pitch_arr_
[i
].octave_i_
= 0;