]> git.donarmstrong.com Git - lilypond.git/blob - mi2mu/mudela-item.cc
release: 0.1.9
[lilypond.git] / mi2mu / mudela-item.cc
1 //
2 // mudela-item.cc -- implement Mudela_item
3 //
4 // copyright 1997 Jan Nieuwenhuizen <jan@digicash.com>
5
6 #include "mi2mu-global.hh"
7 #include "string-convert.hh"
8 #include "duration-convert.hh"
9 #include "mudela-column.hh"
10 #include "mudela-item.hh"
11 #include "mudela-stream.hh"
12 #include "mudela-score.hh"
13
14 Mudela_item::Mudela_item (Mudela_column* mudela_column_l)
15 {
16   mudela_column_l_ = mudela_column_l;
17 }
18
19 Moment
20 Mudela_item::at_mom()
21 {
22   return mudela_column_l_->at_mom();
23 }
24
25 Moment
26 Mudela_item::duration_mom()
27 {
28   return Moment (0);
29 }
30
31 void
32 Mudela_item::output (Mudela_stream& mudela_stream_r)
33 {
34   mudela_stream_r << str() << String (" ");
35 }
36
37 Mudela_key::Mudela_key (int accidentals_i, int minor_i)
38   : Mudela_item (0)
39 {
40   accidentals_i_ = accidentals_i;
41   minor_i_ = minor_i;
42   if  (accidentals_i >= 0)
43         key_i_ =   ((accidentals_i % 7)[ "cgdaebf" ] - 'a' - 2) % 7;
44   else
45         key_i_ =   ((-accidentals_i % 7)[ "cfbeadg" ] - 'a' - 2) % 7;
46 }
47
48 String
49 Mudela_key::str()
50 {
51   String str = "\\key ";
52   if  (!minor_i_) 
53         str += String ((char)  ((key_i_ + 2) % 7 + 'A'));
54   else // heu, -2: should be - 1 1/2: A -> fis
55         str += String ((char)  ((key_i_ + 2 - 2) % 7 + 'a'));
56   str = String ("% \"") + str
57         + String('"') + "; % not supported yet\n"; 
58   return str;
59 }
60
61 String
62 Mudela_key::notename_str (int pitch_i)
63 {
64   // this may seem very smart,
65   // but it-s only an excuse not to read a notename table
66
67   // major scale: do-do
68   // minor scale: la-la  (= + 5)
69   static int notename_i_a[ 12 ] = { 0, 0, 1, 1, 2, 3, 3, 4, 4, 5, 5, 6 };
70   int notename_i = notename_i_a[  (minor_i_ * 5 + pitch_i) % 12 ];
71   
72   static int accidentals_i_a[ 12 ] = { 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0 };
73   int accidental_i = accidentals_i_a[ minor_i_ * 5 + pitch_i % 12 ];
74   if  (accidental_i &&  (accidentals_i_ < 0)) 
75     {
76         accidental_i = - accidental_i;
77         notename_i =  (notename_i + 1) % 7;
78     }
79
80   String notename_str = (char)  ( ((notename_i + 2) % 7) + 'a');
81   while  (accidental_i-- > 0)
82         notename_str += "is";
83   accidental_i++;
84   while  (accidental_i++ < 0)
85         if   ((notename_str == "a") ||  (notename_str == "e"))
86             notename_str += "s";
87         else
88             notename_str += "es";
89   accidental_i--;
90
91   String de_octavate_str = String ('\'',  (Mudela_note::c0_pitch_i_c_ + 11 - pitch_i) / 12);
92   String octavate_str = String ('\'',  (pitch_i - Mudela_note::c0_pitch_i_c_) / 12);
93   return de_octavate_str + notename_str + octavate_str;
94 }
95
96 Mudela_meter::Mudela_meter (int num_i, int den_i, int clocks_4_i, int count_32_i)
97    : Mudela_item (0)
98 {
99   sync_dur_.durlog_i_ = 3 ;
100   sync_f_ = 1.0;
101   if  (count_32_i != 8)
102         warning (String ("#32 in quarter: ") + String (count_32_i));
103   num_i_ = num_i;
104   den_i_ = den_i;
105   clocks_1_i_ = clocks_4_i * 4; 
106 }
107
108 Moment
109 Mudela_meter::bar_mom()
110 {
111   Duration d;
112   d.durlog_i_ =   den_i_;
113   return Moment (num_i_) * Duration_convert::dur2_mom (d);
114 }
115
116 int
117 Mudela_meter::clocks_1_i()
118 {
119   return clocks_1_i_;
120 }
121
122 int
123 Mudela_meter::den_i()
124 {
125   return den_i_;
126 }
127
128 int
129 Mudela_meter::num_i()
130 {
131   return num_i_;
132 }
133
134 String
135 Mudela_meter::str()
136 {
137   String str = "\\meter "
138         + String (num_i_) + "/" + String (1 << den_i_) 
139         + ";\n";
140   return str;
141 }
142
143
144 // statics Mudela_note
145 /*
146  this switch can be used to write simple plets like 
147    c4*2/3 
148  as  
149    \plet 2/3; c4 \plet 1/1;
150  */
151 bool const Mudela_note::simple_plet_b_s = true;
152
153 Mudela_note::Mudela_note (Mudela_column* mudela_column_l, int channel_i, int pitch_i, int dyn_i)
154   : Mudela_item (mudela_column_l)
155 {
156   // junk dynamics
157   (void)dyn_i;
158   channel_i_ = channel_i;
159   pitch_i_ = pitch_i;   
160   end_column_l_ = 0;
161 }
162
163 String
164 Mudela_note::str()
165 {
166   Duration dur = duration();
167   if  (dur.durlog_i_ < -10)
168         return "";
169
170   String name_str 
171     = mudela_column_l_->mudela_score_l_->mudela_key_l_->notename_str (pitch_i_);
172
173   if  (simple_plet_b_s)
174         return name_str + Duration_convert::dur2_str (dur) + " ";
175
176   //ugh
177   String str;
178   if  (dur.plet_b())
179         str += String ("\\plet ")
180             + String_convert::i2dec_str (dur.plet_.iso_i_, 0, 0)
181             + "/"
182             + String_convert::i2dec_str (dur.plet_.type_i_, 0, 0)
183             + "; ";
184
185   str += name_str;
186
187   Duration tmp = dur;
188   tmp.set_plet (1,1);
189   str += Duration_convert::dur2_str (tmp);
190
191   if  (dur.plet_b())
192         str += String (" \\plet 1/1;");
193
194   return str + " ";
195 }
196
197 Duration
198 Mudela_note::duration()
199 {
200   assert (end_column_l_);
201   Moment mom = end_column_l_->at_mom() - at_mom();
202   return Duration_convert::mom2_dur (mom);
203 }
204
205 Moment
206 Mudela_note::duration_mom()
207 {
208 // ugh
209 //    return Duration_convert::dur2_mom (duration());
210   return end_column_l_->at_mom() - at_mom();
211 }
212
213 Mudela_skip::Mudela_skip (Mudela_column* mudela_column_l, Moment skip_mom)
214   : Mudela_item (mudela_column_l)
215 {
216   mom_ = skip_mom;
217 }
218
219 Duration
220 Mudela_skip::duration()
221 {
222         return Duration_convert::mom2_dur (mom_);
223 }
224
225 Moment
226 Mudela_skip::duration_mom()
227 {
228   return Duration_convert::dur2_mom (duration());
229 }
230
231 String
232 Mudela_skip::str()
233 {
234   if  (!mom_)
235         return String ("");
236
237   Duration dur = duration();
238   if  (dur.durlog_i_<-10)
239         return "";
240
241   String str = "\\skip ";
242   str += Duration_convert::dur2_str (dur) + "; ";
243
244   return str;
245 }
246
247 Mudela_tempo::Mudela_tempo (int useconds_per_4_i)
248    : Mudela_item (0)
249 {
250   useconds_per_4_i_ = useconds_per_4_i;
251   seconds_per_1_f_ = (Real)useconds_per_4_i_ * 4 / 1e6;
252 }
253
254 String
255 Mudela_tempo::str()
256 {
257   String str = "\\tempo 4=";
258   str += String (get_tempo_i (Moment (1, 4)));
259   str += ";\n";
260   return str;
261 }
262
263 int 
264 Mudela_tempo::useconds_per_4_i()
265 {
266   return useconds_per_4_i_;
267 }
268
269 int
270 Mudela_tempo::get_tempo_i (Moment moment)
271 {
272   return Moment (60) / moment / Moment (seconds_per_1_f_);
273 }
274
275 Mudela_text::Mudela_text (Mudela_text::Type type, String text_str)
276    : Mudela_item (0)
277 {
278   type_ = type;
279   text_str_ = text_str;
280 }
281
282 String
283 Mudela_text::str()
284 {
285   if  (!text_str_.length_i() 
286         ||  (text_str_.length_i() != (int)strlen (text_str_.ch_C())))
287         return "";
288
289   return "% " + text_str_ + "\n";
290 }
291