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