]> git.donarmstrong.com Git - lilypond.git/blob - lily/musical-pitch.cc
eaf7ab5b7ae01e26659c65e4241a93951abaeb15
[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--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9 #include "musical-pitch.hh"
10 #include "debug.hh"
11 #include "main.hh"
12
13 String Musical_pitch::name_str_arr_arr_[7][5];
14
15 Musical_pitch::Musical_pitch (int n, int a, int o)
16 {
17   notename_i_ = n;
18   accidental_i_ = a;
19   octave_i_ = o;
20 }
21
22 void
23 Musical_pitch::print () const
24 {
25 #ifndef NPRINT
26   DOUT << str ();
27 #endif
28 }
29
30 int
31 Musical_pitch::compare (Musical_pitch const &m1, Musical_pitch const &m2)
32 {
33     int o=  m1.octave_i_ - m2.octave_i_;
34   int n = m1.notename_i_ - m2.notename_i_;
35   int a = m1.accidental_i_ - m2.accidental_i_;
36
37   if (o)
38         return o;
39   if (n)
40         return n;
41   if (a)
42         return a;
43   return 0;
44 }
45
46 int
47 Musical_pitch::steps () const
48 {
49   return  notename_i_ + octave_i_*7;
50 }
51
52 /*
53   should be settable from input to allow "viola"-mode
54  */
55 static Byte pitch_byte_a[  ] = { 0, 2, 4, 5, 7, 9, 11 };
56
57 int
58 Musical_pitch::semitone_pitch () const
59 {
60   return  pitch_byte_a[ notename_i_ % 7 ] + accidental_i_ + octave_i_ * 12;
61 }
62
63 void
64 Musical_pitch::transpose (Musical_pitch delta)
65 {
66   int old_pitch = semitone_pitch ();
67   int delta_pitch = delta.semitone_pitch ();
68   octave_i_ += delta.octave_i_;
69   notename_i_ += delta.notename_i_;
70
71   
72   while  (notename_i_ >= 7)
73     {
74       notename_i_ -= 7;
75       octave_i_ ++;
76     }
77
78   int new_pitch = semitone_pitch ();
79   int delta_acc = new_pitch - old_pitch - delta_pitch;
80   accidental_i_ -= delta_acc;
81 }
82
83 String
84 Musical_pitch::str () const
85 {
86   String s = name_str_arr_arr_[notename_i_ % 7][accidental_i_ + 2];
87
88   if (octave_i_ > 0)
89     {
90       int o = octave_i_ + 1;
91       while (o--)
92         s += to_str ('\'');
93     }
94   else if (octave_i_ <0)
95     {
96       int o = (-octave_i_) - 1;
97       while (o--)
98         s += to_str (',');
99     }
100   return s;
101 }
102
103 /*
104   change me to relative, counting from last pitch p
105   return copy of resulting pitch
106  */
107 Musical_pitch
108 Musical_pitch::to_relative_octave (Musical_pitch p)
109 {
110   int oct_mod = octave_i_  + 1; // account for c' = octave 1 iso. 0 4
111   Musical_pitch up_pitch (p);
112   Musical_pitch down_pitch (p);
113
114   up_pitch.accidental_i_ = accidental_i_;
115   down_pitch.accidental_i_ = accidental_i_;
116   
117   Musical_pitch n = *this;
118   up_pitch.up_to (notename_i_);
119   down_pitch.down_to (notename_i_);
120
121   int h = p.steps ();
122   if (abs (up_pitch.steps () - h) < abs (down_pitch.steps () - h))
123     n = up_pitch;
124   else
125     n = down_pitch;
126   
127   n.octave_i_ += oct_mod;
128
129   *this = n;
130   return *this;
131 }
132
133 void
134 Musical_pitch::up_to (int notename)
135 {
136   if (notename_i_  > notename)
137     {
138       octave_i_ ++;
139     }
140   notename_i_  = notename;
141 }
142
143 void
144 Musical_pitch::down_to (int notename)
145 {
146   if (notename_i_ < notename)
147     {
148       octave_i_ --;
149     }
150   notename_i_ = notename;
151 }
152