summaryrefslogtreecommitdiff
path: root/src/scommands.cc
blob: 7b8c8ed9c53bbc185f7afc9975677cdacc6fbf67 (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include "scommands.hh"
#include "debug.hh"
#include "parseconstruct.hh"

/*
  maybe it's time for a "narrowing" cursor?
  */
PCursor<Command*>
Score_commands::first(Real w)
{
    PCursor<Command*> pc(*this);    
    while (pc.ok() && pc->when < w)
	pc++;
    
     return pc;
}

PCursor<Command*>
Score_commands::last_insertion(Real w)
{    
    PCursor<Command*> pc(*this);    
    while (pc.ok() && pc->when <= w)
	pc++;
    return pc;
}

void
Score_commands::add_seq(svec<Command> com)
{
    if (!com.sz())
	return;
    Real when = com[0].when;

    PCursor<Command*> pc(last_insertion(when));
    for (int i = 0; i < com.sz(); i++) {
	Command *c = new Command(com[i]);
	assert(com[i].when == when);
	if (!pc.ok())
	    pc.add(c);
	else
	    pc.insert(c);
    }
}

void
Score_commands::set_breakable(Real when)
{
    bool found_typeset(false);
    PCursor<Command*> cc = first(when);
    for (; cc.ok() && cc->when == when; cc++) {
	if (cc->isbreak())
	    return;
	if (cc->code == TYPESET)
	    found_typeset=true;
    }

    assert(!found_typeset);
    
    svec<Command> seq;
    Command k(when);
    k.code = BREAK_PRE;
    seq.add(k);
    k.code = BREAK_MIDDLE;
    seq.add(k);
    k.code = BREAK_POST;
    seq.add(k);
    k.code = BREAK_END;
    seq.add(k);

    add_seq(seq);
}

bool
Score_commands::is_breakable(Real w)
{
    PCursor<Command*> cc = first(w);
    for (; cc.ok() && cc->when == w; cc++) {
	if (cc->isbreak())
	    return true;
    }
    return false;
}

void
Score_commands::insert_between(Command victim, PCursor<Command*> firstc,
			       PCursor<Command*> last)
{
    assert(last->when==firstc->when);
    PCursor<Command*> c(firstc+1);
    while (c != last) {  	// hmm what if !last.ok()?
	if (victim.priority > c->priority) {
	    c.insert(new Command(victim));
	    return;
	}
	c++;
    }
    last.insert(new Command(victim));    
}

void
Score_commands::add_command_to_break(Command pre, Command mid, Command post)
{
    Real w = pre.when;
    PCursor<Command*> c ( first(w)), f(c), l(c);

    while (!c->isbreak())
	c++;
    f = c++;
    while (!c->isbreak())
	c++;
    l = c++;
    
    insert_between(pre, f, l);
    f = l;
    while (!c->isbreak())
	c++;
    l = c++;    
    insert_between(mid, f, l);
    f = l;
    while (!c->isbreak())
	c++;
    l = c++;    
    insert_between(post, f, l);
    assert(l.ok() && l->when ==w && l->code == BREAK_END);
}

void
Score_commands::parser_add(Command *c)
{
    bottom().add(c);
}

void
Score_commands::process_add(Command c)
{
    bool encapsulate =false;
    Real w = c.when;
    Command pre(w);
    Command mid(w);
    Command post(w);


    if (c.code == TYPESET) {
	if (c.args[0] == "BAR") {
	    set_breakable(w);
	    encapsulate  = true;
	    mid = c;
	    pre = c;
	}
	if (c.args[0] == "METER" && is_breakable(w)) {
	    encapsulate = true;
	    mid = c;
	    pre = c;
	    post =c;
	}
    }
    
    if (encapsulate)
	add_command_to_break(pre, mid, post);    
    else {
	svec<Command> seq;
	seq.add(c);    
	add_seq(seq);
    }
}

/*
    first and last column should be breakable.
    Remove any command past the last musical column.
    */
void
Score_commands::clean(Real l)
{
    assert(l>0);
    if (!is_breakable(0.0)) {
	Command c(0.0);
	c.code = TYPESET;
	c.args.add("BAR");
	c.args.add("empty");
	process_add(c);
    }
    
    PCursor<Command*> bot(bottom());

    while (bot.ok() && bot->when > l) {
	mtor <<"removing ";
	bot->print();
	bot.del();
	bot = bottom();
    }

    if (!is_breakable(l)) {
	Command c(l);
	c.code = TYPESET;
	c.args.add("BAR");
	c.args.add("||");
	process_add(c);
    }
    OK();
}

void
Score_commands::OK() const
{
    for (PCursor<Command*> cc(*this); cc.ok() && (cc+1).ok(); cc++) {
	assert(cc->when <= (cc+1)->when);
	if (cc->when == (cc+1)->when && !cc->isbreak() && !(cc+1)->isbreak())
	    assert(cc->priority >= (cc+1)->priority);
    }
}

void
Score_commands::print() const
{
    for (PCursor<Command*> cc(*this); cc.ok() ; cc++) {
	cc->print();
    }
}

/*
  TODO
  */
Score_commands*
Score_commands::parse(Real l) const
{
    Score_commands*nc = new Score_commands;
    int beats_per_meas=4;
    Real measlen = 1.0; // 4/4 by default
    
    Real inbar=0.0;
    int barcount=0;
    Real wholes=0.0;
    Real stoppos=0.0;

    {
	Command c(0.0);
	c.code = TYPESET;
	c.args.add("BAR");
	c.args.add("empty");
	nc->process_add(c);
    }
    for (PCursor<Command*> cc(*this); cc.ok() && cc->when <= l; cc++) {
	assert (cc->code==INTERPRET);
	if (cc->args[0] == "METER") {
	    beats_per_meas = cc->args[1].value();
	    int one_beat = cc->args[2].value();
	    measlen = beats_per_meas/Real(one_beat);
	    nc->process_add(*get_meter_command(wholes, beats_per_meas, one_beat));
	}
	if (cc->args[0] == "SKIP") {
	    stoppos = wholes + cc->args[1].value() * measlen +
		cc->args[2].fvalue();
	    wholes += (measlen-inbar); // skip at least 1 measure
	    barcount++;
	    while (wholes <= stoppos) {
		nc->process_add(*get_bar_command(wholes)); // liek
		wholes += measlen;
		barcount ++;		
	    }
	    wholes = stoppos;
	    //something
	}
    }
    
    return nc;
}