]> git.donarmstrong.com Git - lilypond.git/blob - src/score.cc
release: 0.0.22
[lilypond.git] / src / score.cc
1 #include "tstream.hh"
2 #include "score.hh"
3 #include "sccol.hh"
4 #include "pscore.hh"
5 #include "staff.hh"
6 #include "debug.hh"
7 #include "paper.hh"
8
9
10 void
11 Score::process()
12 {
13     *mlog << "\nProcessing music ... ";
14     
15     assert (paper_p_);
16     
17     /// distribute commands to disciples
18     pscore_p_ = new PScore(paper_p_);
19     for (iter_top(staffs_,i); i.ok(); i++) {
20         i->truncate_cols(last());
21         i->set_output(pscore_p_);
22         i->process();
23     }
24
25     // do this after processing, staffs first have to generate PCols.
26     find_col(last(), false)->set_breakable();
27     do_cols();
28     calc_idealspacing();
29
30     // debugging
31     OK();
32     pscore_p_->process();    
33     *mlog << "\n";
34 }
35
36 // remove empty cols.
37 void
38 Score::clean_cols()
39 {    
40     for (iter_top(staffs_,i); i.ok(); i++)
41         i->clean_cols();
42     
43     for (iter_top(cols_,c); c.ok(); ) {
44         if (!c->pcol_l_->used()) {
45             c.del();
46         } else {
47             c->preprocess();
48             c++;
49         }
50     }
51 }
52 /*
53   this sux.  We should have Score_column create the appropriate PCol.
54   Unfortunately, PCols don't know about their position.    
55   */
56 // todo
57 PCursor<Score_column*>
58 Score::create_cols(Moment w)
59 {
60     Score_column* c1 = new Score_column(w);
61     Score_column* c2 = new Score_column(w);
62     
63     c1->musical_ = false;
64     c2->musical_ = true;
65     
66     iter_top(cols_,i);
67
68     for (; i.ok(); i++) {
69         assert(i->when() != w);
70         if (i->when() > w)
71             break;
72     }
73
74     if (!i.ok()) {
75         cols_.bottom().add(c1);
76         cols_.bottom().add(c2);
77         i = cols_.bottom();
78         i --;
79     } else {
80         i.insert(c1);
81         i.insert(c2);
82         i -= 2;
83     }
84     return i;
85 }
86
87 PCursor<Score_column*>
88 Score::find_col(Moment w,bool mus)
89 {
90     iter_top(cols_,i);
91     for (; i.ok(); i++) {
92         if (i->when() == w && i->musical_ == mus)
93             return i;
94         if (i->when() > w)
95             break;
96     }
97     i = create_cols(w);
98     if (mus)
99         i++;
100     return i;
101 }
102
103 void
104 Score::do_cols()
105 {
106     iter_top(cols_,i);
107     for (; i.ok(); i++) {
108         pscore_p_->add(i->pcol_l_);
109     }
110     clean_cols();    // can't move clean_cols() farther up.
111 }
112 Moment
113 Score::last() const
114 {    
115     Moment l = 0;
116     for (iter_top(staffs_,i); i.ok(); i++) {
117         l = l>? i->last();
118     }
119     return l;
120 }
121
122 void
123 Score::OK() const
124 {
125 #ifndef NDEBUG
126     for (iter_top(staffs_,i); i.ok(); i++) {
127         i->OK();
128         assert(i->score_l_ == this);
129     }
130     staffs_.OK();
131     cols_.OK();
132     for (iter_top(cols_,cc); cc.ok() && (cc+1).ok(); cc++) {
133         assert(cc->when() <= (cc+1)->when());
134     }
135 #endif    
136 }
137
138
139 void
140 Score::print() const
141 {
142 #ifndef NPRINT
143     mtor << "score {\n"; 
144     for (iter_top(staffs_,i); i.ok(); i++) {
145         i->print();
146     }
147     for (iter_top(cols_,i); i.ok(); i++) {
148         i->print();
149     }
150     if (pscore_p_)
151         pscore_p_->print();
152     
153     mtor << "}\n";
154 #endif
155 }
156
157 Score::Score(Paperdef*p)
158 {
159     pscore_p_=0;
160     paper_p_ = p;               // ?? safe?
161 }
162
163 Score::~Score()
164 {
165     delete pscore_p_;
166     delete paper_p_;
167 }
168
169 void
170 Score::output(String s)
171 {
172     OK();
173     if (paper_p_->outfile=="")
174         paper_p_->outfile = s;
175     
176     *mlog << "output to " << paper_p_->outfile << "...\n";
177     Tex_stream the_output(paper_p_->outfile);    
178     pscore_p_->output(the_output);
179 }
180
181
182
183 void
184 Score::add(Staff*s)
185 {
186     s->score_l_ = this;
187     staffs_.bottom().add(s);
188 }
189