]> git.donarmstrong.com Git - lilypond.git/blob - midi2ly/mudela-staff.cc
release: 1.2.8
[lilypond.git] / midi2ly / mudela-staff.cc
1 //
2 // mudela-staff.cc -- implement Mudela_staff
3 //
4 // copyright 1997 Jan Nieuwenhuizen <janneke@gnu.org>
5
6 #include <assert.h>
7 #include <ctype.h>
8 #include "moment.hh"
9 #include "duration-convert.hh"
10 #include "string-convert.hh"
11 #include "midi2ly-proto.hh"
12 #include "midi2ly-global.hh"
13 #include "mudela-column.hh"
14 #include "mudela-item.hh"
15 #include "mudela-staff.hh"
16 #include "mudela-stream.hh"
17 #include "mudela-voice.hh"
18 #include "mudela-score.hh"
19
20 #include "killing-cons.tcc"
21
22 extern Mudela_score* mudela_score_l_g;
23
24 Mudela_staff::Mudela_staff (int number_i, String copyright_str, String track_name_str, String instrument_str)
25 {
26   number_i_ = number_i;
27   copyright_str_ = copyright_str;
28   instrument_str_ = instrument_str;
29   name_str_ = track_name_str;
30   mudela_key_l_ = 0;
31   mudela_time_signature_l_ = 0;
32   mudela_tempo_l_ = 0;
33 }
34
35 void
36 Mudela_staff::add_item (Mudela_item* mudela_item_p)
37 {
38   mudela_item_p_list_.append (new Killing_cons <Mudela_item> (mudela_item_p, 0));
39   if (mudela_item_p->mudela_column_l_)
40     mudela_item_p->mudela_column_l_->add_item (mudela_item_p);
41 }
42 /**
43    Walk ITEMS and find voices.  Remove categorised items.
44
45    TODO: collect all channels into separate voices. Use chords for sim
46    notes on same channel.
47    
48  */
49 void
50 Mudela_staff::eat_voice (Cons_list<Mudela_item>& items)
51 {
52   Mudela_voice* voice_p = new Mudela_voice (this);
53   mudela_voice_p_list_.append (new Killing_cons<Mudela_voice> (voice_p, 0));
54
55   //    Moment mom = items.top ()->at_mom ();
56   Moment mom = 0;
57
58   for (Cons<Mudela_item>** pp = &items.head_; *pp;)
59     {
60       Cons<Mudela_item>* i = *pp;
61       if (i->car_->at_mom () > mom)
62         {
63           Moment dur = i->car_->at_mom () - mom;
64           // ugh, need score
65           Mudela_column* start = mudela_score_l_g->find_column_l (mom);
66           voice_p->add_item (new Mudela_skip (start, dur));
67           mom = i->car_->at_mom ();
68           continue;             // unnecessary
69         }
70       
71       Link_array<Mudela_item> now_items;
72       for (Cons<Mudela_item> *cp = i; cp && cp->car_->at_mom () == mom; cp = cp->next_)
73         now_items.push (i->car_);
74
75       Mudela_item * which = 0;
76       Mudela_note * last_note = dynamic_cast<Mudela_note*> (voice_p->last_item_l_);
77
78       for (int i=0; !which && last_note && i < now_items.size (); i++)
79         {
80           Mudela_note * now_note = dynamic_cast<Mudela_note*> (now_items[i]);
81           if (now_note && last_note->channel_i_ != now_note->channel_i_)
82             which = now_note;
83         }
84
85 #if 0
86       // use pitch difference for determining which item to pick.
87       if (!which)
88         {
89           int mindiff = 100000; // ugh
90           for (int i=0; !which && last_note && i < now_items.size (); i++)
91             {
92               Mudela_note *nt = dynamic_cast<Mudela_note*> (now_items[i]);
93               if (!nt)
94                 continue;
95               int diff = abs (last_note->pitch_i_ - nt->pitch_i_ );
96               if(diff < mindiff)
97                 {
98                   mindiff =  diff;
99                   which = now_items [i];
100                 }
101             }
102
103           if (which && mindiff > 18)            // more than 1.5 octaves apart.  Don't put in same voice.
104             {
105               which =0;
106             }
107
108         }
109 #endif
110       
111       if (!which && now_items.size ())
112         which = now_items[0];
113
114       
115
116       if (!which)
117         {
118           pp = &(*pp)->next_;
119           continue;
120         }
121       
122       while ((*pp)->car_ != which)
123         pp = &(*pp)->next_;
124       
125       mom += (*pp)->car_->duration_mom ();
126       Cons<Mudela_item>* c = items.remove_cons (pp);
127       voice_p->add_item (c->car_);
128       delete c;
129     }
130 }
131
132 String
133 Mudela_staff::id_str ()
134 {
135   String id (name_str ());
136   char *cp = id.ch_l ();
137   char *end = cp + id.length_i ();
138   for (;cp < end; cp++)
139     {
140       if (!isalpha (*cp))
141         {
142           *cp = 'X';
143         }
144     }
145   return id;
146 }
147
148 String
149 Mudela_staff::name_str ()
150 {
151   if (name_str_.length_i ())
152     return name_str_;
153   return String ("track") + to_str (char ('A' - 1 + number_i_));
154 }
155
156
157
158 void
159 Mudela_staff::output (Mudela_stream& mudela_stream_r)
160 {
161   int c =0;
162   
163   String trackbody = "";
164   for (Cons<Mudela_voice>* i = mudela_voice_p_list_.head_; i; i = i->next_)
165     {
166       String voicename = id_str () + "voice" + to_str (char (c + 'A'));
167       
168       mudela_stream_r << voicename << " = \\notes ";
169
170       trackbody += "\\"  + voicename + "\n";
171
172       mudela_stream_r << "\n";
173       i->car_->output (mudela_stream_r);
174       c++;      
175     }
176
177   mudela_stream_r << _ ("% midi copyright:") << copyright_str_ << '\n';
178   mudela_stream_r << _ ("% instrument:") << instrument_str_ << '\n';
179   mudela_stream_r << id_str () << " = ";
180   mudela_stream_r << "<\n " << trackbody << " >\n";
181
182   mudela_stream_r << " % " << name_str () << '\n';
183 }
184
185 void
186 Mudela_staff::output_mudela_begin_bar (Mudela_stream& mudela_stream_r, Moment now_mom, int bar_i)
187 {
188   Moment bar_mom = mudela_time_signature_l_->bar_mom ();
189   Moment into_bar_mom = now_mom - Moment (bar_i - 1) * bar_mom;
190   if (bar_i > 1)
191     {
192       if (!into_bar_mom)
193         mudela_stream_r << "|\n";
194     }
195   mudela_stream_r << "% " << String_convert::i2dec_str (bar_i, 0, ' ');
196   if (into_bar_mom)
197     mudela_stream_r << ":" << Duration_convert::dur2_str (Duration_convert::mom2_dur (into_bar_mom));
198   mudela_stream_r << '\n';
199 }
200
201
202 #if 0 // not used for now
203 void
204 Mudela_staff::output_mudela_rest (Mudela_stream& mudela_stream_r, Moment begin_mom, Moment end_mom)
205 {
206   Moment bar_mom = mudela_time_signature_l_->bar_mom ();
207   Moment now_mom = begin_mom;
208
209   int begin_bar_i = (int) (now_mom / bar_mom) + 1;
210   int end_bar_i = (int) (end_mom / bar_mom) + 1;
211
212   if (end_bar_i == begin_bar_i)
213     {
214       output_mudela_rest_remain (mudela_stream_r, end_mom - begin_mom);
215       return;
216     }
217
218   // multiple bars involved
219   int bar_i = (int) (now_mom / bar_mom) + 1;
220
221   //fill current bar
222   Moment begin_bar_mom = Moment (begin_bar_i - 1) * bar_mom;
223   if (now_mom > begin_bar_mom)
224     {
225       int next_bar_i = (int) (now_mom / bar_mom) + 2;
226       Moment next_bar_mom = Moment (next_bar_i - 1) * bar_mom;
227       assert (next_bar_mom <= end_mom);
228
229       Moment remain_mom = next_bar_mom - now_mom;
230       if (remain_mom > Moment (0))
231         {
232           output_mudela_rest_remain (mudela_stream_r, remain_mom);
233           now_mom += remain_mom;
234         }
235
236       bar_i = check_end_bar_i (now_mom, bar_i);
237     }
238
239   // fill whole bars
240   int count_i = end_bar_i - bar_i;
241   for (int i = 0; i < count_i; i++)
242     {
243       int begin_bar_i = check_begin_bar_i (now_mom, bar_i);
244       if (begin_bar_i)
245         output_mudela_begin_bar (mudela_stream_r, now_mom, begin_bar_i);
246       mudela_stream_r << "r1 ";
247       //        *mudela_stream_r.os_p_ << flush;
248       if (begin_bar_i)
249         LOGOUT (NORMAL_ver) << begin_bar_i << flush;
250       bar_i = check_end_bar_i (now_mom, bar_i);
251       now_mom += bar_mom;
252     }
253
254   // use "int i" here, and gcc 2.7.2 hits internal compiler error
255   int ii = check_begin_bar_i (now_mom, bar_i);
256   if (ii)
257     output_mudela_begin_bar (mudela_stream_r, now_mom, ii);
258
259   //    bar_i = check_end_bar_i (now_mom, bar_i);
260
261   Moment remain_mom = end_mom - Moment (end_bar_i - 1) * bar_mom;
262   if (remain_mom > Moment (0))
263     {
264       output_mudela_rest_remain (mudela_stream_r, remain_mom);
265       now_mom += remain_mom;
266     }
267   assert (now_mom == end_mom);
268 }
269
270 void
271 Mudela_staff::output_mudela_rest_remain (Mudela_stream& mudela_stream_r, Moment mom)
272 {
273   if (Duration_convert::no_quantify_b_s)
274     {
275       Duration dur = Duration_convert::mom2_dur (mom);
276       mudela_stream_r << "r" << dur.str () << " ";
277       //        assert (mom == dur.mom ());
278       assert (mom == dur.length ());
279       return;
280     }
281
282   Duration dur = Duration_convert::mom2standardised_dur (mom);
283   if (dur.type_i_>-10)
284     mudela_stream_r << "r" << dur.str () << " ";
285 }
286 #endif
287
288
289 void
290 Mudela_staff::process ()
291 {
292   /*
293      group items into voices
294      */
295
296   assert (mudela_score_l_g);
297   mudela_key_l_ = mudela_score_l_g->mudela_key_l_;
298   mudela_time_signature_l_ = mudela_score_l_g->mudela_time_signature_l_;
299   mudela_tempo_l_ = mudela_score_l_g->mudela_tempo_l_;
300
301   Cons_list<Mudela_item> items;
302   for (Cons<Mudela_item>* i = mudela_item_p_list_.head_; i; i = i->next_)
303     items.append (new Cons<Mudela_item> (i->car_, 0));
304
305   while (items.size_i ())
306     eat_voice (items);
307 }