vkQuake2 doxygen  1.0 dev
jar_xm.h
Go to the documentation of this file.
1 // jar_xm.h - v0.01 - public domain - Joshua Reisenauer, MAR 2016
2 //
3 // HISTORY:
4 //
5 // v0.01 2016-02-22 Setup
6 //
7 //
8 // USAGE:
9 //
10 // In ONE source file, put:
11 //
12 // #define JAR_XM_IMPLEMENTATION
13 // #include "jar_xm.h"
14 //
15 // Other source files should just include jar_xm.h
16 //
17 // SAMPLE CODE:
18 //
19 // jar_xm_context_t *musicptr;
20 // float musicBuffer[48000 / 60];
21 // int intro_load(void)
22 // {
23 // jar_xm_create_context_from_file(&musicptr, 48000, "Song.XM");
24 // return 1;
25 // }
26 // int intro_unload(void)
27 // {
28 // jar_xm_free_context(musicptr);
29 // return 1;
30 // }
31 // int intro_tick(long counter)
32 // {
33 // jar_xm_generate_samples(musicptr, musicBuffer, (48000 / 60) / 2);
34 // if(IsKeyDown(KEY_ENTER))
35 // return 1;
36 // return 0;
37 // }
38 //
39 //
40 // LISCENSE - FOR LIBXM:
41 //
42 // Author: Romain "Artefact2" Dalmaso <artefact2@gmail.com>
43 // Contributor: Dan Spencer <dan@atomicpotato.net>
44 // Repackaged into jar_xm.h By: Joshua Adam Reisenauer <kd7tck@gmail.com>
45 // This program is free software. It comes without any warranty, to the
46 // extent permitted by applicable law. You can redistribute it and/or
47 // modify it under the terms of the Do What The Fuck You Want To Public
48 // License, Version 2, as published by Sam Hocevar. See
49 // http://sam.zoy.org/wtfpl/COPYING for more details.
50 
51 #ifndef INCLUDE_JAR_XM_H
52 #define INCLUDE_JAR_XM_H
53 
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
58 
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <stdint.h>
62 #include <limits.h>
63 #include <string.h>
64 
65 #ifndef true
66  #include <stdbool.h>
67 #endif
68 
69 
70 
71 //-------------------------------------------------------------------------------
72 #ifdef __cplusplus
73 extern "C" {
74 #endif
75 
76 struct jar_xm_context_s;
77 typedef struct jar_xm_context_s jar_xm_context_t;
78 
95 int jar_xm_create_context_from_file(jar_xm_context_t** ctx, uint32_t rate, const char* filename);
96 
109 int jar_xm_create_context(jar_xm_context_t**, const char* moddata, uint32_t rate);
110 
121 int jar_xm_create_context_safe(jar_xm_context_t**, const char* moddata, size_t moddata_length, uint32_t rate);
122 
125 
131 void jar_xm_generate_samples(jar_xm_context_t*, float* output, size_t numsamples);
132 
138 void jar_xm_generate_samples_16bit(jar_xm_context_t* ctx, short* output, size_t numsamples)
139 {
140  float* musicBuffer = malloc((2*numsamples)*sizeof(float));
141  jar_xm_generate_samples(ctx, musicBuffer, numsamples);
142 
143  if(output){
144  size_t x;
145  for(x=0;x<2*numsamples;x++)
146  output[x] = (short)(musicBuffer[x] * SHRT_MAX);
147  }
148 
149  free(musicBuffer);
150 }
151 
157 void jar_xm_generate_samples_8bit(jar_xm_context_t* ctx, char* output, size_t numsamples)
158 {
159  float* musicBuffer = malloc((2*numsamples)*sizeof(float));
160  jar_xm_generate_samples(ctx, musicBuffer, numsamples);
161 
162  if(output){
163  size_t x;
164  for(x=0;x<2*numsamples;x++)
165  output[x] = (char)(musicBuffer[x] * CHAR_MAX);
166  }
167 
168  free(musicBuffer);
169 }
170 
171 
172 
180 void jar_xm_set_max_loop_count(jar_xm_context_t*, uint8_t loopcnt);
181 
186 
187 
188 
195 bool jar_xm_mute_channel(jar_xm_context_t*, uint16_t, bool);
196 
204 bool jar_xm_mute_instrument(jar_xm_context_t*, uint16_t, bool);
205 
206 
207 
210 
213 
214 
215 
218 
221 
224 
230 uint16_t jar_xm_get_number_of_rows(jar_xm_context_t*, uint16_t);
231 
234 
241 
242 
243 
249 void jar_xm_get_playing_speed(jar_xm_context_t*, uint16_t* bpm, uint16_t* tempo);
250 
264 void jar_xm_get_position(jar_xm_context_t*, uint8_t* pattern_index, uint8_t* pattern, uint8_t* row, uint64_t* samples);
265 
273 
283 uint64_t jar_xm_get_latest_trigger_of_sample(jar_xm_context_t*, uint16_t instr, uint16_t sample);
284 
291 
298 
299 #ifdef __cplusplus
300 }
301 #endif
302 //-------------------------------------------------------------------------------
303 #endif//end of INCLUDE_JAR_XM_H
304 
305 
306 
307 
308 
309 //Function Definitions-----------------------------------------------------------
310 #ifdef JAR_XM_IMPLEMENTATION
311 
312 #include <math.h>
313 #include <string.h>
314 
315 #if JAR_XM_DEBUG
316 #include <stdio.h>
317 #define DEBUG(fmt, ...) do { \
318  fprintf(stderr, "%s(): " fmt "\n", __func__, __VA_ARGS__); \
319  fflush(stderr); \
320  } while(0)
321 #else
322 #define DEBUG(...)
323 #endif
324 
325 #if jar_xm_BIG_ENDIAN
326 #error "Big endian platforms are not yet supported, sorry"
327 /* Make sure the compiler stops, even if #error is ignored */
328 extern int __fail[-1];
329 #endif
330 
331 /* ----- XM constants ----- */
332 
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
338 #define NUM_NOTES 96
339 #define NUM_ENVELOPE_POINTS 12
340 #define MAX_NUM_ROWS 256
341 
342 #if JAR_XM_RAMPING
343 #define jar_xm_SAMPLE_RAMPING_POINTS 0x20
344 #endif
345 
346 /* ----- Data types ----- */
347 
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,
354 };
355 typedef enum jar_xm_waveform_type_e jar_xm_waveform_type_t;
356 
357 enum jar_xm_loop_type_e {
358  jar_xm_NO_LOOP,
359  jar_xm_FORWARD_LOOP,
360  jar_xm_PING_PONG_LOOP,
361 };
362 typedef enum jar_xm_loop_type_e jar_xm_loop_type_t;
363 
364 enum jar_xm_frequency_type_e {
365  jar_xm_LINEAR_FREQUENCIES,
366  jar_xm_AMIGA_FREQUENCIES,
367 };
368 typedef enum jar_xm_frequency_type_e jar_xm_frequency_type_t;
369 
370 struct jar_xm_envelope_point_s {
371  uint16_t frame;
372  uint16_t value;
373 };
374 typedef struct jar_xm_envelope_point_s jar_xm_envelope_point_t;
375 
376 struct jar_xm_envelope_s {
377  jar_xm_envelope_point_t points[NUM_ENVELOPE_POINTS];
378  uint8_t num_points;
379  uint8_t sustain_point;
380  uint8_t loop_start_point;
381  uint8_t loop_end_point;
382  bool enabled;
383  bool sustain_enabled;
384  bool loop_enabled;
385 };
386 typedef struct jar_xm_envelope_s jar_xm_envelope_t;
387 
388 struct jar_xm_sample_s {
389  char name[SAMPLE_NAME_LENGTH + 1];
390  int8_t bits; /* Either 8 or 16 */
391 
392  uint32_t length;
393  uint32_t loop_start;
394  uint32_t loop_length;
395  uint32_t loop_end;
396  float volume;
397  int8_t finetune;
398  jar_xm_loop_type_t loop_type;
399  float panning;
400  int8_t relative_note;
401  uint64_t latest_trigger;
402 
403  float* data;
404  };
405  typedef struct jar_xm_sample_s jar_xm_sample_t;
406 
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;
419  bool muted;
420 
421  jar_xm_sample_t* samples;
422  };
423  typedef struct jar_xm_instrument_s jar_xm_instrument_t;
424 
425  struct jar_xm_pattern_slot_s {
426  uint8_t note; /* 1-96, 97 = Key Off note */
427  uint8_t instrument; /* 1-128 */
428  uint8_t volume_column;
429  uint8_t effect_type;
430  uint8_t effect_param;
431  };
432  typedef struct jar_xm_pattern_slot_s jar_xm_pattern_slot_t;
433 
434  struct jar_xm_pattern_s {
435  uint16_t num_rows;
436  jar_xm_pattern_slot_t* slots; /* Array of size num_rows * num_channels */
437  };
438  typedef struct jar_xm_pattern_s jar_xm_pattern_t;
439 
440  struct jar_xm_module_s {
441  char name[MODULE_NAME_LENGTH + 1];
442  char trackername[TRACKER_NAME_LENGTH + 1];
443  uint16_t length;
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];
450 
451  jar_xm_pattern_t* patterns;
452  jar_xm_instrument_t* instruments; /* Instrument 1 has index 0,
453  * instrument 2 has index 1, etc. */
454  };
455  typedef struct jar_xm_module_s jar_xm_module_t;
456 
457  struct jar_xm_channel_context_s {
458  float note;
459  float orig_note; /* The original note before effect modifications, as read in the pattern. */
460  jar_xm_instrument_t* instrument; /* Could be NULL */
461  jar_xm_sample_t* sample; /* Could be NULL */
462  jar_xm_pattern_slot_t* current;
463 
464  float sample_position;
465  float period;
466  float frequency;
467  float step;
468  bool ping; /* For ping-pong samples: true is -->, false is <-- */
469 
470  float volume; /* Ideally between 0 (muted) and 1 (loudest) */
471  float panning; /* Between 0 (left) and 1 (right); 0.5 is centered */
472 
473  uint16_t autovibrato_ticks;
474 
475  bool sustained;
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;
481 
482  float autovibrato_note_offset;
483 
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; /* Where to restart a E6y loop */
501  uint8_t pattern_loop_count; /* How many loop passes have been done */
502  bool vibrato_in_progress;
503  jar_xm_waveform_type_t vibrato_waveform;
504  bool vibrato_waveform_retrigger; /* True if a new note retriggers the waveform */
505  uint8_t vibrato_param;
506  uint16_t vibrato_ticks; /* Position in the waveform */
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;
514  bool tremor_on;
515 
516  uint64_t latest_trigger;
517  bool muted;
518 
519 #if JAR_XM_RAMPING
520  /* These values are updated at the end of each tick, to save
521  * a couple of float operations on every generated sample. */
522  float target_panning;
523  float target_volume;
524 
525  unsigned long frame_count;
526  float end_of_previous_sample[jar_xm_SAMPLE_RAMPING_POINTS];
527 #endif
528 
529  float actual_panning;
530  float actual_volume;
531  };
532  typedef struct jar_xm_channel_context_s jar_xm_channel_context_t;
533 
534  struct jar_xm_context_s {
535  void* allocated_memory;
536  jar_xm_module_t module;
537  uint32_t rate;
538 
539  uint16_t tempo;
540  uint16_t bpm;
541  float global_volume;
542  float amplification;
543 
544 #if JAR_XM_RAMPING
545  /* How much is a channel final volume allowed to change per
546  * sample; this is used to avoid abrubt volume changes which
547  * manifest as "clicks" in the generated sound. */
548  float volume_ramp;
549  float panning_ramp; /* Same for panning. */
550 #endif
551 
552  uint8_t current_table_index;
553  uint8_t current_row;
554  uint16_t current_tick; /* Can go below 255, with high tempo and a pattern delay */
555  float remaining_samples_in_tick;
556  uint64_t generated_samples;
557 
558  bool position_jump;
559  bool pattern_break;
560  uint8_t jump_dest;
561  uint8_t jump_row;
562 
563  /* Extra ticks to be played before going to the next row -
564  * Used for EEy effect */
565  uint16_t extra_ticks;
566 
567  uint8_t* row_loop_count; /* Array of size MAX_NUM_ROWS * module_length */
568  uint8_t loop_count;
569  uint8_t max_loop_count;
570 
571  jar_xm_channel_context_t* channels;
572 };
573 
574 /* ----- Internal API ----- */
575 
576 #if JAR_XM_DEFENSIVE
577 
582 int jar_xm_check_sanity_preload(const char*, size_t);
583 
588 int jar_xm_check_sanity_postload(jar_xm_context_t*);
589 
590 #endif
591 
607 size_t jar_xm_get_memory_needed_for_context(const char*, size_t);
608 
613 char* jar_xm_load_module(jar_xm_context_t*, const char*, size_t, char*);
614 
615 int jar_xm_create_context(jar_xm_context_t** ctxp, const char* moddata, uint32_t rate) {
616  return jar_xm_create_context_safe(ctxp, moddata, SIZE_MAX, rate);
617 }
618 
619 int jar_xm_create_context_safe(jar_xm_context_t** ctxp, const char* moddata, size_t moddata_length, uint32_t rate) {
620 #if JAR_XM_DEFENSIVE
621  int ret;
622 #endif
623  size_t bytes_needed;
624  char* mempool;
625  jar_xm_context_t* ctx;
626 
627 #if JAR_XM_DEFENSIVE
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);
630  return 1;
631  }
632 #endif
633 
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) {
637  /* malloc() failed, trouble ahead */
638  DEBUG("call to malloc() failed, returned %p", (void*)mempool);
639  return 2;
640  }
641 
642  /* Initialize most of the fields to 0, 0.f, NULL or false depending on type */
643  memset(mempool, 0, bytes_needed);
644 
645  ctx = (*ctxp = (jar_xm_context_t*)mempool);
646  ctx->allocated_memory = mempool; /* Keep original pointer for free() */
647  mempool += sizeof(jar_xm_context_t);
648 
649  ctx->rate = rate;
650  mempool = jar_xm_load_module(ctx, moddata, moddata_length, mempool);
651 
652  ctx->channels = (jar_xm_channel_context_t*)mempool;
653  mempool += ctx->module.num_channels * sizeof(jar_xm_channel_context_t);
654 
655  ctx->global_volume = 1.f;
656  ctx->amplification = .25f; /* XXX: some bad modules may still clip. Find out something better. */
657 
658 #if JAR_XM_RAMPING
659  ctx->volume_ramp = (1.f / 128.f);
660  ctx->panning_ramp = (1.f / 128.f);
661 #endif
662 
663  for(uint8_t i = 0; i < ctx->module.num_channels; ++i) {
664  jar_xm_channel_context_t* ch = ctx->channels + i;
665 
666  ch->ping = true;
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;
671 
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;
676  }
677 
678  ctx->row_loop_count = (uint8_t*)mempool;
679  mempool += MAX_NUM_ROWS * sizeof(uint8_t);
680 
681 #if JAR_XM_DEFENSIVE
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);
684  jar_xm_free_context(ctx);
685  return 1;
686  }
687 #endif
688 
689  return 0;
690 }
691 
692 void jar_xm_free_context(jar_xm_context_t* context) {
693  free(context->allocated_memory);
694 }
695 
696 void jar_xm_set_max_loop_count(jar_xm_context_t* context, uint8_t loopcnt) {
697  context->max_loop_count = loopcnt;
698 }
699 
700 uint8_t jar_xm_get_loop_count(jar_xm_context_t* context) {
701  return context->loop_count;
702 }
703 
704 
705 
706 bool jar_xm_mute_channel(jar_xm_context_t* ctx, uint16_t channel, bool mute) {
707  bool old = ctx->channels[channel - 1].muted;
708  ctx->channels[channel - 1].muted = mute;
709  return old;
710 }
711 
712 bool jar_xm_mute_instrument(jar_xm_context_t* ctx, uint16_t instr, bool mute) {
713  bool old = ctx->module.instruments[instr - 1].muted;
714  ctx->module.instruments[instr - 1].muted = mute;
715  return old;
716 }
717 
718 
719 
720 const char* jar_xm_get_module_name(jar_xm_context_t* ctx) {
721  return ctx->module.name;
722 }
723 
724 const char* jar_xm_get_tracker_name(jar_xm_context_t* ctx) {
725  return ctx->module.trackername;
726 }
727 
728 
729 
731  return ctx->module.num_channels;
732 }
733 
735  return ctx->module.length;
736 }
737 
739  return ctx->module.num_patterns;
740 }
741 
742 uint16_t jar_xm_get_number_of_rows(jar_xm_context_t* ctx, uint16_t pattern) {
743  return ctx->module.patterns[pattern].num_rows;
744 }
745 
747  return ctx->module.num_instruments;
748 }
749 
750 uint16_t jar_xm_get_number_of_samples(jar_xm_context_t* ctx, uint16_t instrument) {
751  return ctx->module.instruments[instrument - 1].num_samples;
752 }
753 
754 
755 
756 void jar_xm_get_playing_speed(jar_xm_context_t* ctx, uint16_t* bpm, uint16_t* tempo) {
757  if(bpm) *bpm = ctx->bpm;
758  if(tempo) *tempo = ctx->tempo;
759 }
760 
761 void jar_xm_get_position(jar_xm_context_t* ctx, uint8_t* pattern_index, uint8_t* pattern, uint8_t* row, uint64_t* samples) {
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;
766 }
767 
768 uint64_t jar_xm_get_latest_trigger_of_instrument(jar_xm_context_t* ctx, uint16_t instr) {
769  return ctx->module.instruments[instr - 1].latest_trigger;
770 }
771 
772 uint64_t jar_xm_get_latest_trigger_of_sample(jar_xm_context_t* ctx, uint16_t instr, uint16_t sample) {
773  return ctx->module.instruments[instr - 1].samples[sample].latest_trigger;
774 }
775 
776 uint64_t jar_xm_get_latest_trigger_of_channel(jar_xm_context_t* ctx, uint16_t chn) {
777  return ctx->channels[chn - 1].latest_trigger;
778 }
779 
780 /* .xm files are little-endian. (XXX: Are they really?) */
781 
782 /* Bounded reader macros.
783  * If we attempt to read the buffer out-of-bounds, pretend that the buffer is
784  * infinitely padded with zeroes.
785  */
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)
790 
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;
794 
795  /* how many bytes can be copied without overrunning `src` */
796  size_t copy_bytes = (src_len >= offset) ? (src_len - offset) : 0;
797  copy_bytes = copy_bytes > dst_len ? dst_len : copy_bytes;
798 
799  memcpy(dst_c, src_c + offset, copy_bytes);
800  /* padded bytes */
801  memset(dst_c + copy_bytes, 0, dst_len - copy_bytes);
802 }
803 
804 #if JAR_XM_DEFENSIVE
805 
806 int jar_xm_check_sanity_preload(const char* module, size_t module_length) {
807  if(module_length < 60) {
808  return 4;
809  }
810 
811  if(memcmp("Extended Module: ", module, 17) != 0) {
812  return 1;
813  }
814 
815  if(module[37] != 0x1A) {
816  return 2;
817  }
818 
819  if(module[59] != 0x01 || module[58] != 0x04) {
820  /* Not XM 1.04 */
821  return 3;
822  }
823 
824  return 0;
825 }
826 
827 int jar_xm_check_sanity_postload(jar_xm_context_t* ctx) {
828  /* @todo: plenty of stuff to do here… */
829 
830  /* Check the POT */
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) {
834  /* Cheap fix */
835  --ctx->module.length;
836  DEBUG("trimming invalid POT at pos %X", i);
837  } else {
838  DEBUG("module has invalid POT, pos %X references nonexistent pattern %X",
839  i,
840  ctx->module.pattern_table[i]);
841  return 1;
842  }
843  }
844  }
845 
846  return 0;
847 }
848 
849 #endif
850 
851 size_t jar_xm_get_memory_needed_for_context(const char* moddata, size_t moddata_length) {
852  size_t memory_needed = 0;
853  size_t offset = 60; /* Skip the first header */
854  uint16_t num_channels;
855  uint16_t num_patterns;
856  uint16_t num_instruments;
857 
858  /* Read the module header */
859 
860  num_channels = READ_U16(offset + 8);
861  num_channels = READ_U16(offset + 8);
862 
863  num_patterns = READ_U16(offset + 10);
864  memory_needed += num_patterns * sizeof(jar_xm_pattern_t);
865 
866  num_instruments = READ_U16(offset + 12);
867  memory_needed += num_instruments * sizeof(jar_xm_instrument_t);
868 
869  memory_needed += MAX_NUM_ROWS * READ_U16(offset + 4) * sizeof(uint8_t); /* Module length */
870 
871  /* Header size */
872  offset += READ_U32(offset);
873 
874  /* Read pattern headers */
875  for(uint16_t i = 0; i < num_patterns; ++i) {
876  uint16_t num_rows;
877 
878  num_rows = READ_U16(offset + 5);
879  memory_needed += num_rows * num_channels * sizeof(jar_xm_pattern_slot_t);
880 
881  /* Pattern header length + packed pattern data size */
882  offset += READ_U32(offset) + READ_U16(offset + 7);
883  }
884 
885  /* Read instrument headers */
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;
890 
891  num_samples = READ_U16(offset + 27);
892  memory_needed += num_samples * sizeof(jar_xm_sample_t);
893 
894  if(num_samples > 0) {
895  sample_header_size = READ_U32(offset + 29);
896  }
897 
898  /* Instrument header size */
899  offset += READ_U32(offset);
900 
901  for(uint16_t j = 0; j < num_samples; ++j) {
902  uint32_t sample_size;
903  uint8_t flags;
904 
905  sample_size = READ_U32(offset);
906  flags = READ_U8(offset + 14);
907  sample_size_aggregate += sample_size;
908 
909  if(flags & (1 << 4)) {
910  /* 16 bit sample */
911  memory_needed += sample_size * (sizeof(float) >> 1);
912  } else {
913  /* 8 bit sample */
914  memory_needed += sample_size * sizeof(float);
915  }
916 
917  offset += sample_header_size;
918  }
919 
920  offset += sample_size_aggregate;
921  }
922 
923  memory_needed += num_channels * sizeof(jar_xm_channel_context_t);
924  memory_needed += sizeof(jar_xm_context_t);
925 
926  return memory_needed;
927 }
928 
929 char* jar_xm_load_module(jar_xm_context_t* ctx, const char* moddata, size_t moddata_length, char* mempool) {
930  size_t offset = 0;
931  jar_xm_module_t* mod = &(ctx->module);
932 
933  /* Read XM header */
934  READ_MEMCPY(mod->name, offset + 17, MODULE_NAME_LENGTH);
935  READ_MEMCPY(mod->trackername, offset + 38, TRACKER_NAME_LENGTH);
936  offset += 60;
937 
938  /* Read module header */
939  uint32_t header_size = READ_U32(offset);
940 
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);
946 
947  mod->patterns = (jar_xm_pattern_t*)mempool;
948  mempool += mod->num_patterns * sizeof(jar_xm_pattern_t);
949 
950  mod->instruments = (jar_xm_instrument_t*)mempool;
951  mempool += mod->num_instruments * sizeof(jar_xm_instrument_t);
952 
953  uint16_t flags = READ_U32(offset + 14);
954  mod->frequency_type = (flags & (1 << 0)) ? jar_xm_LINEAR_FREQUENCIES : jar_xm_AMIGA_FREQUENCIES;
955 
956  ctx->tempo = READ_U16(offset + 16);
957  ctx->bpm = READ_U16(offset + 18);
958 
959  READ_MEMCPY(mod->pattern_table, offset + 20, PATTERN_ORDER_TABLE_LENGTH);
960  offset += header_size;
961 
962  /* Read patterns */
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;
966 
967  pat->num_rows = READ_U16(offset + 5);
968 
969  pat->slots = (jar_xm_pattern_slot_t*)mempool;
970  mempool += mod->num_channels * pat->num_rows * sizeof(jar_xm_pattern_slot_t);
971 
972  /* Pattern header length */
973  offset += READ_U32(offset);
974 
975  if(packed_patterndata_size == 0) {
976  /* No pattern data is present */
977  memset(pat->slots, 0, sizeof(jar_xm_pattern_slot_t) * pat->num_rows * mod->num_channels);
978  } else {
979  /* This isn't your typical for loop */
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;
983 
984  if(note & (1 << 7)) {
985  /* MSB is set, this is a compressed packet */
986  ++j;
987 
988  if(note & (1 << 0)) {
989  /* Note follows */
990  slot->note = READ_U8(offset + j);
991  ++j;
992  } else {
993  slot->note = 0;
994  }
995 
996  if(note & (1 << 1)) {
997  /* Instrument follows */
998  slot->instrument = READ_U8(offset + j);
999  ++j;
1000  } else {
1001  slot->instrument = 0;
1002  }
1003 
1004  if(note & (1 << 2)) {
1005  /* Volume column follows */
1006  slot->volume_column = READ_U8(offset + j);
1007  ++j;
1008  } else {
1009  slot->volume_column = 0;
1010  }
1011 
1012  if(note & (1 << 3)) {
1013  /* Effect follows */
1014  slot->effect_type = READ_U8(offset + j);
1015  ++j;
1016  } else {
1017  slot->effect_type = 0;
1018  }
1019 
1020  if(note & (1 << 4)) {
1021  /* Effect parameter follows */
1022  slot->effect_param = READ_U8(offset + j);
1023  ++j;
1024  } else {
1025  slot->effect_param = 0;
1026  }
1027  } else {
1028  /* Uncompressed packet */
1029  slot->note = note;
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);
1034  j += 5;
1035  }
1036  }
1037  }
1038 
1039  offset += packed_patterndata_size;
1040  }
1041 
1042  /* Read instruments */
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;
1046 
1047  READ_MEMCPY(instr->name, offset + 4, INSTRUMENT_NAME_LENGTH);
1048  instr->num_samples = READ_U16(offset + 27);
1049 
1050  if(instr->num_samples > 0) {
1051  /* Read extra header properties */
1052  sample_header_size = READ_U32(offset + 29);
1053  READ_MEMCPY(instr->sample_of_notes, offset + 33, NUM_NOTES);
1054 
1055  instr->volume_envelope.num_points = READ_U8(offset + 225);
1056  instr->panning_envelope.num_points = READ_U8(offset + 226);
1057 
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);
1061  }
1062 
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);
1066  }
1067 
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);
1071 
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);
1075 
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);
1080 
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);
1085 
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;
1091  }
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);
1096 
1097  instr->samples = (jar_xm_sample_t*)mempool;
1098  mempool += instr->num_samples * sizeof(jar_xm_sample_t);
1099  } else {
1100  instr->samples = NULL;
1101  }
1102 
1103  /* Instrument header size */
1104  offset += READ_U32(offset);
1105 
1106  for(uint16_t j = 0; j < instr->num_samples; ++j) {
1107  /* Read sample header */
1108  jar_xm_sample_t* sample = instr->samples + j;
1109 
1110  sample->length = READ_U32(offset);
1111  sample->loop_start = READ_U32(offset + 4);
1112  sample->loop_length = READ_U32(offset + 8);
1113  sample->loop_end = sample->loop_start + sample->loop_length;
1114  sample->volume = (float)READ_U8(offset + 12) / (float)0x40;
1115  sample->finetune = (int8_t)READ_U8(offset + 13);
1116 
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;
1122  } else {
1123  sample->loop_type = jar_xm_PING_PONG_LOOP;
1124  }
1125 
1126  sample->bits = (flags & (1 << 4)) ? 16 : 8;
1127 
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;
1132 
1133  if(sample->bits == 16) {
1134  /* 16 bit sample */
1135  mempool += sample->length * (sizeof(float) >> 1);
1136  sample->loop_start >>= 1;
1137  sample->loop_length >>= 1;
1138  sample->loop_end >>= 1;
1139  sample->length >>= 1;
1140  } else {
1141  /* 8 bit sample */
1142  mempool += sample->length * sizeof(float);
1143  }
1144 
1145  offset += sample_header_size;
1146  }
1147 
1148  for(uint16_t j = 0; j < instr->num_samples; ++j) {
1149  /* Read sample data */
1150  jar_xm_sample_t* sample = instr->samples + j;
1151  uint32_t length = sample->length;
1152 
1153  if(sample->bits == 16) {
1154  int16_t v = 0;
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);
1158  }
1159  offset += sample->length << 1;
1160  } else {
1161  int8_t v = 0;
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);
1165  }
1166  offset += sample->length;
1167  }
1168  }
1169  }
1170 
1171  return mempool;
1172 }
1173 
1174 //-------------------------------------------------------------------------------
1175 //THE FOLLOWING IS FOR PLAYING
1176 //-------------------------------------------------------------------------------
1177 
1178 /* ----- Static functions ----- */
1179 
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);
1189 
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*);
1193 
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);
1198 static float jar_xm_period(jar_xm_context_t*, float);
1199 static float jar_xm_frequency(jar_xm_context_t*, float, float);
1200 static void jar_xm_update_frequency(jar_xm_context_t*, jar_xm_channel_context_t*);
1201 
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*);
1206 
1207 static void jar_xm_post_pattern_change(jar_xm_context_t*);
1208 static void jar_xm_row(jar_xm_context_t*);
1209 static void jar_xm_tick(jar_xm_context_t*);
1210 
1211 static float jar_xm_next_of_sample(jar_xm_channel_context_t*);
1212 static void jar_xm_sample(jar_xm_context_t*, float*, float*);
1213 
1214 /* ----- Other oddities ----- */
1215 
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)
1219 
1220 static const uint16_t amiga_frequencies[] = {
1221  1712, 1616, 1525, 1440, /* C-2, C#2, D-2, D#2 */
1222  1357, 1281, 1209, 1141, /* E-2, F-2, F#2, G-2 */
1223  1077, 1017, 961, 907, /* G#2, A-2, A#2, B-2 */
1224  856, /* C-3 */
1225 };
1226 
1227 static const float multi_retrig_add[] = {
1228  0.f, -1.f, -2.f, -4.f, /* 0, 1, 2, 3 */
1229  -8.f, -16.f, 0.f, 0.f, /* 4, 5, 6, 7 */
1230  0.f, 1.f, 2.f, 4.f, /* 8, 9, A, B */
1231  8.f, 16.f, 0.f, 0.f /* C, D, E, F */
1232 };
1233 
1234 static const float multi_retrig_multiply[] = {
1235  1.f, 1.f, 1.f, 1.f, /* 0, 1, 2, 3 */
1236  1.f, 1.f, .6666667f, .5f, /* 4, 5, 6, 7 */
1237  1.f, 1.f, 1.f, 1.f, /* 8, 9, A, B */
1238  1.f, 1.f, 1.5f, 2.f /* C, D, E, F */
1239 };
1240 
1241 #define jar_xm_CLAMP_UP1F(vol, limit) do { \
1242  if((vol) > (limit)) (vol) = (limit); \
1243  } while(0)
1244 #define jar_xm_CLAMP_UP(vol) jar_xm_CLAMP_UP1F((vol), 1.f)
1245 
1246 #define jar_xm_CLAMP_DOWN1F(vol, limit) do { \
1247  if((vol) < (limit)) (vol) = (limit); \
1248  } while(0)
1249 #define jar_xm_CLAMP_DOWN(vol) jar_xm_CLAMP_DOWN1F((vol), .0f)
1250 
1251 #define jar_xm_CLAMP2F(vol, up, down) do { \
1252  if((vol) > (up)) (vol) = (up); \
1253  else if((vol) < (down)) (vol) = (down); \
1254  } while(0)
1255 #define jar_xm_CLAMP(vol) jar_xm_CLAMP2F((vol), 1.f, .0f)
1256 
1257 #define jar_xm_SLIDE_TOWARDS(val, goal, incr) do { \
1258  if((val) > (goal)) { \
1259  (val) -= (incr); \
1260  jar_xm_CLAMP_DOWN1F((val), (goal)); \
1261  } else if((val) < (goal)) { \
1262  (val) += (incr); \
1263  jar_xm_CLAMP_UP1F((val), (goal)); \
1264  } \
1265  } while(0)
1266 
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)))
1269 
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)
1279 
1280 /* ----- Function definitions ----- */
1281 
1282 static float jar_xm_waveform(jar_xm_waveform_type_t waveform, uint8_t step) {
1283  static unsigned int next_rand = 24492;
1284  step %= 0x40;
1285 
1286  switch(waveform) {
1287 
1288  case jar_xm_SINE_WAVEFORM:
1289  /* Why not use a table? For saving space, and because there's
1290  * very very little actual performance gain. */
1291  return -sinf(2.f * 3.141592f * (float)step / (float)0x40);
1292 
1293  case jar_xm_RAMP_DOWN_WAVEFORM:
1294  /* Ramp down: 1.0f when step = 0; -1.0f when step = 0x40 */
1295  return (float)(0x20 - step) / 0x20;
1296 
1297  case jar_xm_SQUARE_WAVEFORM:
1298  /* Square with a 50% duty */
1299  return (step >= 0x20) ? 1.f : -1.f;
1300 
1301  case jar_xm_RANDOM_WAVEFORM:
1302  /* Use the POSIX.1-2001 example, just to be deterministic
1303  * across different machines */
1304  next_rand = next_rand * 1103515245 + 12345;
1305  return (float)((next_rand >> 16) & 0x7FFF) / (float)0x4000 - 1.f;
1306 
1307  case jar_xm_RAMP_UP_WAVEFORM:
1308  /* Ramp up: -1.f when step = 0; 1.f when step = 0x40 */
1309  return (float)(step - 0x20) / 0x20;
1310 
1311  default:
1312  break;
1313 
1314  }
1315 
1316  return .0f;
1317 }
1318 
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;
1322  float sweep = 1.f;
1323 
1324  if(ch->autovibrato_ticks < instr->vibrato_sweep) {
1325  /* No idea if this is correct, but it sounds close enough… */
1326  sweep = jar_xm_LERP(0.f, 1.f, (float)ch->autovibrato_ticks / (float)instr->vibrato_sweep);
1327  }
1328 
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);
1333 }
1334 
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 =
1338  2.f
1339  * jar_xm_waveform(ch->vibrato_waveform, step)
1340  * (float)(param & 0x0F) / (float)0xF;
1341  jar_xm_update_frequency(ctx, ch);
1342 }
1343 
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);
1346  /* Not so sure about this, it sounds correct by ear compared with
1347  * MilkyTracker, but it could come from other bugs */
1348  ch->tremolo_volume = -1.f * jar_xm_waveform(ch->tremolo_waveform, step)
1349  * (float)(param & 0x0F) / (float)0xF;
1350 }
1351 
1352 static void jar_xm_arpeggio(jar_xm_context_t* ctx, jar_xm_channel_context_t* ch, uint8_t param, uint16_t tick) {
1353  switch(tick % 3) {
1354  case 0:
1355  ch->arp_in_progress = false;
1356  ch->arp_note_offset = 0;
1357  break;
1358  case 2:
1359  ch->arp_in_progress = true;
1360  ch->arp_note_offset = param >> 4;
1361  break;
1362  case 1:
1363  ch->arp_in_progress = true;
1364  ch->arp_note_offset = param & 0x0F;
1365  break;
1366  }
1367 
1368  jar_xm_update_frequency(ctx, ch);
1369 }
1370 
1371 static void jar_xm_tone_portamento(jar_xm_context_t* ctx, jar_xm_channel_context_t* ch) {
1372  /* 3xx called without a note, wait until we get an actual
1373  * target note. */
1374  if(ch->tone_portamento_target_period == 0.f) return;
1375 
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
1381  );
1382  jar_xm_update_frequency(ctx, ch);
1383  }
1384 }
1385 
1386 static void jar_xm_pitch_slide(jar_xm_context_t* ctx, jar_xm_channel_context_t* ch, float period_offset) {
1387  /* Don't ask about the 4.f coefficient. I found mention of it
1388  * nowhere. Found by earâ„¢. */
1389  if(ctx->module.frequency_type == jar_xm_LINEAR_FREQUENCIES) {
1390  period_offset *= 4.f;
1391  }
1392 
1393  ch->period += period_offset;
1394  jar_xm_CLAMP_DOWN(ch->period);
1395  /* XXX: upper bound of period ? */
1396 
1397  jar_xm_update_frequency(ctx, ch);
1398 }
1399 
1400 static void jar_xm_panning_slide(jar_xm_channel_context_t* ch, uint8_t rawval) {
1401  float f;
1402 
1403  if((rawval & 0xF0) && (rawval & 0x0F)) {
1404  /* Illegal state */
1405  return;
1406  }
1407 
1408  if(rawval & 0xF0) {
1409  /* Slide right */
1410  f = (float)(rawval >> 4) / (float)0xFF;
1411  ch->panning += f;
1412  jar_xm_CLAMP_UP(ch->panning);
1413  } else {
1414  /* Slide left */
1415  f = (float)(rawval & 0x0F) / (float)0xFF;
1416  ch->panning -= f;
1417  jar_xm_CLAMP_DOWN(ch->panning);
1418  }
1419 }
1420 
1421 static void jar_xm_volume_slide(jar_xm_channel_context_t* ch, uint8_t rawval) {
1422  float f;
1423 
1424  if((rawval & 0xF0) && (rawval & 0x0F)) {
1425  /* Illegal state */
1426  return;
1427  }
1428 
1429  if(rawval & 0xF0) {
1430  /* Slide up */
1431  f = (float)(rawval >> 4) / (float)0x40;
1432  ch->volume += f;
1433  jar_xm_CLAMP_UP(ch->volume);
1434  } else {
1435  /* Slide down */
1436  f = (float)(rawval & 0x0F) / (float)0x40;
1437  ch->volume -= f;
1438  jar_xm_CLAMP_DOWN(ch->volume);
1439  }
1440 }
1441 
1442 static float jar_xm_envelope_lerp(jar_xm_envelope_point_t* a, jar_xm_envelope_point_t* b, uint16_t pos) {
1443  /* Linear interpolation between two envelope points */
1444  if(pos <= a->frame) return a->value;
1445  else if(pos >= b->frame) return b->value;
1446  else {
1447  float p = (float)(pos - a->frame) / (float)(b->frame - a->frame);
1448  return a->value * (1 - p) + b->value * p;
1449  }
1450 }
1451 
1452 static void jar_xm_post_pattern_change(jar_xm_context_t* ctx) {
1453  /* Loop if necessary */
1454  if(ctx->current_table_index >= ctx->module.length) {
1455  ctx->current_table_index = ctx->module.restart_position;
1456  }
1457 }
1458 
1459 static float jar_xm_linear_period(float note) {
1460  return 7680.f - note * 64.f;
1461 }
1462 
1463 static float jar_xm_linear_frequency(float period) {
1464  return 8363.f * powf(2.f, (4608.f - period) / 768.f);
1465 }
1466 
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];
1472 
1473  if(octave > 0) {
1474  p1 >>= octave;
1475  p2 >>= octave;
1476  } else if(octave < 0) {
1477  p1 <<= (-octave);
1478  p2 <<= (-octave);
1479  }
1480 
1481  return jar_xm_LERP(p1, p2, note - intnote);
1482 }
1483 
1484 static float jar_xm_amiga_frequency(float period) {
1485  if(period == .0f) return .0f;
1486 
1487  /* This is the PAL value. No reason to choose this one over the
1488  * NTSC value. */
1489  return 7093789.2f / (period * 2.f);
1490 }
1491 
1492 static float jar_xm_period(jar_xm_context_t* ctx, float note) {
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);
1498  }
1499  return .0f;
1500 }
1501 
1502 static float jar_xm_frequency(jar_xm_context_t* ctx, float period, float note_offset) {
1503  uint8_t a;
1504  int8_t octave;
1505  float note;
1506  uint16_t p1, p2;
1507 
1508  switch(ctx->module.frequency_type) {
1509 
1510  case jar_xm_LINEAR_FREQUENCIES:
1511  return jar_xm_linear_frequency(period - 64.f * note_offset);
1512 
1513  case jar_xm_AMIGA_FREQUENCIES:
1514  if(note_offset == 0) {
1515  /* A chance to escape from insanity */
1516  return jar_xm_amiga_frequency(period);
1517  }
1518 
1519  /* FIXME: this is very crappy at best */
1520  a = octave = 0;
1521 
1522  /* Find the octave of the current period */
1523  if(period > amiga_frequencies[0]) {
1524  --octave;
1525  while(period > (amiga_frequencies[0] << (-octave))) --octave;
1526  } else if(period < amiga_frequencies[12]) {
1527  ++octave;
1528  while(period < (amiga_frequencies[12] >> octave)) ++octave;
1529  }
1530 
1531  /* Find the smallest note closest to the current period */
1532  for(uint8_t i = 0; i < 12; ++i) {
1533  p1 = amiga_frequencies[i], p2 = amiga_frequencies[i + 1];
1534 
1535  if(octave > 0) {
1536  p1 >>= octave;
1537  p2 >>= octave;
1538  } else if(octave < 0) {
1539  p1 <<= (-octave);
1540  p2 <<= (-octave);
1541  }
1542 
1543  if(p2 <= period && period <= p1) {
1544  a = i;
1545  break;
1546  }
1547  }
1548 
1549  if(JAR_XM_DEBUG && (p1 < period || p2 > period)) {
1550  DEBUG("%i <= %f <= %i should hold but doesn't, this is a bug", p2, period, p1);
1551  }
1552 
1553  note = 12.f * (octave + 2) + a + jar_xm_INVERSE_LERP(p1, p2, period);
1554 
1555  return jar_xm_amiga_frequency(jar_xm_amiga_period(note + note_offset));
1556 
1557  }
1558 
1559  return .0f;
1560 }
1561 
1562 static void jar_xm_update_frequency(jar_xm_context_t* ctx, jar_xm_channel_context_t* ch) {
1563  ch->frequency = jar_xm_frequency(
1564  ctx, ch->period,
1565  (ch->arp_note_offset > 0 ? ch->arp_note_offset : (
1566  ch->vibrato_note_offset + ch->autovibrato_note_offset
1567  ))
1568  );
1569  ch->step = ch->frequency / ctx->rate;
1570 }
1571 
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) {
1576  /* Tone portamento in effect, unclear stuff happens */
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) {
1579  /* Invalid instrument, Cut current note */
1580  jar_xm_cut_note(ch);
1581  ch->instrument = NULL;
1582  ch->sample = NULL;
1583  } else {
1584  ch->instrument = ctx->module.instruments + (s->instrument - 1);
1585  if(s->note == 0 && ch->sample != NULL) {
1586  /* Ghost instrument, trigger note */
1587  /* Sample position is kept, but envelopes are reset */
1588  jar_xm_trigger_note(ctx, ch, jar_xm_TRIGGER_KEEP_SAMPLE_POSITION);
1589  }
1590  }
1591  }
1592 
1593  if(NOTE_IS_VALID(s->note)) {
1594  /* Yes, the real note number is s->note -1. Try finding
1595  * THAT in any of the specs! :-) */
1596 
1597  jar_xm_instrument_t* instr = ch->instrument;
1598 
1599  if(HAS_TONE_PORTAMENTO(ch->current) && instr != NULL && ch->sample != NULL) {
1600  /* Tone portamento in effect */
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) {
1604  /* Bad instrument */
1605  jar_xm_cut_note(ch);
1606  } else {
1607  if(instr->sample_of_notes[s->note - 1] < instr->num_samples) {
1608 #if JAR_XM_RAMPING
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);
1611  }
1612  ch->frame_count = 0;
1613 #endif
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);
1619  } else {
1620  /* Ghost note: keep old volume */
1621  jar_xm_trigger_note(ctx, ch, jar_xm_TRIGGER_KEEP_VOLUME);
1622  }
1623  } else {
1624  /* Bad sample */
1625  jar_xm_cut_note(ch);
1626  }
1627  }
1628  } else if(s->note == 97) {
1629  /* Key Off */
1630  jar_xm_key_off(ch);
1631  }
1632 
1633  switch(s->volume_column >> 4) {
1634 
1635  case 0x5:
1636  if(s->volume_column > 0x50) break;
1637  case 0x1:
1638  case 0x2:
1639  case 0x3:
1640  case 0x4:
1641  /* Set volume */
1642  ch->volume = (float)(s->volume_column - 0x10) / (float)0x40;
1643  break;
1644 
1645  case 0x8: /* Fine volume slide down */
1646  jar_xm_volume_slide(ch, s->volume_column & 0x0F);
1647  break;
1648 
1649  case 0x9: /* Fine volume slide up */
1650  jar_xm_volume_slide(ch, s->volume_column << 4);
1651  break;
1652 
1653  case 0xA: /* Set vibrato speed */
1654  ch->vibrato_param = (ch->vibrato_param & 0x0F) | ((s->volume_column & 0x0F) << 4);
1655  break;
1656 
1657  case 0xC: /* Set panning */
1658  ch->panning = (float)(
1659  ((s->volume_column & 0x0F) << 4) | (s->volume_column & 0x0F)
1660  ) / (float)0xFF;
1661  break;
1662 
1663  case 0xF: /* Tone portamento */
1664  if(s->volume_column & 0x0F) {
1665  ch->tone_portamento_param = ((s->volume_column & 0x0F) << 4)
1666  | (s->volume_column & 0x0F);
1667  }
1668  break;
1669 
1670  default:
1671  break;
1672 
1673  }
1674 
1675  switch(s->effect_type) {
1676 
1677  case 1: /* 1xx: Portamento up */
1678  if(s->effect_param > 0) {
1679  ch->portamento_up_param = s->effect_param;
1680  }
1681  break;
1682 
1683  case 2: /* 2xx: Portamento down */
1684  if(s->effect_param > 0) {
1685  ch->portamento_down_param = s->effect_param;
1686  }
1687  break;
1688 
1689  case 3: /* 3xx: Tone portamento */
1690  if(s->effect_param > 0) {
1691  ch->tone_portamento_param = s->effect_param;
1692  }
1693  break;
1694 
1695  case 4: /* 4xy: Vibrato */
1696  if(s->effect_param & 0x0F) {
1697  /* Set vibrato depth */
1698  ch->vibrato_param = (ch->vibrato_param & 0xF0) | (s->effect_param & 0x0F);
1699  }
1700  if(s->effect_param >> 4) {
1701  /* Set vibrato speed */
1702  ch->vibrato_param = (s->effect_param & 0xF0) | (ch->vibrato_param & 0x0F);
1703  }
1704  break;
1705 
1706  case 5: /* 5xy: Tone portamento + Volume slide */
1707  if(s->effect_param > 0) {
1708  ch->volume_slide_param = s->effect_param;
1709  }
1710  break;
1711 
1712  case 6: /* 6xy: Vibrato + Volume slide */
1713  if(s->effect_param > 0) {
1714  ch->volume_slide_param = s->effect_param;
1715  }
1716  break;
1717 
1718  case 7: /* 7xy: Tremolo */
1719  if(s->effect_param & 0x0F) {
1720  /* Set tremolo depth */
1721  ch->tremolo_param = (ch->tremolo_param & 0xF0) | (s->effect_param & 0x0F);
1722  }
1723  if(s->effect_param >> 4) {
1724  /* Set tremolo speed */
1725  ch->tremolo_param = (s->effect_param & 0xF0) | (ch->tremolo_param & 0x0F);
1726  }
1727  break;
1728 
1729  case 8: /* 8xx: Set panning */
1730  ch->panning = (float)s->effect_param / (float)0xFF;
1731  break;
1732 
1733  case 9: /* 9xx: Sample offset */
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) {
1737  /* Pretend the sample dosen't loop and is done playing */
1738  ch->sample_position = -1;
1739  break;
1740  }
1741  ch->sample_position = final_offset;
1742  }
1743  break;
1744 
1745  case 0xA: /* Axy: Volume slide */
1746  if(s->effect_param > 0) {
1747  ch->volume_slide_param = s->effect_param;
1748  }
1749  break;
1750 
1751  case 0xB: /* Bxx: Position jump */
1752  if(s->effect_param < ctx->module.length) {
1753  ctx->position_jump = true;
1754  ctx->jump_dest = s->effect_param;
1755  }
1756  break;
1757 
1758  case 0xC: /* Cxx: Set volume */
1759  ch->volume = (float)((s->effect_param > 0x40)
1760  ? 0x40 : s->effect_param) / (float)0x40;
1761  break;
1762 
1763  case 0xD: /* Dxx: Pattern break */
1764  /* Jump after playing this line */
1765  ctx->pattern_break = true;
1766  ctx->jump_row = (s->effect_param >> 4) * 10 + (s->effect_param & 0x0F);
1767  break;
1768 
1769  case 0xE: /* EXy: Extended command */
1770  switch(s->effect_param >> 4) {
1771 
1772  case 1: /* E1y: Fine portamento up */
1773  if(s->effect_param & 0x0F) {
1774  ch->fine_portamento_up_param = s->effect_param & 0x0F;
1775  }
1776  jar_xm_pitch_slide(ctx, ch, -ch->fine_portamento_up_param);
1777  break;
1778 
1779  case 2: /* E2y: Fine portamento down */
1780  if(s->effect_param & 0x0F) {
1781  ch->fine_portamento_down_param = s->effect_param & 0x0F;
1782  }
1783  jar_xm_pitch_slide(ctx, ch, ch->fine_portamento_down_param);
1784  break;
1785 
1786  case 4: /* E4y: Set vibrato control */
1787  ch->vibrato_waveform = s->effect_param & 3;
1788  ch->vibrato_waveform_retrigger = !((s->effect_param >> 2) & 1);
1789  break;
1790 
1791  case 5: /* E5y: Set finetune */
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);
1797  }
1798  break;
1799 
1800  case 6: /* E6y: Pattern loop */
1801  if(s->effect_param & 0x0F) {
1802  if((s->effect_param & 0x0F) == ch->pattern_loop_count) {
1803  /* Loop is over */
1804  ch->pattern_loop_count = 0;
1805  break;
1806  }
1807 
1808  /* Jump to the beginning of the loop */
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;
1813  } else {
1814  /* Set loop start point */
1815  ch->pattern_loop_origin = ctx->current_row;
1816  /* Replicate FT2 E60 bug */
1817  ctx->jump_row = ch->pattern_loop_origin;
1818  }
1819  break;
1820 
1821  case 7: /* E7y: Set tremolo control */
1822  ch->tremolo_waveform = s->effect_param & 3;
1823  ch->tremolo_waveform_retrigger = !((s->effect_param >> 2) & 1);
1824  break;
1825 
1826  case 0xA: /* EAy: Fine volume slide up */
1827  if(s->effect_param & 0x0F) {
1828  ch->fine_volume_slide_param = s->effect_param & 0x0F;
1829  }
1830  jar_xm_volume_slide(ch, ch->fine_volume_slide_param << 4);
1831  break;
1832 
1833  case 0xB: /* EBy: Fine volume slide down */
1834  if(s->effect_param & 0x0F) {
1835  ch->fine_volume_slide_param = s->effect_param & 0x0F;
1836  }
1837  jar_xm_volume_slide(ch, ch->fine_volume_slide_param);
1838  break;
1839 
1840  case 0xD: /* EDy: Note delay */
1841  /* XXX: figure this out better. EDx triggers
1842  * the note even when there no note and no
1843  * instrument. But ED0 acts like like a ghost
1844  * note, EDx (x ≠ 0) does not. */
1845  if(s->note == 0 && s->instrument == 0) {
1846  unsigned int flags = jar_xm_TRIGGER_KEEP_VOLUME;
1847 
1848  if(ch->current->effect_param & 0x0F) {
1849  ch->note = ch->orig_note;
1850  jar_xm_trigger_note(ctx, ch, flags);
1851  } else {
1852  jar_xm_trigger_note(
1853  ctx, ch,
1854  flags
1855  | jar_xm_TRIGGER_KEEP_PERIOD
1856  | jar_xm_TRIGGER_KEEP_SAMPLE_POSITION
1857  );
1858  }
1859  }
1860  break;
1861 
1862  case 0xE: /* EEy: Pattern delay */
1863  ctx->extra_ticks = (ch->current->effect_param & 0x0F) * ctx->tempo;
1864  break;
1865 
1866  default:
1867  break;
1868 
1869  }
1870  break;
1871 
1872  case 0xF: /* Fxx: Set tempo/BPM */
1873  if(s->effect_param > 0) {
1874  if(s->effect_param <= 0x1F) {
1875  ctx->tempo = s->effect_param;
1876  } else {
1877  ctx->bpm = s->effect_param;
1878  }
1879  }
1880  break;
1881 
1882  case 16: /* Gxx: Set global volume */
1883  ctx->global_volume = (float)((s->effect_param > 0x40)
1884  ? 0x40 : s->effect_param) / (float)0x40;
1885  break;
1886 
1887  case 17: /* Hxy: Global volume slide */
1888  if(s->effect_param > 0) {
1889  ch->global_volume_slide_param = s->effect_param;
1890  }
1891  break;
1892 
1893  case 21: /* Lxx: Set envelope position */
1894  ch->volume_envelope_frame_count = s->effect_param;
1895  ch->panning_envelope_frame_count = s->effect_param;
1896  break;
1897 
1898  case 25: /* Pxy: Panning slide */
1899  if(s->effect_param > 0) {
1900  ch->panning_slide_param = s->effect_param;
1901  }
1902  break;
1903 
1904  case 27: /* Rxy: Multi retrig note */
1905  if(s->effect_param > 0) {
1906  if((s->effect_param >> 4) == 0) {
1907  /* Keep previous x value */
1908  ch->multi_retrig_param = (ch->multi_retrig_param & 0xF0) | (s->effect_param & 0x0F);
1909  } else {
1910  ch->multi_retrig_param = s->effect_param;
1911  }
1912  }
1913  break;
1914 
1915  case 29: /* Txy: Tremor */
1916  if(s->effect_param > 0) {
1917  /* Tremor x and y params do not appear to be separately
1918  * kept in memory, unlike Rxy */
1919  ch->tremor_param = s->effect_param;
1920  }
1921  break;
1922 
1923  case 33: /* Xxy: Extra stuff */
1924  switch(s->effect_param >> 4) {
1925 
1926  case 1: /* X1y: Extra fine portamento up */
1927  if(s->effect_param & 0x0F) {
1928  ch->extra_fine_portamento_up_param = s->effect_param & 0x0F;
1929  }
1930  jar_xm_pitch_slide(ctx, ch, -1.0f * ch->extra_fine_portamento_up_param);
1931  break;
1932 
1933  case 2: /* X2y: Extra fine portamento down */
1934  if(s->effect_param & 0x0F) {
1935  ch->extra_fine_portamento_down_param = s->effect_param & 0x0F;
1936  }
1937  jar_xm_pitch_slide(ctx, ch, ch->extra_fine_portamento_down_param);
1938  break;
1939 
1940  default:
1941  break;
1942 
1943  }
1944  break;
1945 
1946  default:
1947  break;
1948 
1949  }
1950 }
1951 
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;
1955  ch->ping = true;
1956  }
1957 
1958  if(ch->sample != NULL) {
1959  if(!(flags & jar_xm_TRIGGER_KEEP_VOLUME)) {
1960  ch->volume = ch->sample->volume;
1961  }
1962 
1963  ch->panning = ch->sample->panning;
1964  }
1965 
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;
1973 
1974  ch->autovibrato_ticks = 0;
1975 
1976  if(ch->vibrato_waveform_retrigger) {
1977  ch->vibrato_ticks = 0; /* XXX: should the waveform itself also
1978  * be reset to sine? */
1979  }
1980  if(ch->tremolo_waveform_retrigger) {
1981  ch->tremolo_ticks = 0;
1982  }
1983 
1984  if(!(flags & jar_xm_TRIGGER_KEEP_PERIOD)) {
1985  ch->period = jar_xm_period(ctx, ch->note);
1986  jar_xm_update_frequency(ctx, ch);
1987  }
1988 
1989  ch->latest_trigger = ctx->generated_samples;
1990  if(ch->instrument != NULL) {
1991  ch->instrument->latest_trigger = ctx->generated_samples;
1992  }
1993  if(ch->sample != NULL) {
1994  ch->sample->latest_trigger = ctx->generated_samples;
1995  }
1996 }
1997 
1998 static void jar_xm_cut_note(jar_xm_channel_context_t* ch) {
1999  /* NB: this is not the same as Key Off */
2000  ch->volume = .0f;
2001 }
2002 
2003 static void jar_xm_key_off(jar_xm_channel_context_t* ch) {
2004  /* Key Off */
2005  ch->sustained = false;
2006 
2007  /* If no volume envelope is used, also cut the note */
2008  if(ch->instrument == NULL || !ch->instrument->volume_envelope.enabled) {
2009  jar_xm_cut_note(ch);
2010  }
2011 }
2012 
2013 static void jar_xm_row(jar_xm_context_t* ctx) {
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;
2019  ctx->jump_row = 0;
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;
2025  ctx->jump_row = 0;
2026  jar_xm_post_pattern_change(ctx);
2027  }
2028 
2029  jar_xm_pattern_t* cur = ctx->module.patterns + ctx->module.pattern_table[ctx->current_table_index];
2030  bool in_a_loop = false;
2031 
2032  /* Read notes… */
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;
2036 
2037  ch->current = s;
2038 
2039  if(s->effect_type != 0xE || s->effect_param >> 4 != 0xD) {
2040  jar_xm_handle_note_and_instrument(ctx, ch, s);
2041  } else {
2042  ch->note_delay_param = s->effect_param & 0x0F;
2043  }
2044 
2045  if(!in_a_loop && ch->pattern_loop_count > 0) {
2046  in_a_loop = true;
2047  }
2048  }
2049 
2050  if(!in_a_loop) {
2051  /* No E6y loop is in effect (or we are in the first pass) */
2052  ctx->loop_count = (ctx->row_loop_count[MAX_NUM_ROWS * ctx->current_table_index + ctx->current_row]++);
2053  }
2054 
2055  ctx->current_row++; /* Since this is an uint8, this line can
2056  * increment from 255 to 0, in which case it
2057  * is still necessary to go the next
2058  * pattern. */
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; /* This will be 0 most of
2063  * the time, except when E60
2064  * is used */
2065  ctx->jump_row = 0;
2066  jar_xm_post_pattern_change(ctx);
2067  }
2068 }
2069 
2070 static void jar_xm_envelope_tick(jar_xm_channel_context_t* ch,
2071  jar_xm_envelope_t* env,
2072  uint16_t* counter,
2073  float* outval) {
2074  if(env->num_points < 2) {
2075  /* Don't really know what to do… */
2076  if(env->num_points == 1) {
2077  /* XXX I am pulling this out of my ass */
2078  *outval = (float)env->points[0].value / (float)0x40;
2079  if(*outval > 1) {
2080  *outval = 1;
2081  }
2082  }
2083 
2084  return;
2085  } else {
2086  uint8_t j;
2087 
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;
2092 
2093  if(*counter >= loop_end) {
2094  *counter -= loop_length;
2095  }
2096  }
2097 
2098  for(j = 0; j < (env->num_points - 2); ++j) {
2099  if(env->points[j].frame <= *counter &&
2100  env->points[j+1].frame >= *counter) {
2101  break;
2102  }
2103  }
2104 
2105  *outval = jar_xm_envelope_lerp(env->points + j, env->points + j + 1, *counter) / (float)0x40;
2106 
2107  /* Make sure it is safe to increment frame count */
2108  if(!ch->sustained || !env->sustain_enabled ||
2109  *counter != env->points[env->sustain_point].frame) {
2110  (*counter)++;
2111  }
2112  }
2113 }
2114 
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);
2121  }
2122 
2123  jar_xm_envelope_tick(ch,
2124  &(ch->instrument->volume_envelope),
2125  &(ch->volume_envelope_frame_count),
2126  &(ch->volume_envelope_volume));
2127  }
2128 
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));
2134  }
2135  }
2136 }
2137 
2138 static void jar_xm_tick(jar_xm_context_t* ctx) {
2139  if(ctx->current_tick == 0) {
2140  jar_xm_row(ctx);
2141  }
2142 
2143  for(uint8_t i = 0; i < ctx->module.num_channels; ++i) {
2144  jar_xm_channel_context_t* ch = ctx->channels + i;
2145 
2146  jar_xm_envelopes(ch);
2147  jar_xm_autovibrato(ctx, ch);
2148 
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);
2153  }
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);
2158  }
2159 
2160  switch(ch->current->volume_column >> 4) {
2161 
2162  case 0x6: /* Volume slide down */
2163  if(ctx->current_tick == 0) break;
2164  jar_xm_volume_slide(ch, ch->current->volume_column & 0x0F);
2165  break;
2166 
2167  case 0x7: /* Volume slide up */
2168  if(ctx->current_tick == 0) break;
2169  jar_xm_volume_slide(ch, ch->current->volume_column << 4);
2170  break;
2171 
2172  case 0xB: /* Vibrato */
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++);
2176  break;
2177 
2178  case 0xD: /* Panning slide left */
2179  if(ctx->current_tick == 0) break;
2180  jar_xm_panning_slide(ch, ch->current->volume_column & 0x0F);
2181  break;
2182 
2183  case 0xE: /* Panning slide right */
2184  if(ctx->current_tick == 0) break;
2185  jar_xm_panning_slide(ch, ch->current->volume_column << 4);
2186  break;
2187 
2188  case 0xF: /* Tone portamento */
2189  if(ctx->current_tick == 0) break;
2190  jar_xm_tone_portamento(ctx, ch);
2191  break;
2192 
2193  default:
2194  break;
2195 
2196  }
2197 
2198  switch(ch->current->effect_type) {
2199 
2200  case 0: /* 0xy: Arpeggio */
2201  if(ch->current->effect_param > 0) {
2202  char arp_offset = ctx->tempo % 3;
2203  switch(arp_offset) {
2204  case 2: /* 0 -> x -> 0 -> y -> x -> … */
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);
2209  break;
2210  }
2211  /* No break here, this is intended */
2212  case 1: /* 0 -> 0 -> y -> x -> … */
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);
2217  break;
2218  }
2219  /* No break here, this is intended */
2220  case 0: /* 0 -> y -> x -> … */
2221  jar_xm_arpeggio(ctx, ch, ch->current->effect_param, ctx->current_tick - arp_offset);
2222  default:
2223  break;
2224  }
2225  }
2226  break;
2227 
2228  case 1: /* 1xx: Portamento up */
2229  if(ctx->current_tick == 0) break;
2230  jar_xm_pitch_slide(ctx, ch, -ch->portamento_up_param);
2231  break;
2232 
2233  case 2: /* 2xx: Portamento down */
2234  if(ctx->current_tick == 0) break;
2235  jar_xm_pitch_slide(ctx, ch, ch->portamento_down_param);
2236  break;
2237 
2238  case 3: /* 3xx: Tone portamento */
2239  if(ctx->current_tick == 0) break;
2240  jar_xm_tone_portamento(ctx, ch);
2241  break;
2242 
2243  case 4: /* 4xy: Vibrato */
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++);
2247  break;
2248 
2249  case 5: /* 5xy: Tone portamento + Volume slide */
2250  if(ctx->current_tick == 0) break;
2251  jar_xm_tone_portamento(ctx, ch);
2252  jar_xm_volume_slide(ch, ch->volume_slide_param);
2253  break;
2254 
2255  case 6: /* 6xy: Vibrato + Volume slide */
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);
2260  break;
2261 
2262  case 7: /* 7xy: Tremolo */
2263  if(ctx->current_tick == 0) break;
2264  jar_xm_tremolo(ctx, ch, ch->tremolo_param, ch->tremolo_ticks++);
2265  break;
2266 
2267  case 0xA: /* Axy: Volume slide */
2268  if(ctx->current_tick == 0) break;
2269  jar_xm_volume_slide(ch, ch->volume_slide_param);
2270  break;
2271 
2272  case 0xE: /* EXy: Extended command */
2273  switch(ch->current->effect_param >> 4) {
2274 
2275  case 0x9: /* E9y: Retrigger note */
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);
2280  }
2281  }
2282  break;
2283 
2284  case 0xC: /* ECy: Note cut */
2285  if((ch->current->effect_param & 0x0F) == ctx->current_tick) {
2286  jar_xm_cut_note(ch);
2287  }
2288  break;
2289 
2290  case 0xD: /* EDy: Note delay */
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);
2294  }
2295  break;
2296 
2297  default:
2298  break;
2299 
2300  }
2301  break;
2302 
2303  case 17: /* Hxy: Global volume slide */
2304  if(ctx->current_tick == 0) break;
2305  if((ch->global_volume_slide_param & 0xF0) &&
2306  (ch->global_volume_slide_param & 0x0F)) {
2307  /* Illegal state */
2308  break;
2309  }
2310  if(ch->global_volume_slide_param & 0xF0) {
2311  /* Global slide up */
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);
2315  } else {
2316  /* Global slide down */
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);
2320  }
2321  break;
2322 
2323  case 20: /* Kxx: Key off */
2324  /* Most documentations will tell you the parameter has no
2325  * use. Don't be fooled. */
2326  if(ctx->current_tick == ch->current->effect_param) {
2327  jar_xm_key_off(ch);
2328  }
2329  break;
2330 
2331  case 25: /* Pxy: Panning slide */
2332  if(ctx->current_tick == 0) break;
2333  jar_xm_panning_slide(ch, ch->panning_slide_param);
2334  break;
2335 
2336  case 27: /* Rxy: Multi retrig note */
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];
2342  jar_xm_CLAMP(v);
2343  jar_xm_trigger_note(ctx, ch, 0);
2344  ch->volume = v;
2345  }
2346  break;
2347 
2348  case 29: /* Txy: Tremor */
2349  if(ctx->current_tick == 0) break;
2350  ch->tremor_on = (
2351  (ctx->current_tick - 1) % ((ch->tremor_param >> 4) + (ch->tremor_param & 0x0F) + 2)
2352  >
2353  (ch->tremor_param >> 4)
2354  );
2355  break;
2356 
2357  default:
2358  break;
2359 
2360  }
2361 
2362  float panning, volume;
2363 
2364  panning = ch->panning +
2365  (ch->panning_envelope_panning - .5f) * (.5f - fabsf(ch->panning - .5f)) * 2.0f;
2366 
2367  if(ch->tremor_on) {
2368  volume = .0f;
2369  } else {
2370  volume = ch->volume + ch->tremolo_volume;
2371  jar_xm_CLAMP(volume);
2372  volume *= ch->fadeout_volume * ch->volume_envelope_volume;
2373  }
2374 
2375 #if JAR_XM_RAMPING
2376  ch->target_panning = panning;
2377  ch->target_volume = volume;
2378 #else
2379  ch->actual_panning = panning;
2380  ch->actual_volume = volume;
2381 #endif
2382  }
2383 
2384  ctx->current_tick++;
2385  if(ctx->current_tick >= ctx->tempo + ctx->extra_ticks) {
2386  ctx->current_tick = 0;
2387  ctx->extra_ticks = 0;
2388  }
2389 
2390  /* FT2 manual says number of ticks / second = BPM * 0.4 */
2391  ctx->remaining_samples_in_tick += (float)ctx->rate / ((float)ctx->bpm * 0.4f);
2392 }
2393 
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) {
2396 #if JAR_XM_RAMPING
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);
2400  }
2401 #endif
2402  return .0f;
2403  }
2404  if(ch->sample->length == 0) {
2405  return .0f;
2406  }
2407 
2408  float u, v, t;
2409  uint32_t a, b;
2410  a = (uint32_t)ch->sample_position; /* This cast is fine,
2411  * sample_position will not
2412  * go above integer
2413  * ranges */
2415  b = a + 1;
2416  t = ch->sample_position - a; /* Cheaper than fmodf(., 1.f) */
2417  }
2418  u = ch->sample->data[a];
2419 
2420  switch(ch->sample->loop_type) {
2421 
2422  case jar_xm_NO_LOOP:
2424  v = (b < ch->sample->length) ? ch->sample->data[b] : .0f;
2425  }
2426  ch->sample_position += ch->step;
2427  if(ch->sample_position >= ch->sample->length) {
2428  ch->sample_position = -1;
2429  }
2430  break;
2431 
2432  case jar_xm_FORWARD_LOOP:
2434  v = ch->sample->data[
2435  (b == ch->sample->loop_end) ? ch->sample->loop_start : b
2436  ];
2437  }
2438  ch->sample_position += ch->step;
2439  while(ch->sample_position >= ch->sample->loop_end) {
2440  ch->sample_position -= ch->sample->loop_length;
2441  }
2442  break;
2443 
2444  case jar_xm_PING_PONG_LOOP:
2445  if(ch->ping) {
2446  ch->sample_position += ch->step;
2447  } else {
2448  ch->sample_position -= ch->step;
2449  }
2450  /* XXX: this may not work for very tight ping-pong loops
2451  * (ie switches direction more than once per sample */
2452  if(ch->ping) {
2454  v = (b >= ch->sample->loop_end) ? ch->sample->data[a] : ch->sample->data[b];
2455  }
2456  if(ch->sample_position >= ch->sample->loop_end) {
2457  ch->ping = false;
2458  ch->sample_position = (ch->sample->loop_end << 1) - ch->sample_position;
2459  }
2460  /* sanity checking */
2461  if(ch->sample_position >= ch->sample->length) {
2462  ch->ping = false;
2463  ch->sample_position -= ch->sample->length - 1;
2464  }
2465  } else {
2467  v = u;
2468  u = (b == 1 || b - 2 <= ch->sample->loop_start) ? ch->sample->data[a] : ch->sample->data[b - 2];
2469  }
2470  if(ch->sample_position <= ch->sample->loop_start) {
2471  ch->ping = true;
2472  ch->sample_position = (ch->sample->loop_start << 1) - ch->sample_position;
2473  }
2474  /* sanity checking */
2475  if(ch->sample_position <= .0f) {
2476  ch->ping = true;
2477  ch->sample_position = .0f;
2478  }
2479  }
2480  break;
2481 
2482  default:
2483  v = .0f;
2484  break;
2485  }
2486 
2487  float endval = JAR_XM_LINEAR_INTERPOLATION ? jar_xm_LERP(u, v, t) : u;
2488 
2489 #if JAR_XM_RAMPING
2490  if(ch->frame_count < jar_xm_SAMPLE_RAMPING_POINTS) {
2491  /* Smoothly transition between old and new sample. */
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);
2494  }
2495 #endif
2496 
2497  return endval;
2498 }
2499 
2500 static void jar_xm_sample(jar_xm_context_t* ctx, float* left, float* right) {
2501  if(ctx->remaining_samples_in_tick <= 0) {
2502  jar_xm_tick(ctx);
2503  }
2504  ctx->remaining_samples_in_tick--;
2505 
2506  *left = 0.f;
2507  *right = 0.f;
2508 
2509  if(ctx->max_loop_count > 0 && ctx->loop_count >= ctx->max_loop_count) {
2510  return;
2511  }
2512 
2513  for(uint8_t i = 0; i < ctx->module.num_channels; ++i) {
2514  jar_xm_channel_context_t* ch = ctx->channels + i;
2515 
2516  if(ch->instrument == NULL || ch->sample == NULL || ch->sample_position < 0) {
2517  continue;
2518  }
2519 
2520  const float fval = jar_xm_next_of_sample(ch);
2521 
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;
2525  }
2526 
2527 #if JAR_XM_RAMPING
2528  ch->frame_count++;
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);
2531 #endif
2532  }
2533 
2534  const float fgvol = ctx->global_volume * ctx->amplification;
2535  *left *= fgvol;
2536  *right *= fgvol;
2537 
2538 #if JAR_XM_DEBUG
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);
2541  }
2542 #endif
2543 }
2544 
2545 void jar_xm_generate_samples(jar_xm_context_t* ctx, float* output, size_t numsamples) {
2546  if(ctx && output) {
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));
2550  }
2551  }
2552 }
2553 
2555 {
2556  uint64_t total = 0;
2557  uint8_t currentLoopCount = jar_xm_get_loop_count(ctx);
2558  jar_xm_set_max_loop_count(ctx, 0);
2559 
2560  while(jar_xm_get_loop_count(ctx) == currentLoopCount)
2561  {
2562  total += ctx->remaining_samples_in_tick;
2563  ctx->remaining_samples_in_tick = 0;
2564  jar_xm_tick(ctx);
2565  }
2566 
2567  ctx->loop_count = currentLoopCount;
2568  return total;
2569 }
2570 
2571 
2572 
2573 
2574 
2575 //--------------------------------------------
2576 //FILE LOADER - TODO - NEEDS TO BE CLEANED UP
2577 //--------------------------------------------
2578 
2579 
2580 
2581 #undef DEBUG
2582 #define DEBUG(...) do { \
2583  fprintf(stderr, __VA_ARGS__); \
2584  fflush(stderr); \
2585  } while(0)
2586 
2587 #define DEBUG_ERR(...) do { \
2588  fprintf(stderr, __VA_ARGS__); \
2589  fflush(stderr); \
2590  } while(0)
2591 
2592 #define FATAL(...) do { \
2593  fprintf(stderr, __VA_ARGS__); \
2594  fflush(stderr); \
2595  exit(1); \
2596  } while(0)
2597 
2598 #define FATAL_ERR(...) do { \
2599  fprintf(stderr, __VA_ARGS__); \
2600  fflush(stderr); \
2601  exit(1); \
2602  } while(0)
2603 
2604 
2605 int jar_xm_create_context_from_file(jar_xm_context_t** ctx, uint32_t rate, const char* filename) {
2606  FILE* xmf;
2607  int size;
2608 
2609 #if defined(_MSC_VER) && _MSC_VER >= 1500
2610  xmf = NULL;
2611  fopen_s(&xmf, filename, "rb");
2612 #else
2613  xmf = fopen(filename, "rb");
2614 #endif
2615  if(xmf == NULL) {
2616  DEBUG_ERR("Could not open input file");
2617  *ctx = NULL;
2618  return 3;
2619  }
2620 
2621  fseek(xmf, 0, SEEK_END);
2622  size = ftell(xmf);
2623  rewind(xmf);
2624  if(size == -1) {
2625  fclose(xmf);
2626  DEBUG_ERR("fseek() failed");
2627  *ctx = NULL;
2628  return 4;
2629  }
2630 
2631  char* data = malloc(size + 1);
2632  if(fread(data, 1, size, xmf) < size) {
2633  fclose(xmf);
2634  DEBUG_ERR("fread() failed");
2635  *ctx = NULL;
2636  return 5;
2637  }
2638 
2639  fclose(xmf);
2640 
2641  switch(jar_xm_create_context_safe(ctx, data, size, rate)) {
2642  case 0:
2643  break;
2644 
2645  case 1:
2646  DEBUG("could not create context: module is not sane\n");
2647  *ctx = NULL;
2648  return 1;
2649  break;
2650 
2651  case 2:
2652  FATAL("could not create context: malloc failed\n");
2653  return 2;
2654  break;
2655 
2656  default:
2657  FATAL("could not create context: unknown error\n");
2658  return 6;
2659  break;
2660 
2661  }
2662 
2663  return 0;
2664 }
2665 
2666 
2667 
2668 
2669 #endif//end of JAR_XM_IMPLEMENTATION
2670 //-------------------------------------------------------------------------------
2671 
jar_xm_create_context_safe
int jar_xm_create_context_safe(jar_xm_context_t **, const char *moddata, size_t moddata_length, uint32_t rate)
Create a XM context.
value
GLfloat value
Definition: qgl_win.c:63
jar_xm_get_latest_trigger_of_instrument
uint64_t jar_xm_get_latest_trigger_of_instrument(jar_xm_context_t *, uint16_t)
Get the latest time (in number of generated samples) when a particular instrument was triggered in an...
jar_xm_mute_instrument
bool jar_xm_mute_instrument(jar_xm_context_t *, uint16_t, bool)
Mute or unmute an instrument.
jar_xm_get_number_of_rows
uint16_t jar_xm_get_number_of_rows(jar_xm_context_t *, uint16_t)
Get the number of rows of a pattern.
jar_xm_generate_samples
void jar_xm_generate_samples(jar_xm_context_t *, float *output, size_t numsamples)
Play the module and put the sound samples in an output buffer.
v
GLdouble v
Definition: qgl_win.c:143
jar_xm_context_t
struct jar_xm_context_s jar_xm_context_t
Definition: jar_xm.h:77
x
GLint GLenum GLint x
Definition: qgl_win.c:116
z
GLdouble GLdouble z
Definition: qgl_win.c:283
jar_xm_get_module_length
uint16_t jar_xm_get_module_length(jar_xm_context_t *)
Get the module length (in patterns).
i
int i
Definition: q_shared.c:305
jar_xm_get_tracker_name
const char * jar_xm_get_tracker_name(jar_xm_context_t *)
Get the tracker name as a NUL-terminated string.
note
Definition: jar_mod.h:120
jar_xm_get_latest_trigger_of_sample
uint64_t jar_xm_get_latest_trigger_of_sample(jar_xm_context_t *, uint16_t instr, uint16_t sample)
Get the latest time (in number of generated samples) when a particular sample was triggered in any ch...
jar_xm_get_playing_speed
void jar_xm_get_playing_speed(jar_xm_context_t *, uint16_t *bpm, uint16_t *tempo)
Get the current module speed.
JAR_XM_LINEAR_INTERPOLATION
#define JAR_XM_LINEAR_INTERPOLATION
Definition: jar_xm.h:55
jar_xm_create_context_from_file
int jar_xm_create_context_from_file(jar_xm_context_t **ctx, uint32_t rate, const char *filename)
Create a XM context.
sample::length
muint length
Definition: jar_mod.h:113
j
GLint j
Definition: qgl_win.c:150
u
static int u
Definition: r_part.c:472
jar_xm_get_latest_trigger_of_channel
uint64_t jar_xm_get_latest_trigger_of_channel(jar_xm_context_t *, uint16_t)
Get the latest time (in number of generated samples) when any instrument was triggered in a given cha...
module
Definition: jar_mod.h:127
jar_xm_get_number_of_instruments
uint16_t jar_xm_get_number_of_instruments(jar_xm_context_t *)
Get the number of instruments.
jar_xm_free_context
void jar_xm_free_context(jar_xm_context_t *)
Free a XM context created by jar_xm_create_context().
param
GLfloat param
Definition: qgl_win.c:154
jar_xm_get_number_of_patterns
uint16_t jar_xm_get_number_of_patterns(jar_xm_context_t *)
Get the number of patterns.
t
GLdouble t
Definition: qgl_win.c:328
jar_xm_get_number_of_samples
uint16_t jar_xm_get_number_of_samples(jar_xm_context_t *, uint16_t)
Get the number of samples of an instrument.
jar_xm_create_context
int jar_xm_create_context(jar_xm_context_t **, const char *moddata, uint32_t rate)
Create a XM context.
jar_xm_get_remaining_samples
uint64_t jar_xm_get_remaining_samples(jar_xm_context_t *)
Get the number of remaining samples.
jar_xm_generate_samples_16bit
void jar_xm_generate_samples_16bit(jar_xm_context_t *ctx, short *output, size_t numsamples)
Play the module, resample from 32 bit to 16 bit, and put the sound samples in an output buffer.
Definition: jar_xm.h:138
jar_xm_get_number_of_channels
uint16_t jar_xm_get_number_of_channels(jar_xm_context_t *)
Get the number of channels.
NULL
#define NULL
Definition: q_shared.h:67
channels
channel_t channels[MAX_CHANNELS]
Definition: snd_dma.c:42
current
static int current
Definition: cl_scrn.c:129
sample::finetune
muchar finetune
Definition: jar_mod.h:114
name
cvar_t * name
Definition: cl_main.c:79
s
static fixed16_t s
Definition: r_scan.c:30
rate
cvar_t * rate
Definition: cl_main.c:81
jar_xm_get_loop_count
uint8_t jar_xm_get_loop_count(jar_xm_context_t *)
Get the loop count of the currently playing module.
sample::name
muchar name[22]
Definition: jar_mod.h:112
jar_xm_get_position
void jar_xm_get_position(jar_xm_context_t *, uint8_t *pattern_index, uint8_t *pattern, uint8_t *row, uint64_t *samples)
Get the current position in the module being played.
jar_xm_mute_channel
bool jar_xm_mute_channel(jar_xm_context_t *, uint16_t, bool)
Mute or unmute a channel.
JAR_XM_DEBUG
#define JAR_XM_DEBUG
Definition: jar_xm.h:54
sample
Definition: jar_mod.h:111
right
GLdouble right
Definition: qgl_win.c:159
sample::volume
muchar volume
Definition: jar_mod.h:115
jar_xm_get_module_name
const char * jar_xm_get_module_name(jar_xm_context_t *)
Get the module name as a NUL-terminated string.
pattern
GLushort pattern
Definition: qgl_win.c:217
jar_xm_generate_samples_8bit
void jar_xm_generate_samples_8bit(jar_xm_context_t *ctx, char *output, size_t numsamples)
Play the module, resample from 32 bit to 8 bit, and put the sound samples in an output buffer.
Definition: jar_xm.h:157
points
GLdouble GLdouble GLint GLint const GLdouble * points
Definition: qgl_win.c:225
channel
Definition: jar_mod.h:142
enabled
static qboolean enabled
Definition: cd_win.c:32
jar_xm_set_max_loop_count
void jar_xm_set_max_loop_count(jar_xm_context_t *, uint8_t loopcnt)
Set the maximum number of times a module can loop.