Debian Codegraph @

src/engine/song.cpp

raw
1 /**
2 * Furnace Tracker - multi-system chiptune tracker
3 * Copyright (C) 2021-2025 tildearrow and contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "song.h"
21 #include "../ta-log.h"
22
23
24 bool DivSubSong::walk(int& loopOrder, int& loopRow, int& loopEnd, int chans, int jumpTreatment, int ignoreJumpAtEnd, int firstPat) {
25 loopOrder=0;
26 loopRow=0;
27 loopEnd=-1;
28 int nextOrder=-1;
29 int nextRow=0;
30 int effectVal=0;
31 int lastSuspectedLoopEnd=-1;
32 DivPattern* subPat[DIV_MAX_CHANS];
33 unsigned char wsWalked[8192];
34 memset(wsWalked,0,8192);
35 if (firstPat>0) {
36 memset(wsWalked,255,32*firstPat);
37 }
38 for (int i=firstPat; i<ordersLen; i++) {
39 for (int j=0; j<chans; j++) {
40 subPat[j]=pat[j].getPattern(orders.ord[j][i],false);
41 }
42 if (i>lastSuspectedLoopEnd) {
43 lastSuspectedLoopEnd=i;
44 }
45 for (int j=nextRow; j<patLen; j++) {
46 nextRow=0;
47 bool changingOrder=false;
48 bool jumpingOrder=false;
49 if (wsWalked[((i<<5)+(j>>3))&8191]&(1<<(j&7))) {
50 loopOrder=i;
51 loopRow=j;
52 loopEnd=lastSuspectedLoopEnd;
53 return true;
54 }
55 for (int k=0; k<chans; k++) {
56 for (int l=0; l<pat[k].effectCols; l++) {
57 effectVal=subPat[k]->data[j][5+(l<<1)];
58 if (effectVal<0) effectVal=0;
59 if (subPat[k]->data[j][4+(l<<1)]==0x0d) {
60 if (jumpTreatment==2) {
61 if ((i<ordersLen-1 || !ignoreJumpAtEnd)) {
62 nextOrder=i+1;
63 nextRow=effectVal;
64 jumpingOrder=true;
65 }
66 } else if (jumpTreatment==1) {
67 if (nextOrder==-1 && (i<ordersLen-1 || !ignoreJumpAtEnd)) {
68 nextOrder=i+1;
69 nextRow=effectVal;
70 jumpingOrder=true;
71 }
72 } else {
73 if ((i<ordersLen-1 || !ignoreJumpAtEnd)) {
74 if (!changingOrder) {
75 nextOrder=i+1;
76 }
77 jumpingOrder=true;
78 nextRow=effectVal;
79 }
80 }
81 } else if (subPat[k]->data[j][4+(l<<1)]==0x0b) {
82 if (nextOrder==-1 || jumpTreatment==0) {
83 nextOrder=effectVal;
84 if (jumpTreatment==1 || jumpTreatment==2 || !jumpingOrder) {
85 nextRow=0;
86 }
87 changingOrder=true;
88 }
89 }
90 }
91 }
92
93 wsWalked[((i<<5)+(j>>3))&8191]|=1<<(j&7);
94
95 if (nextOrder!=-1) {
96 i=nextOrder-1;
97 nextOrder=-1;
98 break;
99 }
100 }
101 }
102 return false;
103 }
104
105 double calcRowLenInSeconds(const DivGroovePattern& speeds, float hz, int vN, int vD, int timeBaseFromSong) {
106 double hl=1; //count for 1 row
107 if (hl<=0.0) hl=4.0;
108 double timeBase=timeBaseFromSong+1;
109 double speedSum=0;
110 for (int i=0; i<MIN(16,speeds.len); i++) {
111 speedSum+=speeds.val[i];
112 }
113 speedSum/=MAX(1,speeds.len);
114 if (timeBase<1.0) timeBase=1.0;
115 if (speedSum<1.0) speedSum=1.0;
116 if (vD<1) vD=1;
117 return 1.0/((60.0*hz/(timeBase*hl*speedSum))*(double)vN/(double)vD/60.0);
118 }
119
120 void DivSubSong::findLength(int loopOrder, int loopRow, double fadeoutLen, int& rowsForFadeout, bool& hasFFxx, std::vector<int>& orders_vec, std::vector<DivGroovePattern>& grooves, int& length, int chans, int jumpTreatment, int ignoreJumpAtEnd, int firstPat) {
121 length=0;
122 hasFFxx=false;
123 rowsForFadeout=0;
124
125 float secondsPerThisRow=0.0f;
126
127 DivGroovePattern curSpeeds=speeds; //simulate that we are playing the song, track all speed/BPM/tempo/engine rate changes
128 short curVirtualTempoN=virtualTempoN;
129 short curVirtualTempoD=virtualTempoD;
130 float curHz=hz;
131 double curDivider=(double)timeBase;
132
133 double curLen=0.0; //how many seconds passed since the start of song
134
135 int nextOrder=-1;
136 int nextRow=0;
137 int effectVal=0;
138 int lastSuspectedLoopEnd=-1;
139 DivPattern* subPat[DIV_MAX_CHANS];
140 unsigned char wsWalked[8192];
141 memset(wsWalked,0,8192);
142 if (firstPat>0) {
143 memset(wsWalked,255,32*firstPat);
144 }
145 for (int i=firstPat; i<ordersLen; i++) {
146 bool jumped=false;
147
148 for (int j=0; j<chans; j++) {
149 subPat[j]=pat[j].getPattern(orders.ord[j][i],false);
150 }
151 if (i>lastSuspectedLoopEnd) {
152 lastSuspectedLoopEnd=i;
153 }
154 for (int j=nextRow; j<patLen; j++) {
155 nextRow=0;
156 bool changingOrder=false;
157 bool jumpingOrder=false;
158 if (wsWalked[((i<<5)+(j>>3))&8191]&(1<<(j&7))) {
159 return;
160 }
161 for (int k=0; k<chans; k++) {
162 for (int l=0; l<pat[k].effectCols; l++) {
163 effectVal=subPat[k]->data[j][5+(l<<1)];
164 if (effectVal<0) effectVal=0;
165
166 if (subPat[k]->data[j][4+(l<<1)]==0xff) {
167 hasFFxx=true;
168
169 // FFxx makes YOU SHALL NOT PASS!!! move
170 orders_vec.push_back(j+1); // order len
171 length+=j+1; // add length of order to song length
172
173 return;
174 }
175
176 switch (subPat[k]->data[j][4+(l<<1)]) {
177 case 0x09: { // select groove pattern/speed 1
178 if (grooves.empty()) {
179 if (effectVal>0) curSpeeds.val[0]=effectVal;
180 } else {
181 if (effectVal<(short)grooves.size()) {
182 curSpeeds=grooves[effectVal];
183 //curSpeed=0;
184 }
185 }
186 break;
187 }
188 case 0x0f: { // speed 1/speed 2
189 if (curSpeeds.len==2 && grooves.empty()) {
190 if (effectVal>0) curSpeeds.val[1]=effectVal;
191 } else {
192 if (effectVal>0) curSpeeds.val[0]=effectVal;
193 }
194 break;
195 }
196 case 0xfd: { // virtual tempo num
197 if (effectVal>0) curVirtualTempoN=effectVal;
198 break;
199 }
200 case 0xfe: { // virtual tempo den
201 if (effectVal>0) curVirtualTempoD=effectVal;
202 break;
203 }
204 case 0xf0: { // set Hz by tempo (set bpm)
205 curDivider=(double)effectVal*2.0/5.0;
206 if (curDivider<1) curDivider=1;
207 break;
208 }
209 }
210
211 if (subPat[k]->data[j][4+(l<<1)]==0x0d) {
212 if (jumpTreatment==2) {
213 if ((i<ordersLen-1 || !ignoreJumpAtEnd)) {
214 nextOrder=i+1;
215 nextRow=effectVal;
216 jumpingOrder=true;
217 }
218 } else if (jumpTreatment==1) {
219 if (nextOrder==-1 && (i<ordersLen-1 || !ignoreJumpAtEnd)) {
220 nextOrder=i+1;
221 nextRow=effectVal;
222 jumpingOrder=true;
223 }
224 } else {
225 if ((i<ordersLen-1 || !ignoreJumpAtEnd)) {
226 if (!changingOrder) {
227 nextOrder=i+1;
228 }
229 jumpingOrder=true;
230 nextRow=effectVal;
231 }
232 }
233 } else if (subPat[k]->data[j][4+(l<<1)]==0x0b) {
234 if (nextOrder==-1 || jumpTreatment==0) {
235 nextOrder=effectVal;
236 if (jumpTreatment==1 || jumpTreatment==2 || !jumpingOrder) {
237 nextRow=0;
238 }
239 changingOrder=true;
240 }
241 }
242 }
243 }
244
245 if (i>loopOrder || (i==loopOrder && j>loopRow)) {
246 // we count each row fadeout lasts. When our time is greater than fadeout length we successfully counted the number of fadeout rows
247 if (curLen<=fadeoutLen && fadeoutLen>0.0) {
248 secondsPerThisRow=calcRowLenInSeconds(speeds,curHz,curVirtualTempoN,curVirtualTempoD,curDivider);
249 curLen+=secondsPerThisRow;
250 rowsForFadeout++;
251 }
252 }
253
254 wsWalked[((i<<5)+(j>>3))&8191]|=1<<(j&7);
255
256 if (nextOrder!=-1) {
257 i=nextOrder-1;
258 orders_vec.push_back(j+1); // order len
259 length+=j+1; // add length of order to song length
260 jumped=true;
261 nextOrder=-1;
262 break;
263 }
264 }
265 if (!jumped) { // if no jump occured we add full pattern length
266 orders_vec.push_back(patLen); // order len
267 length+=patLen; // add length of order to song length
268 }
269 }
270 }
271
272 void DivSubSong::clearData() {
273 for (int i=0; i<DIV_MAX_CHANS; i++) {
274 pat[i].wipePatterns();
275 }
276
277 memset(orders.ord,0,DIV_MAX_CHANS*DIV_MAX_PATTERNS);
278 ordersLen=1;
279 }
280
281 void DivSubSong::optimizePatterns() {
282 for (int i=0; i<DIV_MAX_CHANS; i++) {
283 logD("optimizing channel %d...",i);
284 std::vector<std::pair<int,int>> clearOuts=pat[i].optimize();
285 for (auto& j: clearOuts) {
286 for (int k=0; k<DIV_MAX_PATTERNS; k++) {
287 if (orders.ord[i][k]==j.first) {
288 orders.ord[i][k]=j.second;
289 }
290 }
291 }
292 }
293 }
294
295 void DivSubSong::rearrangePatterns() {
296 for (int i=0; i<DIV_MAX_CHANS; i++) {
297 logD("re-arranging channel %d...",i);
298 std::vector<std::pair<int,int>> clearOuts=pat[i].rearrange();
299 for (auto& j: clearOuts) {
300 for (int k=0; k<DIV_MAX_PATTERNS; k++) {
301 if (orders.ord[i][k]==j.first) {
302 orders.ord[i][k]=j.second;
303 }
304 }
305 }
306 }
307 }
308
309 void DivSubSong::sortOrders() {
310 for (int i=0; i<DIV_MAX_CHANS; i++) {
311 DivPattern* patPointer[DIV_MAX_PATTERNS];
312 unsigned char orderMap[DIV_MAX_PATTERNS];
313 bool seen[DIV_MAX_PATTERNS];
314 int orderMapLen=0;
315
316 memcpy(patPointer,pat[i].data,DIV_MAX_PATTERNS*sizeof(void*));
317 memset(orderMap,0,DIV_MAX_PATTERNS);
318 memset(seen,0,DIV_MAX_PATTERNS*sizeof(bool));
319
320 // 1. sort orders
321 for (int j=0; j<ordersLen; j++) {
322 if (!seen[orders.ord[i][j]]) {
323 orderMap[orders.ord[i][j]]=orderMapLen++;
324 seen[orders.ord[i][j]]=true;
325 }
326 }
327
328 // 2. populate the rest
329 for (int j=0; j<DIV_MAX_PATTERNS; j++) {
330 if (!seen[j]) orderMap[j]=orderMapLen++;
331 }
332
333 // 3. swap pattern pointers
334 for (int j=0; j<DIV_MAX_PATTERNS; j++) {
335 pat[i].data[orderMap[j]]=patPointer[j];
336 }
337
338 // 4. swap orders
339 for (int j=0; j<ordersLen; j++) {
340 orders.ord[i][j]=orderMap[orders.ord[i][j]];
341 }
342 }
343 }
344
345 void DivSubSong::makePatUnique() {
346 for (int i=0; i<DIV_MAX_CHANS; i++) {
347 logD("making channel %d unique...",i);
348 bool seen[DIV_MAX_PATTERNS];
349 bool used[DIV_MAX_PATTERNS];
350
351 memset(seen,0,DIV_MAX_PATTERNS*sizeof(bool));
352 memset(used,0,DIV_MAX_PATTERNS*sizeof(bool));
353
354 // 1. populate used patterns
355 for (int j=0; j<ordersLen; j++) {
356 used[orders.ord[i][j]]=true;
357 }
358
359 // 2. make patterns unique
360 for (int j=0; j<ordersLen; j++) {
361 if (seen[orders.ord[i][j]]) {
362 for (int k=0; k<DIV_MAX_PATTERNS; k++) {
363 if (!used[k]) {
364 // copy here
365 DivPattern* dest=pat[i].getPattern(k,true);
366 DivPattern* src=pat[i].getPattern(orders.ord[i][j],false);
367 src->copyOn(dest);
368 used[k]=true;
369 orders.ord[i][j]=k;
370 break;
371 }
372 }
373 } else {
374 seen[orders.ord[i][j]]=true;
375 }
376 }
377 }
378 }
379
380 void DivSong::findSubSongs(int chans) {
381 std::vector<DivSubSong*> newSubSongs;
382 for (DivSubSong* i: subsong) {
383 std::vector<int> subSongStart;
384 std::vector<int> subSongEnd;
385 int loopOrder=0;
386 int loopRow=0;
387 int loopEnd=-1;
388 int curStart=-1;
389
390 // find possible subsongs
391 logD("finding subsongs...");
392 while (++curStart<i->ordersLen) {
393 if (!i->walk(loopOrder,loopRow,loopEnd,chans,jumpTreatment,ignoreJumpAtEnd,curStart)) break;
394
395 // make sure we don't pick the same range twice
396 if (!subSongEnd.empty()) {
397 if (subSongEnd.back()==loopEnd) continue;
398 }
399
400 logV("found a subsong: %d-%d",curStart,loopEnd);
401 subSongStart.push_back(curStart);
402 subSongEnd.push_back(loopEnd);
403 }
404
405 // if this is the only song, quit
406 if (subSongStart.size()<2) {
407 subSongStart.clear();
408 subSongEnd.clear();
409 newSubSongs.clear();
410 continue;
411 }
412
413 // now copy the song
414 bool isTouched[DIV_MAX_CHANS][DIV_MAX_PATTERNS];
415 memset(isTouched,0,DIV_MAX_CHANS*DIV_MAX_PATTERNS*sizeof(bool));
416 for (size_t j=1; j<subSongStart.size(); j++) {
417 bool isUsed[DIV_MAX_CHANS][DIV_MAX_PATTERNS];
418 int start=subSongStart[j];
419 int end=subSongEnd[j];
420
421 DivSubSong* theCopy=new DivSubSong;
422
423 theCopy->name=i->name;
424 theCopy->notes=i->notes;
425 theCopy->hilightA=i->hilightA;
426 theCopy->hilightB=i->hilightB;
427 theCopy->timeBase=i->timeBase;
428 theCopy->arpLen=i->arpLen;
429 theCopy->speeds=i->speeds;
430 theCopy->virtualTempoN=i->virtualTempoN;
431 theCopy->virtualTempoD=i->virtualTempoD;
432 theCopy->hz=i->hz;
433 theCopy->patLen=i->patLen;
434
435 // copy orders
436 memset(isUsed,0,DIV_MAX_CHANS*DIV_MAX_PATTERNS*sizeof(bool));
437 for (int k=start, kIndex=0; k<=end; k++, kIndex++) {
438 for (int l=0; l<DIV_MAX_CHANS; l++) {
439 theCopy->orders.ord[l][kIndex]=i->orders.ord[l][k];
440 isUsed[l][i->orders.ord[l][k]]=true;
441 isTouched[l][i->orders.ord[l][k]]=true;
442 }
443 }
444 theCopy->ordersLen=end-start+1;
445
446 memcpy(theCopy->chanShow,i->chanShow,DIV_MAX_CHANS*sizeof(bool));
447 memcpy(theCopy->chanShowChanOsc,i->chanShowChanOsc,DIV_MAX_CHANS*sizeof(bool));
448 memcpy(theCopy->chanCollapse,i->chanCollapse,DIV_MAX_CHANS);
449
450 for (int k=0; k<DIV_MAX_CHANS; k++) {
451 theCopy->chanName[k]=i->chanName[k];
452 theCopy->chanShortName[j]=i->chanShortName[k];
453
454 theCopy->pat[k].effectCols=i->pat[k].effectCols;
455
456 for (int l=0; l<DIV_MAX_PATTERNS; l++) {
457 if (i->pat[k].data[l]==NULL) continue;
458 if (!isUsed[k][l]) continue;
459 DivPattern* origPat=i->pat[k].getPattern(l,false);
460 DivPattern* copyPat=theCopy->pat[k].getPattern(l,true);
461 origPat->copyOn(copyPat);
462 }
463 }
464
465 newSubSongs.push_back(theCopy);
466 }
467
468 // and cut this one
469 i->ordersLen=subSongEnd[0]+1;
470
471 // remove taken patterns as well, as long as they're not used in the original subsong
472 // first unmark patterns which are used
473 for (int j=subSongStart[0]; j<=subSongEnd[0]; j++) {
474 for (int k=0; k<DIV_MAX_CHANS; k++) {
475 isTouched[k][i->orders.ord[k][j]]=false;
476 }
477 }
478
479 // then remove the rest
480 for (int j=0; j<DIV_MAX_CHANS; j++) {
481 for (int k=0; k<DIV_MAX_PATTERNS; k++) {
482 if (isTouched[j][k]) {
483 if (i->pat[j].data[k]!=NULL) {
484 delete i->pat[j].data[k];
485 i->pat[j].data[k]=NULL;
486 }
487 }
488 }
489 }
490 }
491
492 // append every subsong we found
493 for (DivSubSong* i: newSubSongs) {
494 subsong.push_back(i);
495 }
496
497 }
498
499 void DivSong::clearSongData() {
500 for (DivSubSong* i: subsong) {
501 i->clearData();
502 delete i;
503 }
504 subsong.clear();
505 subsong.push_back(new DivSubSong);
506 }
507
508 void DivSong::clearInstruments() {
509 for (DivInstrument* i: ins) {
510 delete i;
511 }
512 ins.clear();
513 insLen=0;
514 }
515
516 void DivSong::clearWavetables() {
517 for (DivWavetable* i: wave) {
518 delete i;
519 }
520 wave.clear();
521 waveLen=0;
522 }
523
524 void DivSong::clearSamples() {
525 for (DivSample* i: sample) {
526 delete i;
527 }
528 sample.clear();
529 sampleLen=0;
530 }
531
532 void DivSong::unload() {
533 for (DivInstrument* i: ins) {
534 delete i;
535 }
536 ins.clear();
537 insLen=0;
538
539 for (DivWavetable* i: wave) {
540 delete i;
541 }
542 wave.clear();
543 waveLen=0;
544
545 for (DivSample* i: sample) {
546 delete i;
547 }
548 sample.clear();
549 sampleLen=0;
550
551 for (DivSubSong* i: subsong) {
552 i->clearData();
553 delete i;
554 }
555 subsong.clear();
556 }