]> git.donarmstrong.com Git - lilypond.git/blob - mi2mu/mudela-score.cc
2a82a630c1720061eb7eabd3db58887d053e3e59
[lilypond.git] / mi2mu / mudela-score.cc
1 //
2 // mudela-score.cc -- implement Mudela_score
3 //
4 // copyright 1997 Jan Nieuwenhuizen <jan@digicash.com>
5
6 #include "moment.hh"
7 #include "duration.hh"
8 #include "duration-convert.hh"
9 #include "mi2mu-global.hh"
10 #include "mudela-column.hh"
11 #include "mudela-item.hh"
12 #include "mudela-score.hh"
13 #include "mudela-staff.hh"
14 #include "mudela-stream.hh"
15
16 Mudela_score::Mudela_score (int format_i, int tracks_i, int tempo_i)
17 {
18     format_i_ = format_i;
19     tracks_i_ = tracks_i;
20     tempo_i_ = tempo_i;
21     mudela_column_p_list_.bottom().add (new Mudela_column (this, Moment (0)));
22 }
23
24 Mudela_score::~Mudela_score()
25 {
26 }
27
28 void 
29 Mudela_score::add_item (Mudela_item* mudela_item_p)
30 {
31     mudela_staff_p_list_.bottom()->add_item (mudela_item_p);
32 }
33
34 void
35 Mudela_score::add_staff (Mudela_staff* mudela_staff_p)
36 {
37     mudela_staff_p_list_.bottom().add (mudela_staff_p);
38 }
39
40 Mudela_column*
41 Mudela_score::mudela_column_l (Moment mom)
42 {
43     for  (PCursor<Mudela_column*> i (mudela_column_p_list_); i.ok(); i++) {
44         if  (i->at_mom() > mom) {
45             Mudela_column* p = new Mudela_column (this, mom);
46             i.insert (p);
47             return p;
48         }
49         if  (i->at_mom() == mom)
50             return *i;
51     }
52
53     Mudela_column* p = new Mudela_column (this, mom);
54     mudela_column_p_list_.bottom().add (p);
55     return p;
56 }
57
58 void
59 Mudela_score::output (String filename_str)
60 {
61     LOGOUT(NORMAL_ver) << "Lily output to " << filename_str << " ..." << endl;
62     
63     // ugh, ugly midi type 1 fix
64     if  ( (mudela_staff_p_list_.size() == 1) && !mudela_staff_p_list_.top()->number_i_)
65         mudela_staff_p_list_.top()->number_i_ = 1;
66
67     int track_i = 0;
68     Mudela_stream mudela_stream (filename_str);
69     for  (PCursor<Mudela_staff*> i (mudela_staff_p_list_); i.ok(); i++) {
70         LOGOUT(NORMAL_ver) << "track " << track_i++ << ": " << flush;
71         i->output (mudela_stream);
72         mudela_stream << "\n";
73         LOGOUT(NORMAL_ver) << endl;
74     }
75
76     mudela_stream << "\\score{\n";
77     if  (mudela_staff_p_list_.size() > 1)
78         mudela_stream << "<\n\\multi 3;\n";
79     for  (PCursor<Mudela_staff*> i (mudela_staff_p_list_); i.ok(); i++) {
80         if  ( (mudela_staff_p_list_.size() != 1) 
81             &&  (i == mudela_staff_p_list_.top()))
82             continue;
83         mudela_stream << "\\melodic{ ";
84         mudela_stream << "\\$" << i->id_str();
85         mudela_stream << " }\n";
86     }
87     if  (mudela_staff_p_list_.size() > 1)
88         mudela_stream << ">\n";
89
90     mudela_stream << "\\paper{}\n";
91
92     mudela_stream << "\\midi{ ";
93         // let's not use silly 0 track
94         mudela_staff_p_list_.bottom()->mudela_tempo_p_->output (mudela_stream);
95     mudela_stream << "}\n";
96
97     mudela_stream << "}\n";
98 }
99
100 void
101 Mudela_score::process()
102 {
103     LOGOUT(NORMAL_ver) << "\nProcessing..." << endl;
104         
105     LOGOUT(DEBUG_ver) << "columns\n";
106     for  (PCursor<Mudela_column*> i (mudela_column_p_list_); i.ok(); i++)
107         LOGOUT(DEBUG_ver) << "At: " << i->at_mom() << "\n";
108
109     settle_columns();
110     filter_tempo();
111     quantify_columns();
112     quantify_durations();
113
114     LOGOUT(NORMAL_ver) << "\nCreating voices..." << endl;
115     int track_i = 0;
116     for  (PCursor<Mudela_staff*> i (mudela_staff_p_list_); i.ok(); i++)  {
117         LOGOUT(NORMAL_ver) << "track " << track_i++ << ": " << flush;
118         i->process();
119         LOGOUT(NORMAL_ver) << endl;
120     }
121 }
122
123 void
124 Mudela_score::filter_tempo()
125 {
126     LOGOUT(NORMAL_ver) << "\nNOT Filtering tempo..." << endl;
127 }
128
129 void
130 Mudela_score::quantify_columns()
131 {
132     // ugh
133     if  (Duration_convert::no_quantify_b_s) {
134         LOGOUT(NORMAL_ver) << "\nNOT Quantifying columns..." << endl;
135         return;
136     }
137
138     LOGOUT(NORMAL_ver) << "\nQuantifying columns..." << endl;
139
140     int n = 32 >? Duration_convert::no_smaller_than_i_s;
141     Moment s = Moment (1, n);
142     Moment sh = Moment (1, 2 * n);
143     for  (int i = 0; i < column_l_array_.size(); i++) {
144 //      Moment mom = column_l_array_[ i ]->at_mom();
145 //      column_l_array_[ i ]->at_mom_ = Duration_convert::dur2_mom (dur);
146         column_l_array_[ i ]->at_mom_ =
147 //          s * (int) ( (sh + column_l_array_[ i ]->at_mom()) / s);
148             s * (int) ( (column_l_array_[ i ]->at_mom()) / s);
149         LOGOUT(NORMAL_ver) << '.';
150     } 
151     LOGOUT(NORMAL_ver) << endl;
152 }
153
154 void
155 Mudela_score::quantify_durations()
156 {
157 //    LOGOUT(NORMAL_ver) << "\nQuantifying durations..." << endl;
158 }
159
160 void
161 Mudela_score::settle_columns()
162 {
163 //    LOGOUT(NORMAL_ver) << "\nNOT Settling columns..." << endl;
164 //    return;
165     LOGOUT(NORMAL_ver) << "\nSettling columns..." << endl;
166
167     assert (!column_l_array_.size());
168     int n = mudela_column_p_list_.size();
169 // huh?
170 //    column_l_array_.set_size (n);
171     for  (PCursor<Mudela_column*> i (mudela_column_p_list_); i.ok(); i++)
172         column_l_array_.push (*i);
173
174     int start_i = 0;
175     int end_i = 0;
176     Moment start_mom = 0;
177     Duration smallest_dur;
178     smallest_dur.type_i_ =  64;
179     Moment const noise_mom = Duration_convert::dur2_mom (smallest_dur)
180         / Moment (2);
181     for  (int i = 0; i < n; i++) {
182         if  (!start_i) {
183             start_i = end_i = i;
184             start_mom = column_l_array_[ i ]->at_mom();
185             continue;
186         }
187
188         // find all columns within noise's distance
189         while  ( (i < n)
190             &&  (column_l_array_[ i ]->at_mom() - start_mom < noise_mom))
191             end_i = ++i;
192
193         // bluntly set all to time of first in group
194         for  (int j = start_i; j < end_i; j++)
195             column_l_array_[ j ]->at_mom_ = start_mom;
196
197         start_i = end_i = 0;
198     }
199 }
200