-
Notifications
You must be signed in to change notification settings - Fork 20
/
unmo3.cpp
1370 lines (1093 loc) · 27.4 KB
/
unmo3.cpp
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//#include <emscripten.h>
//#include <windows.h>
#define OutputDebugStringA(a) (void)(a)
#include <memory.h>
#include <stdlib.h>
//#include <malloc.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "unmo3.h"
#include "spec.h"
extern unsigned char mo3[];
typedef int mo3_long;
typedef unsigned int mo3_ulong;
#define READ_CTRL_BIT(n) \
data<<=1; \
carry = (data >= (1<<(n))); \
data&=((1<<(n))-1); \
if (data==0) { \
data = *src++; \
data = (data<<1) + 1; \
carry = (data >= (1<<(n))); \
data&=((1<<(n))-1); \
}
/* length coded within control stream :
* most significant bit is 1
* than the first bit of each bits pair (noted n1),
* until second bit is 0 (noted n0)
*/
#define DECODE_CTRL_BITS { \
strLen++; \
do { \
READ_CTRL_BIT(8); \
strLen = (strLen<<1) + carry; \
READ_CTRL_BIT(8); \
}while(carry); \
}
/*
* this smart algorithm has been designed by Ian Luck
*/
unsigned char * mo3_unpack(unsigned char *src, unsigned char *dst, mo3_long size)
{
unsigned short data;
char carry; // x86 carry (used to propagate the most significant bit from one byte to another)
mo3_long strLen; // length of previous string
mo3_long strOffset; // string offset
unsigned char *string; // pointer to previous string
unsigned char *initDst;
mo3_ulong ebp, previousPtr;
mo3_ulong initSize;
data = 0L;
carry = 0;
strLen = 0;
previousPtr = 0;
initDst = dst;
initSize = size;
*dst++ = *src++; // the first byte is not compressed
size--; // bytes left to decompress
while (size>0) {
READ_CTRL_BIT(8);
if (!carry) { // a 0 ctrl bit means 'copy', not compressed byte
*dst++ = *src++;
size--;
}
else { // a 1 ctrl bit means compressed bytes are following
ebp = 0; // lenth adjustement
DECODE_CTRL_BITS; // read length, anf if strLen>3 (coded using more than 1 bits pair) also part of the offset value
strLen -=3;
if ((mo3_long)(strLen)<0) { // means LZ ptr with same previous relative LZ ptr (saved one)
strOffset = previousPtr; // restore previous Ptr
strLen++;
}
else { // LZ ptr in ctrl stream
strOffset = (strLen<<8) | *src++; // read less significant offset byte from stream
strLen = 0;
strOffset = ~strOffset;
if (strOffset<-1280)
ebp++;
ebp++; // length is always at least 1
if (strOffset<-32000)
ebp++;
previousPtr = strOffset; // save current Ptr
}
// read the next 2 bits as part of strLen
READ_CTRL_BIT(8);
strLen = (strLen<<1) + carry;
READ_CTRL_BIT(8);
strLen = (strLen<<1) + carry;
if (strLen==0) { // length do not fit in 2 bits
DECODE_CTRL_BITS; // decode length : 1 is the most significant bit,
strLen+=2; // then first bit of each bits pairs (noted n1), until n0.
}
strLen = strLen + ebp; // length adjustement
if (size>=strLen) {
string = dst + strOffset; // pointer to previous string
if(string < initDst || string >= dst)
break;
size-=strLen;
for(; strLen>0; strLen--)
*dst++ = *string++; // copies it
}
else {
// malformed stream
break;
}
} // compressed bytes
}
if ( (dst-initDst)!=initSize )
{
}
return src;
}
int stb_vorbis_decode_memory(const unsigned char *mem, int len, int *channels, int *sample_rate, short **output);
unsigned short GET_WORD(void* p)
{
return ((unsigned char*)p)[0] | (((unsigned char*)p)[1]<<8);
}
unsigned int GET_DWORD(void* p)
{
return ((unsigned char*)p)[0] | (((unsigned char*)p)[1]<<8) | (((unsigned char*)p)[2]<<16) | (((unsigned char*)p)[3]<<24);
}
bool OpenSong(SONG* song)
{
mo3_hdr* file_hdr = (mo3_hdr*)mo3;
unsigned char* ptr = (unsigned char*)(file_hdr+1);
if (file_hdr->ver<5)
return false; // we require ver to be 5.
unsigned char* data_hdr = (unsigned char*)malloc(file_hdr->hdrlen);
ptr = mo3_unpack(ptr,data_hdr,file_hdr->hdrlen);
unsigned char* hdr_ptr = data_hdr;
char* song_name = (char*)hdr_ptr;
hdr_ptr+=strlen(song_name)+1;
char* it_message = (char*)hdr_ptr;
hdr_ptr+=strlen(it_message)+1;
mo3_nfo* nfo = (mo3_nfo*)hdr_ptr;
hdr_ptr+=sizeof(mo3_nfo);
unsigned char* song_seq = hdr_ptr;
hdr_ptr += nfo->song_len;
// voice seq (for each pattern, and each channel, number of voice data) : identical voices are detected and factorized at compression.
unsigned short* voice_seq = (unsigned short*)hdr_ptr;
hdr_ptr += 2 * nfo->patterns * nfo->channels;
// pattern length table (size=nb_patt*2)
unsigned short* pattern_len = (unsigned short*)hdr_ptr;
hdr_ptr += 2*nfo->patterns;
// voice is column in a pattern
// because they are likely to repeat accros patterns
// we will bind their use to each pattern channel
// pattern is effectively VoicePtr column[channels];
// voices are still compressed using repetitions of N type,value pairs
// we fetch byte, upper 4 bits means repeat count, lower 4 bits means num of {type,data} pairs
unsigned char** voice_data = (unsigned char**)malloc(sizeof(unsigned char*)*nfo->unique_voices);
for (int i=0; i<nfo->unique_voices; i++)
{
// do we really need to parse'em all in order to just get head ptr to each voice?
// no, it is enought to get voice lengths :)
unsigned int voice_len = GET_DWORD(hdr_ptr);
hdr_ptr+=4;
voice_data[i] = hdr_ptr;
hdr_ptr += voice_len;
}
int sfx_instr = -1;
mo3_instr** instr_data = (mo3_instr**)malloc(sizeof(mo3_instr*)*nfo->instruments);
for (int i=0; i<nfo->instruments; i++)
{
char* instr_name = (char*)hdr_ptr;
hdr_ptr += strlen(instr_name)+1;
if (strcmp(instr_name,"SFX")==0)
{
sfx_instr = i;
}
if (file_hdr->ver>=5)
{
char* file_name = (char*)hdr_ptr;
hdr_ptr += strlen(file_name)+1;
}
instr_data[i] = (mo3_instr*)hdr_ptr;
hdr_ptr += sizeof(mo3_instr);
}
mo3_sample** sample_data = (mo3_sample**)malloc(sizeof(mo3_sample*)*nfo->samples);
for (int i=0; i<nfo->samples; i++)
{
char* sample_name = (char*)hdr_ptr;
hdr_ptr += strlen(sample_name)+1;
if (file_hdr->ver>=5)
{
char* file_name = (char*)hdr_ptr;
hdr_ptr += strlen(file_name)+1;
}
sample_data[i] = (mo3_sample*)hdr_ptr;
hdr_ptr += sizeof(mo3_sample);
// warning, we are overwriting unused part of decded data!
sample_data[i]->ogg_header = i;
if ( file_hdr->ver>=5 && (sample_data[i]->flags&0x5000) == 0x5000 )
{
sample_data[i]->ogg_header = GET_WORD(hdr_ptr);
hdr_ptr+=2;
}
}
// there may be some extra info between current hdr_ptr and hdr_data + file_hdr->hdrLen
// skip? ...
unsigned char* ogg_data = ptr;
short* temp_out[256]={0};
for (int i=0; i<nfo->samples; i++)
{
temp_out[i] = 0;
// sample_data[i]->audio_data = 0;
// assume everything is ogged
if (sample_data[i]->size)
{
int channels=0;
int rate=0;
short* output=0;
if (sample_data[i]->ogg_header == i)
{
sample_data[i]->ogg_data = ptr-ogg_data;
int len = stb_vorbis_decode_memory(ptr, sample_data[i]->compressed_size, &channels, &rate, &output);
if (len != sample_data[i]->size)
{
int a=0;
// problem?
}
temp_out[i] = output;
//sample_data[i]->audio_data = output;
}
else
{
// make temp alloc containing shared header and this sample data
unsigned char* ogg = (unsigned char*)malloc( sample_data[i]->ogg_header_size + sample_data[i]->compressed_size );
int j = i+(short)sample_data[i]->ogg_header;
memcpy(ogg, ogg_data + sample_data[j]->ogg_data, sample_data[i]->ogg_header_size);
memcpy(ogg+sample_data[i]->ogg_header_size, ptr, sample_data[i]->compressed_size);
int len = stb_vorbis_decode_memory(ogg, sample_data[i]->ogg_header_size + sample_data[i]->compressed_size, &channels, &rate, &output);
if (len != sample_data[i]->size)
{
int a=0;
// problem?
}
temp_out[i] = output;
//sample_data[i]->audio_data = output;
free(ogg);
}
ptr+=sample_data[i]->compressed_size;
}
}
for (int i=0; i<nfo->samples; i++)
{
sample_data[i]->audio_data = 0;
if (sample_data[i]->size)
{
sample_data[i]->audio_data = temp_out[i];
// cross-mix sample at the loop point
if (sample_data[i]->audio_data && (sample_data[i]->flags & 0x0030) == 0x0010)
{
// only cycle-loops (no bidi)
int mix_len = sample_data[i]->end - sample_data[i]->start;
mix_len = MIN(mix_len,(int)sample_data[i]->start);
mix_len = MIN(mix_len,256); // about 5ms
// [......aA|.........Bb|......]
for (int m=0; m<mix_len; m++)
{
int ia = m + sample_data[i]->start - mix_len;
int ib = m + sample_data[i]->end - mix_len;
short a = sample_data[i]->audio_data[ia];
short b = sample_data[i]->audio_data[ib];
float wa = (float)m/mix_len;
float wb = 1.0f-wa;
sample_data[i]->audio_data[ib] = (short)(wa*a+wb*b);
}
mix_len = MIN(mix_len, (int)(sample_data[i]->size - sample_data[i]->end));
// [........|Aa.........|bB....]
for (int m=0; m<mix_len; m++)
{
int ia = m + sample_data[i]->start;
int ib = m + sample_data[i]->end;
short a = sample_data[i]->audio_data[ia];
short b = sample_data[i]->audio_data[ib];
float wb = (float)m/mix_len;
float wa = 1.0f-wb;
sample_data[i]->audio_data[ib] = (short)(wa*a+wb*b);
}
}
}
}
// collect all
song->hdr = file_hdr;
song->nfo = nfo;
song->_data_hdr = data_hdr;
song->song_seq = song_seq;
song->voice_seq = voice_seq;
song->pattern_len = pattern_len;
song->voice_data = voice_data;
song->instr_data = instr_data;
song->sample_data = sample_data;
// last part is to extract SFX samples
song->sfx_instr = sfx_instr;
return true;
}
void CloseSong(SONG* song)
{
if (song->_data_hdr)
{
for (int i=0; i<song->nfo->samples; i++)
{
if (song->sample_data[i]->audio_data)
free(song->sample_data[i]->audio_data);
}
free(song->voice_data);
free(song->instr_data);
free(song->sample_data);
free(song->_data_hdr);
song->_data_hdr = 0;
}
}
// commands
struct XMChannel
{
unsigned char* voice_ptr;
int row_reps;
// main context
mo3_instr* instr;
mo3_sample* sample;
float frq;
int key;
int period;
int vol;
int pan;
int ofs;
float frq0;
int vol0;
int pan0;
// envelope
int age;
int evol0;
int evol1;
// tick effects
int vol_slide;
int tone_porta;
int slide;
int porta_target;
int vibrato;
int vibrato_memory;
int porta_memory;
int vibpos;
int Smp(float t); // <- should be optimized by storing some extra context (prev sample pos?) so no % is in use
void Evl();
};
void XMChannel::Evl()
{
if (instr && (instr->vol_envelope.flags==1) && instr->vol_envelope.nodes>0)
{
// find pair of nodes sourrounding instrument age
int i;
for (i=0; i<instr->vol_envelope.nodes; i++)
{
short t = instr->vol_envelope.node[2*i];
if (age < t)
break;
}
if (i==0)
{
short v = instr->vol_envelope.node[1];
evol0 = v;
evol1 = v;
}
else
if (i==instr->vol_envelope.nodes)
{
// stop!
instr=0;
sample=0;
}
else
{
short t0 = instr->vol_envelope.node[2*(i-1)];
short t1 = instr->vol_envelope.node[2*i];
short v0 = instr->vol_envelope.node[2*(i-1)+1];
short v1 = instr->vol_envelope.node[2*i+1];
evol0 = v0 + (age - t0)*(v1-v0) / (t1-t0);
evol1 = v0 + (age+1 - t0)*(v1-v0) / (t1-t0);
}
}
else
{
evol0 = 64;
evol1 = 64;
}
}
int XMChannel::Smp(float t)
{
unsigned int t0 = (int)floorf(t);
unsigned int t1 = t0+1;
float w0 = t1-t;
float w1 = t-t0;
if (sample->flags & 0x0010)
{
// loop enabled
if (sample->flags & 0x0020)
{
// bidi loop
if (t1>=sample->end)
{
unsigned int period = sample->end - sample->start;
unsigned int phase0 = (t0 - sample->start) % (2*period);
unsigned int phase1 = (t1 - sample->start) % (2*period);
if (phase0<period)
t0 = phase0 + sample->start;
else
t0 = sample->end - (phase0-period) - 1;
if (phase1<period)
t1 = phase1 + sample->start;
else
t1 = sample->end - (phase1-period) - 1;
}
}
else
{
// cycle loop
if (t1>=sample->end)
{
unsigned int period = sample->end - sample->start;
unsigned int phase0 = (t0 - sample->start) % period;
unsigned int phase1 = (t1 - sample->start) % period;
t0 = phase0 + sample->start;
t1 = phase1 + sample->start;
}
}
}
else
if (t1>=sample->size)
{
instr=0;
sample=0;
return 0;
}
short a0 = sample->audio_data[t0];
short a1 = sample->audio_data[t1];
return (int)(a0*w0 + a1*w1);
}
struct XMPlayer
{
XMPlayer();
~XMPlayer();
void Load();
SONG song;
int song_seq; // song position ( % song->nfo->song_len )
int pattern; // current pattern
int row; // processed in current pattern ( % song->pattern_len[pattern] )
int tick; // processed in current row ( % song->nfo->ticks_per_row )
int aud_frq; // initialized with first call to pull_audio
int aud_rem; // how many samples are already written for current tick to output audio
XMChannel chn[32*2];
void Tck();
void Row();
void Cmd(int c, unsigned char* data, int pairs);
void Mix(int chn, int frq, short* buffer, int samples);
// TEMPO:
// ticks_per_sec = tempo*24/60 (ie tempo=125,ticks_per_row=6,rows_per_pattern=64 -> tps=50, pattern_dur=64*6/50=7.68s)
// EXTERNAL CONTROLL, requires CS!
int fade_out; // -1 no fade
int fade_ofs;
int cmd_sync; // -1 nutting
int cmd_start;
int cmd_loop_a;
int cmd_loop_b;
int loop_a;
int loop_b;
unsigned char ext_vol;
unsigned int tempo;
};
static XMPlayer xm_player;
XMPlayer::~XMPlayer()
{
CloseSong(&song);
}
XMPlayer::XMPlayer()
{
song._data_hdr = 0;
song.sfx_instr = -1;
}
void XMPlayer::Load()
{
if (!song._data_hdr)
OpenSong(&song);
ext_vol = 192;
tempo = song.nfo->initial_tempo;
fade_out = -1;
cmd_sync = -1;
loop_a = 0;
loop_b = 28;
song_seq = 23; // 0
pattern = song.song_seq[song_seq];
row = 0;
tick = 0;
memset(chn,0,sizeof(XMChannel)*32*2);
for (int c=0; c<song.nfo->channels; c++)
{
chn[c].key = 0xFF;
chn[c].voice_ptr = song.voice_data[ GET_WORD(song.voice_seq + pattern*song.nfo->channels + c ) ];
}
Row();
}
void XMPlayer::Tck()
{
for (int c=0; c<song.nfo->channels; c++)
{
chn[c].vol0 = chn[c].vol;
chn[c].pan0 = chn[c].pan;
chn[c].frq0 = chn[c].frq;
}
// clear all faders
memset(chn+song.nfo->channels,0,sizeof(XMChannel)*song.nfo->channels);
tick++;
if (tick == song.nfo->ticks_per_row)
{
bool done = false;
lock_player();
if (cmd_sync==0 || cmd_sync==1 && row+1 == GET_WORD(song.pattern_len+pattern))
{
xm_player.fade_out = -1;
xm_player.fade_ofs = 0;
if (cmd_loop_a>=0 && cmd_loop_a<song.nfo->song_len)
loop_a = cmd_loop_a;
if (cmd_loop_b>loop_a && cmd_loop_b<=song.nfo->song_len)
loop_b = cmd_loop_b;
if (cmd_start>=0 && cmd_start<song.nfo->song_len)
{
done = true;
song_seq = cmd_start;
pattern = song.song_seq[song_seq];
row=0;
tick=0;
for (int c=0; c<song.nfo->channels; c++)
{
chn[c].sample=0;
chn[c].instr=0;
chn[c].row_reps=0;
chn[c].voice_ptr = song.voice_data[ GET_WORD(song.voice_seq + pattern*song.nfo->channels + c ) ];
}
Row();
}
cmd_sync = -1;
}
unlock_player();
if (!done)
{
tick=0;
row++;
if (row == GET_WORD(song.pattern_len+pattern))
{
row=0;
song_seq++;
if (song_seq == loop_b || song_seq == song.nfo->song_len)
{
song_seq = loop_a;
for (int c=0; c<song.nfo->channels; c++)
{
chn[c].sample=0;
chn[c].instr=0;
}
}
pattern = song.song_seq[song_seq];
OutputDebugStringA("\n");
for (int c=0; c<song.nfo->channels; c++)
{
chn[c].row_reps=0;
chn[c].voice_ptr = song.voice_data[ GET_WORD(song.voice_seq + pattern*song.nfo->channels + c ) ];
}
}
Row();
}
}
// calc slopes for current tick
for (int c=0; c<song.nfo->channels; c++)
{
// apply tck fx
if (tick) {
chn[c].vol += chn[c].vol_slide;
if (chn[c].sample) {
if (chn[c].tone_porta) {
int i = chn[c].tone_porta;
while (i--) {
int diff = chn[c].porta_target-chn[c].period;
if (!diff)
break;
chn[c].period += diff > 0 ?1:-1;
}
}
if (chn[c].slide) {
chn[c].period += chn[c].slide;
}
}
}
if (chn[c].sample) {
float vib = 0;
if (chn[c].vibrato) {
if (chn[c].vibrato) {
chn[c].vibpos += chn[c].vibrato & 0xf0;
vib = sin(6.28 * chn[c].vibpos/1024.0) * (chn[c].vibrato & 0xf);
}
}
chn[c].frq = 8363.0f * powf( 2.0f, (chn[c].period/16.0 - 48 + (chn[c].sample->transpose + ((int)chn[c].sample->finetune-128)/128.0f) + vib/16.0 ) / 12.0f );
}
if (chn[c].vol<0)
chn[c].vol=0;
if (chn[c].vol>64)
chn[c].vol=64;
// calc envelope slope
chn[c].age++;
chn[c].Evl();
}
}
void XMPlayer::Row()
{
// reset fx on new a row
for (int c=0; c<song.nfo->channels; c++)
{
chn[c].vol_slide = 0;
chn[c].tone_porta = 0;
chn[c].slide = 0;
chn[c].vibrato = 0;
}
char r[32];
sprintf_s(r,32,"%02d / %02d : ", pattern, row);
OutputDebugStringA(r);
// main task is to advance
for (int c=0; c<song.nfo->channels; c++)
{
if (!chn[c].voice_ptr)
{
Cmd(c,0,0); // dummy dump
continue;
}
unsigned char* ptr = chn[c].voice_ptr;
unsigned char n = *ptr;
if (n==0)
{
chn[c].voice_ptr = 0;
Cmd(c,0,0); // dummy dump
continue;
}
ptr++;
int reps = (n>>4)&0x0F;
chn[c].row_reps++;
int pairs = n&0x0F;
if (pairs)
{
Cmd(c,ptr,pairs);
ptr+=2*pairs;
}
else
{
Cmd(c,0,0); // dummy dump
}
if (chn[c].row_reps == reps)
{
chn[c].row_reps=0;
chn[c].voice_ptr = ptr;
if (*ptr==0)
chn[c].voice_ptr=0;
}
}
OutputDebugStringA("\n");
}
const static char* notes[12]=
{
"C-",
"C#",
"D-",
"D#",
"E-",
"F-",
"F#",
"G-",
"G#",
"A-",
"A#",
"B-"
};
void XMPlayer::Cmd(int c, unsigned char* data, int pairs)
{
unsigned char* ptr;
char dbg[32]="--- -- -- - -- | ";
bool has_instr = false, porta = false;
int p;
for (p=0, ptr=data; p<pairs; p++,ptr+=2)
{
switch ((Mo3CmdType)ptr[0])
{
case tone_portamento:
case vol_slide_tone_porta:
{
porta = true;
break;
}
}
}
for (p=0, ptr=data; p<pairs; p++,ptr+=2)
{
switch ((Mo3CmdType)ptr[0])
{
case note:
{
int key = ptr[1];
if (key==0xff)
{
dbg[0]='X';
dbg[1]='X';
dbg[2]='X';
}
else if (key==0xfe)
{
dbg[0]='^';
dbg[1]='^';
dbg[2]='^';
}
else if (chn[c].sample && !porta)
{
// if prev note is still alive
// we should move its context to nano-fadeout
// current params with upto 1-tick fade during mixing
int fade_ch = c+song.nfo->channels;
chn[fade_ch] = chn[c];
// here we store fade progress in output samples
chn[fade_ch].vol_slide = 0;
// clear others
chn[fade_ch].tone_porta=0;
chn[fade_ch].vibrato=0;
}
if (porta) {
chn[c].porta_target = key*16;
} else {
chn[c].key = key;
chn[c].period = key*16;
}
int o = key/12;
int n = key-12*o;
dbg[0]=notes[n][0];
dbg[1]=notes[n][1];
dbg[2]='1'+o;
dbg[6]='v';
dbg[7]='0'+chn[c].vol/10;
dbg[8]='0'+chn[c].vol%10;
break;
}
case instr:
{
has_instr = true;
int i = ptr[1];
dbg[4]='0'+(i+1)/10;
dbg[5]='0'+(i+1)%10;
chn[c].instr = song.instr_data[i];
chn[c].sample = song.sample_data[ chn[c].instr->sample_map[chn[c].key] >> 16 ];
chn[c].vol = chn[c].sample->volume;
chn[c].pan = chn[c].sample->panning;
chn[c].ofs = 0;
chn[c].vibpos = 0;
//chn[c].frq = 8363.0f * powf( 2.0f, (chn[c].period/16.0 - 48 + (chn[c].sample->transpose + ((int)chn[c].sample->finetune-128)/128.0f) ) / 12.0f );
chn[c].vol0 = chn[c].vol;
chn[c].pan0 = chn[c].pan;
chn[c].frq0 = chn[c].frq;
chn[c].age = 0;
chn[c].Evl();
dbg[6]='v';
dbg[7]='0'+chn[c].vol/10;
dbg[8]='0'+chn[c].vol%10;
break;
}
case set_vol:
{
chn[c].vol = ptr[1];
if (has_instr)
chn[c].vol0 = chn[c].vol;
dbg[6]='v';
dbg[7]='0'+chn[c].vol/10;
dbg[8]='0'+chn[c].vol%10;
break;
}
case set_pan:
{
chn[c].pan = ptr[1]; // multiplied by 4! (it's OK because it matches sample->panning)
if (has_instr)
chn[c].pan0 = chn[c].pan;
dbg[6]='p';
dbg[7]='0'+(chn[c].pan>>2)/10;
dbg[8]='0'+(chn[c].pan>>2)%10;
break;
}
case sample_offs:
{
if (has_instr)
{
// hi risk of audible pop, let's fadein
// check if sample at this offset is grater than -20db?
// then fade in
chn[c].vol0=0;
chn[c].ofs = ptr[1]*256;
}
dbg[10]='9';
dbg[12]="0123456789ABCDEF" [ ptr[1]/16 ];
dbg[13]="0123456789ABCDEF" [ ptr[1]%16 ];