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