]> git.donarmstrong.com Git - lilypond.git/blob - mi2mu/mudela-staff.cc
release: 0.1.24
[lilypond.git] / mi2mu / mudela-staff.cc
1 //
2 // mudela-staff.cc -- implement Mudela_staff
3 //
4 // copyright 1997 Jan Nieuwenhuizen <jan@digicash.com>
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 "mi2mu-proto.hh"
12 #include "mi2mu-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 extern Mudela_score* mudela_score_l_g;
21
22 Mudela_staff::Mudela_staff (int number_i, String copyright_str, String track_name_str, String instrument_str)
23 {
24   number_i_ = number_i;
25   copyright_str_ = copyright_str;
26   instrument_str_ = instrument_str;
27   name_str_ = track_name_str;
28   mudela_key_l_ = 0;
29   mudela_meter_l_ = 0;
30   mudela_tempo_l_ = 0;
31 }
32
33 void
34 Mudela_staff::add_item (Mudela_item* mudela_item_p)
35 {
36   mudela_item_p_list_.bottom().add (mudela_item_p);
37   if  (mudela_item_p->mudela_column_l_)
38     mudela_item_p->mudela_column_l_->add_item (mudela_item_p);
39 }
40
41 void
42 Mudela_staff::eat_voice (Link_list<Mudela_item*>& items)
43 {
44   Mudela_voice* voice_p = new Mudela_voice (this);
45   mudela_voice_p_list_.bottom().add (voice_p);
46
47   //    Moment mom = items.top()->at_mom();
48   Moment mom = 0;
49
50   for  (PCursor<Mudela_item*> i (items); i.ok();)
51     {
52       LOGOUT(DEBUG_ver) << "At: " << i->at_mom() << "; ";
53       LOGOUT(DEBUG_ver) << "dur: " << i->duration_mom() << "; ";
54       LOGOUT(DEBUG_ver) << "mom: " << mom << " -> ";
55       if  (i->at_mom() > mom)
56         {
57           Moment dur = i->at_mom() - mom;
58           // ugh, need score
59           Mudela_column* start = mudela_score_l_g->find_column_l (mom);
60           voice_p->add_item (new Mudela_skip (start, dur));
61           mom = i->at_mom();
62         }
63       if  (i->at_mom() == mom)
64         {
65           mom = i->at_mom() + i->duration_mom();
66           voice_p->add_item (i.remove_p());
67           // ugh
68         }
69       else if  (i.ok())
70         i++;
71       LOGOUT(DEBUG_ver) << "mom: " << mom << "\n";
72     }
73 }
74
75 String
76 Mudela_staff::id_str()
77 {
78   String str = name_str();
79   for  (int i = 0; i < str.length_i(); i++)
80     if  ( (!i && !isalpha (str[ i ]))
81           || !isalnum (str[ i ]))
82       * (str.ch_l() + i) = '_';
83   return str;
84 }
85
86 String
87 Mudela_staff::name_str()
88 {
89   if  (name_str_.length_i())
90     return name_str_;
91   return String ("track") + String (number_i_);
92 }
93
94 void
95 Mudela_staff::output (Mudela_stream& mudela_stream_r)
96 {
97   mudela_stream_r << "$" << id_str() << " = \\melodic";
98   mudela_stream_r <<  (mudela_voice_p_list_.size() > 1 ? "<" : "{");
99   mudela_stream_r << "\n";
100   mudela_stream_r << _("% midi copyright:") << copyright_str_ << "\n";
101   mudela_stream_r << _("% instrument:") << instrument_str_ << "\n";
102
103   if  (mudela_voice_p_list_.size() == 1)
104     mudela_voice_p_list_.top()->output (mudela_stream_r);
105   else
106     for  (PCursor<Mudela_voice*> i (mudela_voice_p_list_); i.ok(); i++)
107       {
108         mudela_stream_r << "{ ";
109         i->output (mudela_stream_r);
110         mudela_stream_r << "} ";
111       }
112
113   mudela_stream_r <<  (mudela_voice_p_list_.size() > 1 ? "\n>" : "\n}");
114   mudela_stream_r << " % " << name_str() << "\n";
115 }
116
117 void
118 Mudela_staff::output_mudela_begin_bar (Mudela_stream& mudela_stream_r, Moment now_mom, int bar_i)
119 {
120   Moment bar_mom = mudela_meter_l_->bar_mom();
121   Moment into_bar_mom = now_mom - Moment (bar_i - 1) * bar_mom;
122   if  (bar_i > 1)
123     {
124       if  (!into_bar_mom)
125         mudela_stream_r << "|\n";
126     }
127   mudela_stream_r << "% " << String_convert::i2dec_str (bar_i, 0, ' ');
128   if  (into_bar_mom)
129     mudela_stream_r << ":" << Duration_convert::dur2_str (Duration_convert::mom2_dur (into_bar_mom));
130   mudela_stream_r << "\n";
131 }
132
133
134 #if 0 // not used for now
135 void
136 Mudela_staff::output_mudela_rest (Mudela_stream& mudela_stream_r, Moment begin_mom, Moment end_mom)
137 {
138   Moment bar_mom = mudela_meter_l_->bar_mom();
139   Moment now_mom = begin_mom;
140
141   int begin_bar_i = (int) (now_mom / bar_mom) + 1;
142   int end_bar_i = (int) (end_mom / bar_mom) + 1;
143
144   if  (end_bar_i == begin_bar_i)
145     {
146       output_mudela_rest_remain (mudela_stream_r, end_mom - begin_mom);
147       return;
148     }
149
150   // multiple bars involved
151   int bar_i = (int) (now_mom / bar_mom) + 1;
152
153   //fill current bar
154   Moment begin_bar_mom = Moment (begin_bar_i - 1) * bar_mom;
155   if  (now_mom > begin_bar_mom)
156     {
157       int next_bar_i = (int) (now_mom / bar_mom) + 2;
158       Moment next_bar_mom = Moment (next_bar_i - 1) * bar_mom;
159       assert (next_bar_mom <= end_mom);
160
161       Moment remain_mom = next_bar_mom - now_mom;
162       if  (remain_mom > Moment (0))
163         {
164           output_mudela_rest_remain (mudela_stream_r, remain_mom);
165           now_mom += remain_mom;
166         }
167
168       bar_i = check_end_bar_i (now_mom, bar_i);
169     }
170
171   // fill whole bars
172   int count_i = end_bar_i - bar_i;
173   for  (int i = 0; i < count_i; i++)
174     {
175       int begin_bar_i = check_begin_bar_i (now_mom, bar_i);
176       if  (begin_bar_i)
177         output_mudela_begin_bar (mudela_stream_r, now_mom, begin_bar_i);
178       mudela_stream_r << "r1 ";
179       //        *mudela_stream_r.os_p_ << flush;
180       if  (begin_bar_i)
181         LOGOUT(NORMAL_ver) << begin_bar_i << flush;
182       bar_i = check_end_bar_i (now_mom, bar_i);
183       now_mom += bar_mom;
184     }
185
186   // use "int i" here, and gcc 2.7.2 hits internal compiler error
187   int ii = check_begin_bar_i (now_mom, bar_i);
188   if  (ii)
189     output_mudela_begin_bar (mudela_stream_r, now_mom, ii);
190
191   //    bar_i = check_end_bar_i (now_mom, bar_i);
192
193   Moment remain_mom = end_mom - Moment (end_bar_i - 1) * bar_mom;
194   if  (remain_mom > Moment (0))
195     {
196       output_mudela_rest_remain (mudela_stream_r, remain_mom);
197       now_mom += remain_mom;
198     }
199   assert (now_mom == end_mom);
200 }
201
202 void
203 Mudela_staff::output_mudela_rest_remain (Mudela_stream& mudela_stream_r, Moment mom)
204 {
205   if  (Duration_convert::no_quantify_b_s)
206     {
207       Duration dur = Duration_convert::mom2_dur (mom);
208       mudela_stream_r << "r" << dur.str() << " ";
209       //        assert (mom == dur.mom());
210       assert (mom == dur.length());
211       return;
212     }
213
214   Duration dur = Duration_convert::mom2standardised_dur (mom);
215   if  (dur.type_i_>-10)
216     mudela_stream_r << "r" << dur.str() << " ";
217 }
218 #endif
219
220
221 void
222 Mudela_staff::process()
223 {
224   /*
225      group items into voices
226      */
227
228   assert (mudela_score_l_g);
229   mudela_key_l_ = mudela_score_l_g->mudela_key_l_;
230   mudela_meter_l_ = mudela_score_l_g->mudela_meter_l_;
231   mudela_tempo_l_ = mudela_score_l_g->mudela_tempo_l_;
232
233   Link_list<Mudela_item*> items;
234   for  (PCursor<Mudela_item*> i (mudela_item_p_list_); i.ok(); i++)
235     items.bottom().add (*i);
236
237   while  (items.size())
238     eat_voice (items);
239 }