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