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