-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
jar_mod.h
1596 lines (1347 loc) · 50.8 KB
/
jar_mod.h
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
// jar_mod.h - v0.01 - public domain C0 - Joshua Reisenauer
//
// HISTORY:
//
// v0.01 2016-03-12 Setup
//
//
// USAGE:
//
// In ONE source file, put:
//
// #define JAR_MOD_IMPLEMENTATION
// #include "jar_mod.h"
//
// Other source files should just include jar_mod.h
//
// SAMPLE CODE:
// jar_mod_context_t modctx;
// short samplebuff[4096];
// bool bufferFull = false;
// int intro_load(void)
// {
// jar_mod_init(&modctx);
// jar_mod_load_file(&modctx, "file.mod");
// return 1;
// }
// int intro_unload(void)
// {
// jar_mod_unload(&modctx);
// return 1;
// }
// int intro_tick(long counter)
// {
// if(!bufferFull)
// {
// jar_mod_fillbuffer(&modctx, samplebuff, 4096, 0);
// bufferFull=true;
// }
// if(IsKeyDown(KEY_ENTER))
// return 1;
// return 0;
// }
//
//
// LISCENSE:
//
// Written by: Jean-François DEL NERO (http://hxc2001.com/) <Email : jeanfrancoisdelnero <> free.fr>
// Adapted to jar_mod by: Joshua Adam Reisenauer <[email protected]>
// This program is free software. It comes without any warranty, to the
// extent permitted by applicable law. You can redistribute it and/or
// modify it under the terms of the Do What The Fuck You Want To Public
// License, Version 2, as published by Sam Hocevar. See
// http://sam.zoy.org/wtfpl/COPYING for more details.
///////////////////////////////////////////////////////////////////////////////////
// HxCMOD Core API:
// -------------------------------------------
// int jar_mod_init(jar_mod_context_t * modctx)
//
// - Initialize the jar_mod_context_t buffer. Must be called before doing anything else.
// Return 1 if success. 0 in case of error.
// -------------------------------------------
// mulong jar_mod_load_file(jar_mod_context_t * modctx, const char* filename)
//
// - "Load" a MOD from file, context must already be initialized.
// Return size of file in bytes.
// -------------------------------------------
// void jar_mod_fillbuffer( jar_mod_context_t * modctx, short * outbuffer, unsigned long nbsample, jar_mod_tracker_buffer_state * trkbuf )
//
// - Generate and return the next samples chunk to outbuffer.
// nbsample specify the number of stereo 16bits samples you want.
// The output format is by default signed 48000Hz 16-bit Stereo PCM samples, otherwise it is changed with jar_mod_setcfg().
// The output buffer size in bytes must be equal to ( nbsample * 2 * channels ).
// The optional trkbuf parameter can be used to get detailed status of the player. Put NULL/0 is unused.
// -------------------------------------------
// void jar_mod_unload( jar_mod_context_t * modctx )
// - "Unload" / clear the player status.
// -------------------------------------------
///////////////////////////////////////////////////////////////////////////////////
#ifndef INCLUDE_JAR_MOD_H
#define INCLUDE_JAR_MOD_H
// Allow custom memory allocators
#ifndef JARMOD_MALLOC
#define JARMOD_MALLOC(sz) malloc(sz)
#endif
#ifndef JARMOD_FREE
#define JARMOD_FREE(p) free(p)
#endif
// Basic type
typedef unsigned char muchar;
typedef unsigned short muint;
typedef short mint;
typedef unsigned long mulong;
#define NUMMAXCHANNELS 32
#define MAXNOTES 12*12
#define DEFAULT_SAMPLE_RATE 48000
//
// MOD file structures
//
#pragma pack(1)
typedef struct {
muchar name[22];
muint length;
muchar finetune;
muchar volume;
muint reppnt;
muint replen;
} sample;
typedef struct {
muchar sampperiod;
muchar period;
muchar sampeffect;
muchar effect;
} note;
typedef struct {
muchar title[20];
sample samples[31];
muchar length; // length of tablepos
muchar protracker;
muchar patterntable[128];
muchar signature[4];
muchar speed;
} module;
#pragma pack()
//
// HxCMod Internal structures
//
typedef struct {
char* sampdata;
muint sampnum;
muint length;
muint reppnt;
muint replen;
mulong samppos;
muint period;
muchar volume;
mulong ticks;
muchar effect;
muchar parameffect;
muint effect_code;
mint decalperiod;
mint portaspeed;
mint portaperiod;
mint vibraperiod;
mint Arpperiods[3];
muchar ArpIndex;
mint oldk;
muchar volumeslide;
muchar vibraparam;
muchar vibrapointeur;
muchar finetune;
muchar cut_param;
muint patternloopcnt;
muint patternloopstartpoint;
} channel;
typedef struct {
module song;
char* sampledata[31];
note* patterndata[128];
mulong playrate;
muint tablepos;
muint patternpos;
muint patterndelay;
muint jump_loop_effect;
muchar bpm;
mulong patternticks;
mulong patterntickse;
mulong patternticksaim;
mulong sampleticksconst;
mulong samplenb;
channel channels[NUMMAXCHANNELS];
muint number_of_channels;
muint fullperiod[MAXNOTES * 8];
muint mod_loaded;
mint last_r_sample;
mint last_l_sample;
mint stereo;
mint stereo_separation;
mint bits;
mint filter;
muchar *modfile; // the raw mod file
mulong modfilesize;
muint loopcount;
} jar_mod_context_t;
//
// Player states structures
//
typedef struct track_state_
{
unsigned char instrument_number;
unsigned short cur_period;
unsigned char cur_volume;
unsigned short cur_effect;
unsigned short cur_parameffect;
}track_state;
typedef struct tracker_state_
{
int number_of_tracks;
int bpm;
int speed;
int cur_pattern;
int cur_pattern_pos;
int cur_pattern_table_pos;
unsigned int buf_index;
track_state tracks[32];
}tracker_state;
typedef struct tracker_state_instrument_
{
char name[22];
int active;
}tracker_state_instrument;
typedef struct jar_mod_tracker_buffer_state_
{
int nb_max_of_state;
int nb_of_state;
int cur_rd_index;
int sample_step;
char name[64];
tracker_state_instrument instruments[31];
tracker_state * track_state_buf;
}jar_mod_tracker_buffer_state;
#ifdef __cplusplus
extern "C" {
#endif
bool jar_mod_init(jar_mod_context_t * modctx);
bool jar_mod_setcfg(jar_mod_context_t * modctx, int samplerate, int bits, int stereo, int stereo_separation, int filter);
void jar_mod_fillbuffer(jar_mod_context_t * modctx, short * outbuffer, unsigned long nbsample, jar_mod_tracker_buffer_state * trkbuf);
void jar_mod_unload(jar_mod_context_t * modctx);
mulong jar_mod_load_file(jar_mod_context_t * modctx, const char* filename);
mulong jar_mod_current_samples(jar_mod_context_t * modctx);
mulong jar_mod_max_samples(jar_mod_context_t * modctx);
void jar_mod_seek_start(jar_mod_context_t * ctx);
#ifdef __cplusplus
}
#endif
//--------------------------------------------------------------------
//-------------------------------------------------------------------------------
#ifdef JAR_MOD_IMPLEMENTATION
#include <stdio.h>
#include <stdlib.h>
//#include <stdbool.h>
// Effects list
#define EFFECT_ARPEGGIO 0x0 // Supported
#define EFFECT_PORTAMENTO_UP 0x1 // Supported
#define EFFECT_PORTAMENTO_DOWN 0x2 // Supported
#define EFFECT_TONE_PORTAMENTO 0x3 // Supported
#define EFFECT_VIBRATO 0x4 // Supported
#define EFFECT_VOLSLIDE_TONEPORTA 0x5 // Supported
#define EFFECT_VOLSLIDE_VIBRATO 0x6 // Supported
#define EFFECT_VOLSLIDE_TREMOLO 0x7 // - TO BE DONE -
#define EFFECT_SET_PANNING 0x8 // - TO BE DONE -
#define EFFECT_SET_OFFSET 0x9 // Supported
#define EFFECT_VOLUME_SLIDE 0xA // Supported
#define EFFECT_JUMP_POSITION 0xB // Supported
#define EFFECT_SET_VOLUME 0xC // Supported
#define EFFECT_PATTERN_BREAK 0xD // Supported
#define EFFECT_EXTENDED 0xE
#define EFFECT_E_FINE_PORTA_UP 0x1 // Supported
#define EFFECT_E_FINE_PORTA_DOWN 0x2 // Supported
#define EFFECT_E_GLISSANDO_CTRL 0x3 // - TO BE DONE -
#define EFFECT_E_VIBRATO_WAVEFORM 0x4 // - TO BE DONE -
#define EFFECT_E_SET_FINETUNE 0x5 // - TO BE DONE -
#define EFFECT_E_PATTERN_LOOP 0x6 // Supported
#define EFFECT_E_TREMOLO_WAVEFORM 0x7 // - TO BE DONE -
#define EFFECT_E_SET_PANNING_2 0x8 // - TO BE DONE -
#define EFFECT_E_RETRIGGER_NOTE 0x9 // - TO BE DONE -
#define EFFECT_E_FINE_VOLSLIDE_UP 0xA // Supported
#define EFFECT_E_FINE_VOLSLIDE_DOWN 0xB // Supported
#define EFFECT_E_NOTE_CUT 0xC // Supported
#define EFFECT_E_NOTE_DELAY 0xD // - TO BE DONE -
#define EFFECT_E_PATTERN_DELAY 0xE // Supported
#define EFFECT_E_INVERT_LOOP 0xF // - TO BE DONE -
#define EFFECT_SET_SPEED 0xF0 // Supported
#define EFFECT_SET_TEMPO 0xF2 // Supported
#define PERIOD_TABLE_LENGTH MAXNOTES
#define FULL_PERIOD_TABLE_LENGTH ( PERIOD_TABLE_LENGTH * 8 )
static const short periodtable[]=
{
27392, 25856, 24384, 23040, 21696, 20480, 19328, 18240, 17216, 16256, 15360, 14496,
13696, 12928, 12192, 11520, 10848, 10240, 9664, 9120, 8606, 8128, 7680, 7248,
6848, 6464, 6096, 5760, 5424, 5120, 4832, 4560, 4304, 4064, 3840, 3624,
3424, 3232, 3048, 2880, 2712, 2560, 2416, 2280, 2152, 2032, 1920, 1812,
1712, 1616, 1524, 1440, 1356, 1280, 1208, 1140, 1076, 1016, 960, 906,
856, 808, 762, 720, 678, 640, 604, 570, 538, 508, 480, 453,
428, 404, 381, 360, 339, 320, 302, 285, 269, 254, 240, 226,
214, 202, 190, 180, 170, 160, 151, 143, 135, 127, 120, 113,
107, 101, 95, 90, 85, 80, 75, 71, 67, 63, 60, 56,
53, 50, 47, 45, 42, 40, 37, 35, 33, 31, 30, 28,
27, 25, 24, 22, 21, 20, 19, 18, 17, 16, 15, 14,
13, 13, 12, 11, 11, 10, 9, 9, 8, 8, 7, 7
};
static const short sintable[]={
0, 24, 49, 74, 97, 120, 141,161,
180, 197, 212, 224, 235, 244, 250,253,
255, 253, 250, 244, 235, 224, 212,197,
180, 161, 141, 120, 97, 74, 49, 24
};
typedef struct modtype_
{
unsigned char signature[5];
int numberofchannels;
}modtype;
modtype modlist[]=
{
{ "M!K!",4},
{ "M.K.",4},
{ "FLT4",4},
{ "FLT8",8},
{ "4CHN",4},
{ "6CHN",6},
{ "8CHN",8},
{ "10CH",10},
{ "12CH",12},
{ "14CH",14},
{ "16CH",16},
{ "18CH",18},
{ "20CH",20},
{ "22CH",22},
{ "24CH",24},
{ "26CH",26},
{ "28CH",28},
{ "30CH",30},
{ "32CH",32},
{ "",0}
};
///////////////////////////////////////////////////////////////////////////////////
static void memcopy( void * dest, void *source, unsigned long size )
{
unsigned long i;
unsigned char * d,*s;
d=(unsigned char*)dest;
s=(unsigned char*)source;
for(i=0;i<size;i++)
{
d[i]=s[i];
}
}
static void memclear( void * dest, unsigned char value, unsigned long size )
{
unsigned long i;
unsigned char * d;
d=(unsigned char*)dest;
for(i=0;i<size;i++)
{
d[i]=value;
}
}
static int memcompare( unsigned char * buf1, unsigned char * buf2, unsigned int size )
{
unsigned int i;
i = 0;
while(i<size)
{
if(buf1[i] != buf2[i])
{
return 0;
}
i++;
}
return 1;
}
static int getnote( jar_mod_context_t * mod, unsigned short period, int finetune )
{
int i;
for(i = 0; i < FULL_PERIOD_TABLE_LENGTH; i++)
{
if(period >= mod->fullperiod[i])
{
return i;
}
}
return MAXNOTES;
}
static void worknote( note * nptr, channel * cptr, char t, jar_mod_context_t * mod )
{
muint sample, period, effect, operiod;
muint curnote, arpnote;
sample = (nptr->sampperiod & 0xF0) | (nptr->sampeffect >> 4);
period = ((nptr->sampperiod & 0xF) << 8) | nptr->period;
effect = ((nptr->sampeffect & 0xF) << 8) | nptr->effect;
operiod = cptr->period;
if ( period || sample )
{
if( sample && sample < 32 )
{
cptr->sampnum = sample - 1;
}
if( period || sample )
{
cptr->sampdata = (char *) mod->sampledata[cptr->sampnum];
cptr->length = mod->song.samples[cptr->sampnum].length;
cptr->reppnt = mod->song.samples[cptr->sampnum].reppnt;
cptr->replen = mod->song.samples[cptr->sampnum].replen;
cptr->finetune = (mod->song.samples[cptr->sampnum].finetune)&0xF;
if(effect>>8!=4 && effect>>8!=6)
{
cptr->vibraperiod=0;
cptr->vibrapointeur=0;
}
}
if( (sample != 0) && ( (effect>>8) != EFFECT_VOLSLIDE_TONEPORTA ) )
{
cptr->volume = mod->song.samples[cptr->sampnum].volume;
cptr->volumeslide = 0;
}
if( ( (effect>>8) != EFFECT_TONE_PORTAMENTO && (effect>>8)!=EFFECT_VOLSLIDE_TONEPORTA) )
{
if (period!=0)
cptr->samppos = 0;
}
cptr->decalperiod = 0;
if( period )
{
if(cptr->finetune)
{
if( cptr->finetune <= 7 )
{
period = mod->fullperiod[getnote(mod,period,0) + cptr->finetune];
}
else
{
period = mod->fullperiod[getnote(mod,period,0) - (16 - (cptr->finetune)) ];
}
}
cptr->period = period;
}
}
cptr->effect = 0;
cptr->parameffect = 0;
cptr->effect_code = effect;
switch (effect >> 8)
{
case EFFECT_ARPEGGIO:
/*
[0]: Arpeggio
Where [0][x][y] means "play note, note+x semitones, note+y
semitones, then return to original note". The fluctuations are
carried out evenly spaced in one pattern division. They are usually
used to simulate chords, but this doesn't work too well. They are
also used to produce heavy vibrato. A major chord is when x=4, y=7.
A minor chord is when x=3, y=7.
*/
if(effect&0xff)
{
cptr->effect = EFFECT_ARPEGGIO;
cptr->parameffect = effect&0xff;
cptr->ArpIndex = 0;
curnote = getnote(mod,cptr->period,cptr->finetune);
cptr->Arpperiods[0] = cptr->period;
arpnote = curnote + (((cptr->parameffect>>4)&0xF)*8);
if( arpnote >= FULL_PERIOD_TABLE_LENGTH )
arpnote = FULL_PERIOD_TABLE_LENGTH - 1;
cptr->Arpperiods[1] = mod->fullperiod[arpnote];
arpnote = curnote + (((cptr->parameffect)&0xF)*8);
if( arpnote >= FULL_PERIOD_TABLE_LENGTH )
arpnote = FULL_PERIOD_TABLE_LENGTH - 1;
cptr->Arpperiods[2] = mod->fullperiod[arpnote];
}
break;
case EFFECT_PORTAMENTO_UP:
/*
[1]: Slide up
Where [1][x][y] means "smoothly decrease the period of current
sample by x*16+y after each tick in the division". The
ticks/division are set with the 'set speed' effect (see below). If
the period of the note being played is z, then the final period
will be z - (x*16 + y)*(ticks - 1). As the slide rate depends on
the speed, changing the speed will change the slide. You cannot
slide beyond the note B3 (period 113).
*/
cptr->effect = EFFECT_PORTAMENTO_UP;
cptr->parameffect = effect&0xff;
break;
case EFFECT_PORTAMENTO_DOWN:
/*
[2]: Slide down
Where [2][x][y] means "smoothly increase the period of current
sample by x*16+y after each tick in the division". Similar to [1],
but lowers the pitch. You cannot slide beyond the note C1 (period
856).
*/
cptr->effect = EFFECT_PORTAMENTO_DOWN;
cptr->parameffect = effect&0xff;
break;
case EFFECT_TONE_PORTAMENTO:
/*
[3]: Slide to note
Where [3][x][y] means "smoothly change the period of current sample
by x*16+y after each tick in the division, never sliding beyond
current period". The period-length in this channel's division is a
parameter to this effect, and hence is not played. Sliding to a
note is similar to effects [1] and [2], but the slide will not go
beyond the given period, and the direction is implied by that
period. If x and y are both 0, then the old slide will continue.
*/
cptr->effect = EFFECT_TONE_PORTAMENTO;
if( (effect&0xff) != 0 )
{
cptr->portaspeed = (short)(effect&0xff);
}
if(period!=0)
{
cptr->portaperiod = period;
cptr->period = operiod;
}
break;
case EFFECT_VIBRATO:
/*
[4]: Vibrato
Where [4][x][y] means "oscillate the sample pitch using a
particular waveform with amplitude y/16 semitones, such that (x *
ticks)/64 cycles occur in the division". The waveform is set using
effect [14][4]. By placing vibrato effects on consecutive
divisions, the vibrato effect can be maintained. If either x or y
are 0, then the old vibrato values will be used.
*/
cptr->effect = EFFECT_VIBRATO;
if( ( effect & 0x0F ) != 0 ) // Depth continue or change ?
cptr->vibraparam = (cptr->vibraparam & 0xF0) | ( effect & 0x0F );
if( ( effect & 0xF0 ) != 0 ) // Speed continue or change ?
cptr->vibraparam = (cptr->vibraparam & 0x0F) | ( effect & 0xF0 );
break;
case EFFECT_VOLSLIDE_TONEPORTA:
/*
[5]: Continue 'Slide to note', but also do Volume slide
Where [5][x][y] means "either slide the volume up x*(ticks - 1) or
slide the volume down y*(ticks - 1), at the same time as continuing
the last 'Slide to note'". It is illegal for both x and y to be
non-zero. You cannot slide outside the volume range 0..64. The
period-length in this channel's division is a parameter to this
effect, and hence is not played.
*/
if( period != 0 )
{
cptr->portaperiod = period;
cptr->period = operiod;
}
cptr->effect = EFFECT_VOLSLIDE_TONEPORTA;
if( ( effect & 0xFF ) != 0 )
cptr->volumeslide = ( effect & 0xFF );
break;
case EFFECT_VOLSLIDE_VIBRATO:
/*
[6]: Continue 'Vibrato', but also do Volume slide
Where [6][x][y] means "either slide the volume up x*(ticks - 1) or
slide the volume down y*(ticks - 1), at the same time as continuing
the last 'Vibrato'". It is illegal for both x and y to be non-zero.
You cannot slide outside the volume range 0..64.
*/
cptr->effect = EFFECT_VOLSLIDE_VIBRATO;
if( (effect & 0xFF) != 0 )
cptr->volumeslide = (effect & 0xFF);
break;
case EFFECT_SET_OFFSET:
/*
[9]: Set sample offset
Where [9][x][y] means "play the sample from offset x*4096 + y*256".
The offset is measured in words. If no sample is given, yet one is
still playing on this channel, it should be retriggered to the new
offset using the current volume.
*/
cptr->samppos = ((effect>>4) * 4096) + ((effect&0xF)*256);
break;
case EFFECT_VOLUME_SLIDE:
/*
[10]: Volume slide
Where [10][x][y] means "either slide the volume up x*(ticks - 1) or
slide the volume down y*(ticks - 1)". If both x and y are non-zero,
then the y value is ignored (assumed to be 0). You cannot slide
outside the volume range 0..64.
*/
cptr->effect = EFFECT_VOLUME_SLIDE;
cptr->volumeslide = (effect & 0xFF);
break;
case EFFECT_JUMP_POSITION:
/*
[11]: Position Jump
Where [11][x][y] means "stop the pattern after this division, and
continue the song at song-position x*16+y". This shifts the
'pattern-cursor' in the pattern table (see above). Legal values for
x*16+y are from 0 to 127.
*/
mod->tablepos = (effect & 0xFF);
if(mod->tablepos >= mod->song.length)
{
mod->tablepos = 0;
}
mod->patternpos = 0;
mod->jump_loop_effect = 1;
break;
case EFFECT_SET_VOLUME:
/*
[12]: Set volume
Where [12][x][y] means "set current sample's volume to x*16+y".
Legal volumes are 0..64.
*/
cptr->volume = (effect & 0xFF);
break;
case EFFECT_PATTERN_BREAK:
/*
[13]: Pattern Break
Where [13][x][y] means "stop the pattern after this division, and
continue the song at the next pattern at division x*10+y" (the 10
is not a typo). Legal divisions are from 0 to 63 (note Protracker
exception above).
*/
mod->patternpos = ( ((effect>>4)&0xF)*10 + (effect&0xF) ) * mod->number_of_channels;
mod->jump_loop_effect = 1;
mod->tablepos++;
if(mod->tablepos >= mod->song.length)
{
mod->tablepos = 0;
}
break;
case EFFECT_EXTENDED:
switch( (effect>>4) & 0xF )
{
case EFFECT_E_FINE_PORTA_UP:
/*
[14][1]: Fineslide up
Where [14][1][x] means "decrement the period of the current sample
by x". The incrementing takes place at the beginning of the
division, and hence there is no actual sliding. You cannot slide
beyond the note B3 (period 113).
*/
cptr->period -= (effect & 0xF);
if( cptr->period < 113 )
cptr->period = 113;
break;
case EFFECT_E_FINE_PORTA_DOWN:
/*
[14][2]: Fineslide down
Where [14][2][x] means "increment the period of the current sample
by x". Similar to [14][1] but shifts the pitch down. You cannot
slide beyond the note C1 (period 856).
*/
cptr->period += (effect & 0xF);
if( cptr->period > 856 )
cptr->period = 856;
break;
case EFFECT_E_FINE_VOLSLIDE_UP:
/*
[14][10]: Fine volume slide up
Where [14][10][x] means "increment the volume of the current sample
by x". The incrementing takes place at the beginning of the
division, and hence there is no sliding. You cannot slide beyond
volume 64.
*/
cptr->volume += (effect & 0xF);
if( cptr->volume>64 )
cptr->volume = 64;
break;
case EFFECT_E_FINE_VOLSLIDE_DOWN:
/*
[14][11]: Fine volume slide down
Where [14][11][x] means "decrement the volume of the current sample
by x". Similar to [14][10] but lowers volume. You cannot slide
beyond volume 0.
*/
cptr->volume -= (effect & 0xF);
if( cptr->volume > 200 )
cptr->volume = 0;
break;
case EFFECT_E_PATTERN_LOOP:
/*
[14][6]: Loop pattern
Where [14][6][x] means "set the start of a loop to this division if
x is 0, otherwise after this division, jump back to the start of a
loop and play it another x times before continuing". If the start
of the loop was not set, it will default to the start of the
current pattern. Hence 'loop pattern' cannot be performed across
multiple patterns. Note that loops do not support nesting, and you
may generate an infinite loop if you try to nest 'loop pattern's.
*/
if( effect & 0xF )
{
if( cptr->patternloopcnt )
{
cptr->patternloopcnt--;
if( cptr->patternloopcnt )
{
mod->patternpos = cptr->patternloopstartpoint;
mod->jump_loop_effect = 1;
}
else
{
cptr->patternloopstartpoint = mod->patternpos ;
}
}
else
{
cptr->patternloopcnt = (effect & 0xF);
mod->patternpos = cptr->patternloopstartpoint;
mod->jump_loop_effect = 1;
}
}
else // Start point
{
cptr->patternloopstartpoint = mod->patternpos;
}
break;
case EFFECT_E_PATTERN_DELAY:
/*
[14][14]: Delay pattern
Where [14][14][x] means "after this division there will be a delay
equivalent to the time taken to play x divisions after which the
pattern will be resumed". The delay only relates to the
interpreting of new divisions, and all effects and previous notes
continue during delay.
*/
mod->patterndelay = (effect & 0xF);
break;
case EFFECT_E_NOTE_CUT:
/*
[14][12]: Cut sample
Where [14][12][x] means "after the current sample has been played
for x ticks in this division, its volume will be set to 0". This
implies that if x is 0, then you will not hear any of the sample.
If you wish to insert "silence" in a pattern, it is better to use a
"silence"-sample (see above) due to the lack of proper support for
this effect.
*/
cptr->effect = EFFECT_E_NOTE_CUT;
cptr->cut_param = (effect & 0xF);
if(!cptr->cut_param)
cptr->volume = 0;
break;
default:
break;
}
break;
case 0xF:
/*
[15]: Set speed
Where [15][x][y] means "set speed to x*16+y". Though it is nowhere
near that simple. Let z = x*16+y. Depending on what values z takes,
different units of speed are set, there being two: ticks/division
and beats/minute (though this one is only a label and not strictly
true). If z=0, then what should technically happen is that the
module stops, but in practice it is treated as if z=1, because
there is already a method for stopping the module (running out of
patterns). If z<=32, then it means "set ticks/division to z"
otherwise it means "set beats/minute to z" (convention says that
this should read "If z<32.." but there are some composers out there
that defy conventions). Default values are 6 ticks/division, and
125 beats/minute (4 divisions = 1 beat). The beats/minute tag is
only meaningful for 6 ticks/division. To get a more accurate view
of how things work, use the following formula:
24 * beats/minute
divisions/minute = -----------------
ticks/division
Hence divisions/minute range from 24.75 to 6120, eg. to get a value
of 2000 divisions/minute use 3 ticks/division and 250 beats/minute.
If multiple "set speed" effects are performed in a single division,
the ones on higher-numbered channels take precedence over the ones
on lower-numbered channels. This effect has a large number of
different implementations, but the one described here has the
widest usage.
*/
if( (effect&0xFF) < 0x21 )
{
if( effect&0xFF )
{
mod->song.speed = effect&0xFF;
mod->patternticksaim = (long)mod->song.speed * ((mod->playrate * 5 ) / (((long)2 * (long)mod->bpm)));
}
}
if( (effect&0xFF) >= 0x21 )
{
/// HZ = 2 * BPM / 5
mod->bpm = effect&0xFF;
mod->patternticksaim = (long)mod->song.speed * ((mod->playrate * 5 ) / (((long)2 * (long)mod->bpm)));
}
break;
default:
// Unsupported effect
break;
}
}
static void workeffect( note * nptr, channel * cptr )
{
switch(cptr->effect)
{
case EFFECT_ARPEGGIO:
if( cptr->parameffect )
{
cptr->decalperiod = cptr->period - cptr->Arpperiods[cptr->ArpIndex];
cptr->ArpIndex++;
if( cptr->ArpIndex>2 )
cptr->ArpIndex = 0;
}
break;
case EFFECT_PORTAMENTO_UP:
if(cptr->period)
{
cptr->period -= cptr->parameffect;
if( cptr->period < 113 || cptr->period > 20000 )
cptr->period = 113;
}
break;
case EFFECT_PORTAMENTO_DOWN:
if(cptr->period)
{
cptr->period += cptr->parameffect;
if( cptr->period > 20000 )
cptr->period = 20000;
}
break;
case EFFECT_VOLSLIDE_TONEPORTA:
case EFFECT_TONE_PORTAMENTO:
if( cptr->period && ( cptr->period != cptr->portaperiod ) && cptr->portaperiod )
{
if( cptr->period > cptr->portaperiod )
{
if( cptr->period - cptr->portaperiod >= cptr->portaspeed )
{
cptr->period -= cptr->portaspeed;
}
else
{
cptr->period = cptr->portaperiod;
}
}
else
{
if( cptr->portaperiod - cptr->period >= cptr->portaspeed )
{
cptr->period += cptr->portaspeed;
}
else
{
cptr->period = cptr->portaperiod;
}
}
if( cptr->period == cptr->portaperiod )
{
// If the slide is over, don't let it to be retriggered.
cptr->portaperiod = 0;
}
}
if( cptr->effect == EFFECT_VOLSLIDE_TONEPORTA )
{
if( cptr->volumeslide > 0x0F )
{
cptr->volume = cptr->volume + (cptr->volumeslide>>4);
if(cptr->volume>63)
cptr->volume = 63;
}
else
{
cptr->volume = cptr->volume - (cptr->volumeslide);
if(cptr->volume>63)
cptr->volume=0;
}
}
break;
case EFFECT_VOLSLIDE_VIBRATO:
case EFFECT_VIBRATO:
cptr->vibraperiod = ( (cptr->vibraparam&0xF) * sintable[cptr->vibrapointeur&0x1F] )>>7;
if( cptr->vibrapointeur > 31 )
cptr->vibraperiod = -cptr->vibraperiod;