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 Musical_pitch::to_scm ()const
16 return gh_list (gh_int2scm (octave_i_
),
17 gh_int2scm (notename_i_
),
18 gh_int2scm (accidental_i_
),
23 Musical_pitch::Musical_pitch (SCM s
)
25 octave_i_
= gh_scm2int (gh_car (s
));
26 notename_i_
= gh_scm2int (gh_cadr (s
));
27 accidental_i_
= gh_scm2int (gh_caddr (s
));
30 Musical_pitch::Musical_pitch (int n
, int a
, int o
)
38 Musical_pitch::print () const
46 Musical_pitch::compare (Musical_pitch
const &m1
, Musical_pitch
const &m2
)
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_
;
62 Musical_pitch::steps () const
64 return notename_i_
+ octave_i_
*7;
68 should be settable from input to allow "viola"-mode
70 static Byte pitch_byte_a
[ ] = { 0, 2, 4, 5, 7, 9, 11 };
73 Musical_pitch::semitone_pitch () const
75 return pitch_byte_a
[ notename_i_
% 7 ] + accidental_i_
+ octave_i_
* 12;
79 Musical_pitch::transpose (Musical_pitch delta
)
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_
;
87 while (notename_i_
>= 7)
93 int new_pitch
= semitone_pitch ();
94 int delta_acc
= new_pitch
- old_pitch
- delta_pitch
;
95 accidental_i_
-= delta_acc
;
100 // nice test for internationalisation strings
101 char const *accname
[] = {"double flat", "flat", "natural",
102 "sharp" , "double sharp"};
104 char const *accname
[] = {"eses", "es", "", "is" , "isis"};
108 Musical_pitch::str () const
110 int n
= (notename_i_
+ 2) % 7;
111 String s
= to_str (char(n
+ 'a'));
113 s
+= String (accname
[accidental_i_
+ 2]);
117 int o
= octave_i_
+ 1;
121 else if (octave_i_
<0)
123 int o
= (-octave_i_
) - 1;
133 change me to relative, counting from last pitch p
134 return copy of resulting pitch
137 Musical_pitch::to_relative_octave (Musical_pitch p
)
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
);
143 up_pitch
.accidental_i_
= accidental_i_
;
144 down_pitch
.accidental_i_
= accidental_i_
;
146 Musical_pitch n
= *this;
147 up_pitch
.up_to (notename_i_
);
148 down_pitch
.down_to (notename_i_
);
151 if (abs (up_pitch
.steps () - h
) < abs (down_pitch
.steps () - h
))
156 n
.octave_i_
+= oct_mod
;
163 Musical_pitch::up_to (int notename
)
165 if (notename_i_
> notename
)
169 notename_i_
= notename
;
173 Musical_pitch::down_to (int notename
)
175 if (notename_i_
< notename
)
179 notename_i_
= notename
;