| 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 |
if (typeof abc2svg == "undefined") |
| 26 |
var abc2svg = {} |
| 27 |
|
| 28 |
abc2svg.combine = { |
| 29 |
|
| 30 |
|
| 31 |
comb_v: function() { |
| 32 |
var C = abc2svg.C, |
| 33 |
abc = this |
| 34 |
|
| 35 |
|
| 36 |
function get_cmb(s) { |
| 37 |
var p, |
| 38 |
s2 = s.ts_next, |
| 39 |
i = s.p_v.id.indexOf('.') |
| 40 |
|
| 41 |
if (i >= 0) { |
| 42 |
p = s.p_v.id.slice(0, i) |
| 43 |
while (s2 && s2.time == s.time) { |
| 44 |
if (s2.p_v.id.indexOf(p) == 0) |
| 45 |
break |
| 46 |
s2 = s2.ts_next |
| 47 |
} |
| 48 |
} |
| 49 |
return s2 |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
function may_combine(s) { |
| 54 |
var nhd2, |
| 55 |
s2 = get_cmb(s) |
| 56 |
|
| 57 |
if (!s2 || (s2.type != C.NOTE && s2.type != C.REST)) |
| 58 |
return false |
| 59 |
if (s2.st != s.st |
| 60 |
|| s2.time != s.time |
| 61 |
|| s2.dur != s.dur) |
| 62 |
return false |
| 63 |
if (s.combine <= 0 |
| 64 |
&& s2.type != s.type) |
| 65 |
return false |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
if (s.a_gch && s2.a_gch) |
| 70 |
return false |
| 71 |
if (s.type == C.REST) { |
| 72 |
if (s.type == s2.type) { |
| 73 |
if (s.invis && !s2.invis) |
| 74 |
return |
| 75 |
} else if (s.combine <= 2) { |
| 76 |
return |
| 77 |
} |
| 78 |
return true |
| 79 |
} |
| 80 |
if (s2.beam_st != s.beam_st |
| 81 |
|| s2.beam_end != s.beam_end) |
| 82 |
return false; |
| 83 |
nhd2 = s2.nhd |
| 84 |
if (s.combine <= 1 |
| 85 |
&& s.notes[0].pit <= s2.notes[nhd2].pit + 1) |
| 86 |
return false |
| 87 |
return true |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
function combine_notes(s, s2) { |
| 92 |
var nhd, type, m, not |
| 93 |
|
| 94 |
|
| 95 |
for (m = 0; m <= s2.nhd; m++) { |
| 96 |
not = abc.clone(s2.notes[m]) |
| 97 |
not.noplay = true |
| 98 |
s.notes.push(not) |
| 99 |
} |
| 100 |
s.nhd = nhd = s.notes.length - 1; |
| 101 |
s.notes.sort(abc2svg.pitcmp) |
| 102 |
|
| 103 |
if (s.combine >= 3) { |
| 104 |
|
| 105 |
for (m = nhd; m > 0; m--) { |
| 106 |
if (s.notes[m].pit == s.notes[m - 1].pit |
| 107 |
&& s.notes[m].acc == s.notes[m - 1].acc) |
| 108 |
s.notes.splice(m, 1) |
| 109 |
} |
| 110 |
s.nhd = nhd = s.notes.length - 1 |
| 111 |
} |
| 112 |
|
| 113 |
s.ymx = 3 * (s.notes[nhd].pit - 18) + 4; |
| 114 |
s.ymn = 3 * (s.notes[0].pit - 18) - 4; |
| 115 |
|
| 116 |
|
| 117 |
type = s.notes[0].tie_ty |
| 118 |
if ((type & 0x07) == C.SL_AUTO) |
| 119 |
s.notes[0].tie_ty = C.SL_BELOW | (type & C.SL_DOTTED); |
| 120 |
type = s.notes[nhd].tie_ty |
| 121 |
if ((type & 0x07) == C.SL_AUTO) |
| 122 |
s.notes[nhd].tie_ty = C.SL_ABOVE | (type & C.SL_DOTTED) |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
function do_combine(s) { |
| 128 |
var s2, s3, type, i, n, sl |
| 129 |
|
| 130 |
s2 = get_cmb(s) |
| 131 |
|
| 132 |
|
| 133 |
if (!s.in_tuplet |
| 134 |
&& s2.combine != undefined && s2.combine >= 0 |
| 135 |
&& may_combine(s2)) |
| 136 |
s2 = do_combine(s2) |
| 137 |
|
| 138 |
if (s.type != s2.type) { |
| 139 |
if (s2.type != C.REST) { |
| 140 |
s2 = s; |
| 141 |
s = s2.ts_next |
| 142 |
} |
| 143 |
} else if (s.type == C.REST) { |
| 144 |
if (s.invis |
| 145 |
&& !s2.invis) |
| 146 |
delete s.invis |
| 147 |
s.multi = 0 |
| 148 |
} else { |
| 149 |
combine_notes(s, s2) |
| 150 |
if (s2.ti1) |
| 151 |
s.ti1 = true |
| 152 |
if (s2.ti2) |
| 153 |
s.ti2 = true |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
if (s2.sls) { |
| 160 |
if (s.sls) |
| 161 |
Array.prototype.push.apply(s.sls, s2.sls) |
| 162 |
else |
| 163 |
s.sls = s2.sls |
| 164 |
for (i = 0; i < s2.sls.length; i++) { |
| 165 |
sl = s2.sls[i] |
| 166 |
if (sl.se) |
| 167 |
sl.se.slsr = s |
| 168 |
sl.ty = C.SL_BELOW |
| 169 |
} |
| 170 |
delete s2.sls |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
s3 = s2.slsr |
| 176 |
if (s3) { |
| 177 |
for (i = 0; i < s3.sls.length; i++) { |
| 178 |
sl = s3.sls[i] |
| 179 |
if (sl.se == s2) |
| 180 |
sl.se = s |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
if (s2.a_gch) |
| 185 |
s.a_gch = s2.a_gch |
| 186 |
if (s2.a_dd) { |
| 187 |
if (!s.a_dd) |
| 188 |
s.a_dd = s2.a_dd |
| 189 |
else |
| 190 |
Array.prototype.push.apply(s.a_dd, s2.a_dd) |
| 191 |
} |
| 192 |
|
| 193 |
s2.play = s2.invis = true |
| 194 |
return s |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
var s, s2, g, i, r |
| 199 |
|
| 200 |
for (s = abc.get_tsfirst(); s; s = s.ts_next) { |
| 201 |
switch (s.type) { |
| 202 |
case C.REST: |
| 203 |
if (s.combine == undefined || s.combine < 0) |
| 204 |
continue |
| 205 |
if (may_combine(s)) |
| 206 |
s = do_combine(s) |
| 207 |
|
| 208 |
default: |
| 209 |
continue |
| 210 |
case C.NOTE: |
| 211 |
if (s.combine == undefined || s.combine <= 0) |
| 212 |
continue |
| 213 |
break |
| 214 |
} |
| 215 |
|
| 216 |
if (!s.beam_st) |
| 217 |
continue |
| 218 |
|
| 219 |
s2 = s |
| 220 |
while (1) { |
| 221 |
if (!may_combine(s2)) { |
| 222 |
s2 = null |
| 223 |
break |
| 224 |
} |
| 225 |
|
| 226 |
if (s2.beam_end) |
| 227 |
break |
| 228 |
do { |
| 229 |
s2 = s2.next |
| 230 |
} while (s2.type != C.NOTE && s2.type != C.REST) |
| 231 |
} |
| 232 |
if (!s2) |
| 233 |
continue |
| 234 |
s2 = s |
| 235 |
while (1) { |
| 236 |
s2 = do_combine(s2) |
| 237 |
|
| 238 |
if (s2.beam_end) |
| 239 |
break |
| 240 |
do { |
| 241 |
s2 = s2.next |
| 242 |
} while (s2.type != C.NOTE && s2.type != C.REST) |
| 243 |
} |
| 244 |
} |
| 245 |
}, |
| 246 |
|
| 247 |
do_pscom: function(of, text) { |
| 248 |
if (text.slice(0, 13) == "voicecombine ") |
| 249 |
this.set_v_param("combine", text.split(/[ \t]/)[1]) |
| 250 |
else |
| 251 |
of(text) |
| 252 |
}, |
| 253 |
|
| 254 |
new_note: function(of, gr, tp) { |
| 255 |
var curvoice = this.get_curvoice() |
| 256 |
var s = of(gr, tp) |
| 257 |
if (s && s.notes && curvoice.combine != undefined) |
| 258 |
s.combine = curvoice.combine |
| 259 |
return s |
| 260 |
}, |
| 261 |
|
| 262 |
set_stem_dir: function(of) { |
| 263 |
of(); |
| 264 |
abc2svg.combine.comb_v.call(this) |
| 265 |
}, |
| 266 |
|
| 267 |
|
| 268 |
set_vp: function(of, a) { |
| 269 |
var i, |
| 270 |
curvoice = this.get_curvoice() |
| 271 |
|
| 272 |
for (i = 0; i < a.length; i++) { |
| 273 |
if (a[i] == "combine=") { |
| 274 |
curvoice.combine = a[i + 1] |
| 275 |
break |
| 276 |
} |
| 277 |
} |
| 278 |
of(a) |
| 279 |
}, |
| 280 |
|
| 281 |
set_hooks: function(abc) { |
| 282 |
abc.do_pscom = abc2svg.combine.do_pscom.bind(abc, abc.do_pscom); |
| 283 |
abc.new_note = abc2svg.combine.new_note.bind(abc, abc.new_note); |
| 284 |
abc.set_stem_dir = abc2svg.combine.set_stem_dir.bind(abc, abc.set_stem_dir); |
| 285 |
abc.set_vp = abc2svg.combine.set_vp.bind(abc, abc.set_vp) |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
if (!abc2svg.mhooks) |
| 290 |
abc2svg.mhooks = {} |
| 291 |
abc2svg.mhooks.combine = abc2svg.combine.set_hooks |