]> git.donarmstrong.com Git - lilypond.git/blob - lily/key.cc
b7d9f22a23e9ef414e4750e051ebea0f43e72f17
[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--1998 Han-Wen Nienhuys <hanwen@stack.nl>
7
8   TODO
9   transposition.
10 */
11
12 #include "key.hh"
13 #include "debug.hh"
14 #include "musical-pitch.hh"
15
16 const int NUMBER_OF_OCTAVES=14;         // ugh..
17 const int ZEROOCTAVE=7;
18
19
20 void
21 Octave_key::print () const
22 {
23   for (int i= 0; i < 7 ; i++)
24     DOUT << "note " << i << " acc: " << accidental_i_arr_[i] << "\n";
25 }
26
27
28
29 Octave_key::Octave_key()
30 {
31   accidental_i_arr_.set_size (7);
32   clear ();
33 }
34
35 void
36 Octave_key::clear ()
37 {
38   for (int i= 0; i < 7 ; i++)
39     accidental_i_arr_[i] = 0;
40 }
41
42 Key::Key()
43 {
44   multi_octave_b_ = false;
45   octaves_.set_size (NUMBER_OF_OCTAVES);
46 }
47
48 int 
49 Key::octave_to_index (int o) const
50 {
51   int i = o + ZEROOCTAVE;
52   if (i < 0)
53     {
54       warning ("Don't have that many octaves (" + String (o) + ")");
55       i = 0;
56     }
57   if (i >= NUMBER_OF_OCTAVES)
58     {
59       warning ("Don't have that many octaves (" + String (o) + ")");
60       i = NUMBER_OF_OCTAVES -1;
61     }
62   return i;
63 }
64
65 Octave_key const&
66 Key::oct (int i) const
67 {
68   return octaves_[octave_to_index (i)];    
69 }
70
71
72 void
73 Octave_key::set (int i, int a)
74 {
75   if (a <= -3)
76     {
77       warning ("Underdone accidentals (" + String (a)+ ")");
78       a = -2;
79     }
80   if (a >= 3)
81     {
82       warning ("Overdone accidentals (" + String (a) + ")");
83       a = 2;
84     }
85   accidental_i_arr_[i]=a;
86 }
87
88 void
89 Key::set (Musical_pitch p)
90 {
91   int   i = octave_to_index (p.octave_i_);
92   octaves_[i].set (p.notename_i_,p.accidental_i_);
93 }
94
95 void
96 Key::set (int n, int a)
97 {
98   for (int i= 0; i < NUMBER_OF_OCTAVES ; i++)
99     octaves_[i].set (n,a);
100 }
101 void
102 Key::clear ()
103 {
104   for (int i= 0; i < NUMBER_OF_OCTAVES ; i++)
105     octaves_[i].clear ();
106 }
107 void
108 Key::print () const
109 {
110   for (int i= 0; i < NUMBER_OF_OCTAVES ; i++)
111     {
112       DOUT << "octave " << i - ZEROOCTAVE << " Octave_key { ";
113       octaves_[i].print ();
114       DOUT << "}\n";
115     }
116 }
117
118 bool
119 Key::different_acc (Musical_pitch p)const
120 {
121   return oct (p.octave_i_).acc (p.notename_i_) == p.accidental_i_;
122 }