]> git.donarmstrong.com Git - lilypond.git/blob - lily/musical-pitch.cc
release: 1.3.93
[lilypond.git] / lily / musical-pitch.cc
1 /*   
2   musical-pitch.cc --  implement Musical_pitch
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 1998--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9 #include "musical-pitch.hh"
10 #include "debug.hh"
11 #include "main.hh"
12
13 int
14 compare (Array<Musical_pitch>* left, Array<Musical_pitch>* right)
15 {
16   assert (left);
17   assert (right);
18   
19   if (left->size () == right->size ())
20     {
21       for (int i = 0; i < left->size (); i++)
22         {
23           int r = Musical_pitch::compare ((*left)[i], (*right)[i]);
24           if (r)
25             return r;
26         }
27     }
28   else
29     return 1;
30
31   return 0;
32 }
33
34 SCM
35 Musical_pitch::to_scm ()const
36 {
37   return gh_list (gh_int2scm (octave_i_),
38                   gh_int2scm (notename_i_),
39                   gh_int2scm (accidental_i_),
40                   SCM_UNDEFINED);
41 }
42
43
44 Musical_pitch::Musical_pitch (SCM s)
45 {
46   octave_i_ = gh_scm2int (gh_car (s));
47   notename_i_ = gh_scm2int (gh_cadr (s));
48   accidental_i_ = gh_scm2int (gh_caddr (s));
49 }
50
51 Musical_pitch::Musical_pitch (int n, int a, int o)
52 {
53   notename_i_ = n;
54   accidental_i_ = a;
55   octave_i_ = o;
56 }
57
58
59 int
60 Musical_pitch::compare (Musical_pitch const &m1, Musical_pitch const &m2)
61 {
62     int o=  m1.octave_i_ - m2.octave_i_;
63   int n = m1.notename_i_ - m2.notename_i_;
64   int a = m1.accidental_i_ - m2.accidental_i_;
65
66   if (o)
67         return o;
68   if (n)
69         return n;
70   if (a)
71         return a;
72   return 0;
73 }
74
75 int
76 Musical_pitch::steps () const
77 {
78   return  notename_i_ + octave_i_*7;
79 }
80
81 /*
82   should be settable from input to allow "viola"-mode
83  */
84 static Byte pitch_byte_a[  ] = { 0, 2, 4, 5, 7, 9, 11 };
85
86 int
87 Musical_pitch::semitone_pitch () const
88 {
89   return  pitch_byte_a[ notename_i_ % 7 ] + accidental_i_ + octave_i_ * 12;
90 }
91
92 void
93 Musical_pitch::transpose (Musical_pitch delta)
94 {
95   int old_pitch = semitone_pitch ();
96   int delta_pitch = delta.semitone_pitch ();
97   octave_i_ += delta.octave_i_;
98   notename_i_ += delta.notename_i_;
99
100   
101   while  (notename_i_ >= 7)
102     {
103       notename_i_ -= 7;
104       octave_i_ ++;
105     }
106
107   int new_pitch = semitone_pitch ();
108   int delta_acc = new_pitch - old_pitch - delta_pitch;
109   accidental_i_ -= delta_acc;
110 }
111
112
113 #if 0
114 // nice test for internationalisation strings
115 char const *accname[] = {"double flat", "flat", "natural",
116                          "sharp" , "double sharp"};
117 #else
118 char const *accname[] = {"eses", "es", "", "is" , "isis"};
119 #endif
120
121 String
122 Musical_pitch::str () const
123 {
124   int n = (notename_i_ + 2) % 7;
125   String s = to_str (char(n + 'a'));
126   if (accidental_i_)
127     s += String (accname[accidental_i_ + 2]);
128
129   if (octave_i_ > 0)
130     {
131       int o = octave_i_ + 1;
132       while (o--)
133         s += "'";
134     }
135   else if (octave_i_ <0)
136     {
137       int o = (-octave_i_) - 1;
138       while (o--)
139         s += to_str (',');
140     }
141
142
143   return s;
144 }
145
146 /*
147   change me to relative, counting from last pitch p
148   return copy of resulting pitch
149  */
150 Musical_pitch
151 Musical_pitch::to_relative_octave (Musical_pitch p)
152 {
153   int oct_mod = octave_i_  + 1; // account for c' = octave 1 iso. 0 4
154   Musical_pitch up_pitch (p);
155   Musical_pitch down_pitch (p);
156
157   up_pitch.accidental_i_ = accidental_i_;
158   down_pitch.accidental_i_ = accidental_i_;
159   
160   Musical_pitch n = *this;
161   up_pitch.up_to (notename_i_);
162   down_pitch.down_to (notename_i_);
163
164   int h = p.steps ();
165   if (abs (up_pitch.steps () - h) < abs (down_pitch.steps () - h))
166     n = up_pitch;
167   else
168     n = down_pitch;
169   
170   n.octave_i_ += oct_mod;
171
172   *this = n;
173   return *this;
174 }
175
176 void
177 Musical_pitch::up_to (int notename)
178 {
179   if (notename_i_  > notename)
180     {
181       octave_i_ ++;
182     }
183   notename_i_  = notename;
184 }
185
186 void
187 Musical_pitch::down_to (int notename)
188 {
189   if (notename_i_ < notename)
190     {
191       octave_i_ --;
192     }
193   notename_i_ = notename;
194 }
195