summaryrefslogtreecommitdiff
path: root/staff.cc
blob: 0827c953f015cc38bec5910e35c2abf0ef22beca (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "staff.hh"
#include "stcol.hh"
#include "sccol.hh"
#include "debug.hh"


void
Staff::clean_cols()
{
    PCursor<Staff_column *> stc(cols);
    for(; stc.ok(); ){
	if (!stc->score_column->used())
	    stc.del();
	else
	    stc++;
    }
}

Staff_column *
Staff::get_col(Mtime w, bool mus)
{
    Score_column* sc = score_->find_col(w,mus);
    assert(sc->when == w);
    PCursor<Staff_column *> stc(cols);
    for (; stc.ok(); stc++) {
	if (*sc  < *stc->score_column)
	    break;
	if (sc == stc->score_column)
	    return stc;
    }
    Staff_column* newst = create_col(sc);

    if (!stc.ok()) {
	cols.bottom().add(newst);
	return cols.bottom();
    }
    
    if (mus) {
	stc.insert(newst);
	return newst;
    }

    if ((stc-1)->when() == newst->when()) {
	stc--;
    }

    stc.insert(newst);
    
    return newst;
}


void
Staff::add_voice(Voice *v)
{
    voices.bottom().add(v);
}

/*
    put all stuff grouped vertically in the Staff_cols
    */
void
Staff::setup_staffcols()
{
    
    for (PCursor<Voice*> vc(voices); vc.ok(); vc++) {

	Mtime now = vc->start;
	for (PCursor<Voice_element *> ve(vc->elts); ve.ok(); ve++) {

	    Staff_column *sc=get_col(now,true);
	    sc->add(ve);
	    now += ve->duration;	    
	}	
    }

    for (PCursor<Command*> cc(commands); cc.ok(); cc++) {
	Staff_column *sc=get_col(cc->when,false);
	sc->s_commands.add(cc);
    }
}

/// merge commands from score
void
Staff::add_commands(PointerList<Command*> const &cl)
{
    PCursor<Command*> score_c(cl);
    PCursor<Command*> cc(commands);
    
    while (score_c.ok()) {
	while (cc.ok() && cc->when <= score_c->when)
	    cc++;
	
	Command*nc = new Command (*(* score_c));
	if (cc.ok()) {
	    // cc->when > score_c->when
	    cc.insert( nc );
	} else {
	    commands.bottom().add( nc);
	    cc = commands.bottom();
	}
	score_c++;
    }

    // now integrate break commands with other commands.
    // maybe do this in derived functions.
}

void
Staff::process()
{
    setup_staffcols();
    OK();
    for (PCursor<Staff_column*> sc(cols); sc.ok(); sc++) {
	sc->process_commands();
	sc->process_requests();
    }
    grant_requests();
}

void
Staff::OK() const
{
#ifndef NDEBUG
    cols.OK();
    commands.OK();
    voices.OK();
    assert(score_);    
#endif    
}


Mtime
Staff::last() const {
    Mtime l = 0.0;
    for (PCursor<Voice*> vc(voices); vc.ok(); vc++) {
	l = MAX(l, vc->last());
    }
    return l;
}


void
Staff::print() const
{
        #ifndef NPRINT

    mtor << "Staff {\n";
    for (PCursor<Voice*> vc(voices); vc.ok(); vc++) {
	vc->print();
	
    }
    mtor <<"}\n";
    #endif
}