2 musical-pitch.cc -- implement Musical_pitch
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 #include "musical-pitch.hh"
14 to_scm (Musical_pitch p
)
16 return gh_list (gh_int2scm (p
.notename_i_
),
17 gh_int2scm (p
.accidental_i_
),
18 gh_int2scm (p
.octave_i_
),
23 TODO: check -- is_pitch () ?
26 scm_to (SCM s
, Musical_pitch
* )
28 return Musical_pitch (gh_scm2int (gh_car (s
)),
29 gh_scm2int (gh_cadr (s
)),
30 gh_scm2int (gh_caddr (s
)));
33 Musical_pitch::Musical_pitch (int n
, int a
, int o
)
41 Musical_pitch::print () const
49 Musical_pitch::compare (Musical_pitch
const &m1
, Musical_pitch
const &m2
)
51 int o
= m1
.octave_i_
- m2
.octave_i_
;
52 int n
= m1
.notename_i_
- m2
.notename_i_
;
53 int a
= m1
.accidental_i_
- m2
.accidental_i_
;
65 Musical_pitch::steps () const
67 return notename_i_
+ octave_i_
*7;
71 should be settable from input to allow "viola"-mode
73 static Byte pitch_byte_a
[ ] = { 0, 2, 4, 5, 7, 9, 11 };
76 Musical_pitch::semitone_pitch () const
78 return pitch_byte_a
[ notename_i_
% 7 ] + accidental_i_
+ octave_i_
* 12;
82 Musical_pitch::transpose (Musical_pitch delta
)
84 int old_pitch
= semitone_pitch ();
85 int delta_pitch
= delta
.semitone_pitch ();
86 octave_i_
+= delta
.octave_i_
;
87 notename_i_
+= delta
.notename_i_
;
90 while (notename_i_
>= 7)
96 int new_pitch
= semitone_pitch ();
97 int delta_acc
= new_pitch
- old_pitch
- delta_pitch
;
98 accidental_i_
-= delta_acc
;
103 // nice test for internationalisation strings
104 char const *accname
[] = {"double flat", "flat", "natural",
105 "sharp" , "double sharp"};
107 char const *accname
[] = {"eses", "es", "", "is" , "isis"};
111 Musical_pitch::str () const
113 int n
= (notename_i_
+ 2) % 7;
114 String s
= to_str (char(n
+ 'a'));
116 s
+= String (accname
[accidental_i_
+ 2]);
120 int o
= octave_i_
+ 1;
124 else if (octave_i_
<0)
126 int o
= (-octave_i_
) - 1;
136 change me to relative, counting from last pitch p
137 return copy of resulting pitch
140 Musical_pitch::to_relative_octave (Musical_pitch p
)
142 int oct_mod
= octave_i_
+ 1; // account for c' = octave 1 iso. 0 4
143 Musical_pitch
up_pitch (p
);
144 Musical_pitch
down_pitch (p
);
146 up_pitch
.accidental_i_
= accidental_i_
;
147 down_pitch
.accidental_i_
= accidental_i_
;
149 Musical_pitch n
= *this;
150 up_pitch
.up_to (notename_i_
);
151 down_pitch
.down_to (notename_i_
);
154 if (abs (up_pitch
.steps () - h
) < abs (down_pitch
.steps () - h
))
159 n
.octave_i_
+= oct_mod
;
166 Musical_pitch::up_to (int notename
)
168 if (notename_i_
> notename
)
172 notename_i_
= notename
;
176 Musical_pitch::down_to (int notename
)
178 if (notename_i_
< notename
)
182 notename_i_
= notename
;