51 #ifndef INCLUDE_JAR_XM_H
52 #define INCLUDE_JAR_XM_H
54 #define JAR_XM_DEBUG 0
55 #define JAR_XM_LINEAR_INTERPOLATION 1 // speed increase with decrease in quality
56 #define JAR_XM_DEFENSIVE 1
57 #define JAR_XM_RAMPING 1
76 struct jar_xm_context_s;
140 float* musicBuffer = malloc((2*numsamples)*
sizeof(
float));
145 for(
x=0;
x<2*numsamples;
x++)
146 output[
x] = (
short)(musicBuffer[
x] * SHRT_MAX);
159 float* musicBuffer = malloc((2*numsamples)*
sizeof(
float));
164 for(
x=0;
x<2*numsamples;
x++)
165 output[
x] = (
char)(musicBuffer[
x] * CHAR_MAX);
303 #endif//end of INCLUDE_JAR_XM_H
310 #ifdef JAR_XM_IMPLEMENTATION
317 #define DEBUG(fmt, ...) do { \
318 fprintf(stderr, "%s(): " fmt "\n", __func__, __VA_ARGS__); \
325 #if jar_xm_BIG_ENDIAN
326 #error "Big endian platforms are not yet supported, sorry"
328 extern int __fail[-1];
333 #define SAMPLE_NAME_LENGTH 22
334 #define INSTRUMENT_NAME_LENGTH 22
335 #define MODULE_NAME_LENGTH 20
336 #define TRACKER_NAME_LENGTH 20
337 #define PATTERN_ORDER_TABLE_LENGTH 256
339 #define NUM_ENVELOPE_POINTS 12
340 #define MAX_NUM_ROWS 256
343 #define jar_xm_SAMPLE_RAMPING_POINTS 0x20
348 enum jar_xm_waveform_type_e {
349 jar_xm_SINE_WAVEFORM = 0,
350 jar_xm_RAMP_DOWN_WAVEFORM = 1,
351 jar_xm_SQUARE_WAVEFORM = 2,
352 jar_xm_RANDOM_WAVEFORM = 3,
353 jar_xm_RAMP_UP_WAVEFORM = 4,
355 typedef enum jar_xm_waveform_type_e jar_xm_waveform_type_t;
357 enum jar_xm_loop_type_e {
360 jar_xm_PING_PONG_LOOP,
362 typedef enum jar_xm_loop_type_e jar_xm_loop_type_t;
364 enum jar_xm_frequency_type_e {
365 jar_xm_LINEAR_FREQUENCIES,
366 jar_xm_AMIGA_FREQUENCIES,
368 typedef enum jar_xm_frequency_type_e jar_xm_frequency_type_t;
370 struct jar_xm_envelope_point_s {
374 typedef struct jar_xm_envelope_point_s jar_xm_envelope_point_t;
376 struct jar_xm_envelope_s {
377 jar_xm_envelope_point_t
points[NUM_ENVELOPE_POINTS];
379 uint8_t sustain_point;
380 uint8_t loop_start_point;
381 uint8_t loop_end_point;
383 bool sustain_enabled;
386 typedef struct jar_xm_envelope_s jar_xm_envelope_t;
388 struct jar_xm_sample_s {
389 char name[SAMPLE_NAME_LENGTH + 1];
394 uint32_t loop_length;
398 jar_xm_loop_type_t loop_type;
400 int8_t relative_note;
401 uint64_t latest_trigger;
405 typedef struct jar_xm_sample_s jar_xm_sample_t;
407 struct jar_xm_instrument_s {
408 char name[INSTRUMENT_NAME_LENGTH + 1];
409 uint16_t num_samples;
410 uint8_t sample_of_notes[NUM_NOTES];
411 jar_xm_envelope_t volume_envelope;
412 jar_xm_envelope_t panning_envelope;
413 jar_xm_waveform_type_t vibrato_type;
414 uint8_t vibrato_sweep;
415 uint8_t vibrato_depth;
416 uint8_t vibrato_rate;
417 uint16_t volume_fadeout;
418 uint64_t latest_trigger;
421 jar_xm_sample_t* samples;
423 typedef struct jar_xm_instrument_s jar_xm_instrument_t;
425 struct jar_xm_pattern_slot_s {
428 uint8_t volume_column;
430 uint8_t effect_param;
432 typedef struct jar_xm_pattern_slot_s jar_xm_pattern_slot_t;
434 struct jar_xm_pattern_s {
436 jar_xm_pattern_slot_t* slots;
438 typedef struct jar_xm_pattern_s jar_xm_pattern_t;
440 struct jar_xm_module_s {
441 char name[MODULE_NAME_LENGTH + 1];
442 char trackername[TRACKER_NAME_LENGTH + 1];
444 uint16_t restart_position;
445 uint16_t num_channels;
446 uint16_t num_patterns;
447 uint16_t num_instruments;
448 jar_xm_frequency_type_t frequency_type;
449 uint8_t pattern_table[PATTERN_ORDER_TABLE_LENGTH];
451 jar_xm_pattern_t* patterns;
452 jar_xm_instrument_t* instruments;
455 typedef struct jar_xm_module_s jar_xm_module_t;
457 struct jar_xm_channel_context_s {
460 jar_xm_instrument_t* instrument;
462 jar_xm_pattern_slot_t*
current;
464 float sample_position;
473 uint16_t autovibrato_ticks;
476 float fadeout_volume;
477 float volume_envelope_volume;
478 float panning_envelope_panning;
479 uint16_t volume_envelope_frame_count;
480 uint16_t panning_envelope_frame_count;
482 float autovibrato_note_offset;
484 bool arp_in_progress;
485 uint8_t arp_note_offset;
486 uint8_t volume_slide_param;
487 uint8_t fine_volume_slide_param;
488 uint8_t global_volume_slide_param;
489 uint8_t panning_slide_param;
490 uint8_t portamento_up_param;
491 uint8_t portamento_down_param;
492 uint8_t fine_portamento_up_param;
493 uint8_t fine_portamento_down_param;
494 uint8_t extra_fine_portamento_up_param;
495 uint8_t extra_fine_portamento_down_param;
496 uint8_t tone_portamento_param;
497 float tone_portamento_target_period;
498 uint8_t multi_retrig_param;
499 uint8_t note_delay_param;
500 uint8_t pattern_loop_origin;
501 uint8_t pattern_loop_count;
502 bool vibrato_in_progress;
503 jar_xm_waveform_type_t vibrato_waveform;
504 bool vibrato_waveform_retrigger;
505 uint8_t vibrato_param;
506 uint16_t vibrato_ticks;
507 float vibrato_note_offset;
508 jar_xm_waveform_type_t tremolo_waveform;
509 bool tremolo_waveform_retrigger;
510 uint8_t tremolo_param;
511 uint8_t tremolo_ticks;
512 float tremolo_volume;
513 uint8_t tremor_param;
516 uint64_t latest_trigger;
522 float target_panning;
525 unsigned long frame_count;
526 float end_of_previous_sample[jar_xm_SAMPLE_RAMPING_POINTS];
529 float actual_panning;
532 typedef struct jar_xm_channel_context_s jar_xm_channel_context_t;
534 struct jar_xm_context_s {
535 void* allocated_memory;
552 uint8_t current_table_index;
554 uint16_t current_tick;
555 float remaining_samples_in_tick;
556 uint64_t generated_samples;
565 uint16_t extra_ticks;
567 uint8_t* row_loop_count;
569 uint8_t max_loop_count;
582 int jar_xm_check_sanity_preload(
const char*,
size_t);
607 size_t jar_xm_get_memory_needed_for_context(
const char*,
size_t);
628 if((ret = jar_xm_check_sanity_preload(moddata, moddata_length))) {
629 DEBUG(
"jar_xm_check_sanity_preload() returned %i, module is not safe to load", ret);
634 bytes_needed = jar_xm_get_memory_needed_for_context(moddata, moddata_length);
635 mempool = malloc(bytes_needed);
636 if(mempool ==
NULL && bytes_needed > 0) {
638 DEBUG(
"call to malloc() failed, returned %p", (
void*)mempool);
643 memset(mempool, 0, bytes_needed);
646 ctx->allocated_memory = mempool;
650 mempool = jar_xm_load_module(ctx, moddata, moddata_length, mempool);
652 ctx->channels = (jar_xm_channel_context_t*)mempool;
653 mempool += ctx->module.num_channels *
sizeof(jar_xm_channel_context_t);
655 ctx->global_volume = 1.f;
656 ctx->amplification = .25f;
659 ctx->volume_ramp = (1.f / 128.f);
660 ctx->panning_ramp = (1.f / 128.f);
663 for(uint8_t
i = 0;
i < ctx->module.num_channels; ++
i) {
664 jar_xm_channel_context_t* ch = ctx->channels +
i;
667 ch->vibrato_waveform = jar_xm_SINE_WAVEFORM;
668 ch->vibrato_waveform_retrigger =
true;
669 ch->tremolo_waveform = jar_xm_SINE_WAVEFORM;
670 ch->tremolo_waveform_retrigger =
true;
672 ch->volume = ch->volume_envelope_volume = ch->fadeout_volume = 1.0f;
673 ch->panning = ch->panning_envelope_panning = .5f;
674 ch->actual_volume = .0f;
675 ch->actual_panning = .5f;
678 ctx->row_loop_count = (uint8_t*)mempool;
679 mempool += MAX_NUM_ROWS *
sizeof(uint8_t);
682 if((ret = jar_xm_check_sanity_postload(ctx))) {
683 DEBUG(
"jar_xm_check_sanity_postload() returned %i, module is not safe to play", ret);
693 free(context->allocated_memory);
697 context->max_loop_count = loopcnt;
701 return context->loop_count;
707 bool old = ctx->channels[
channel - 1].muted;
708 ctx->channels[
channel - 1].muted = mute;
713 bool old = ctx->module.instruments[instr - 1].muted;
714 ctx->module.instruments[instr - 1].muted = mute;
721 return ctx->module.name;
725 return ctx->module.trackername;
731 return ctx->module.num_channels;
735 return ctx->module.length;
739 return ctx->module.num_patterns;
743 return ctx->module.patterns[
pattern].num_rows;
747 return ctx->module.num_instruments;
751 return ctx->module.instruments[instrument - 1].num_samples;
757 if(bpm) *bpm = ctx->bpm;
758 if(tempo) *tempo = ctx->tempo;
762 if(pattern_index) *pattern_index = ctx->current_table_index;
763 if(
pattern) *
pattern = ctx->module.pattern_table[ctx->current_table_index];
764 if(row) *row = ctx->current_row;
765 if(samples) *samples = ctx->generated_samples;
769 return ctx->module.instruments[instr - 1].latest_trigger;
773 return ctx->module.instruments[instr - 1].samples[
sample].latest_trigger;
777 return ctx->channels[chn - 1].latest_trigger;
786 #define READ_U8(offset) (((offset) < moddata_length) ? (*(uint8_t*)(moddata + (offset))) : 0)
787 #define READ_U16(offset) ((uint16_t)READ_U8(offset) | ((uint16_t)READ_U8((offset) + 1) << 8))
788 #define READ_U32(offset) ((uint32_t)READ_U16(offset) | ((uint32_t)READ_U16((offset) + 2) << 16))
789 #define READ_MEMCPY(ptr, offset, length) memcpy_pad(ptr, length, moddata, moddata_length, offset)
791 static inline void memcpy_pad(
void* dst,
size_t dst_len,
const void* src,
size_t src_len,
size_t offset) {
792 uint8_t* dst_c = dst;
793 const uint8_t* src_c = src;
796 size_t copy_bytes = (src_len >= offset) ? (src_len - offset) : 0;
797 copy_bytes = copy_bytes > dst_len ? dst_len : copy_bytes;
799 memcpy(dst_c, src_c + offset, copy_bytes);
801 memset(dst_c + copy_bytes, 0, dst_len - copy_bytes);
806 int jar_xm_check_sanity_preload(
const char*
module,
size_t module_length) {
807 if(module_length < 60) {
811 if(memcmp(
"Extended Module: ",
module, 17) != 0) {
831 for(uint8_t
i = 0;
i < ctx->module.length; ++
i) {
832 if(ctx->module.pattern_table[
i] >= ctx->module.num_patterns) {
833 if(
i+1 == ctx->module.length && ctx->module.length > 1) {
835 --ctx->module.length;
836 DEBUG(
"trimming invalid POT at pos %X",
i);
838 DEBUG(
"module has invalid POT, pos %X references nonexistent pattern %X",
840 ctx->module.pattern_table[
i]);
851 size_t jar_xm_get_memory_needed_for_context(
const char* moddata,
size_t moddata_length) {
852 size_t memory_needed = 0;
854 uint16_t num_channels;
855 uint16_t num_patterns;
856 uint16_t num_instruments;
860 num_channels = READ_U16(offset + 8);
861 num_channels = READ_U16(offset + 8);
863 num_patterns = READ_U16(offset + 10);
864 memory_needed += num_patterns *
sizeof(jar_xm_pattern_t);
866 num_instruments = READ_U16(offset + 12);
867 memory_needed += num_instruments *
sizeof(jar_xm_instrument_t);
869 memory_needed += MAX_NUM_ROWS * READ_U16(offset + 4) *
sizeof(uint8_t);
872 offset += READ_U32(offset);
875 for(uint16_t
i = 0;
i < num_patterns; ++
i) {
878 num_rows = READ_U16(offset + 5);
879 memory_needed += num_rows * num_channels *
sizeof(jar_xm_pattern_slot_t);
882 offset += READ_U32(offset) + READ_U16(offset + 7);
886 for(uint16_t
i = 0;
i < num_instruments; ++
i) {
887 uint16_t num_samples;
888 uint32_t sample_header_size = 0;
889 uint32_t sample_size_aggregate = 0;
891 num_samples = READ_U16(offset + 27);
892 memory_needed += num_samples *
sizeof(jar_xm_sample_t);
894 if(num_samples > 0) {
895 sample_header_size = READ_U32(offset + 29);
899 offset += READ_U32(offset);
901 for(uint16_t
j = 0;
j < num_samples; ++
j) {
902 uint32_t sample_size;
905 sample_size = READ_U32(offset);
906 flags = READ_U8(offset + 14);
907 sample_size_aggregate += sample_size;
909 if(flags & (1 << 4)) {
911 memory_needed += sample_size * (
sizeof(float) >> 1);
914 memory_needed += sample_size *
sizeof(float);
917 offset += sample_header_size;
920 offset += sample_size_aggregate;
923 memory_needed += num_channels *
sizeof(jar_xm_channel_context_t);
926 return memory_needed;
929 char* jar_xm_load_module(
jar_xm_context_t* ctx,
const char* moddata,
size_t moddata_length,
char* mempool) {
931 jar_xm_module_t* mod = &(ctx->module);
934 READ_MEMCPY(mod->name, offset + 17, MODULE_NAME_LENGTH);
935 READ_MEMCPY(mod->trackername, offset + 38, TRACKER_NAME_LENGTH);
939 uint32_t header_size = READ_U32(offset);
941 mod->length = READ_U16(offset + 4);
942 mod->restart_position = READ_U16(offset + 6);
943 mod->num_channels = READ_U16(offset + 8);
944 mod->num_patterns = READ_U16(offset + 10);
945 mod->num_instruments = READ_U16(offset + 12);
947 mod->patterns = (jar_xm_pattern_t*)mempool;
948 mempool += mod->num_patterns *
sizeof(jar_xm_pattern_t);
950 mod->instruments = (jar_xm_instrument_t*)mempool;
951 mempool += mod->num_instruments *
sizeof(jar_xm_instrument_t);
953 uint16_t flags = READ_U32(offset + 14);
954 mod->frequency_type = (flags & (1 << 0)) ? jar_xm_LINEAR_FREQUENCIES : jar_xm_AMIGA_FREQUENCIES;
956 ctx->tempo = READ_U16(offset + 16);
957 ctx->bpm = READ_U16(offset + 18);
959 READ_MEMCPY(mod->pattern_table, offset + 20, PATTERN_ORDER_TABLE_LENGTH);
960 offset += header_size;
963 for(uint16_t
i = 0;
i < mod->num_patterns; ++
i) {
964 uint16_t packed_patterndata_size = READ_U16(offset + 7);
965 jar_xm_pattern_t* pat = mod->patterns +
i;
967 pat->num_rows = READ_U16(offset + 5);
969 pat->slots = (jar_xm_pattern_slot_t*)mempool;
970 mempool += mod->num_channels * pat->num_rows *
sizeof(jar_xm_pattern_slot_t);
973 offset += READ_U32(offset);
975 if(packed_patterndata_size == 0) {
977 memset(pat->slots, 0,
sizeof(jar_xm_pattern_slot_t) * pat->num_rows * mod->num_channels);
980 for(uint16_t
j = 0, k = 0;
j < packed_patterndata_size; ++k) {
981 uint8_t
note = READ_U8(offset +
j);
982 jar_xm_pattern_slot_t* slot = pat->slots + k;
984 if(
note & (1 << 7)) {
988 if(
note & (1 << 0)) {
990 slot->note = READ_U8(offset +
j);
996 if(
note & (1 << 1)) {
998 slot->instrument = READ_U8(offset +
j);
1001 slot->instrument = 0;
1004 if(
note & (1 << 2)) {
1006 slot->volume_column = READ_U8(offset +
j);
1009 slot->volume_column = 0;
1012 if(
note & (1 << 3)) {
1014 slot->effect_type = READ_U8(offset +
j);
1017 slot->effect_type = 0;
1020 if(
note & (1 << 4)) {
1022 slot->effect_param = READ_U8(offset +
j);
1025 slot->effect_param = 0;
1030 slot->instrument = READ_U8(offset +
j + 1);
1031 slot->volume_column = READ_U8(offset +
j + 2);
1032 slot->effect_type = READ_U8(offset +
j + 3);
1033 slot->effect_param = READ_U8(offset +
j + 4);
1039 offset += packed_patterndata_size;
1043 for(uint16_t
i = 0;
i < ctx->module.num_instruments; ++
i) {
1044 uint32_t sample_header_size = 0;
1045 jar_xm_instrument_t* instr = mod->instruments +
i;
1047 READ_MEMCPY(instr->name, offset + 4, INSTRUMENT_NAME_LENGTH);
1048 instr->num_samples = READ_U16(offset + 27);
1050 if(instr->num_samples > 0) {
1052 sample_header_size = READ_U32(offset + 29);
1053 READ_MEMCPY(instr->sample_of_notes, offset + 33, NUM_NOTES);
1055 instr->volume_envelope.num_points = READ_U8(offset + 225);
1056 instr->panning_envelope.num_points = READ_U8(offset + 226);
1058 for(uint8_t
j = 0;
j < instr->volume_envelope.num_points; ++
j) {
1059 instr->volume_envelope.points[
j].frame = READ_U16(offset + 129 + 4 *
j);
1060 instr->volume_envelope.points[
j].value = READ_U16(offset + 129 + 4 *
j + 2);
1063 for(uint8_t
j = 0;
j < instr->panning_envelope.num_points; ++
j) {
1064 instr->panning_envelope.points[
j].frame = READ_U16(offset + 177 + 4 *
j);
1065 instr->panning_envelope.points[
j].value = READ_U16(offset + 177 + 4 *
j + 2);
1068 instr->volume_envelope.sustain_point = READ_U8(offset + 227);
1069 instr->volume_envelope.loop_start_point = READ_U8(offset + 228);
1070 instr->volume_envelope.loop_end_point = READ_U8(offset + 229);
1072 instr->panning_envelope.sustain_point = READ_U8(offset + 230);
1073 instr->panning_envelope.loop_start_point = READ_U8(offset + 231);
1074 instr->panning_envelope.loop_end_point = READ_U8(offset + 232);
1076 uint8_t flags = READ_U8(offset + 233);
1077 instr->volume_envelope.enabled = flags & (1 << 0);
1078 instr->volume_envelope.sustain_enabled = flags & (1 << 1);
1079 instr->volume_envelope.loop_enabled = flags & (1 << 2);
1081 flags = READ_U8(offset + 234);
1082 instr->panning_envelope.enabled = flags & (1 << 0);
1083 instr->panning_envelope.sustain_enabled = flags & (1 << 1);
1084 instr->panning_envelope.loop_enabled = flags & (1 << 2);
1086 instr->vibrato_type = READ_U8(offset + 235);
1087 if(instr->vibrato_type == 2) {
1088 instr->vibrato_type = 1;
1089 }
else if(instr->vibrato_type == 1) {
1090 instr->vibrato_type = 2;
1092 instr->vibrato_sweep = READ_U8(offset + 236);
1093 instr->vibrato_depth = READ_U8(offset + 237);
1094 instr->vibrato_rate = READ_U8(offset + 238);
1095 instr->volume_fadeout = READ_U16(offset + 239);
1097 instr->samples = (jar_xm_sample_t*)mempool;
1098 mempool += instr->num_samples *
sizeof(jar_xm_sample_t);
1100 instr->samples =
NULL;
1104 offset += READ_U32(offset);
1106 for(uint16_t
j = 0;
j < instr->num_samples; ++
j) {
1108 jar_xm_sample_t*
sample = instr->samples +
j;
1111 sample->loop_start = READ_U32(offset + 4);
1112 sample->loop_length = READ_U32(offset + 8);
1114 sample->
volume = (float)READ_U8(offset + 12) / (float)0x40;
1117 uint8_t flags = READ_U8(offset + 14);
1118 if((flags & 3) == 0) {
1119 sample->loop_type = jar_xm_NO_LOOP;
1120 }
else if((flags & 3) == 1) {
1121 sample->loop_type = jar_xm_FORWARD_LOOP;
1123 sample->loop_type = jar_xm_PING_PONG_LOOP;
1126 sample->bits = (flags & (1 << 4)) ? 16 : 8;
1128 sample->panning = (float)READ_U8(offset + 15) / (float)0xFF;
1129 sample->relative_note = (int8_t)READ_U8(offset + 16);
1130 READ_MEMCPY(
sample->
name, 18, SAMPLE_NAME_LENGTH);
1131 sample->data = (
float*)mempool;
1136 sample->loop_start >>= 1;
1137 sample->loop_length >>= 1;
1145 offset += sample_header_size;
1148 for(uint16_t
j = 0;
j < instr->num_samples; ++
j) {
1150 jar_xm_sample_t*
sample = instr->samples +
j;
1155 for(uint32_t k = 0; k < length; ++k) {
1156 v =
v + (int16_t)READ_U16(offset + (k << 1));
1157 sample->data[k] = (float)
v / (
float)(1 << 15);
1162 for(uint32_t k = 0; k < length; ++k) {
1163 v =
v + (int8_t)READ_U8(offset + k);
1164 sample->data[k] = (float)
v / (
float)(1 << 7);
1180 static float jar_xm_waveform(jar_xm_waveform_type_t, uint8_t);
1181 static void jar_xm_autovibrato(
jar_xm_context_t*, jar_xm_channel_context_t*);
1182 static void jar_xm_vibrato(
jar_xm_context_t*, jar_xm_channel_context_t*, uint8_t, uint16_t);
1183 static void jar_xm_tremolo(
jar_xm_context_t*, jar_xm_channel_context_t*, uint8_t, uint16_t);
1184 static void jar_xm_arpeggio(
jar_xm_context_t*, jar_xm_channel_context_t*, uint8_t, uint16_t);
1185 static void jar_xm_tone_portamento(
jar_xm_context_t*, jar_xm_channel_context_t*);
1186 static void jar_xm_pitch_slide(
jar_xm_context_t*, jar_xm_channel_context_t*,
float);
1187 static void jar_xm_panning_slide(jar_xm_channel_context_t*, uint8_t);
1188 static void jar_xm_volume_slide(jar_xm_channel_context_t*, uint8_t);
1190 static float jar_xm_envelope_lerp(jar_xm_envelope_point_t*, jar_xm_envelope_point_t*, uint16_t);
1191 static void jar_xm_envelope_tick(jar_xm_channel_context_t*, jar_xm_envelope_t*, uint16_t*,
float*);
1192 static void jar_xm_envelopes(jar_xm_channel_context_t*);
1194 static float jar_xm_linear_period(
float);
1195 static float jar_xm_linear_frequency(
float);
1196 static float jar_xm_amiga_period(
float);
1197 static float jar_xm_amiga_frequency(
float);
1200 static void jar_xm_update_frequency(
jar_xm_context_t*, jar_xm_channel_context_t*);
1202 static void jar_xm_handle_note_and_instrument(
jar_xm_context_t*, jar_xm_channel_context_t*, jar_xm_pattern_slot_t*);
1203 static void jar_xm_trigger_note(
jar_xm_context_t*, jar_xm_channel_context_t*,
unsigned int flags);
1204 static void jar_xm_cut_note(jar_xm_channel_context_t*);
1205 static void jar_xm_key_off(jar_xm_channel_context_t*);
1211 static float jar_xm_next_of_sample(jar_xm_channel_context_t*);
1216 #define jar_xm_TRIGGER_KEEP_VOLUME (1 << 0)
1217 #define jar_xm_TRIGGER_KEEP_PERIOD (1 << 1)
1218 #define jar_xm_TRIGGER_KEEP_SAMPLE_POSITION (1 << 2)
1220 static const uint16_t amiga_frequencies[] = {
1221 1712, 1616, 1525, 1440,
1222 1357, 1281, 1209, 1141,
1223 1077, 1017, 961, 907,
1227 static const float multi_retrig_add[] = {
1228 0.f, -1.f, -2.f, -4.f,
1229 -8.f, -16.f, 0.f, 0.f,
1234 static const float multi_retrig_multiply[] = {
1236 1.f, 1.f, .6666667f, .5f,
1241 #define jar_xm_CLAMP_UP1F(vol, limit) do { \
1242 if((vol) > (limit)) (vol) = (limit); \
1244 #define jar_xm_CLAMP_UP(vol) jar_xm_CLAMP_UP1F((vol), 1.f)
1246 #define jar_xm_CLAMP_DOWN1F(vol, limit) do { \
1247 if((vol) < (limit)) (vol) = (limit); \
1249 #define jar_xm_CLAMP_DOWN(vol) jar_xm_CLAMP_DOWN1F((vol), .0f)
1251 #define jar_xm_CLAMP2F(vol, up, down) do { \
1252 if((vol) > (up)) (vol) = (up); \
1253 else if((vol) < (down)) (vol) = (down); \
1255 #define jar_xm_CLAMP(vol) jar_xm_CLAMP2F((vol), 1.f, .0f)
1257 #define jar_xm_SLIDE_TOWARDS(val, goal, incr) do { \
1258 if((val) > (goal)) { \
1260 jar_xm_CLAMP_DOWN1F((val), (goal)); \
1261 } else if((val) < (goal)) { \
1263 jar_xm_CLAMP_UP1F((val), (goal)); \
1267 #define jar_xm_LERP(u, v, t) ((u) + (t) * ((v) - (u)))
1268 #define jar_xm_INVERSE_LERP(u, v, lerp) (((lerp) - (u)) / ((v) - (u)))
1270 #define HAS_TONE_PORTAMENTO(s) ((s)->effect_type == 3 \
1271 || (s)->effect_type == 5 \
1272 || ((s)->volume_column >> 4) == 0xF)
1273 #define HAS_ARPEGGIO(s) ((s)->effect_type == 0 \
1274 && (s)->effect_param != 0)
1275 #define HAS_VIBRATO(s) ((s)->effect_type == 4 \
1276 || (s)->effect_param == 6 \
1277 || ((s)->volume_column >> 4) == 0xB)
1278 #define NOTE_IS_VALID(n) ((n) > 0 && (n) < 97)
1282 static float jar_xm_waveform(jar_xm_waveform_type_t waveform, uint8_t step) {
1283 static unsigned int next_rand = 24492;
1288 case jar_xm_SINE_WAVEFORM:
1291 return -sinf(2.f * 3.141592f * (
float)step / (
float)0x40);
1293 case jar_xm_RAMP_DOWN_WAVEFORM:
1295 return (
float)(0x20 - step) / 0x20;
1297 case jar_xm_SQUARE_WAVEFORM:
1299 return (step >= 0x20) ? 1.f : -1.f;
1301 case jar_xm_RANDOM_WAVEFORM:
1304 next_rand = next_rand * 1103515245 + 12345;
1305 return (
float)((next_rand >> 16) & 0x7FFF) / (float)0x4000 - 1.f;
1307 case jar_xm_RAMP_UP_WAVEFORM:
1309 return (
float)(step - 0x20) / 0x20;
1319 static void jar_xm_autovibrato(
jar_xm_context_t* ctx, jar_xm_channel_context_t* ch) {
1320 if(ch->instrument ==
NULL || ch->instrument->vibrato_depth == 0)
return;
1321 jar_xm_instrument_t* instr = ch->instrument;
1324 if(ch->autovibrato_ticks < instr->vibrato_sweep) {
1326 sweep = jar_xm_LERP(0.f, 1.f, (
float)ch->autovibrato_ticks / (
float)instr->vibrato_sweep);
1329 unsigned int step = ((ch->autovibrato_ticks++) * instr->vibrato_rate) >> 2;
1330 ch->autovibrato_note_offset = .25f * jar_xm_waveform(instr->vibrato_type, step)
1331 * (float)instr->vibrato_depth / (
float)0xF * sweep;
1332 jar_xm_update_frequency(ctx, ch);
1335 static void jar_xm_vibrato(
jar_xm_context_t* ctx, jar_xm_channel_context_t* ch, uint8_t
param, uint16_t pos) {
1336 unsigned int step = pos * (
param >> 4);
1337 ch->vibrato_note_offset =
1339 * jar_xm_waveform(ch->vibrato_waveform, step)
1340 * (float)(
param & 0x0F) / (float)0xF;
1341 jar_xm_update_frequency(ctx, ch);
1344 static void jar_xm_tremolo(
jar_xm_context_t* ctx, jar_xm_channel_context_t* ch, uint8_t
param, uint16_t pos) {
1345 unsigned int step = pos * (
param >> 4);
1348 ch->tremolo_volume = -1.f * jar_xm_waveform(ch->tremolo_waveform, step)
1349 * (float)(
param & 0x0F) / (float)0xF;
1352 static void jar_xm_arpeggio(
jar_xm_context_t* ctx, jar_xm_channel_context_t* ch, uint8_t
param, uint16_t tick) {
1355 ch->arp_in_progress =
false;
1356 ch->arp_note_offset = 0;
1359 ch->arp_in_progress =
true;
1360 ch->arp_note_offset =
param >> 4;
1363 ch->arp_in_progress =
true;
1364 ch->arp_note_offset =
param & 0x0F;
1368 jar_xm_update_frequency(ctx, ch);
1371 static void jar_xm_tone_portamento(
jar_xm_context_t* ctx, jar_xm_channel_context_t* ch) {
1374 if(ch->tone_portamento_target_period == 0.f)
return;
1376 if(ch->period != ch->tone_portamento_target_period) {
1377 jar_xm_SLIDE_TOWARDS(ch->period,
1378 ch->tone_portamento_target_period,
1379 (ctx->module.frequency_type == jar_xm_LINEAR_FREQUENCIES ?
1380 4.f : 1.f) * ch->tone_portamento_param
1382 jar_xm_update_frequency(ctx, ch);
1386 static void jar_xm_pitch_slide(
jar_xm_context_t* ctx, jar_xm_channel_context_t* ch,
float period_offset) {
1389 if(ctx->module.frequency_type == jar_xm_LINEAR_FREQUENCIES) {
1390 period_offset *= 4.f;
1393 ch->period += period_offset;
1394 jar_xm_CLAMP_DOWN(ch->period);
1397 jar_xm_update_frequency(ctx, ch);
1400 static void jar_xm_panning_slide(jar_xm_channel_context_t* ch, uint8_t rawval) {
1403 if((rawval & 0xF0) && (rawval & 0x0F)) {
1410 f = (float)(rawval >> 4) / (float)0xFF;
1412 jar_xm_CLAMP_UP(ch->panning);
1415 f = (float)(rawval & 0x0F) / (float)0xFF;
1417 jar_xm_CLAMP_DOWN(ch->panning);
1421 static void jar_xm_volume_slide(jar_xm_channel_context_t* ch, uint8_t rawval) {
1424 if((rawval & 0xF0) && (rawval & 0x0F)) {
1431 f = (float)(rawval >> 4) / (float)0x40;
1433 jar_xm_CLAMP_UP(ch->volume);
1436 f = (float)(rawval & 0x0F) / (float)0x40;
1438 jar_xm_CLAMP_DOWN(ch->volume);
1442 static float jar_xm_envelope_lerp(jar_xm_envelope_point_t* a, jar_xm_envelope_point_t* b, uint16_t pos) {
1444 if(pos <= a->frame)
return a->value;
1445 else if(pos >= b->frame)
return b->value;
1447 float p = (float)(pos - a->frame) / (float)(b->frame - a->frame);
1448 return a->value * (1 - p) + b->value * p;
1454 if(ctx->current_table_index >= ctx->module.length) {
1455 ctx->current_table_index = ctx->module.restart_position;
1459 static float jar_xm_linear_period(
float note) {
1460 return 7680.f -
note * 64.f;
1463 static float jar_xm_linear_frequency(
float period) {
1464 return 8363.f * powf(2.f, (4608.f - period) / 768.f);
1467 static float jar_xm_amiga_period(
float note) {
1468 unsigned int intnote =
note;
1469 uint8_t a = intnote % 12;
1470 int8_t octave =
note / 12.f - 2;
1471 uint16_t p1 = amiga_frequencies[a], p2 = amiga_frequencies[a + 1];
1476 }
else if(octave < 0) {
1481 return jar_xm_LERP(p1, p2,
note - intnote);
1484 static float jar_xm_amiga_frequency(
float period) {
1485 if(period == .0f)
return .0f;
1489 return 7093789.2f / (period * 2.f);
1493 switch(ctx->module.frequency_type) {
1494 case jar_xm_LINEAR_FREQUENCIES:
1495 return jar_xm_linear_period(
note);
1496 case jar_xm_AMIGA_FREQUENCIES:
1497 return jar_xm_amiga_period(
note);
1502 static float jar_xm_frequency(
jar_xm_context_t* ctx,
float period,
float note_offset) {
1508 switch(ctx->module.frequency_type) {
1510 case jar_xm_LINEAR_FREQUENCIES:
1511 return jar_xm_linear_frequency(period - 64.f * note_offset);
1513 case jar_xm_AMIGA_FREQUENCIES:
1514 if(note_offset == 0) {
1516 return jar_xm_amiga_frequency(period);
1523 if(period > amiga_frequencies[0]) {
1525 while(period > (amiga_frequencies[0] << (-octave))) --octave;
1526 }
else if(period < amiga_frequencies[12]) {
1528 while(period < (amiga_frequencies[12] >> octave)) ++octave;
1532 for(uint8_t
i = 0;
i < 12; ++
i) {
1533 p1 = amiga_frequencies[
i], p2 = amiga_frequencies[
i + 1];
1538 }
else if(octave < 0) {
1543 if(p2 <= period && period <= p1) {
1550 DEBUG(
"%i <= %f <= %i should hold but doesn't, this is a bug", p2, period, p1);
1553 note = 12.f * (octave + 2) + a + jar_xm_INVERSE_LERP(p1, p2, period);
1555 return jar_xm_amiga_frequency(jar_xm_amiga_period(
note + note_offset));
1562 static void jar_xm_update_frequency(
jar_xm_context_t* ctx, jar_xm_channel_context_t* ch) {
1563 ch->frequency = jar_xm_frequency(
1565 (ch->arp_note_offset > 0 ? ch->arp_note_offset : (
1566 ch->vibrato_note_offset + ch->autovibrato_note_offset
1569 ch->step = ch->frequency / ctx->rate;
1572 static void jar_xm_handle_note_and_instrument(
jar_xm_context_t* ctx, jar_xm_channel_context_t* ch,
1573 jar_xm_pattern_slot_t*
s) {
1574 if(
s->instrument > 0) {
1575 if(HAS_TONE_PORTAMENTO(ch->current) && ch->instrument !=
NULL && ch->sample !=
NULL) {
1577 jar_xm_trigger_note(ctx, ch, jar_xm_TRIGGER_KEEP_PERIOD | jar_xm_TRIGGER_KEEP_SAMPLE_POSITION);
1578 }
else if(
s->instrument > ctx->module.num_instruments) {
1580 jar_xm_cut_note(ch);
1581 ch->instrument =
NULL;
1584 ch->instrument = ctx->module.instruments + (
s->instrument - 1);
1585 if(
s->note == 0 && ch->sample !=
NULL) {
1588 jar_xm_trigger_note(ctx, ch, jar_xm_TRIGGER_KEEP_SAMPLE_POSITION);
1593 if(NOTE_IS_VALID(
s->note)) {
1597 jar_xm_instrument_t* instr = ch->instrument;
1599 if(HAS_TONE_PORTAMENTO(ch->current) && instr !=
NULL && ch->sample !=
NULL) {
1601 ch->note =
s->note + ch->sample->relative_note + ch->sample->finetune / 128.f - 1.f;
1602 ch->tone_portamento_target_period = jar_xm_period(ctx, ch->note);
1603 }
else if(instr ==
NULL || ch->instrument->num_samples == 0) {
1605 jar_xm_cut_note(ch);
1607 if(instr->sample_of_notes[
s->note - 1] < instr->num_samples) {
1609 for(
unsigned int z = 0;
z < jar_xm_SAMPLE_RAMPING_POINTS; ++
z) {
1610 ch->end_of_previous_sample[
z] = jar_xm_next_of_sample(ch);
1612 ch->frame_count = 0;
1614 ch->sample = instr->samples + instr->sample_of_notes[
s->note - 1];
1615 ch->orig_note = ch->note =
s->note + ch->sample->relative_note
1616 + ch->sample->finetune / 128.f - 1.f;
1617 if(
s->instrument > 0) {
1618 jar_xm_trigger_note(ctx, ch, 0);
1621 jar_xm_trigger_note(ctx, ch, jar_xm_TRIGGER_KEEP_VOLUME);
1625 jar_xm_cut_note(ch);
1628 }
else if(
s->note == 97) {
1633 switch(
s->volume_column >> 4) {
1636 if(
s->volume_column > 0x50)
break;
1642 ch->volume = (float)(
s->volume_column - 0x10) / (float)0x40;
1646 jar_xm_volume_slide(ch,
s->volume_column & 0x0F);
1650 jar_xm_volume_slide(ch,
s->volume_column << 4);
1654 ch->vibrato_param = (ch->vibrato_param & 0x0F) | ((
s->volume_column & 0x0F) << 4);
1658 ch->panning = (float)(
1659 ((
s->volume_column & 0x0F) << 4) | (
s->volume_column & 0x0F)
1664 if(
s->volume_column & 0x0F) {
1665 ch->tone_portamento_param = ((
s->volume_column & 0x0F) << 4)
1666 | (
s->volume_column & 0x0F);
1675 switch(
s->effect_type) {
1678 if(
s->effect_param > 0) {
1679 ch->portamento_up_param =
s->effect_param;
1684 if(
s->effect_param > 0) {
1685 ch->portamento_down_param =
s->effect_param;
1690 if(
s->effect_param > 0) {
1691 ch->tone_portamento_param =
s->effect_param;
1696 if(
s->effect_param & 0x0F) {
1698 ch->vibrato_param = (ch->vibrato_param & 0xF0) | (
s->effect_param & 0x0F);
1700 if(
s->effect_param >> 4) {
1702 ch->vibrato_param = (
s->effect_param & 0xF0) | (ch->vibrato_param & 0x0F);
1707 if(
s->effect_param > 0) {
1708 ch->volume_slide_param =
s->effect_param;
1713 if(
s->effect_param > 0) {
1714 ch->volume_slide_param =
s->effect_param;
1719 if(
s->effect_param & 0x0F) {
1721 ch->tremolo_param = (ch->tremolo_param & 0xF0) | (
s->effect_param & 0x0F);
1723 if(
s->effect_param >> 4) {
1725 ch->tremolo_param = (
s->effect_param & 0xF0) | (ch->tremolo_param & 0x0F);
1730 ch->panning = (float)
s->effect_param / (
float)0xFF;
1734 if(ch->sample !=
NULL && NOTE_IS_VALID(
s->note)) {
1735 uint32_t final_offset =
s->effect_param << (ch->sample->bits == 16 ? 7 : 8);
1736 if(final_offset >= ch->sample->length) {
1738 ch->sample_position = -1;
1741 ch->sample_position = final_offset;
1746 if(
s->effect_param > 0) {
1747 ch->volume_slide_param =
s->effect_param;
1752 if(
s->effect_param < ctx->module.length) {
1753 ctx->position_jump =
true;
1754 ctx->jump_dest =
s->effect_param;
1759 ch->volume = (float)((
s->effect_param > 0x40)
1760 ? 0x40 :
s->effect_param) / (
float)0x40;
1765 ctx->pattern_break =
true;
1766 ctx->jump_row = (
s->effect_param >> 4) * 10 + (
s->effect_param & 0x0F);
1770 switch(
s->effect_param >> 4) {
1773 if(
s->effect_param & 0x0F) {
1774 ch->fine_portamento_up_param =
s->effect_param & 0x0F;
1776 jar_xm_pitch_slide(ctx, ch, -ch->fine_portamento_up_param);
1780 if(
s->effect_param & 0x0F) {
1781 ch->fine_portamento_down_param =
s->effect_param & 0x0F;
1783 jar_xm_pitch_slide(ctx, ch, ch->fine_portamento_down_param);
1787 ch->vibrato_waveform =
s->effect_param & 3;
1788 ch->vibrato_waveform_retrigger = !((
s->effect_param >> 2) & 1);
1792 if(NOTE_IS_VALID(ch->current->note) && ch->sample !=
NULL) {
1793 ch->note = ch->current->note + ch->sample->relative_note +
1794 (float)(((
s->effect_param & 0x0F) - 8) << 4) / 128.f - 1.f;
1795 ch->period = jar_xm_period(ctx, ch->note);
1796 jar_xm_update_frequency(ctx, ch);
1801 if(
s->effect_param & 0x0F) {
1802 if((
s->effect_param & 0x0F) == ch->pattern_loop_count) {
1804 ch->pattern_loop_count = 0;
1809 ch->pattern_loop_count++;
1810 ctx->position_jump =
true;
1811 ctx->jump_row = ch->pattern_loop_origin;
1812 ctx->jump_dest = ctx->current_table_index;
1815 ch->pattern_loop_origin = ctx->current_row;
1817 ctx->jump_row = ch->pattern_loop_origin;
1822 ch->tremolo_waveform =
s->effect_param & 3;
1823 ch->tremolo_waveform_retrigger = !((
s->effect_param >> 2) & 1);
1827 if(
s->effect_param & 0x0F) {
1828 ch->fine_volume_slide_param =
s->effect_param & 0x0F;
1830 jar_xm_volume_slide(ch, ch->fine_volume_slide_param << 4);
1834 if(
s->effect_param & 0x0F) {
1835 ch->fine_volume_slide_param =
s->effect_param & 0x0F;
1837 jar_xm_volume_slide(ch, ch->fine_volume_slide_param);
1845 if(
s->note == 0 &&
s->instrument == 0) {
1846 unsigned int flags = jar_xm_TRIGGER_KEEP_VOLUME;
1848 if(ch->current->effect_param & 0x0F) {
1849 ch->note = ch->orig_note;
1850 jar_xm_trigger_note(ctx, ch, flags);
1852 jar_xm_trigger_note(
1855 | jar_xm_TRIGGER_KEEP_PERIOD
1856 | jar_xm_TRIGGER_KEEP_SAMPLE_POSITION
1863 ctx->extra_ticks = (ch->current->effect_param & 0x0F) * ctx->tempo;
1873 if(
s->effect_param > 0) {
1874 if(
s->effect_param <= 0x1F) {
1875 ctx->tempo =
s->effect_param;
1877 ctx->bpm =
s->effect_param;
1883 ctx->global_volume = (float)((
s->effect_param > 0x40)
1884 ? 0x40 :
s->effect_param) / (
float)0x40;
1888 if(
s->effect_param > 0) {
1889 ch->global_volume_slide_param =
s->effect_param;
1894 ch->volume_envelope_frame_count =
s->effect_param;
1895 ch->panning_envelope_frame_count =
s->effect_param;
1899 if(
s->effect_param > 0) {
1900 ch->panning_slide_param =
s->effect_param;
1905 if(
s->effect_param > 0) {
1906 if((
s->effect_param >> 4) == 0) {
1908 ch->multi_retrig_param = (ch->multi_retrig_param & 0xF0) | (
s->effect_param & 0x0F);
1910 ch->multi_retrig_param =
s->effect_param;
1916 if(
s->effect_param > 0) {
1919 ch->tremor_param =
s->effect_param;
1924 switch(
s->effect_param >> 4) {
1927 if(
s->effect_param & 0x0F) {
1928 ch->extra_fine_portamento_up_param =
s->effect_param & 0x0F;
1930 jar_xm_pitch_slide(ctx, ch, -1.0f * ch->extra_fine_portamento_up_param);
1934 if(
s->effect_param & 0x0F) {
1935 ch->extra_fine_portamento_down_param =
s->effect_param & 0x0F;
1937 jar_xm_pitch_slide(ctx, ch, ch->extra_fine_portamento_down_param);
1952 static void jar_xm_trigger_note(
jar_xm_context_t* ctx, jar_xm_channel_context_t* ch,
unsigned int flags) {
1953 if(!(flags & jar_xm_TRIGGER_KEEP_SAMPLE_POSITION)) {
1954 ch->sample_position = 0.f;
1958 if(ch->sample !=
NULL) {
1959 if(!(flags & jar_xm_TRIGGER_KEEP_VOLUME)) {
1960 ch->volume = ch->sample->volume;
1963 ch->panning = ch->sample->panning;
1966 ch->sustained =
true;
1967 ch->fadeout_volume = ch->volume_envelope_volume = 1.0f;
1968 ch->panning_envelope_panning = .5f;
1969 ch->volume_envelope_frame_count = ch->panning_envelope_frame_count = 0;
1970 ch->vibrato_note_offset = 0.f;
1971 ch->tremolo_volume = 0.f;
1972 ch->tremor_on =
false;
1974 ch->autovibrato_ticks = 0;
1976 if(ch->vibrato_waveform_retrigger) {
1977 ch->vibrato_ticks = 0;
1980 if(ch->tremolo_waveform_retrigger) {
1981 ch->tremolo_ticks = 0;
1984 if(!(flags & jar_xm_TRIGGER_KEEP_PERIOD)) {
1985 ch->period = jar_xm_period(ctx, ch->note);
1986 jar_xm_update_frequency(ctx, ch);
1989 ch->latest_trigger = ctx->generated_samples;
1990 if(ch->instrument !=
NULL) {
1991 ch->instrument->latest_trigger = ctx->generated_samples;
1993 if(ch->sample !=
NULL) {
1994 ch->sample->latest_trigger = ctx->generated_samples;
1998 static void jar_xm_cut_note(jar_xm_channel_context_t* ch) {
2003 static void jar_xm_key_off(jar_xm_channel_context_t* ch) {
2005 ch->sustained =
false;
2008 if(ch->instrument ==
NULL || !ch->instrument->volume_envelope.enabled) {
2009 jar_xm_cut_note(ch);
2014 if(ctx->position_jump) {
2015 ctx->current_table_index = ctx->jump_dest;
2016 ctx->current_row = ctx->jump_row;
2017 ctx->position_jump =
false;
2018 ctx->pattern_break =
false;
2020 jar_xm_post_pattern_change(ctx);
2021 }
else if(ctx->pattern_break) {
2022 ctx->current_table_index++;
2023 ctx->current_row = ctx->jump_row;
2024 ctx->pattern_break =
false;
2026 jar_xm_post_pattern_change(ctx);
2029 jar_xm_pattern_t* cur = ctx->module.patterns + ctx->module.pattern_table[ctx->current_table_index];
2030 bool in_a_loop =
false;
2033 for(uint8_t
i = 0;
i < ctx->module.num_channels; ++
i) {
2034 jar_xm_pattern_slot_t*
s = cur->slots + ctx->current_row * ctx->module.num_channels +
i;
2035 jar_xm_channel_context_t* ch = ctx->channels +
i;
2039 if(
s->effect_type != 0xE ||
s->effect_param >> 4 != 0xD) {
2040 jar_xm_handle_note_and_instrument(ctx, ch,
s);
2042 ch->note_delay_param =
s->effect_param & 0x0F;
2045 if(!in_a_loop && ch->pattern_loop_count > 0) {
2052 ctx->loop_count = (ctx->row_loop_count[MAX_NUM_ROWS * ctx->current_table_index + ctx->current_row]++);
2059 if(!ctx->position_jump && !ctx->pattern_break &&
2060 (ctx->current_row >= cur->num_rows || ctx->current_row == 0)) {
2061 ctx->current_table_index++;
2062 ctx->current_row = ctx->jump_row;
2066 jar_xm_post_pattern_change(ctx);
2070 static void jar_xm_envelope_tick(jar_xm_channel_context_t* ch,
2071 jar_xm_envelope_t* env,
2074 if(env->num_points < 2) {
2076 if(env->num_points == 1) {
2078 *outval = (float)env->points[0].value / (
float)0x40;
2088 if(env->loop_enabled) {
2089 uint16_t loop_start = env->points[env->loop_start_point].frame;
2090 uint16_t loop_end = env->points[env->loop_end_point].frame;
2091 uint16_t loop_length = loop_end - loop_start;
2093 if(*counter >= loop_end) {
2094 *counter -= loop_length;
2098 for(
j = 0;
j < (env->num_points - 2); ++
j) {
2099 if(env->points[
j].frame <= *counter &&
2100 env->points[
j+1].frame >= *counter) {
2105 *outval = jar_xm_envelope_lerp(env->points +
j, env->points +
j + 1, *counter) / (float)0x40;
2108 if(!ch->sustained || !env->sustain_enabled ||
2109 *counter != env->points[env->sustain_point].frame) {
2115 static void jar_xm_envelopes(jar_xm_channel_context_t* ch) {
2116 if(ch->instrument !=
NULL) {
2117 if(ch->instrument->volume_envelope.enabled) {
2118 if(!ch->sustained) {
2119 ch->fadeout_volume -= (float)ch->instrument->volume_fadeout / 65536.f;
2120 jar_xm_CLAMP_DOWN(ch->fadeout_volume);
2123 jar_xm_envelope_tick(ch,
2124 &(ch->instrument->volume_envelope),
2125 &(ch->volume_envelope_frame_count),
2126 &(ch->volume_envelope_volume));
2129 if(ch->instrument->panning_envelope.enabled) {
2130 jar_xm_envelope_tick(ch,
2131 &(ch->instrument->panning_envelope),
2132 &(ch->panning_envelope_frame_count),
2133 &(ch->panning_envelope_panning));
2139 if(ctx->current_tick == 0) {
2143 for(uint8_t
i = 0;
i < ctx->module.num_channels; ++
i) {
2144 jar_xm_channel_context_t* ch = ctx->channels +
i;
2146 jar_xm_envelopes(ch);
2147 jar_xm_autovibrato(ctx, ch);
2149 if(ch->arp_in_progress && !HAS_ARPEGGIO(ch->current)) {
2150 ch->arp_in_progress =
false;
2151 ch->arp_note_offset = 0;
2152 jar_xm_update_frequency(ctx, ch);
2154 if(ch->vibrato_in_progress && !HAS_VIBRATO(ch->current)) {
2155 ch->vibrato_in_progress =
false;
2156 ch->vibrato_note_offset = 0.f;
2157 jar_xm_update_frequency(ctx, ch);
2160 switch(ch->current->volume_column >> 4) {
2163 if(ctx->current_tick == 0)
break;
2164 jar_xm_volume_slide(ch, ch->current->volume_column & 0x0F);
2168 if(ctx->current_tick == 0)
break;
2169 jar_xm_volume_slide(ch, ch->current->volume_column << 4);
2173 if(ctx->current_tick == 0)
break;
2174 ch->vibrato_in_progress =
false;
2175 jar_xm_vibrato(ctx, ch, ch->vibrato_param, ch->vibrato_ticks++);
2179 if(ctx->current_tick == 0)
break;
2180 jar_xm_panning_slide(ch, ch->current->volume_column & 0x0F);
2184 if(ctx->current_tick == 0)
break;
2185 jar_xm_panning_slide(ch, ch->current->volume_column << 4);
2189 if(ctx->current_tick == 0)
break;
2190 jar_xm_tone_portamento(ctx, ch);
2198 switch(ch->current->effect_type) {
2201 if(ch->current->effect_param > 0) {
2202 char arp_offset = ctx->tempo % 3;
2203 switch(arp_offset) {
2205 if(ctx->current_tick == 1) {
2206 ch->arp_in_progress =
true;
2207 ch->arp_note_offset = ch->current->effect_param >> 4;
2208 jar_xm_update_frequency(ctx, ch);
2213 if(ctx->current_tick == 0) {
2214 ch->arp_in_progress =
false;
2215 ch->arp_note_offset = 0;
2216 jar_xm_update_frequency(ctx, ch);
2221 jar_xm_arpeggio(ctx, ch, ch->current->effect_param, ctx->current_tick - arp_offset);
2229 if(ctx->current_tick == 0)
break;
2230 jar_xm_pitch_slide(ctx, ch, -ch->portamento_up_param);
2234 if(ctx->current_tick == 0)
break;
2235 jar_xm_pitch_slide(ctx, ch, ch->portamento_down_param);
2239 if(ctx->current_tick == 0)
break;
2240 jar_xm_tone_portamento(ctx, ch);
2244 if(ctx->current_tick == 0)
break;
2245 ch->vibrato_in_progress =
true;
2246 jar_xm_vibrato(ctx, ch, ch->vibrato_param, ch->vibrato_ticks++);
2250 if(ctx->current_tick == 0)
break;
2251 jar_xm_tone_portamento(ctx, ch);
2252 jar_xm_volume_slide(ch, ch->volume_slide_param);
2256 if(ctx->current_tick == 0)
break;
2257 ch->vibrato_in_progress =
true;
2258 jar_xm_vibrato(ctx, ch, ch->vibrato_param, ch->vibrato_ticks++);
2259 jar_xm_volume_slide(ch, ch->volume_slide_param);
2263 if(ctx->current_tick == 0)
break;
2264 jar_xm_tremolo(ctx, ch, ch->tremolo_param, ch->tremolo_ticks++);
2268 if(ctx->current_tick == 0)
break;
2269 jar_xm_volume_slide(ch, ch->volume_slide_param);
2273 switch(ch->current->effect_param >> 4) {
2276 if(ctx->current_tick != 0 && ch->current->effect_param & 0x0F) {
2277 if(!(ctx->current_tick % (ch->current->effect_param & 0x0F))) {
2278 jar_xm_trigger_note(ctx, ch, 0);
2279 jar_xm_envelopes(ch);
2285 if((ch->current->effect_param & 0x0F) == ctx->current_tick) {
2286 jar_xm_cut_note(ch);
2291 if(ch->note_delay_param == ctx->current_tick) {
2292 jar_xm_handle_note_and_instrument(ctx, ch, ch->current);
2293 jar_xm_envelopes(ch);
2304 if(ctx->current_tick == 0)
break;
2305 if((ch->global_volume_slide_param & 0xF0) &&
2306 (ch->global_volume_slide_param & 0x0F)) {
2310 if(ch->global_volume_slide_param & 0xF0) {
2312 float f = (float)(ch->global_volume_slide_param >> 4) / (float)0x40;
2313 ctx->global_volume += f;
2314 jar_xm_CLAMP_UP(ctx->global_volume);
2317 float f = (float)(ch->global_volume_slide_param & 0x0F) / (float)0x40;
2318 ctx->global_volume -= f;
2319 jar_xm_CLAMP_DOWN(ctx->global_volume);
2326 if(ctx->current_tick == ch->current->effect_param) {
2332 if(ctx->current_tick == 0)
break;
2333 jar_xm_panning_slide(ch, ch->panning_slide_param);
2337 if(ctx->current_tick == 0)
break;
2338 if(((ch->multi_retrig_param) & 0x0F) == 0)
break;
2339 if((ctx->current_tick % (ch->multi_retrig_param & 0x0F)) == 0) {
2340 float v = ch->volume * multi_retrig_multiply[ch->multi_retrig_param >> 4]
2341 + multi_retrig_add[ch->multi_retrig_param >> 4];
2343 jar_xm_trigger_note(ctx, ch, 0);
2349 if(ctx->current_tick == 0)
break;
2351 (ctx->current_tick - 1) % ((ch->tremor_param >> 4) + (ch->tremor_param & 0x0F) + 2)
2353 (ch->tremor_param >> 4)
2362 float panning, volume;
2364 panning = ch->panning +
2365 (ch->panning_envelope_panning - .5f) * (.5f - fabsf(ch->panning - .5f)) * 2.0f;
2370 volume = ch->volume + ch->tremolo_volume;
2371 jar_xm_CLAMP(volume);
2372 volume *= ch->fadeout_volume * ch->volume_envelope_volume;
2376 ch->target_panning = panning;
2377 ch->target_volume = volume;
2379 ch->actual_panning = panning;
2380 ch->actual_volume = volume;
2384 ctx->current_tick++;
2385 if(ctx->current_tick >= ctx->tempo + ctx->extra_ticks) {
2386 ctx->current_tick = 0;
2387 ctx->extra_ticks = 0;
2391 ctx->remaining_samples_in_tick += (float)ctx->rate / ((
float)ctx->bpm * 0.4f);
2394 static float jar_xm_next_of_sample(jar_xm_channel_context_t* ch) {
2395 if(ch->instrument ==
NULL || ch->sample ==
NULL || ch->sample_position < 0) {
2397 if(ch->frame_count < jar_xm_SAMPLE_RAMPING_POINTS) {
2398 return jar_xm_LERP(ch->end_of_previous_sample[ch->frame_count], .0f,
2399 (
float)ch->frame_count / (
float)jar_xm_SAMPLE_RAMPING_POINTS);
2404 if(ch->sample->length == 0) {
2410 a = (uint32_t)ch->sample_position;
2416 t = ch->sample_position - a;
2418 u = ch->sample->data[a];
2420 switch(ch->sample->loop_type) {
2422 case jar_xm_NO_LOOP:
2424 v = (b < ch->sample->length) ? ch->sample->data[b] : .0f;
2426 ch->sample_position += ch->step;
2427 if(ch->sample_position >= ch->sample->length) {
2428 ch->sample_position = -1;
2432 case jar_xm_FORWARD_LOOP:
2434 v = ch->sample->data[
2435 (b == ch->sample->loop_end) ? ch->sample->loop_start : b
2438 ch->sample_position += ch->step;
2439 while(ch->sample_position >= ch->sample->loop_end) {
2440 ch->sample_position -= ch->sample->loop_length;
2444 case jar_xm_PING_PONG_LOOP:
2446 ch->sample_position += ch->step;
2448 ch->sample_position -= ch->step;
2454 v = (b >= ch->sample->loop_end) ? ch->sample->data[a] : ch->sample->data[b];
2456 if(ch->sample_position >= ch->sample->loop_end) {
2458 ch->sample_position = (ch->sample->loop_end << 1) - ch->sample_position;
2461 if(ch->sample_position >= ch->sample->length) {
2463 ch->sample_position -= ch->sample->length - 1;
2468 u = (b == 1 || b - 2 <= ch->sample->loop_start) ? ch->sample->data[a] : ch->sample->data[b - 2];
2470 if(ch->sample_position <= ch->sample->loop_start) {
2472 ch->sample_position = (ch->sample->loop_start << 1) - ch->sample_position;
2475 if(ch->sample_position <= .0f) {
2477 ch->sample_position = .0f;
2490 if(ch->frame_count < jar_xm_SAMPLE_RAMPING_POINTS) {
2492 return jar_xm_LERP(ch->end_of_previous_sample[ch->frame_count], endval,
2493 (
float)ch->frame_count / (
float)jar_xm_SAMPLE_RAMPING_POINTS);
2501 if(ctx->remaining_samples_in_tick <= 0) {
2504 ctx->remaining_samples_in_tick--;
2509 if(ctx->max_loop_count > 0 && ctx->loop_count >= ctx->max_loop_count) {
2513 for(uint8_t
i = 0;
i < ctx->module.num_channels; ++
i) {
2514 jar_xm_channel_context_t* ch = ctx->channels +
i;
2516 if(ch->instrument ==
NULL || ch->sample ==
NULL || ch->sample_position < 0) {
2520 const float fval = jar_xm_next_of_sample(ch);
2522 if(!ch->muted && !ch->instrument->muted) {
2523 *left += fval * ch->actual_volume * (1.f - ch->actual_panning);
2524 *
right += fval * ch->actual_volume * ch->actual_panning;
2529 jar_xm_SLIDE_TOWARDS(ch->actual_volume, ch->target_volume, ctx->volume_ramp);
2530 jar_xm_SLIDE_TOWARDS(ch->actual_panning, ch->target_panning, ctx->panning_ramp);
2534 const float fgvol = ctx->global_volume * ctx->amplification;
2539 if(fabs(*left) > 1 || fabs(*
right) > 1) {
2540 DEBUG(
"clipping frame: %f %f, this is a bad module or a libxm bug", *left, *
right);
2547 ctx->generated_samples += numsamples;
2548 for(
size_t i = 0;
i < numsamples;
i++) {
2549 jar_xm_sample(ctx, output + (2 *
i), output + (2 *
i + 1));
2562 total += ctx->remaining_samples_in_tick;
2563 ctx->remaining_samples_in_tick = 0;
2567 ctx->loop_count = currentLoopCount;
2582 #define DEBUG(...) do { \
2583 fprintf(stderr, __VA_ARGS__); \
2587 #define DEBUG_ERR(...) do { \
2588 fprintf(stderr, __VA_ARGS__); \
2592 #define FATAL(...) do { \
2593 fprintf(stderr, __VA_ARGS__); \
2598 #define FATAL_ERR(...) do { \
2599 fprintf(stderr, __VA_ARGS__); \
2609 #if defined(_MSC_VER) && _MSC_VER >= 1500
2611 fopen_s(&xmf, filename,
"rb");
2613 xmf = fopen(filename,
"rb");
2616 DEBUG_ERR(
"Could not open input file");
2621 fseek(xmf, 0, SEEK_END);
2626 DEBUG_ERR(
"fseek() failed");
2631 char* data = malloc(size + 1);
2632 if(fread(data, 1, size, xmf) < size) {
2634 DEBUG_ERR(
"fread() failed");
2646 DEBUG(
"could not create context: module is not sane\n");
2652 FATAL(
"could not create context: malloc failed\n");
2657 FATAL(
"could not create context: unknown error\n");
2669 #endif//end of JAR_XM_IMPLEMENTATION