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